Commit dece2d04 authored by Matija Obreza's avatar Matija Obreza

Support for DOI upload

parent f5af16a3
......@@ -17,13 +17,15 @@
package org.genesys2.server.persistence.domain;
import java.util.List;
import java.util.Set;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
public interface AccessionCustomRepository {
List<Accession> find(FaoInstitute institute, List<String> acceNumbs, List<String> genera) throws NonUniqueAccessionException;
List<Accession> find(FaoInstitute institute, Set<AccessionHeaderJson> accessionIds) throws NonUniqueAccessionException;
}
......@@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
......@@ -30,12 +31,13 @@ import javax.persistence.criteria.JoinType;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
......@@ -43,7 +45,7 @@ import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional(readOnly = true)
public class AccessionCustomRepositoryImpl implements AccessionCustomRepository, InitializingBean {
public static final Log LOG = LogFactory.getLog(AccessionCustomRepositoryImpl.class);
public static final Logger LOG = LoggerFactory.getLogger(AccessionCustomRepositoryImpl.class);
@PersistenceContext
private EntityManager em;
......@@ -51,11 +53,12 @@ public class AccessionCustomRepositoryImpl implements AccessionCustomRepository,
private CriteriaBuilder criteriaBuilder;
@Override
public List<Accession> find(FaoInstitute institute, List<String> acceNumbs, List<String> genera) throws NonUniqueAccessionException {
public List<Accession> find(FaoInstitute institute, Set<AccessionHeaderJson> accessionIds) throws NonUniqueAccessionException {
boolean uniqueAcceNumbs = institute.hasUniqueAcceNumbs();
CriteriaQuery<Accession> cq = criteriaBuilder.createQuery(Accession.class);
Root<Accession> root = cq.from(Accession.class);
cq.distinct(true);
cq.select(root);
root.fetch("stoRage", JoinType.LEFT);
......@@ -65,19 +68,26 @@ public class AccessionCustomRepositoryImpl implements AccessionCustomRepository,
}
List<Predicate> restrictions = new ArrayList<Predicate>();
Set<String> uniqueDois = accessionIds.stream().map(aid -> aid.doi).filter(doi -> doi != null).distinct().collect(Collectors.toSet());
Set<String> acceNumbs = accessionIds.stream().map(aid -> aid.acceNumb).filter(acceNumb -> acceNumb != null).distinct().collect(Collectors.toSet());
if (uniqueDois.size() > 0) {
restrictions.add(root.get("doi").in(uniqueDois));
LOG.debug("*** {} dois={}", institute.getCode(), uniqueDois);
}
if (uniqueAcceNumbs) {
restrictions.add(root.get("accessionName").in(acceNumbs));
if (LOG.isDebugEnabled())
LOG.debug("*** " + institute.getCode() + " " + acceNumbs);
LOG.debug("*** {} accenumbs={}", institute.getCode(), acceNumbs);
} else {
// A lot of .. (acceNumb=? and genus=?)
for (int i = 0; i < acceNumbs.size(); i++) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("accessionName"), acceNumbs.get(i)),
criteriaBuilder.equal(tax.get("genus"), genera.get(i))));
for (AccessionHeaderJson ah : accessionIds) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("accessionName"), ah.acceNumb), criteriaBuilder.equal(tax.get("genus"), ah.genus)));
}
}
cq.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("institute"), institute), criteriaBuilder.or(restrictions.toArray(new Predicate[] {}))));
cq.distinct(true);
List<Accession> res = em.createQuery(cq).getResultList();
if (LOG.isDebugEnabled())
......
......@@ -16,7 +16,7 @@
package org.genesys2.server.service.impl;
import static org.genesys2.util.NumberUtils.*;
import static org.genesys2.util.NumberUtils.areEqual;
import java.util.ArrayList;
import java.util.Date;
......@@ -26,6 +26,10 @@ import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang.StringUtils;
......@@ -60,15 +64,12 @@ import org.genesys2.server.servlet.controller.rest.PleaseRetryException;
import org.genesys2.server.servlet.controller.rest.model.AccessionAliasJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
import org.genesys2.util.DOIUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
@Service
public class BatchRESTServiceImpl implements BatchRESTService {
......@@ -97,7 +98,7 @@ 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')")
......@@ -131,21 +132,17 @@ public class BatchRESTServiceImpl implements BatchRESTService {
Taxonomy2 ensuredTaxonomy = null;
try {
ensuredTaxonomy = taxonomyService.find(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor());
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() + ")");
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());
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());
ensuredTaxonomy = taxonomyService.find(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(), current.getSubtAuthor());
if (ensuredTaxonomy == null) {
throw new PleaseRetryException("Something is wrong with taxonomyManager for " + current.toString());
......@@ -197,17 +194,10 @@ public class BatchRESTServiceImpl implements BatchRESTService {
final Map<AccessionId, ArrayNode> donorNumbs = new HashMap<AccessionId, ArrayNode>();
final Map<AccessionId, ArrayNode> collNumbs = new HashMap<AccessionId, ArrayNode>();
List<String> acceNumbs = new ArrayList<String>();
List<String> genera = new ArrayList<String>();
for (AccessionHeaderJson dataJson : batch.keySet()) {
acceNumbs.add(dataJson.acceNumb);
genera.add(dataJson.genus);
}
List<Accession> loaded = null;
try {
loaded = accessionCustomRepository.find(institute, acceNumbs, genera);
loaded = accessionCustomRepository.find(institute, batch.keySet());
} catch (NonUniqueAccessionException e) {
LOG.warn(e.getMessage());
throw new RESTApiException(e.getMessage());
......@@ -226,10 +216,18 @@ public class BatchRESTServiceImpl implements BatchRESTService {
throw new RESTApiException("Accession does not belong to instCode=" + institute.getCode() + " acn=" + dataJson);
}
Accession accession = loaded.stream()
.filter(a -> useUniqueAcceNumbs ? (a.getAccessionName().equalsIgnoreCase(dataJson.acceNumb))
: (a.getAccessionName().equalsIgnoreCase(dataJson.acceNumb) && a.getTaxonomy().getGenus().equalsIgnoreCase(dataJson.genus)))
.findFirst().orElse(null);
Accession accession = loaded.stream().filter(a -> {
if (StringUtils.isNotBlank(a.getDoi())) {
if (a.getDoi().equals(dataJson.doi))
return true;
}
if (useUniqueAcceNumbs) {
return a.getAccessionName().equalsIgnoreCase(dataJson.acceNumb);
} else {
return a.getAccessionName().equalsIgnoreCase(dataJson.acceNumb) && a.getTaxonomy().getGenus().equalsIgnoreCase(dataJson.genus);
}
}).findFirst().orElse(null);
final ObjectNode accnJson = batch.get(dataJson);
......@@ -241,6 +239,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
accession.setAccessionId(new AccessionId());
accession.setInstitute(institute);
accession.setDoi(dataJson.doi);
accession.setAccessionName(dataJson.acceNumb);
if (accnJson.get(Api1Constants.Accession.GENUS) == null && accnJson.get(Api1Constants.Accession.GENUS_NEW) == null) {
......@@ -257,14 +256,18 @@ public class BatchRESTServiceImpl implements BatchRESTService {
upsertResult.setUUID(accession.getUuid());
}
if (accession.getAccessionId().isPersisted() || useUniqueAcceNumbs && accnJson.get(Api1Constants.Accession.GENUS) != null
|| accnJson.get(Api1Constants.Accession.GENUS_NEW) != null || accnJson.get(Api1Constants.Accession.SPECIES) != null
|| accnJson.get(Api1Constants.Accession.SPAUTHOR) != null || accnJson.get(Api1Constants.Accession.SUBTAXA) != null
|| accnJson.get(Api1Constants.Accession.SUBTAUTHOR) != null) {
if (accession.getAccessionId().isPersisted() || useUniqueAcceNumbs && accnJson.get(Api1Constants.Accession.GENUS) != null || accnJson.get(
Api1Constants.Accession.GENUS_NEW) != null || accnJson.get(Api1Constants.Accession.SPECIES) != null || accnJson.get(Api1Constants.Accession.SPAUTHOR) != null || accnJson
.get(Api1Constants.Accession.SUBTAXA) != null || accnJson.get(Api1Constants.Accession.SUBTAUTHOR) != null) {
updateTaxonomy(accession, accnJson);
}
updateDoi(accession, accnJson.get(Api1Constants.Accession.DOI));
if (accession.getDoi()!=null) {
// Update ACCENUMB if DOI is provided
accession.setAccessionName(dataJson.acceNumb);
}
updateCrop(accession, accnJson.get(Api1Constants.Accession.CROPNAME));
updateAcceNumb(accession, accnJson.get(Api1Constants.Accession.ACCENUMB_NEW));
updateOrgCty(accession, accnJson.get(Api1Constants.Accession.ORIGCTY));
......@@ -468,8 +471,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
}
if (accnJson.has(Api1Constants.Accession.DONORCODE) || accnJson.has(Api1Constants.Accession.DONORNUMB)
|| accnJson.has(Api1Constants.Accession.DONORNAME)) {
if (accnJson.has(Api1Constants.Accession.DONORCODE) || accnJson.has(Api1Constants.Accession.DONORNUMB) || accnJson.has(Api1Constants.Accession.DONORNAME)) {
AccessionExchange accnExch = genesysService.listAccessionExchange(accession.getAccessionId());
if (accnExch == null) {
accnExch = new AccessionExchange();
......@@ -514,7 +516,6 @@ public class BatchRESTServiceImpl implements BatchRESTService {
upsertResponse.setResult(upsertResult);
}
List<Accession> savedData = null;
if (toSave.size() > 0) {
......@@ -524,8 +525,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
// Iterate savedData to extract UUIDs
upsertResponses.stream().forEach(response -> {
Accession accession = saved.stream().filter(a -> useUniqueAcceNumbs ? (a.getAccessionName().equalsIgnoreCase(response.getAcceNumb()))
: (a.getAccessionName().equalsIgnoreCase(response.getAcceNumb()) && a.getTaxonomy().getGenus().equalsIgnoreCase(response.getGenus())))
.findFirst().orElse(null);
: (a.getAccessionName().equalsIgnoreCase(response.getAcceNumb()) && a.getTaxonomy().getGenus().equalsIgnoreCase(response.getGenus()))).findFirst().orElse(null);
UpsertResult result = response.getResult();
if (accession != null) {
......@@ -537,8 +537,8 @@ public class BatchRESTServiceImpl implements BatchRESTService {
result.setUUID(accession.getUuid());
}
});
savedData=saved;
savedData = saved;
}
if (toSaveColl.size() > 0) {
......@@ -582,13 +582,52 @@ public class BatchRESTServiceImpl implements BatchRESTService {
updateAccessionAliases(collNumbs, AliasType.COLLNUMB, false);
LOG.info("Done saving data");
// Update PDCI
genesysService.updatePDCI(savedData.stream().map(a -> a.getId()).collect(Collectors.toSet()));
return upsertResponses;
}
/**
* Update accession DOI in Genesys. If DOI exists in Genesys, it must be provided by the client and must match current value. If DOI does not exist in Genesys,
* it will be registered.
*
* @param accession
* @param value
* @return
* @throws RESTApiException
*/
private boolean updateDoi(AccessionData accession, JsonNode value) throws RESTApiException {
if (accession.getDoi() == null) {
// Genesys does not have DOI for the accession
if (value != null) {
if (value.isTextual()) {
if (DOIUtils.isValidDoi(value.textValue())) {
accession.setDoi(value.textValue());
return true;
} else {
throw new RESTApiException("Invalid DOI value " + value.textValue());
}
} else {
throw new RESTApiDataTypeException("DOI must be a string.");
}
}
return false;
} else {
// Genesys has DOI for the accession
if (value == null || !value.isTextual()) {
throw new RESTApiException("DOI must be resubmitted for accession with DOI " + accession.getDoi() + " ACCENUMB=" + accession.getAccessionName());
} else {
if (StringUtils.equals(accession.getDoi(), value.textValue())) {
return false;
} else {
throw new RESTApiException("Provided DOI does not match existing DOI " + accession.getDoi() + " ACCENUMB=" + accession.getAccessionName());
}
}
}
}
private boolean updateCrop(Accession accession, JsonNode value) throws RESTApiDataTypeException {
if (value != null) {
if (value.isNull()) {
......@@ -738,7 +777,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
private boolean updateAcceNumb(AccessionData accession, JsonNode value) throws RESTApiDataTypeException, RESTApiException {
if (value != null) {
// Rename is possible only if accession exists
if (! accession.getAccessionId().isPersisted()) {
if (!accession.getAccessionId().isPersisted()) {
throw new RESTApiException("Cannot rename a new accession entry");
}
......@@ -787,10 +826,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
private boolean updateUuid(AccessionData accession, JsonNode value) throws RESTApiValueException {
// We don't allow UUID updates
/*
* if (value != null) { final UUID uuid = value.isNull() ? null :
* UUID.fromString(value.textValue());
*
* if (accession.getUuid() == null && uuid != null) {
* if (value != null) { final UUID uuid = value.isNull() ? null : UUID.fromString(value.textValue()); if (accession.getUuid() == null && uuid != null) {
* accession.getAccessionId().setUuid(uuid); return true; } }
*/
// No change
......@@ -827,12 +863,10 @@ public class BatchRESTServiceImpl implements BatchRESTService {
Taxonomy2 ensuredTaxonomy = null;
try {
ensuredTaxonomy = taxonomyService.find(current.getGenus(), current.getSpecies(), current.getSpAuthor(), current.getSubtaxa(),
current.getSubtAuthor());
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() + ")");
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) {
......@@ -1047,6 +1081,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
// Find aliases to remove
for (final AccessionAlias aa : existingAliases) {
if (null == CollectionUtils.find(aliases, new Predicate<AccessionAliasJson>() {
@Override
public boolean evaluate(AccessionAliasJson alias) {
return StringUtils.equals(alias.name, aa.getName());
......@@ -1056,7 +1091,9 @@ public class BatchRESTServiceImpl implements BatchRESTService {
}
}
// Add or update
for (final AccessionAliasJson aa : aliases) {
for (
final AccessionAliasJson aa : aliases) {
AccessionAlias accessionAlias = CollectionUtils.find(existingAliases, new Predicate<AccessionAlias>() {
@Override
public boolean evaluate(AccessionAlias alias) {
......
......@@ -283,7 +283,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Override
public Page<AccessionDetails> listAccessionsDetails(Collection<Long> accessionIds, Pageable pageable) {
if (! accessionIds.isEmpty()) {
if (!accessionIds.isEmpty()) {
Page<Accession> data = accessionRepository.findById(accessionIds, pageable);
return new PageImpl<AccessionDetails>(getAccessionDetails(data.getContent().stream().map(a -> a.getId()).collect(Collectors.toList())), pageable, data.getTotalElements());
} else {
......@@ -330,14 +330,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Override
public Accession getAccession(long accessionId) {
Accession accession = accessionRepository.findOne(accessionId);
if (accession != null) {
accession.getStoRage().size();
if (accession.getCountryOfOrigin() != null)
accession.getCountryOfOrigin().getId();
if (accession.getCrop() != null)
accession.getCrop().getId();
}
return accession;
return lazyLoadAccession(accession);
}
@Override
......@@ -347,8 +340,13 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
}
private Accession lazyLoadAccession(Accession accession) {
if (accession != null)
if (accession != null) {
accession.getStoRage().size();
if (accession.getCountryOfOrigin() != null)
accession.getCountryOfOrigin().getId();
if (accession.getCrop() != null)
accession.getCrop().getId();
}
return accession;
}
......@@ -854,8 +852,8 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
}
/**
* Update {@link SvalbardDeposit} data and link with Genesys accessions. The primary key for {@link SvalbardDeposit}
* is the <code>sgsv_id</code> as provided by NordGen.
* Update {@link SvalbardDeposit} data and link with Genesys accessions. The primary key for {@link SvalbardDeposit} is the <code>sgsv_id</code> as provided by
* NordGen.
*
* Any existing SGSV records are first deleted. Inserted and linked with accessions in this method.
*
......@@ -1180,7 +1178,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
final BufferedWriter osw = new BufferedWriter(new OutputStreamWriter(zos));
osw.write("<?xml version='1.0' encoding='utf-8'?>\n");
osw.write(
"<archive xmlns=\"http://rs.tdwg.org/dwc/text/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd\">\n");
"<archive xmlns=\"http://rs.tdwg.org/dwc/text/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd\">\n");
osw.write("<core encoding=\"UTF-8\" fieldsTerminatedBy=\",\" linesTerminatedBy=\"\\n\" fieldsEnclosedBy=\"&quot;\" ignoreHeaderLines=\"0\">\n");
osw.write("\t<files><location>core.csv</location></files>\n");
osw.write("\t<id index=\"0\" />\n");
......@@ -1276,8 +1274,8 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
private void writeAccessionsCore(final AppliedFilters filters, ZipOutputStream zos) throws IOException {
@SuppressWarnings("resource")
final CSVWriter csv = new CSVWriter(new BufferedWriter(new OutputStreamWriter(zos)), ',', '"', '\\', "\n");
csv.writeNext(new String[] { "genesysId", "uuid", "instCode", "acceNumb", "genus", "species", "fullTaxa", "orgCty", "acqSrc", "acqDate", "mlsStat", "available", "historic", "storage",
"sampStat", "duplSite", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate", "doi" });
csv.writeNext(new String[] { "genesysId", "uuid", "instCode", "acceNumb", "genus", "species", "fullTaxa", "orgCty", "acqSrc", "acqDate", "mlsStat", "available", "historic",
"storage", "sampStat", "duplSite", "createdBy", "createdDate", "lastModifiedBy", "lastModifiedDate", "doi" });
final ResultSetHelper csvResultsetHelper = new CSVResultSetHelper();
......@@ -1448,7 +1446,7 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
final BufferedWriter osw = new BufferedWriter(new OutputStreamWriter(zos));
osw.write("<?xml version='1.0' encoding='utf-8'?>\n");
osw.write(
"<archive xmlns=\"http://rs.tdwg.org/dwc/text/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd\">\n");
"<archive xmlns=\"http://rs.tdwg.org/dwc/text/\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://rs.tdwg.org/dwc/text/ http://rs.tdwg.org/dwc/text/tdwg_dwc_text.xsd\">\n");
osw.write("<core encoding=\"UTF-8\" fieldsTerminatedBy=\",\" linesTerminatedBy=\"\\n\" fieldsEnclosedBy=\"&quot;\" ignoreHeaderLines=\"0\">\n");
osw.write("\t<files><location>core.csv</location></files>\n");
osw.write("\t<id index=\"0\" />\n");
......
......@@ -49,6 +49,8 @@ import org.genesys2.server.service.impl.SearchException;
import org.genesys2.server.servlet.controller.rest.model.AccessionHeaderJson;
import org.genesys2.server.servlet.controller.rest.model.AccessionNamesJson;
import org.genesys2.spring.ResourceNotFoundException;
import org.genesys2.util.DOIUtils;
import org.genesys2.util.InvalidDOIException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.CannotAcquireLockException;
import org.springframework.data.domain.Page;
......@@ -109,8 +111,7 @@ public class AccessionController extends RestController {
* @throws NonUniqueAccessionException
*/
@RequestMapping(value = "/exists/{instCode}/{genus:.+}", method = RequestMethod.GET, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody boolean exists(@PathVariable("instCode") String instCode, @PathVariable("genus") String genus,
@RequestParam("acceNumb") String acceNumb) throws NonUniqueAccessionException {
public @ResponseBody boolean exists(@PathVariable("instCode") String instCode, @PathVariable("genus") String genus, @RequestParam("acceNumb") String acceNumb) throws NonUniqueAccessionException {
if (LOG.isDebugEnabled()) {
LOG.debug("Checking if accn exists " + instCode + "." + acceNumb + " genus=" + genus);
}
......@@ -195,8 +196,7 @@ public class AccessionController extends RestController {
* @throws RESTApiException
*/
@RequestMapping(value = "/{instCode}/upsert", method = { RequestMethod.POST, RequestMethod.PUT }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody Object upsertInstituteAccession(@PathVariable("instCode") String instCode, @RequestBody String content)
throws JsonProcessingException, IOException, RESTApiException {
public @ResponseBody Object upsertInstituteAccession(@PathVariable("instCode") String instCode, @RequestBody String content) throws JsonProcessingException, IOException, RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
......@@ -210,18 +210,26 @@ public class AccessionController extends RestController {
if (json.isArray()) {
for (final JsonNode j : json) {
final AccessionHeaderJson dataJson = readAid3(j);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
try {
final AccessionHeaderJson dataJson = readAid3(j);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
}
batch.put(dataJson, (ObjectNode) j);
} catch (InvalidDOIException e) {
throw new RESTApiException("Invalid DOI", e);
}
batch.put(dataJson, (ObjectNode) j);
}
} else {
final AccessionHeaderJson dataJson = readAid3(json);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
try {
final AccessionHeaderJson dataJson = readAid3(json);
if (!instCode.equals(dataJson.instCode)) {
throw new RuntimeException("Accession does not belong to instCode=" + instCode + " acn=" + dataJson);
}
batch.put(dataJson, (ObjectNode) json);
} catch (InvalidDOIException e) {
throw new RESTApiException("Invalid DOI", e);
}
batch.put(dataJson, (ObjectNode) json);
}
Throwable cause = null;
......@@ -295,10 +303,8 @@ public class AccessionController extends RestController {
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/names", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = {
MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody List<AccessionOpResponse> upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<AccessionNamesJson> batch)
throws RESTApiException {
@RequestMapping(value = "/{instCode}/names", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody List<AccessionOpResponse> upsertAccessionNames(@PathVariable("instCode") String instCode, @RequestBody List<AccessionNamesJson> batch) throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
final FaoInstitute institute = instituteService.getInstitute(instCode);
......@@ -357,10 +363,9 @@ public class AccessionController extends RestController {
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/delete-named", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = {
MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody List<AccessionOpResponse> deleteAccessions(@PathVariable("instCode") String instCode, @RequestBody List<AccessionHeaderJson> batch)
throws RESTApiException {
@RequestMapping(value = "/{instCode}/delete-named", method = { RequestMethod.POST, RequestMethod.PUT }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = {
MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody List<AccessionOpResponse> deleteAccessions(@PathVariable("instCode") String instCode, @RequestBody List<AccessionHeaderJson> batch) throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
final FaoInstitute institute = instituteService.getInstitute(instCode);
......@@ -422,8 +427,7 @@ public class AccessionController extends RestController {
* @throws IOException
* @throws JsonProcessingException
*/
@RequestMapping(value = "/{instCode}/delete", method = { RequestMethod.POST }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = {
MediaType.APPLICATION_JSON_VALUE })
@RequestMapping(value = "/{instCode}/delete", method = { RequestMethod.POST }, consumes = { MediaType.APPLICATION_JSON_VALUE }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody int deleteAccessionsById(@PathVariable("instCode") String instCode, @RequestBody List<Long> batch) throws RESTApiException {
// User's permission to WRITE to this WIEWS institute are checked in
// BatchRESTService.
......@@ -445,8 +449,7 @@ public class AccessionController extends RestController {
// FIXME Not using institute...
@RequestMapping(value = "/{instCode}/list", method = { RequestMethod.GET }, produces = { MediaType.APPLICATION_JSON_VALUE })
public @ResponseBody Page<AccessionDetails> list(@PathVariable("instCode") String instCode, @RequestParam("page") int page,
@RequestParam("query") String query) throws SearchException {
public @ResponseBody Page<AccessionDetails> list(@PathVariable("instCode") String instCode, @RequestParam("page") int page, @RequestParam("query") String query) throws SearchException {