Commit 33e2f892 authored by Matija Obreza's avatar Matija Obreza

Merge branch 'accession-updates-instcode' into 'master'

Accession updates instcode

See merge request genesys-pgr/genesys-server!511
parents ebcf4e56 40f7a90e
......@@ -259,7 +259,7 @@ public class AccessionController extends ApiBaseController {
LOG.debug("Caused by {}", lastException.getMessage(), lastException);
}
// record error
response.add(new AccessionOpResponse(update.get("instituteCode").asText(), update.get("accessionNumber").asText(), update.get("taxonomy").get("genus").asText())
response.add(new AccessionOpResponse(update)
.setResult(new UpsertResult(UpsertResult.Type.ERROR)).setError(lastException.getMessage()));
}
}
......@@ -327,7 +327,7 @@ public class AccessionController extends ApiBaseController {
LOG.info("Error deleting {}: {}", update.get("instituteCode").asText(), e.getMessage());
}
AccessionOpResponse accessionResponse = new AccessionOpResponse(update.get("instituteCode").asText(), update.get("accessionNumber").asText(), update.get("taxonomy").get("genus").asText());
AccessionOpResponse accessionResponse = new AccessionOpResponse(update);
accessionResponse.setError(getDetailedErrorMessage(e));
response.add(accessionResponse);
}
......
......@@ -160,7 +160,7 @@ public class AccessionUploadController {
if (lastException != null) {
LOG.warn("Upsert failed due to: {} data={}", lastException.getMessage(), update, lastException);
// record error
response.add(new AccessionOpResponse(update.get("instituteCode").asText(), update.get("accessionNumber").asText(), update.get("taxonomy").get("genus").asText())
response.add(new AccessionOpResponse(update)
.setResult(new UpsertResult(UpsertResult.Type.ERROR)).setError(getDetailedErrorMessage(lastException)));
}
}
......@@ -227,7 +227,7 @@ public class AccessionUploadController {
LOG.info("Error deleting {}: {}", accn.get("instituteCode"), e.getMessage());
}
AccessionOpResponse accessionResponse = new AccessionOpResponse(accn.get("instituteCode").asText(), accn.get("accessionNumber").asText(), accn.get("taxonomy").get("genus").asText());
AccessionOpResponse accessionResponse = new AccessionOpResponse(accn);
accessionResponse.setError(getDetailedErrorMessage(e));
response.add(accessionResponse);
}
......
......@@ -149,7 +149,7 @@ public class AccessionUploadController extends ApiBaseController {
if (lastException != null) {
LOG.warn("Upsert failed due to: {} data={}", lastException.getMessage(), update, lastException);
// record error
response.add(new AccessionOpResponse(update.get("instituteCode").asText(), update.get("accessionNumber").asText(), update.get("taxonomy").get("genus").asText())
response.add(new AccessionOpResponse(update)
.setResult(new AccessionOpResponse.UpsertResult(AccessionOpResponse.UpsertResult.Type.ERROR)).setError(getDetailedErrorMessage(lastException)));
}
}
......@@ -217,7 +217,7 @@ public class AccessionUploadController extends ApiBaseController {
LOG.info("Error deleting {}: {}", accn.get("instituteCode"), e.getMessage());
}
AccessionOpResponse accessionResponse = new AccessionOpResponse(accn.get("instituteCode").asText(), accn.get("accessionNumber").asText(), accn.get("taxonomy").get("genus").asText());
AccessionOpResponse accessionResponse = new AccessionOpResponse(accn);
accessionResponse.setError(getDetailedErrorMessage(e));
response.add(accessionResponse);
}
......
......@@ -51,8 +51,7 @@ public class AccessionAlias extends BasicModel implements AccessionRelated, Self
private static final Pattern HAS_WORD_CHARACTER = Pattern.compile("\\w", Pattern.MULTILINE);
// INSTCODE:alias
private static Pattern USED_BY_PATTERN = Pattern.compile("^(([A-Z]{3}\\d{3,4}):(.+))?$");
private static Pattern USED_BY_PATTERN = Pattern.compile("^((\\p{Alnum}+):(.+))?$");
public static enum AliasType {
ACCENAME(0), DONORNUMB(1), BREDNUMB(2), DATABASEID(3), LOCALNAME(4), OTHERNUMB(5), COLLNUMB(6);
......@@ -101,9 +100,9 @@ public class AccessionAlias extends BasicModel implements AccessionRelated, Self
@Column(length = 2)
private String lang;
@javax.validation.constraints.Pattern(regexp = "[A-Z]{3}\\d{3,4}")
@Size(max = 7)
@Column(length = 7)
@javax.validation.constraints.Pattern(regexp = "\\p{Alnum}+")
@Size(max = 64)
@Column(length = 64)
private String usedBy;
public AccessionAlias() {
......
......@@ -48,7 +48,7 @@ public class AccessionCounter implements InitializingBean, DisposableBean {
// Interval time (in ms) for scanning the queue for expired updates
private static final int DELAY_BETWEEN_QUEUESCANS = 5000; // 5 sec
// If a request comes before this delay (in ms), reschedule
private static final long MINTIME_BETWEEN_UPDATES = 60 * 60 * 1000; // 1 min
private static final long MINTIME_BETWEEN_UPDATES = 60 * 1000; // 1 min
private DelayQueue<DelayedObject<String>> instituteQueue;
......@@ -150,7 +150,7 @@ public class AccessionCounter implements InitializingBean, DisposableBean {
@Override
public long getDelay(TimeUnit unit) {
long delay = unit.convert(expiryTime - System.currentTimeMillis(), TimeUnit.MILLISECONDS);
LOG.trace("{} scheduled in {}ms", this.obj, delay);
LOG.trace("{} scheduled in {}{}", this.obj, delay, unit.name());
return delay;
}
......
......@@ -3,6 +3,11 @@ package org.genesys2.server.service.worker;
import java.io.Serializable;
import java.util.UUID;
import org.genesys2.server.model.genesys.Accession;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.NullNode;
/**
* Upsert response
*/
......@@ -10,6 +15,7 @@ public class AccessionOpResponse implements Serializable {
private static final long serialVersionUID = 3266359129838078166L;
private String instCode;
private String doi;
private String acceNumb;
private String genus;
private String error;
......@@ -25,8 +31,7 @@ public class AccessionOpResponse implements Serializable {
}
/**
* @param action
* UPSERT or INSERT or DELETE
* @param action UPSERT or INSERT or DELETE
*/
public UpsertResult(Type action) {
this.action = action;
......@@ -50,16 +55,42 @@ public class AccessionOpResponse implements Serializable {
}
}
public AccessionOpResponse(String instCode, String acceNumb, String genus) {
this.instCode = instCode;
this.acceNumb = acceNumb;
this.genus = genus;
public AccessionOpResponse(Accession accession) {
this.instCode = accession.getHoldingInstitute();
this.doi = accession.getDoi();
this.acceNumb = accession.getAccessionNumber();
this.genus = accession.getTaxonomy() != null ? accession.getTaxonomy().getGenus() : null;
}
public AccessionOpResponse(JsonNode accn) {
this.instCode = jsonT(accn, 0, "instituteCode");
this.doi = jsonT(accn, 0, "doi");
this.acceNumb = jsonT(accn, 0, "accessionNumber");
this.genus = jsonT(accn, 0, "taxonomy", "genus");
}
private static String jsonT(JsonNode node, int pos, String... path) {
if (node == null || pos > path.length - 1) {
return null;
}
JsonNode ch = node.get(path[pos]);
if (ch == null || ch instanceof NullNode) {
return null;
} else if (pos == path.length - 1) {
return ch.asText();
} else {
return jsonT(ch, pos + 1, path);
}
}
public String getInstCode() {
return instCode;
}
public String getDoi() {
return doi;
}
public String getAcceNumb() {
return acceNumb;
}
......
......@@ -36,6 +36,7 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.commons.text.WordUtils;
import org.genesys.blocks.model.BasicModel;
import org.genesys.blocks.security.SecurityContextUtil;
import org.genesys2.server.api.PleaseRetryException;
import org.genesys2.server.exception.InvalidApiUsageException;
import org.genesys2.server.model.genesys.Accession;
......@@ -162,11 +163,11 @@ public class AccessionUploader implements InitializingBean {
throw new InvalidApiUsageException("instituteCode, accessionNumber OR doi missing in " + accn);
}
accessions.add(accession);
responses.add(new AccessionOpResponse(accession.getInstituteCode(), accession.getAccessionNumber(), accession.getTaxonomy().getGenus()));
responses.add(new AccessionOpResponse(accession));
} catch (IOException e) {
LOG.error("Could not parse input: {}", e.getMessage(), e);
accessions.add(null); // need to match get(index) with responses and updates
responses.add(new AccessionOpResponse(accn.get("instituteCode").asText(), accn.get("accessionNumber").asText(), accn.get("taxonomy").get("genus").asText())
responses.add(new AccessionOpResponse(accn)
.setResult(new UpsertResult(UpsertResult.Type.ERROR)).setError(e.getMessage()));
}
}
......@@ -275,6 +276,9 @@ public class AccessionUploader implements InitializingBean {
if ("doi".equals(fieldName)) {
updateDoi(updateA.getDoi(), accession);
} else if ("newInstituteCode".equals(fieldName)) {
updateInstitute(update.get("newInstituteCode").textValue(), accession);
} else if ("accessionNumber".equals(fieldName) || "newAcceNumb".equals(fieldName)) {
updateAccessionNumber(update.get("accessionNumber"), update.get("newAcceNumb"), accession);
......@@ -338,6 +342,22 @@ public class AccessionUploader implements InitializingBean {
return accession;
}
/**
* Allow owner to change accession's institute code.
*
* @param newInstituteCode
* @param accession
*/
private void updateInstitute(String newInstituteCode, Accession accession) {
if (! newInstituteCode.equalsIgnoreCase(accession.getInstituteCode())) {
FaoInstitute newInstitute = faoInstituteRepository.findByCode(newInstituteCode);
if (SecurityContextUtil.hasRole("ADMINISTRATOR") || SecurityContextUtil.hasPermission(newInstitute, "WRITE")) {
accession.setInstitute(newInstitute);
accessionCounter.recountInstitute(newInstitute);
}
}
}
private void updateDoi(String doi, Accession accession) {
if (StringUtils.isNotBlank(doi)) {
if (accession.getDoi() != null && ! StringUtils.equals(doi, accession.getDoi())) {
......@@ -703,7 +723,7 @@ public class AccessionUploader implements InitializingBean {
LOG.error("Could not parse input: {}", e.getMessage(), e);
toBeDeleted.add(null); // need to match get(index) with responses and updates
AccessionOpResponse response = new AccessionOpResponse(accn.get("instituteCode").asText(), accn.get("accessionNumber").asText(), accn.get("taxonomy").get("genus").asText());
AccessionOpResponse response = new AccessionOpResponse(accn);
response.setResult(new UpsertResult(UpsertResult.Type.ERROR)).setError(e.getMessage());
responses.add(response);
}
......@@ -715,7 +735,7 @@ public class AccessionUploader implements InitializingBean {
for (Accession deletion: toBeDeleted) {
AccessionOpResponse response;
responses.add(response = new AccessionOpResponse(deletion.getHoldingInstitute(), deletion.getAccessionNumber(), deletion.getTaxonomy().getGenus()));
responses.add(response = new AccessionOpResponse(deletion));
Accession accession = existingAccessions.stream()
.filter(existing -> (
......
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