Commit a5ef1ddd authored by Matija Obreza's avatar Matija Obreza

Merge branch 'fix-accenumb-rename' into 'master'

Fix accenumb rename See merge request genesys-pgr/genesys-server!279
parents 844df264 ddf92020
Pipeline #7519 passed with stages
in 6 minutes 51 seconds
......@@ -67,7 +67,6 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.base.Stopwatch;
import com.google.common.collect.Iterators;
import javassist.util.proxy.Proxy;
......@@ -150,17 +149,6 @@ public class AccessionUploader implements InitializingBean {
}
}
// Security check
for (int i = 0; i < accessions.size(); i++) {
Accession accession = accessions.get(i);
if (accession == null) {
continue;
}
if (!institute.getCode().equals(accession.getInstituteCode())) {
throw new InvalidApiUsageException("Accession does not belong to institute " + institute.getCode());
}
}
LOG.debug("Processsed incoming JSON for {} accessions in {}ms", updates.size(), stopWatch.getTime());
List<Accession> existingAccessions = accessionRepository.find(accessions);
......@@ -200,6 +188,16 @@ public class AccessionUploader implements InitializingBean {
}
try {
// Accessions with registered DOI require the DOI to be provided
if (accession.getDoi() != null && updateA.getDoi() == null) {
throw new InvalidApiUsageException("DOI not included for accession with registered DOI=" + accession.getDoi());
}
if (! accession.getInstitute().getId().equals(institute.getId())) {
throw new InvalidApiUsageException("Accession does not belong to institute " + institute.getCode());
}
accession = applyChanges(update, updateA, accession);
LOG.trace("Applied changes to 1 accession after {}ms", stopWatch.getTime());
......@@ -218,7 +216,11 @@ public class AccessionUploader implements InitializingBean {
} catch (PleaseRetryException e) {
throw e;
} catch (InvalidApiUsageException e) {
LOG.error(e.getMessage(), e);
if (LOG.isDebugEnabled()) {
LOG.error(e.getMessage(), e);
} else {
LOG.error(e.getMessage());
}
response.setError(e.getMessage());
}
}
......@@ -235,9 +237,9 @@ public class AccessionUploader implements InitializingBean {
/**
* Check incoming JSON and update values accordingly
*
* @param update
* @param updateA
* @param accession
* @param update JSON node
* @param updateA Received accession data
* @param accession Genesys accession data
* @return
*/
private Accession applyChanges(JsonNode update, Accession updateA, Accession accession) {
......@@ -245,7 +247,13 @@ public class AccessionUploader implements InitializingBean {
update.fieldNames().forEachRemaining(fieldName -> {
LOG.trace("Updating {}", fieldName);
if ("taxonomy".equals(fieldName)) {
if ("doi".equals(fieldName)) {
updateDoi(updateA.getDoi(), accession);
} else if ("accessionNumber".equals(fieldName) || "newAcceNumb".equals(fieldName)) {
updateAccessionNumber(update.get("accessionNumber"), update.get("newAcceNumb"), accession);
} else if ("taxonomy".equals(fieldName)) {
stopWatch.split();
updateTaxonomy(update.get("taxonomy"), updateA.getTaxonomy(), accession);
LOG.debug("Updated taxonomy for 1 accession in {}ms", stopWatch.getSplitTime());
......@@ -302,6 +310,41 @@ public class AccessionUploader implements InitializingBean {
return accession;
}
private void updateDoi(String doi, Accession accession) {
if (StringUtils.isNotBlank(doi)) {
if (accession.getDoi() != null && ! StringUtils.equals(doi, accession.getDoi())) {
throw new InvalidApiUsageException("Cannot update DOI from " + accession.getDoi() + " to " + doi);
}
accession.setDoi(doi);
} else if (accession.getDoi() != null) {
// Only reaches here if DOI is provided and blank
throw new InvalidApiUsageException("Missing DOI for accession " + accession.getAccessionNumber() + " with doi=" + accession.getDoi());
}
}
private void updateAccessionNumber(JsonNode nodeAcceNumb, JsonNode nodeNewNumb, Accession accession) {
String accessionNumber = nodeAcceNumb.textValue();
String newAcceNumb = nodeNewNumb == null ? null : nodeNewNumb.textValue();
if (! accession.isPersisted()) {
if (newAcceNumb != null) {
throw new InvalidApiUsageException("Refusing insert when NEWACCENUMB (newAcceNumb) is provided");
}
// Set provided accessionNumber and ignore newAcceNumb
accession.setAccessionNumber(accessionNumber);
} else if (accession.getDoi() != null) {
// When accession has a DOI, override accession number
accession.setAccessionNumber(accessionNumber);
} else if (newAcceNumb != null) {
if (! StringUtils.equals(newAcceNumb, accession.getAccessionNumber())) {
LOG.warn("Renaming accession {}: {} to {}", accession.getInstitute().getCode(), accession.getAccessionNumber(), newAcceNumb);
accession.setAccessionNumber(newAcceNumb);
}
}
}
private void updateCrop(JsonNode jsonNode, Accession accession) {
if (jsonNode.isNull()) {
accession.setCropName(null);
......@@ -460,6 +503,10 @@ public class AccessionUploader implements InitializingBean {
while (fieldNames.hasNext() && (fieldName = fieldNames.next()) != null) {
LOG.trace("Copying {}.{}", clazz.getName(), fieldName);
Field field = ReflectionUtils.findField(clazz, fieldName);
if (field == null) {
LOG.warn("Client provided field {} that does not exist on {}", fieldName, clazz);
throw new InvalidApiUsageException("Field " + fieldName + " not valid for " + clazz.getName());
}
ReflectionUtils.makeAccessible(field);
// Find getter for lazy-loading
......
......@@ -110,9 +110,9 @@
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<p>
<c:out value="${accession.accessionName}"/>
<c:out value="${accession.accessionNumber}"/>
</p>
<local:auditlog-changes auditLogs="${auditAccession['accessionName']}"/>
<local:auditlog-changes auditLogs="${auditAccession['accessionNumber']}"/>
</div>
</div>
......
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