Commit 549ec867 authored by Matija Obreza's avatar Matija Obreza
Browse files

Test for late registration of Accessions with Datasets

parent 6be77327
......@@ -25,10 +25,12 @@ import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.types.Predicate;
import com.querydsl.core.types.dsl.SetPath;
import org.genesys2.server.model.genesys.QAccessionRef;
import org.genesys2.server.service.filter.AccessionFilter;
/**
* The Class AccessionRefFilter.
*
* @author Viacheslav Pavlov
* @author Matija Obreza
*/
public class AccessionRefFilter {
......@@ -47,6 +49,9 @@ public class AccessionRefFilter {
/** The acce numb. */
public StringFilter acceNumb;
/** Matched accession filter */
public AccessionFilter accession;
/**
* Builds the query.
......@@ -73,6 +78,9 @@ public class AccessionRefFilter {
if (acceNumb != null) {
and.and(acceNumb.buildQuery(identifier.acceNumb));
}
if (accession != null) {
and.and(accession.buildQuery(identifier.accession));
}
return and;
}
......
package org.genesys2.server.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.QDataset;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.QSubset;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.persistence.SubsetRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.List;
@Aspect
@Component
public class AccessionAspect {
/** The Constant LOG. */
public static final Logger LOG = LoggerFactory.getLogger(AccessionAspect.class);
@Autowired
private DatasetRepository datasetRepository;
@Autowired
private SubsetRepository subsetRepository;
@Around(value = "execution(* org.genesys2.server.persistence.AccessionRepository.delete(*)) && args(accession)")
public Object aroundDeleteAccession(ProceedingJoinPoint joinPoint, Accession accession) throws Throwable {
LOG.debug("Around delete accession" + accession);
List<Dataset> datasets = (List<Dataset>) datasetRepository.findAll(QDataset.dataset.accessionRefs.any().accession.eq(accession));
for (Dataset dataset: datasets) {
dataset.getAccessionRefs().stream()
.filter(accessionRef -> accessionRef.getAccession().equals(accession))
.forEach(accessionRef -> accessionRef.setAccession(null));
datasetRepository.save(dataset);
}
List<Subset> subsets = (List<Subset>) subsetRepository.findAll(QSubset.subset.accessions.any().accession.eq(accession));
for (Subset subset: subsets) {
subset.getAccessions().stream()
.filter(accessionRef -> accessionRef.getAccession().equals(accession))
.forEach(accessionRef -> accessionRef.setAccession(null));
subsetRepository.save(subset);
}
return joinPoint.proceed();
}
@Around(value = "execution(* org.genesys2.server.persistence.AccessionRepository.save(*)) && args(accession)")
public Object aroundSaveAccession(ProceedingJoinPoint joinPoint, Accession accession) throws Throwable {
LOG.debug("Around save accession" + accession);
List<Dataset> datasets = (List<Dataset>) datasetRepository.findAll(
QDataset.dataset.accessionRefs.any().acceNumb.eq(accession.getAccessionNumber())
.and(QDataset.dataset.accessionRefs.any().genus.eq(accession.getGenus()))
.and(QDataset.dataset.accessionRefs.any().instCode.eq(accession.getInstCode()))
);
for (Dataset dataset: datasets) {
for (AccessionRef accRef: dataset.getAccessionRefs()) {
accRef.setAccession(accession);
LOG.debug("Setting accessionRef.id of {accRef} to {acceId} for dataset: {dataset}", accRef.toString(), accession.getId(), dataset.toString());
}
datasetRepository.save(dataset);
}
List<Subset> subsets = (List<Subset>) subsetRepository.findAll(
QSubset.subset.accessions.any().acceNumb.eq(accession.getAccessionNumber())
.and(QSubset.subset.accessions.any().genus.eq(accession.getGenus()))
.and(QSubset.subset.accessions.any().instCode.eq(accession.getInstCode()))
);
for (Subset subset: subsets) {
for (AccessionRef accRef: subset.getAccessions()) {
accRef.setAccession(accession);
LOG.debug("Setting accessionRef.id of {accRef} to {acceId} for subset: {subset}", accRef.toString(), subset.toString());
}
subsetRepository.save(subset);
}
return joinPoint.proceed();
}
}
......@@ -26,7 +26,6 @@ import javax.persistence.ManyToOne;
import org.genesys.blocks.model.JsonViews;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import com.fasterxml.jackson.annotation.JsonUnwrapped;
import com.fasterxml.jackson.annotation.JsonView;
/**
......
/*
* Copyright 2018 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.genesys2.server.service.worker;
import java.util.Collection;
import java.util.List;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.QDataset;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.QSubset;
import org.genesys2.server.model.impl.Subset;
import org.genesys2.server.persistence.SubsetRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
/**
* Update {@link AccessionRef} updates when accessions are added or removed from
* Genesys.
*
* @author Viacheslav Pavlov
* @author Matija Obreza
*/
@Aspect
@Component
public class AccessionRefAspect {
/** The Constant LOG. */
public static final Logger LOG = LoggerFactory.getLogger(AccessionRefAspect.class);
@Autowired
private DatasetRepository datasetRepository;
@Autowired
private SubsetRepository subsetRepository;
@Before(value = "execution(* org.genesys2.server.persistence.AccessionRepository.delete(*)) && args(arg)")
public void beforeDeleteAccession(JoinPoint joinPoint, Object arg) throws Throwable {
LOG.debug("Before delete accession" + arg);
if (arg == null) {
return;
}
if (arg instanceof Collection<?>) {
((Collection<?>) arg).forEach(accession -> {
datasetsDereference((Accession) accession);
subsetsDereference((Accession) accession);
});
} else if (arg instanceof Accession) {
datasetsDereference((Accession) arg);
subsetsDereference((Accession) arg);
}
}
private void subsetsDereference(Accession accession) {
List<Subset> subsets = (List<Subset>) subsetRepository.findAll(QSubset.subset.accessions.any().accession.eq(accession));
for (Subset subset : subsets) {
subset.getAccessions().stream().filter(accessionRef -> accessionRef.getAccession().equals(accession)).forEach(accessionRef -> accessionRef.setAccession(null));
subsetRepository.save(subset);
}
}
private void datasetsDereference(Accession accession) {
List<Dataset> datasets = (List<Dataset>) datasetRepository.findAll(QDataset.dataset.accessionRefs.any().accession.eq(accession));
for (Dataset dataset : datasets) {
dataset.getAccessionRefs().stream().filter(accessionRef -> accessionRef != null).filter(accessionRef -> accessionRef.getAccession().getUuid().equals(accession
.getUuid())).forEach(accessionRef -> accessionRef.setAccession(null));
datasetRepository.save(dataset);
}
}
@AfterReturning(value = "execution(* org.genesys2.server.persistence.AccessionRepository.save(*))", returning = "returnObject")
public void afterSaveAccessions(JoinPoint joinPoint, Object returnObject) throws Throwable {
if (returnObject == null) {
return;
}
if (returnObject instanceof Collection<?>) {
((Collection<?>) returnObject).forEach(accession -> {
updateDatasets((Accession) accession);
updateSubsets((Accession) accession);
});
} else if (returnObject instanceof Accession) {
updateDatasets((Accession) returnObject);
updateSubsets((Accession) returnObject);
}
}
private void updateSubsets(Accession accession) {
List<Subset> subsets = (List<Subset>) subsetRepository.findAll(QSubset.subset.accessions.any().acceNumb.eq(accession.getAccessionNumber()).and(QSubset.subset.accessions
.any().genus.eq(accession.getTaxonomy().getGenus())).and(QSubset.subset.accessions.any().instCode.eq(accession.getInstitute().getCode())));
for (Subset subset : subsets) {
for (AccessionRef accRef : subset.getAccessions()) {
accRef.setAccession(accession);
LOG.debug("Setting accessionRef.id of {accRef} to {acceId} for subset: {subset}", accRef.toString(), subset.toString());
}
subsetRepository.save(subset);
}
}
private void updateDatasets(Accession accession) {
LOG.debug("After returning saved accession" + accession);
List<Dataset> datasets = (List<Dataset>) datasetRepository.findAll(QDataset.dataset.accessionRefs.any().acceNumb.eq(accession.getAccessionNumber()).and(
QDataset.dataset.accessionRefs.any().genus.eq(accession.getTaxonomy().getGenus())).and(QDataset.dataset.accessionRefs.any().instCode.eq(accession.getInstitute()
.getCode())));
for (Dataset dataset : datasets) {
for (AccessionRef accRef : dataset.getAccessionRefs()) {
accRef.setAccession(accession);
LOG.debug("Setting accessionRef.id of {accRef} to {acceId} for dataset: {dataset}", accRef.toString(), accession.getId(), dataset.toString());
}
datasetRepository.save(dataset);
}
}
}
......@@ -16,10 +16,10 @@
package org.genesys.test.catalog.services;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.genesys.catalog.model.Partner;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetCreator;
import org.genesys.catalog.model.dataset.DatasetLocation;
......@@ -27,19 +27,23 @@ import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.dataset.DatasetVersionsRepository;
import org.genesys.catalog.service.DatasetService;
import org.genesys.filerepository.persistence.RepositoryFilePersistence;
import org.genesys2.server.api.model.AccessionHeaderJson;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.worker.AccessionUploader;
import org.junit.After;
import org.junit.Before;
import org.springframework.beans.factory.annotation.Autowired;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.collect.Lists;
public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
......@@ -74,6 +78,8 @@ public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
private InstituteService instituteService;
@Autowired
private TaxonomyService taxonomyService;
@Autowired
private AccessionUploader accessionUploader;
public AbstractDatasetServiceTest() {
super();
......@@ -155,21 +161,47 @@ public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
return accessionRef;
}
protected Accession buildAndSaveAccession(final String acceNumb, final String instCode, final String genus) {
FaoInstitute institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
Accession a = new Accession();
a.setAccessionId(new AccessionId());
a.setInstitute(institute);
a.setAccessionNumber(acceNumb);
Taxonomy2 taxon = new Taxonomy2();
taxon.setGenus(genus);
a.setTaxonomy(taxonomyService.ensureTaxonomy(taxon));
return accessionRepository.save(a);
protected Accession upsertAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
ObjectMapper objectMapper = new ObjectMapper();
ArrayNode updates = objectMapper.createArrayNode();
ObjectNode accession = updates.addObject();
accession.put("instituteCode", instCode);
accession.put("accessionNumber", acceNumb);
ObjectNode taxa = accession.putObject("taxonomy");
taxa.put("genus", genus);
accessionUploader.upsertAccessions(institute, updates);
return accessionRepository.findOne(institute, null, acceNumb, genus);
}
protected void deleteAccession(final String instCode, final String acceNumb, final String genus) {
FaoInstitute institute = instituteService.getInstitute(instCode);
if (institute == null) {
institute = new FaoInstitute();
institute.setCode(instCode);
institute.setFullName("Test institute " + instCode);
instituteService.update(Lists.newArrayList(institute));
institute = instituteService.getInstitute(instCode);
}
AccessionHeaderJson ah=new AccessionHeaderJson();
ah.instCode=instCode;
ah.acceNumb=acceNumb;
ah.genus=genus;
List<AccessionHeaderJson> identifiers=Lists.newArrayList(ah);
accessionUploader.deleteAccessions(institute, identifiers);
assert(accessionRepository.findOne(institute, null, acceNumb, genus) == null);
}
}
\ No newline at end of file
......@@ -32,10 +32,9 @@ import java.util.stream.Collectors;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
import org.genesys.catalog.exceptions.NotFoundElement;
import org.genesys.catalog.model.filters.AccessionRefFilter;
import org.genesys2.server.model.genesys.AccessionRef;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.model.dataset.DatasetLocation;
import org.genesys.catalog.model.filters.AccessionRefFilter;
import org.genesys.catalog.model.filters.DatasetFilter;
import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.model.traits.Descriptor.DataType;
......@@ -45,6 +44,7 @@ import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionRef;
import org.junit.Test;
import org.springframework.dao.ConcurrencyFailureException;
import org.springframework.dao.DataIntegrityViolationException;
......@@ -163,7 +163,7 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
@Test
public void testLookupAccessionsForAccessionIdentifiersInDataset() {
final Accession accession = buildAndSaveAccession("TestAccNum", TEST_INSTCODE, "TestGen");
final Accession accession = upsertAccession(TEST_INSTCODE, "TestAccNum", "TestGen");
Dataset input = buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, null, PublishState.DRAFT);
input = datasetService.createDataset(input);
......@@ -192,9 +192,9 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
@Test
public void testCreateAndUpdateAccessionsInDataset() {
final Accession accession1 = buildAndSaveAccession(TEST_ACCNUM, TEST_INSTCODE, TEST_GEN);
final Accession accession2 = buildAndSaveAccession(TEST_ACCNUM2, TEST_INSTCODE2, TEST_GEN2);
final Accession accession3 = buildAndSaveAccession(TEST_ACCNUM3, TEST_INSTCODE3, TEST_GEN3);
final Accession accession1 = upsertAccession(TEST_INSTCODE, TEST_ACCNUM, TEST_GEN);
final Accession accession2 = upsertAccession(TEST_INSTCODE2, TEST_ACCNUM2, TEST_GEN2);
final Accession accession3 = upsertAccession(TEST_INSTCODE3, TEST_ACCNUM3, TEST_GEN3);
Dataset input = buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, null, PublishState.DRAFT);
input = datasetService.createDataset(input);
......@@ -241,7 +241,7 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
@Test
public void testListDatasetsByAccession() {
final Accession accession = buildAndSaveAccession(TEST_ACCNUM, TEST_INSTCODE, TEST_GEN);
final Accession accession = upsertAccession(TEST_INSTCODE, TEST_ACCNUM, TEST_GEN);
Dataset input = buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, null, PublishState.PUBLISHED);
input = datasetService.createDataset(input);
......@@ -793,4 +793,84 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertEquals(savedLocation2.getEndDate(), loadedDataset.getEndDate());
}
@Test
public void testLateAccessionRegistration() {
final Set<AccessionRef> ids1 = new HashSet<>();
ids1.add(makeAccessionIdentifier(TEST_INSTCODE, "A1", "Musa", null));
ids1.add(makeAccessionIdentifier(TEST_INSTCODE, "A2", "Musa", null));
ids1.add(makeAccessionIdentifier(TEST_INSTCODE, "A8", "Musa", null));
final Dataset dataset1 = datasetService.createDataset(buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, ids1, PublishState.PUBLISHED));
assertThat(dataset1.getAccessionCount(), is(3));
dataset1.getAccessionRefs().forEach(acceRef -> assertThat(acceRef.getAccession(), nullValue()));
final Set<AccessionRef> ids2 = new HashSet<>();
ids2.add(makeAccessionIdentifier(TEST_INSTCODE, "A1", "Musa", null));
ids2.add(makeAccessionIdentifier(TEST_INSTCODE, "A2", "Musa", null));
ids2.add(makeAccessionIdentifier(TEST_INSTCODE, "A3", "Manihot", null));
final Dataset dataset2 = datasetService.createDataset(buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, ids2, PublishState.PUBLISHED));
assertThat(dataset2.getAccessionCount(), is(3));
dataset2.getAccessionRefs().forEach(acceRef -> assertThat(acceRef.getAccession(), nullValue()));
final Accession accession1 = upsertAccession(TEST_INSTCODE, "A2", "Musa");
List<Dataset> datasets1 = datasetService.listByAccession(accession1);
assertThat(datasets1, not(nullValue()));
assertThat(datasetRepository.count(), is(2l));
assertThat(datasets1.size(), is(2));
datasets1.forEach(ds -> assertThat(datasetService.loadDataset(ds).getAccessionRefs().size(), is(3)));
final Accession accession2 = upsertAccession(TEST_INSTCODE, "A8", "Musa");
datasets1 = datasetService.listByAccession(accession2);
assertThat(datasets1.size(), is(1));
assertThat(datasets1.get(0).getUuid(), is(dataset1.getUuid()));
assertThat(datasetService.loadDataset(datasets1.get(0)).getAccessionRefs().size(), is(3));
}
@Test
public void testAccessionDeleted() {
final Set<AccessionRef> ids1 = new HashSet<>();
ids1.add(makeAccessionIdentifier(TEST_INSTCODE, "A1", "Musa", null));
ids1.add(makeAccessionIdentifier(TEST_INSTCODE, "A2", "Musa", null));
ids1.add(makeAccessionIdentifier(TEST_INSTCODE, "A8", "Musa", null));
Dataset dataset1 = datasetService.createDataset(buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, ids1, PublishState.PUBLISHED));
assertThat(dataset1.getAccessionCount(), is(3));
final Set<AccessionRef> ids2 = new HashSet<>();
ids2.add(makeAccessionIdentifier(TEST_INSTCODE, "A1", "Musa", null));
ids2.add(makeAccessionIdentifier(TEST_INSTCODE, "A2", "Musa", null));
ids2.add(makeAccessionIdentifier(TEST_INSTCODE, "A3", "Manihot", null));
Dataset dataset2 = datasetService.createDataset(buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, ids2, PublishState.PUBLISHED));
assertThat(dataset2.getAccessionCount(), is(3));
upsertAccession(TEST_INSTCODE, "A2", "Musa");
upsertAccession(TEST_INSTCODE, "A8", "Musa");
dataset1 = datasetService.loadDataset(dataset1.getUuid());
dataset2 = datasetService.loadDataset(dataset2.getUuid());
AccessionRef acceRef1 = dataset1.getAccessionRefs().stream().filter(acceRef -> acceRef.getAcceNumb().equals("A8") && acceRef.getAccession() != null).findFirst().orElse(null);
AccessionRef acceRef2 = dataset2.getAccessionRefs().stream().filter(acceRef -> acceRef.getAccession() != null).findFirst().orElse(null);
assertThat(acceRef1, notNullValue());
assertThat(acceRef1.getAccession(), notNullValue());
assertThat(acceRef1.getAccession().getAccessionNumber(), is("A8"));
assertThat(acceRef2, notNullValue());
assertThat(acceRef2.getAccession(), notNullValue());
assertThat(acceRef2.getAccession().getAccessionNumber(), is("A2"));
deleteAccession(TEST_INSTCODE, "A2", "Musa");
dataset2 = datasetService.loadDataset(dataset2.getUuid());
assertThat(dataset2.getAccessionCount(), is(3));
assertThat(dataset2.getAccessionRefs().size(), is(3));
dataset2.getAccessionRefs().forEach(acceRef -> assertThat(acceRef.getAccession(), nullValue()));
deleteAccession(TEST_INSTCODE, "A8", "Musa");
dataset1 = datasetService.loadDataset(dataset1.getUuid());
assertThat(dataset1.getAccessionCount(), is(3));
assertThat(dataset1.getAccessionRefs().size(), is(3));
dataset1.getAccessionRefs().forEach(acceRef -> assertThat(acceRef.getAccession(), nullValue()));
}
}
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