Commit 0beb1829 authored by Matija Obreza's avatar Matija Obreza

@Cacheable must not have unless = "#result == null"

Ensuring taxonomies exist before upserting accession data
parent cfe09784
......@@ -35,4 +35,6 @@ public interface BatchRESTService {
int deleteAccessions(FaoInstitute institute, List<AccessionHeaderJson> batch) throws RESTApiException;
int deleteAccessionsById(FaoInstitute institute, List<Long> batch);
void ensureTaxonomies(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) throws RESTApiException;
}
......@@ -61,7 +61,6 @@ import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Service
@Transactional(readOnly = true)
public class BatchRESTServiceImpl implements BatchRESTService {
private final Log LOG = LogFactory.getLog(getClass());
......@@ -87,6 +86,47 @@ public class BatchRESTServiceImpl implements BatchRESTService {
@Autowired
private TaxonomyManager taxonomyManager;
@Override
// Read-only, everything happens in manager
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')")
public void ensureTaxonomies(FaoInstitute institute, Map<AccessionHeaderJson, ObjectNode> batch) throws RESTApiException {
for (final AccessionHeaderJson dataJson : batch.keySet()) {
final ObjectNode accnJson = batch.get(dataJson);
final Taxonomy2 current = new Taxonomy2();
// Load JSON values into "current"
current.setGenus(stringIfProvided(accnJson.get("genus"), current.getGenus()));
current.setGenus(stringIfProvided(accnJson.get("newGenus"), current.getGenus()));
current.setSpecies(StringUtils.defaultIfBlank(stringIfProvided(accnJson.get("species"), current.getSpecies()), "sp."));
current.setSpAuthor(StringUtils.defaultIfBlank(stringIfProvided(accnJson.get("spauthor"), current.getSpAuthor()), ""));
current.setSubtaxa(StringUtils.defaultIfBlank(stringIfProvided(accnJson.get("subtaxa"), current.getSubtaxa()), ""));
current.setSubtAuthor(StringUtils.defaultIfBlank(stringIfProvided(accnJson.get("subtauthor"), current.getSubtAuthor()), ""));
Taxonomy2 ensuredTaxonomy = null;
try {
ensuredTaxonomy = taxonomyService.find(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor());
} catch (Throwable e) {
LOG.warn("*** lower(t.genus)=lower(" + current.getGenus() + ") and lower(t.species)=lower(" + current.getSpecies()
+ ") and lower(t.spauthor)=lower(" + current.getSpAuthor() + ") and lower(t.subtaxa)=lower(" + current.getSubtaxa()
+ ") and lower(subtauthor)=lower(" + current.getSubtAuthor() + ")");
}
if (ensuredTaxonomy == null) {
LOG.warn("Adding new taxonomy: " + current);
ensuredTaxonomy = taxonomyManager.ensureTaxonomy2(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor());
LOG.info("Registered: " + ensuredTaxonomy);
ensuredTaxonomy = taxonomyService.find(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor());
if (ensuredTaxonomy == null) {
throw new RuntimeException("Something is seriously wrong with taxonomyManager!");
}
}
}
}
@Override
@Transactional
@PreAuthorize("hasRole('ADMINISTRATOR') or hasPermission(#institute, 'WRITE') or hasPermission(#institute, 'CREATE')")
......@@ -498,7 +538,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
return updated;
}
public static boolean updateAccessionStorage(Accession accession, ArrayNode arr) {
private static boolean updateAccessionStorage(Accession accession, ArrayNode arr) {
boolean updated = false;
List<Integer> as = accession.getStoRage();
......@@ -698,9 +738,10 @@ public class BatchRESTServiceImpl implements BatchRESTService {
+ ") and lower(t.spauthor)=lower(" + current.getSpAuthor() + ") and lower(t.subtaxa)=lower(" + current.getSubtaxa()
+ ") and lower(subtauthor)=lower(" + current.getSubtAuthor() + ")");
}
if (ensuredTaxonomy == null) {
ensuredTaxonomy = taxonomyManager.ensureTaxonomy2(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor());
LOG.warn("Could not find taxonomy for upsert " + current);
throw new RESTApiException("Could not find taxonomy " + current.getTaxonName());
}
if (!ensuredTaxonomy.sameAs(taxonomy)) {
......@@ -733,25 +774,6 @@ public class BatchRESTServiceImpl implements BatchRESTService {
return currentValue;
}
public static <T> String listToString(List<T> list) {
if (list == null || list.isEmpty()) {
return null;
}
final StringBuffer mcpdArr = new StringBuffer(20);
for (final T st : list) {
if (st != null && !StringUtils.isBlank(st.toString())) {
if (mcpdArr.length() > 0) {
mcpdArr.append(";");
}
mcpdArr.append(st.toString());
}
}
return StringUtils.defaultIfBlank(mcpdArr.toString(), null);
}
private String arrayToString(ArrayNode arr) {
if (arr == null || arr.isNull()) {
return null;
......
......@@ -83,7 +83,7 @@ public class TaxonomyServiceImpl implements TaxonomyService {
}
@Override
@Cacheable(value = "hibernate.org.genesys2.server.model.impl.Taxonomy2.fullname", key = "#genus + '-' + #species + '-' + #spAuthor + '-' + #subtaxa + '-' + #subtAuthor")
@Cacheable(value = "hibernate.org.genesys2.server.model.impl.Taxonomy2.fullname", key = "#genus + '-' + #species + '-' + #spAuthor + '-' + #subtaxa + '-' + #subtAuthor", unless = "#result == null")
public Taxonomy2 find(String genus, String species, String spAuthor, String subtaxa, String subtAuthor) {
return taxonomy2Repository.findByGenusAndSpeciesAndSpAuthorAndSubtaxaAndSubtAuthor(genus, species, spAuthor, subtaxa, subtAuthor);
}
......
......@@ -223,6 +223,10 @@ public class AccessionController extends RestController {
}
batch.put(dataJson, (ObjectNode) json);
}
// Step 1: Ensure all taxonomic data provided by client is persisted
batchRESTService.ensureTaxonomies(institute, batch);
for (int i = 0; i < 10; i++) {
try {
......
package org.genesys2.server.service.impl;
import static org.junit.Assert.assertTrue;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class ListToStringTest {
@Test
public void testLongListToString() {
List<Long> list = null;
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list = new ArrayList<Long>();
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list.add(1l);
assertTrue("1".equals(BatchRESTServiceImpl.listToString(list)));
list.add(2l);
list.add(3l);
assertTrue("1;2;3".equals(BatchRESTServiceImpl.listToString(list)));
list.add(3l);
assertTrue("1;2;3;3".equals(BatchRESTServiceImpl.listToString(list)));
}
@Test
public void testIntListToString() {
List<Integer> list = null;
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list = new ArrayList<Integer>();
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list.add(1);
assertTrue("1".equals(BatchRESTServiceImpl.listToString(list)));
list.add(2);
list.add(3);
assertTrue("1;2;3".equals(BatchRESTServiceImpl.listToString(list)));
list.add(3);
assertTrue("1;2;3;3".equals(BatchRESTServiceImpl.listToString(list)));
}
@Test
public void testStringListToString() {
List<String> list = null;
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list = new ArrayList<String>();
assertTrue(null == BatchRESTServiceImpl.listToString(list));
list.add("1");
assertTrue("1".equals(BatchRESTServiceImpl.listToString(list)));
list.add("2");
list.add("3");
assertTrue("1;2;3".equals(BatchRESTServiceImpl.listToString(list)));
list.add("3");
assertTrue("1;2;3;3".equals(BatchRESTServiceImpl.listToString(list)));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment