Commit a8b0d605 authored by Matija Obreza's avatar Matija Obreza
Browse files

AccessionIdentifier3 includes DOI, AccessionRepository refactored

parent dece2d04
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -17,9 +17,12 @@
package org.genesys2.server.model.impl;
public interface AccessionIdentifier3 {
String getDoi();
String getHoldingInstitute();
String getAccessionName();
String getAccessionNumber();
String getGenus();
}
......@@ -20,12 +20,13 @@ import java.util.List;
import java.util.Set;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.impl.AccessionIdentifier3;
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, Set<AccessionHeaderJson> accessionIds) throws NonUniqueAccessionException;
List<Accession> find(FaoInstitute institute, Set<? extends AccessionIdentifier3> accessionIds) throws NonUniqueAccessionException;
Accession findOne(FaoInstitute institute, String doi, String acceNumb, String genus);
}
......@@ -35,7 +35,7 @@ import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
public interface AccessionRepository extends JpaRepository<Accession, Long> {
public interface AccessionRepository extends JpaRepository<Accession, Long>, AccessionCustomRepository {
@Query("select a.id from Accession a")
public List<Long> listAccessionsIds(Pageable pageable);
......@@ -55,14 +55,14 @@ public interface AccessionRepository extends JpaRepository<Accession, Long> {
long countByOrigin(Country country);
/**
* Return the number of active ({@link Accession#historic} == false) accession records where holding institute is in the specified country
* Return the number of active ({@link Accession#historic} == false) accession records where holding institute is in the specified country
*/
@Query("select count(a) from Accession a where a.institute.country = ?1 and a.historic = false")
long countByLocation(Country country);
@Query(value = "select a.cropName, count(a.id) from Accession a where a.institute = ?1 and a.historic = false group by a.cropName order by count(a) desc", countQuery = "select count(distinct a.cropName) from Accession a where a.institute = ?1")
Page<Object[]> statisticsCropNameInInstitute(FaoInstitute institute, Pageable pageable);
@Query(value = "select t.genus, count(a.id) from Accession a inner join a.taxonomy t where a.institute = ?1 and a.historic = false group by t.genus order by count(a) desc", countQuery = "select count(distinct a.taxonomy.taxGenus) from Accession a where a.institute = ?1")
Page<Object[]> statisticsGenusInInstitute(FaoInstitute institute, Pageable pageable);
......@@ -92,11 +92,8 @@ public interface AccessionRepository extends JpaRepository<Accession, Long> {
@Query("select a from Accession a where a.institute.code = ?1 and a.accessionName = ?2")
Accession findByInstituteCodeAndAccessionName(String instCode, String accessionName);
@Query("select distinct a from Accession a where a.accessionName=?2 and (a.institute.code=?1 or a.institute.codeSGSV=?1) and a.taxonomy.genus=?3")
Accession findOneSGSV(String instCode, String acceNumb, String genus);
@Query("select a from Accession a where a.institute.code=:instCode and a.accessionName=:acceNumb and a.taxonomy.genus=:genus")
Accession findOne(@Param("instCode") String holdingInstitute, @Param("acceNumb") String accessionName, @Param("genus") String genus);
@Query("select distinct a from Accession a where a.accessionName=?2 and (a.institute.code=?1 or a.institute.codeSGSV=?1) and a.taxonomy.genus=?3")
Accession findOneSGSV(String instCode, String acceNumb, String genus);
@Query("select count(a.id) from Accession a where a.id in ( ?1 ) and a.historic = false and (a.availability is null or a.availability = true) and a.institute.allowMaterialRequests = true")
long countAvailableForDistribution(Set<Long> accessionIds);
......@@ -114,20 +111,20 @@ public interface AccessionRepository extends JpaRepository<Accession, Long> {
public List<Long> listAccessionsIds(Taxonomy2 taxonomy);
@Modifying
@Query(nativeQuery=true, value="delete from accession where id = ?1")
@Query(nativeQuery = true, value = "delete from accession where id = ?1")
public void deleteActive(long id);
@Query("select a from Accession a where a.accessionId.uuid = ?1")
public Accession findOneByUuid(UUID uuid);
public List<Accession> findByAccessionName(String acceNumb);
@Query("select a from Accession a")
Stream<Accession> streamAll();
public long countByHistoric(boolean historic);
@Query(nativeQuery=true, value="update accession a inner join sgsvdeposit sd on sd.acceId=a.id set a.inSGSV=true where sd.id in (?1)")
@Query(nativeQuery = true, value = "update accession a inner join sgsvdeposit sd on sd.acceId=a.id set a.inSGSV=true where sd.id in (?1)")
@Modifying
public void setInSvalbard(List<Long> sgsvId);
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2017 Global Crop Diversity Trust
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
......@@ -33,9 +33,9 @@ import javax.persistence.criteria.Root;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.AccessionIdentifier3;
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;
......@@ -44,8 +44,8 @@ import org.springframework.transaction.annotation.Transactional;
@Repository
@Transactional(readOnly = true)
public class AccessionCustomRepositoryImpl implements AccessionCustomRepository, InitializingBean {
public static final Logger LOG = LoggerFactory.getLogger(AccessionCustomRepositoryImpl.class);
public class AccessionRepositoryCustomImpl implements AccessionCustomRepository, InitializingBean {
public static final Logger LOG = LoggerFactory.getLogger(AccessionRepositoryCustomImpl.class);
@PersistenceContext
private EntityManager em;
......@@ -53,7 +53,12 @@ public class AccessionCustomRepositoryImpl implements AccessionCustomRepository,
private CriteriaBuilder criteriaBuilder;
@Override
public List<Accession> find(FaoInstitute institute, Set<AccessionHeaderJson> accessionIds) throws NonUniqueAccessionException {
public void afterPropertiesSet() throws Exception {
this.criteriaBuilder = em.getCriteriaBuilder();
}
@Override
public List<Accession> find(FaoInstitute institute, Set<? extends AccessionIdentifier3> accessionIds) throws NonUniqueAccessionException {
boolean uniqueAcceNumbs = institute.hasUniqueAcceNumbs();
CriteriaQuery<Accession> cq = criteriaBuilder.createQuery(Accession.class);
......@@ -68,8 +73,8 @@ 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());
Set<String> uniqueDois = accessionIds.stream().map(aid -> aid.getDoi()).filter(doi -> doi != null).distinct().collect(Collectors.toSet());
Set<String> acceNumbs = accessionIds.stream().map(aid -> aid.getAccessionNumber()).filter(acceNumb -> acceNumb != null).distinct().collect(Collectors.toSet());
if (uniqueDois.size() > 0) {
restrictions.add(root.get("doi").in(uniqueDois));
......@@ -82,8 +87,8 @@ public class AccessionCustomRepositoryImpl implements AccessionCustomRepository,
LOG.debug("*** {} accenumbs={}", institute.getCode(), acceNumbs);
} else {
// A lot of .. (acceNumb=? and genus=?)
for (AccessionHeaderJson ah : accessionIds) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("accessionName"), ah.acceNumb), criteriaBuilder.equal(tax.get("genus"), ah.genus)));
for (AccessionIdentifier3 ah : accessionIds) {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("accessionName"), ah.getAccessionNumber()), criteriaBuilder.equal(tax.get("genus"), ah.getGenus())));
}
}
cq.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("institute"), institute), criteriaBuilder.or(restrictions.toArray(new Predicate[] {}))));
......@@ -110,7 +115,26 @@ public class AccessionCustomRepositoryImpl implements AccessionCustomRepository,
}
@Override
public void afterPropertiesSet() throws Exception {
this.criteriaBuilder = em.getCriteriaBuilder();
public Accession findOne(FaoInstitute institute, String doi, String acceNumb, String genus) {
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);
Join<Object, Object> tax = root.join("taxonomy");
List<Predicate> restrictions = new ArrayList<Predicate>();
if (doi != null) {
restrictions.add(criteriaBuilder.equal(root.get("doi"), doi));
} else if (institute.hasUniqueAcceNumbs()) {
restrictions.add(criteriaBuilder.equal(root.get("accessionName"), acceNumb));
} else {
restrictions.add(criteriaBuilder.and(criteriaBuilder.equal(root.get("accessionName"), acceNumb), criteriaBuilder.equal(tax.get("genus"), genus)));
}
cq.where(criteriaBuilder.and(criteriaBuilder.equal(root.get("institute"), institute), criteriaBuilder.or(restrictions.toArray(new Predicate[] {}))));
return em.createQuery(cq).getSingleResult();
}
}
......@@ -115,7 +115,7 @@ public interface GenesysService {
Accession getAccession(AccessionIdentifier3 aid3) throws NonUniqueAccessionException;
Accession getAccession(String instCode, String acceNumb, String genus) throws NonUniqueAccessionException;
Accession getAccession(String instCode, String doi, String acceNumb, String genus) throws NonUniqueAccessionException;
Page<Object[]> statisticsCropNameByInstitute(FaoInstitute faoInstitute, Pageable pageRequest);
......
......@@ -50,7 +50,7 @@ import org.genesys2.server.model.impl.Country;
import org.genesys2.server.model.impl.Crop;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.model.json.Api1Constants;
import org.genesys2.server.persistence.domain.AccessionCustomRepository;
import org.genesys2.server.persistence.domain.AccessionRepository;
import org.genesys2.server.service.AccessionOpResponse;
import org.genesys2.server.service.AccessionOpResponse.UpsertResult;
import org.genesys2.server.service.BatchRESTService;
......@@ -94,7 +94,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
InstituteService instituteService;
@Autowired
AccessionCustomRepository accessionCustomRepository;
AccessionRepository accessionRepository;
@Autowired
private TaxonomyManager taxonomyManager;
......@@ -197,7 +197,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
List<Accession> loaded = null;
try {
loaded = accessionCustomRepository.find(institute, batch.keySet());
loaded = accessionRepository.find(institute, batch.keySet());
} catch (NonUniqueAccessionException e) {
LOG.warn(e.getMessage());
throw new RESTApiException(e.getMessage());
......@@ -1007,7 +1007,6 @@ public class BatchRESTServiceImpl implements BatchRESTService {
public List<AccessionOpResponse> upsertAccessionNames(FaoInstitute institute, List<AccessionNamesJson> batch) throws RESTApiException {
LOG.info("Batch processing " + batch.size() + " entries for " + institute);
final boolean useUniqueAcceNumbs = institute.hasUniqueAcceNumbs();
final List<AccessionOpResponse> upsertResponses = new ArrayList<AccessionOpResponse>();
final List<AccessionAlias> toSave = new ArrayList<AccessionAlias>();
......@@ -1024,11 +1023,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
Accession accession = null;
try {
if (useUniqueAcceNumbs) {
accession = genesysService.getAccession(institute.getCode(), dataJson.acceNumb);
} else {
accession = genesysService.getAccession(institute.getCode(), dataJson.acceNumb, dataJson.genus);
}
accession = genesysService.getAccession(dataJson);
} catch (NonUniqueAccessionException e) {
LOG.warn(e.getMessage());
throw new RESTApiException(e.getMessage());
......@@ -1123,7 +1118,6 @@ public class BatchRESTServiceImpl implements BatchRESTService {
final List<AccessionOpResponse> upsertResponses = new ArrayList<AccessionOpResponse>();
final List<Accession> toDelete = new ArrayList<Accession>(batch.size());
final boolean useUniqueAcceNumbs = institute.hasUniqueAcceNumbs();
for (final AccessionHeaderJson dataJson : batch) {
if (LOG.isDebugEnabled()) {
......@@ -1140,11 +1134,7 @@ public class BatchRESTServiceImpl implements BatchRESTService {
Accession accession;
try {
if (useUniqueAcceNumbs) {
accession = genesysService.getAccession(institute.getCode(), dataJson.acceNumb);
} else {
accession = genesysService.getAccession(institute.getCode(), dataJson.acceNumb, dataJson.genus);
}
accession = genesysService.getAccession(dataJson);
} catch (NonUniqueAccessionException e) {
LOG.warn(e.getMessage());
throw new RESTApiException(e.getMessage());
......
......@@ -294,11 +294,11 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
@Override
public Accession getAccession(AccessionIdentifier3 aid3) throws NonUniqueAccessionException {
try {
Accession accession = accessionRepository.findOne(aid3.getHoldingInstitute(), aid3.getAccessionName(), aid3.getGenus());
Accession accession = accessionRepository.findOne(instituteRepository.findByCode(aid3.getHoldingInstitute()), aid3.getDoi(), aid3.getAccessionNumber(), aid3.getGenus());
return lazyLoadAccession(accession);
} catch (IncorrectResultSizeDataAccessException e) {
LOG.error("Duplicate accession name instCode=" + aid3.getHoldingInstitute() + " acceNumb=" + aid3.getAccessionName() + " genus=" + aid3.getGenus());
throw new NonUniqueAccessionException(aid3.getHoldingInstitute(), aid3.getAccessionName(), aid3.getGenus());
LOG.error("Duplicate accession name instCode=" + aid3.getHoldingInstitute() + " acceNumb=" + aid3.getAccessionNumber() + " genus=" + aid3.getGenus());
throw new NonUniqueAccessionException(aid3.getHoldingInstitute(), aid3.getAccessionNumber(), aid3.getGenus());
}
}
......@@ -314,12 +314,9 @@ public class GenesysServiceImpl implements GenesysService, DatasetService {
}
@Override
public Accession getAccession(String instCode, String acceNumb, String genus) throws NonUniqueAccessionException {
if (genus == null) {
throw new NullPointerException("Genus is required to load accession by instCode, acceNumb and genus");
}
public Accession getAccession(String instCode, String doi, String acceNumb, String genus) throws NonUniqueAccessionException {
try {
Accession accession = accessionRepository.findOne(instCode, acceNumb, genus);
Accession accession = accessionRepository.findOne(instituteRepository.findByCode(instCode), doi, acceNumb, genus);
return lazyLoadAccession(accession);
} catch (IncorrectResultSizeDataAccessException e) {
LOG.error("Duplicate accession name instCode=" + instCode + " acceNumb=" + acceNumb);
......
......@@ -212,7 +212,8 @@ public class AccessionController extends BaseController {
throw new ResourceNotFoundException();
}
try {
final Accession accession = genesysService.getAccession(holdingInstitute, accessionName, genus);
final String doi = null;
final Accession accession = genesysService.getAccession(holdingInstitute, doi, accessionName, genus);
if (accession == null) {
throw new ResourceNotFoundException();
} else {
......
......@@ -116,7 +116,8 @@ public class AccessionController extends RestController {
LOG.debug("Checking if accn exists " + instCode + "." + acceNumb + " genus=" + genus);
}
final Accession accession = genesysService.getAccession(instCode, acceNumb, genus);
final String doi = null;
final Accession accession = genesysService.getAccession(instCode, doi, acceNumb, genus);
if (accession == null) {
LOG.warn("No accession " + instCode + "." + acceNumb + " genus=" + genus);
......
......@@ -219,6 +219,8 @@ public class DatasetController extends RestController {
if (Api1Constants.Accession.INSTCODE.equals(key)) {
dataJson.instCode = value.asText();
} else if (Api1Constants.Accession.DOI.equals(key)) {
dataJson.doi = value.asText();
} else if (Api1Constants.Accession.ACCENUMB.equals(key)) {
dataJson.acceNumb = value.asText();
} else if (Api1Constants.Accession.GENUS.equals(key)) {
......@@ -270,6 +272,7 @@ public class DatasetController extends RestController {
public static class DataJson implements AccessionIdentifier3 {
public String instCode;
public String doi;
public String acceNumb;
public String genus;
private final Map<Long, List<Object>> methodValues = new HashMap<Long, List<Object>>();
......@@ -292,7 +295,12 @@ public class DatasetController extends RestController {
}
@Override
public String getAccessionName() {
public String getDoi() {
return doi;
}
@Override
public String getAccessionNumber() {
return acceNumb;
}
......@@ -305,6 +313,7 @@ public class DatasetController extends RestController {
public String toString() {
return MessageFormat.format("AID3 instCode={0} acceNumb={1} genus={2}", instCode, acceNumb, genus);
}
}
public static class MetadataJson {
......
......@@ -26,6 +26,7 @@ public class AccessionHeaderJson implements AccessionIdentifier3 {
public String acceNumb;
public String genus;
@Override
public String getDoi() {
return doi;
}
......@@ -36,7 +37,7 @@ public class AccessionHeaderJson implements AccessionIdentifier3 {
}
@Override
public String getAccessionName() {
public String getAccessionNumber() {
return acceNumb;
}
......
......@@ -16,7 +16,8 @@
package org.genesys2.server.service.worker;
import static org.junit.Assert.*;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import java.io.IOException;
import java.io.InputStream;
......@@ -220,11 +221,11 @@ public class SGSVUpdateTest {
genesysService.saveAccessions(institute, accessions);
}
private void deleteAccession(String instCode, String acceNumb, String genus, String species) throws NonUniqueAccessionException {
private void deleteAccession(String instCode, String doi, String acceNumb, String genus, String species) throws NonUniqueAccessionException {
final FaoInstitute institute = instituteService.getInstitute(instCode);
final List<Accession> accessions = new ArrayList<Accession>();
accessions.add(genesysService.getAccession(instCode, acceNumb, genus));
accessions.add(genesysService.getAccession(instCode, doi, acceNumb, genus));
genesysService.removeAccessions(institute, accessions);
}
......@@ -343,8 +344,8 @@ public class SGSVUpdateTest {
assertTrue(svalbardRepository.findAll().size() == 2);
deleteAccession("INS001", "TEST0", "Genus", "sp.");
deleteAccession("INS001", "TEST1", "Genus", "sp.");
deleteAccession("INS001", null, "TEST0", "Genus", "sp.");
deleteAccession("INS001", null, "TEST1", "Genus", "sp.");
assertTrue(svalbardRepository.findAll().size() == 0);
}
......
......@@ -46,8 +46,6 @@ import org.genesys2.server.filerepository.service.impl.FilesystemStorageServiceI
import org.genesys2.server.filerepository.service.impl.ImageGalleryServiceImpl;
import org.genesys2.server.filerepository.service.impl.RepositoryServiceImpl;
import org.genesys2.server.filerepository.service.impl.ThumbnailGenerator1;
import org.genesys2.server.persistence.domain.AccessionCustomRepository;
import org.genesys2.server.persistence.domain.AccessionCustomRepositoryImpl;
import org.genesys2.server.persistence.domain.AccessionHistoricRepository;
import org.genesys2.server.persistence.domain.AccessionRepository;
import org.genesys2.server.persistence.domain.ArticleRepository;
......@@ -399,11 +397,6 @@ public abstract class AbstractRestTest extends BaseSpringTest {
return new BatchRESTServiceImpl();
}
@Bean
public AccessionCustomRepository accessionCustomRepository() {
return new AccessionCustomRepositoryImpl();
}
@Bean
public TaxonomyManager taxonomyManager() {
return new TaxonomyManager();
......@@ -615,9 +608,6 @@ public abstract class AbstractRestTest extends BaseSpringTest {
@Autowired
protected AccessionRepository accessionRepository;
@Autowired
protected AccessionCustomRepository accessionCustomRepository;
@Autowired
protected PermissionController permissionController;
......
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