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

Subsets with #accessionRefs instead of #accessions

- Follows Datasets model
parent 549ec867
......@@ -92,14 +92,14 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
@JsonView({ JsonViews.Public.class })
private FaoInstitute institute;
/** The accessions. */
/** The accessionRefs. */
@ElementCollection(fetch = FetchType.LAZY)
@CollectionTable(name = "subset_accessions", joinColumns = @JoinColumn(name = "subsetId"),
// index
indexes = { @Index(columnList = "subsetId, instCode, acceNumb"), @Index(columnList = "subsetId, genus") })
@Field(type = FieldType.Object)
@JsonView({ JsonViews.Public.class })
private List<AccessionRef> accessions;
private List<AccessionRef> accessionRefs;
/** The accession count. */
@Column(name = "accession_count", nullable = false)
......@@ -115,11 +115,9 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
@JsonView({ JsonViews.Public.class })
private List<SubsetCreator> creators;
/** The crops. */
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "subset_crops", joinColumns = @JoinColumn(name = "subsetId"),
indexes = { @Index(columnList = "subsetId, crop") })
@CollectionTable(name = "subset_crops", joinColumns = @JoinColumn(name = "subsetId"), indexes = { @Index(columnList = "subsetId, crop") })
@Column(name = "crop", nullable = false, length = Crop.CROP_SHORTNAME_LENGTH)
@JsonView({ JsonViews.Minimal.class })
private Set<String> crops;
......@@ -141,13 +139,14 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
trimStringsToNull();
this.wiewsCode = this.institute.getCode();
if (this.getAccessions() != null) {
this.accessionCount=this.getAccessions().size();
if (this.getAccessionRefs() != null) {
this.accessionCount = this.getAccessionRefs().size();
}
}
/* (non-Javadoc)
/*
* (non-Javadoc)
* @see org.genesys.blocks.security.model.AclAwareModel#aclParentObject()
*/
@Override
......@@ -273,21 +272,21 @@ public class Subset extends UuidModel implements AclAwareModel, SelfCleaning {
}
/**
* Gets the accessions.
* Gets the accessionRefs.
*
* @return the accessions
* @return the accessionRefs
*/
public List<AccessionRef> getAccessions() {
return accessions;
public List<AccessionRef> getAccessionRefs() {
return accessionRefs;
}
/**
* Sets the accessions.
* Sets the accessionRefs.
*
* @param accessions the new accessions
* @param accessionRefs the new accessionRefs
*/
public void setAccessions(List<AccessionRef> accessions) {
this.accessions = accessions;
public void setAccessionRefs(List<AccessionRef> accessions) {
this.accessionRefs = accessions;
}
/**
......
......@@ -90,7 +90,7 @@ public class SubsetController extends BaseController {
throw new ResourceNotFoundException(e.getMessage());
}
final List<UUID> accessionUuids = subset.getAccessions().stream().skip((page - 1) * 50).limit(50).map(AccessionRef::getAccession).map(AccessionData::getUuid).collect(Collectors.toList());
final List<UUID> accessionUuids = subset.getAccessionRefs().stream().skip((page - 1) * 50).limit(50).map(AccessionRef::getAccession).map(AccessionData::getUuid).collect(Collectors.toList());
List<AccessionData> content = genesysService.listActiveAndHistoric(accessionUuids);
PageImpl<AccessionData> accessions = new PageImpl<>(content, new PageRequest(page - 1, 50), subset.getAccessionCount());
......
......@@ -134,7 +134,7 @@ public class SubsetServiceImpl implements SubsetService {
}
final Subset subset = new Subset();
copyValues(subset, source);
copyAccessions(subset, source.getAccessions());
copyAccessions(subset, source.getAccessionRefs());
subset.setState(PublishState.DRAFT);
return deepLoad(subsetRepository.save(subset));
}
......@@ -147,16 +147,16 @@ public class SubsetServiceImpl implements SubsetService {
*/
private void copyAccessions(final Subset target, final List<AccessionRef> list) {
if ((list == null) || (list.size() == 0)) {
if (target.getAccessions() != null)
target.getAccessions().clear();
if (target.getAccessionRefs() != null)
target.getAccessionRefs().clear();
return;
}
if (target.getAccessions() != null && !target.getAccessions().isEmpty()) {
target.getAccessions().clear();
target.getAccessions().addAll(list.stream().distinct().collect(Collectors.toList()));
if (target.getAccessionRefs() != null && !target.getAccessionRefs().isEmpty()) {
target.getAccessionRefs().clear();
target.getAccessionRefs().addAll(list.stream().distinct().collect(Collectors.toList()));
} else {
target.setAccessions(list.stream().distinct().collect(Collectors.toList()));
target.setAccessionRefs(list.stream().distinct().collect(Collectors.toList()));
}
}
......@@ -201,8 +201,8 @@ public class SubsetServiceImpl implements SubsetService {
subset.getInstitute().getId();
if (subset.getCrops() != null)
subset.getCrops().size();
if (subset.getAccessions() != null)
subset.getAccessions().size();
if (subset.getAccessionRefs() != null)
subset.getAccessionRefs().size();
if (subset.getCreators() != null)
subset.getCreators().size();
......@@ -258,8 +258,8 @@ public class SubsetServiceImpl implements SubsetService {
LOG.info("Update Subset.");
final Subset subset = loadSubset(source);
subset.getAccessions().clear();
copyAccessions(subset, source.getAccessions());
subset.getAccessionRefs().clear();
copyAccessions(subset, source.getAccessionRefs());
copyValues(subset, source);
return deepLoad(subsetRepository.save(subset));
......@@ -293,7 +293,7 @@ public class SubsetServiceImpl implements SubsetService {
final Set<UUID> uuidsToRemove = accessions.stream().map(AccessionData::getUuid).collect(Collectors.toSet());
// Keep accessions that are not in the list
subset.setAccessions(subset.getAccessions().stream().filter(accessionId -> !uuidsToRemove.contains(accessionId.getAccession().getUuid())).collect(Collectors.toList()));
subset.setAccessionRefs(subset.getAccessionRefs().stream().filter(accessionId -> !uuidsToRemove.contains(accessionId.getAccession().getUuid())).collect(Collectors.toList()));
subset.setState(PublishState.DRAFT);
......@@ -312,13 +312,13 @@ public class SubsetServiceImpl implements SubsetService {
final List<AccessionData> accessions = accessionRepository.findActiveAndHistoric(accessionsUuids);
final Set<UUID> currentAccessions = subset.getAccessions().stream().map(AccessionRef::getAccession).map(Accession::getUuid).collect(Collectors.toSet());
final Set<UUID> currentAccessions = subset.getAccessionRefs().stream().map(AccessionRef::getAccession).map(Accession::getUuid).collect(Collectors.toSet());
for (AccessionData accession : accessions) {
if (currentAccessions.contains(accession.getUuid())) {
LOG.warn("Current Subset already contains accession, ignoring");
} else {
subset.getAccessions().add(accession.toAccessionRef());
subset.getAccessionRefs().add(accession.toAccessionRef());
currentAccessions.add(accession.getUuid());
}
}
......@@ -396,7 +396,7 @@ public class SubsetServiceImpl implements SubsetService {
@Override
public List<Subset> listByAccession(Accession accession) {
return (List<Subset>) subsetRepository.findAll(QSubset.subset.accessions.any().accession.eq(accession));
return (List<Subset>) subsetRepository.findAll(QSubset.subset.accessionRefs.any().accession.eq(accession));
}
@Transactional
......
......@@ -74,9 +74,9 @@ public class AccessionRefAspect {
}
private void subsetsDereference(Accession accession) {
List<Subset> subsets = (List<Subset>) subsetRepository.findAll(QSubset.subset.accessions.any().accession.eq(accession));
List<Subset> subsets = (List<Subset>) subsetRepository.findAll(QSubset.subset.accessionRefs.any().accession.eq(accession));
for (Subset subset : subsets) {
subset.getAccessions().stream().filter(accessionRef -> accessionRef.getAccession().equals(accession)).forEach(accessionRef -> accessionRef.setAccession(null));
subset.getAccessionRefs().stream().filter(accessionRef -> accessionRef.getAccession().equals(accession)).forEach(accessionRef -> accessionRef.setAccession(null));
subsetRepository.save(subset);
}
}
......@@ -109,10 +109,10 @@ public class AccessionRefAspect {
}
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())));
List<Subset> subsets = (List<Subset>) subsetRepository.findAll(QSubset.subset.accessionRefs.any().acceNumb.eq(accession.getAccessionNumber()).and(QSubset.subset.accessionRefs
.any().genus.eq(accession.getTaxonomy().getGenus())).and(QSubset.subset.accessionRefs.any().instCode.eq(accession.getInstitute().getCode())));
for (Subset subset : subsets) {
for (AccessionRef accRef : subset.getAccessions()) {
for (AccessionRef accRef : subset.getAccessionRefs()) {
accRef.setAccession(accession);
LOG.debug("Setting accessionRef.id of {accRef} to {acceId} for subset: {subset}", accRef.toString(), subset.toString());
}
......
......@@ -121,7 +121,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
@Transactional
public void cleanup() throws Exception {
for (Subset subset : subsetRepository.findAll()) {
subset.setAccessions(new ArrayList<>());
subset.setAccessionRefs(new ArrayList<>());
subsetRepository.save(subset);
}
subsetRepository.deleteAll();
......@@ -162,7 +162,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
.andExpect(jsonPath("$.publisher", is(PUBLISHER)))
.andExpect(jsonPath("$.dateCreated", is(DATE_CREATED)))
.andExpect(jsonPath("$.rights", is(RIGHTS)))
.andExpect(jsonPath("$.accessions", hasSize(2)));
.andExpect(jsonPath("$.accessionRefs", hasSize(2)));
/*@formatter:on*/
}
......@@ -172,15 +172,15 @@ public class SubsetRestControllerTest extends AbstractApiTest {
storedSubset.setTitle(TITLE_2);
storedSubset.setDescription(DESCRIPTION_2);
assertThat(storedSubset.getAccessions().size(), is(2));
assertThat(storedSubset.getAccessionRefs().size(), is(2));
final AccessionRef accessionRef = storedSubset.getAccessions().stream().findFirst().get();
final AccessionRef accessionRef = storedSubset.getAccessionRefs().stream().findFirst().get();
storedSubset.getAccessions().clear();
assertThat(storedSubset.getAccessions().size(), is(0));
storedSubset.getAccessionRefs().clear();
assertThat(storedSubset.getAccessionRefs().size(), is(0));
storedSubset.getAccessions().add(accessionRef);
assertThat(storedSubset.getAccessions().size(), is(1));
storedSubset.getAccessionRefs().add(accessionRef);
assertThat(storedSubset.getAccessionRefs().size(), is(1));
storedSubset.getInstitute().setSettings(null);
storedSubset.getInstitute().setNetworks(null);
......@@ -197,7 +197,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.title", is(TITLE_2)))
.andExpect(jsonPath("$.description", is(DESCRIPTION_2)))
.andExpect(jsonPath("$.accessions", hasSize(1)));
.andExpect(jsonPath("$.accessionRefs", hasSize(1)));
/*@formatter:on*/
}
......@@ -258,9 +258,9 @@ public class SubsetRestControllerTest extends AbstractApiTest {
@Test
public void removeAccessionsFromSubsetTest() throws Exception {
final Subset subset = subsetService.create(institute, setUpSubset());
assertThat(subset.getAccessions().size(), is(2));
assertThat(subset.getAccessionRefs().size(), is(2));
final Set<UUID> accessionsUuid = subset.getAccessions().stream().map(AccessionRef::getAccession).map(Accession::getUuid).collect(Collectors.toSet());
final Set<UUID> accessionsUuid = subset.getAccessionRefs().stream().map(AccessionRef::getAccession).map(Accession::getUuid).collect(Collectors.toSet());
/*@formatter:off*/
mockMvc.perform(post(SubsetController.API_BASE.concat("/remove-accessions/{UUID},{version}"), subset.getUuid(), subset.getVersion())
......@@ -272,13 +272,13 @@ public class SubsetRestControllerTest extends AbstractApiTest {
/*@formatter:on*/
final Subset updated = subsetService.get(subset.getUuid());
assertThat(updated.getAccessions().size(), is(0));
assertThat(updated.getAccessionRefs().size(), is(0));
}
@Test
public void addAccessionsToSubsetTest() throws Exception {
final Subset subset = subsetService.create(institute, setUpSubset());
assertThat(subset.getAccessions().size(), is(2));
assertThat(subset.getAccessionRefs().size(), is(2));
List<AccessionRef> newAccessions = setUpAccessions();
final Set<UUID> accessionsUuid = newAccessions.stream().map(AccessionRef::getAccession).map(Accession::getUuid).collect(Collectors.toSet());
......@@ -293,7 +293,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
/*@formatter:on*/
final Subset updated = subsetService.get(subset.getUuid());
assertThat(updated.getAccessions().size(), is(4));
assertThat(updated.getAccessionRefs().size(), is(4));
}
@Test
......@@ -364,7 +364,7 @@ public class SubsetRestControllerTest extends AbstractApiTest {
subset.setPublisher(PUBLISHER);
subset.setDateCreated(DATE_CREATED);
subset.setRights(RIGHTS);
subset.setAccessions(setUpAccessions());
subset.setAccessionRefs(setUpAccessions());
return subset;
......
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