Commit 9c1470c0 authored by Matija Obreza's avatar Matija Obreza

Display links to datasets of Accession

parent 3b966bed
......@@ -15,12 +15,21 @@
*/
package org.genesys.catalog.model.dataset;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import java.io.Serializable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.impl.AccessionIdentifier3;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
/**
* Identifies an accession by it's INSTCODE, ACCENUMB and GENUS. Use of DOI
* remains optional.
......@@ -54,6 +63,13 @@ public class AccessionIdentifier implements AccessionIdentifier3, Serializable {
@Column(nullable = true, length = 100)
private String species;
/** The accession. */
@ManyToOne(optional = true, fetch = FetchType.LAZY, cascade = {})
@JoinColumn(name = "accessionId", nullable = true)
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "uuid")
@JsonIdentityReference(alwaysAsId = true)
private AccessionId accession;
/**
* Gets the doi.
*
......@@ -144,6 +160,24 @@ public class AccessionIdentifier implements AccessionIdentifier3, Serializable {
this.species = species;
}
/**
* Gets the accession.
*
* @return the accession
*/
public AccessionId getAccession() {
return accession;
}
/**
* Sets the accession.
*
* @param accession the new accession
*/
public void setAccession(final AccessionId accession) {
this.accession = accession;
}
/**
* Gets holding institute code.
*
......@@ -223,6 +257,24 @@ public class AccessionIdentifier implements AccessionIdentifier3, Serializable {
} else if (!species.equals(other.species)) {
return false;
}
if (accession == null) {
if (other.accession != null) {
return false;
}
} else {
if (other.accession != null) {
if (!accession.getUuid().equals(other.accession.getUuid())) {
return false;
}
} else {
return false;
}
}
return true;
}
@Override
public String toString() {
return "Dataset.AccessionIdentifier " + instCode + " " + acceNumb + " " + genus;
}
}
......@@ -15,7 +15,6 @@
*/
package org.genesys.catalog.model.dataset;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonView;
import org.genesys.blocks.auditlog.annotations.Audited;
......@@ -30,7 +29,6 @@ import org.genesys.catalog.model.traits.Descriptor;
import org.genesys.catalog.service.PublishValidationInterface;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.util.MCPDUtil;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
......@@ -81,12 +79,6 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
@JsonView({ JsonViews.Public.class })
private Set<AccessionIdentifier> accessionIdentifiers;
/** The accessions. */
@ManyToMany(cascade = {}, fetch = FetchType.LAZY)
@JoinTable(name = "dataset_acce", joinColumns = @JoinColumn(name = "datasetId"), inverseJoinColumns = @JoinColumn(name = "acce_id"))
@JsonIdentityReference(alwaysAsId = true)
private List<AccessionId> accessions;
/** The descriptors. */
@ManyToMany(fetch = FetchType.LAZY, cascade = {})
@JoinTable(name = "dataset_descriptor", joinColumns = @JoinColumn(name = "datasetId", referencedColumnName = "id"), inverseJoinColumns = @JoinColumn(name = "descriptorId", referencedColumnName = "id"))
......@@ -537,24 +529,6 @@ public class Dataset extends UuidModel implements Publishable, SelfCleaning, Pub
this.accessionIdentifiers = accessionIdentifiers;
}
/**
* Gets the accessions.
*
* @return the accessions
*/
public List<AccessionId> getAccessions() {
return accessions;
}
/**
* Sets the accessions.
*
* @param accessions the new accessions
*/
public void setAccessions(final List<AccessionId> accessions) {
this.accessions = accessions;
}
/**
* Gets the descriptors.
*
......
......@@ -258,4 +258,6 @@ public interface DatasetService {
*/
List<Object[]> lastPublished();
void rematchDatasetAccessions();
}
......@@ -15,7 +15,7 @@
*/
package org.genesys.catalog.service.impl;
import static org.genesys.catalog.model.dataset.QDataset.dataset;
import static org.genesys.catalog.model.dataset.QDataset.*;
import java.io.IOException;
import java.nio.file.Paths;
......@@ -27,7 +27,6 @@ import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import com.google.common.collect.Lists;
import org.apache.commons.lang.StringUtils;
import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.exceptions.InvalidApiUsageException;
......@@ -51,9 +50,9 @@ import org.genesys2.server.model.PublishState;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.security.SecurityUtils;
import org.genesys2.server.service.GenesysService;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.genesys2.util.JPAUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
......@@ -69,6 +68,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.google.common.collect.Lists;
import com.querydsl.core.BooleanBuilder;
......@@ -99,8 +99,8 @@ public class DatasetServiceImpl implements DatasetService {
private SecurityUtils securityUtils;
@Autowired
private GenesysService genesysService;
private AccessionRepository accessionRepository;
/** The file repository path. */
@Value("${file.repository.datasets.folder}")
public String datasetRepositoryPath;
......@@ -128,8 +128,7 @@ public class DatasetServiceImpl implements DatasetService {
dataset.setCrops(source.getCrops());
if (source.getAccessionIdentifiers() != null && !source.getAccessionIdentifiers().isEmpty()) {
dataset.setAccessionIdentifiers(new HashSet<>(source.getAccessionIdentifiers()));
dataset.setAccessions(lookupMatchingAccessions(dataset.getAccessionIdentifiers()));
dataset.setAccessionIdentifiers(lookupMatchingAccessions(new HashSet<>(source.getAccessionIdentifiers())));
}
copyDescriptors(dataset, source.getDescriptors());
......@@ -177,8 +176,7 @@ public class DatasetServiceImpl implements DatasetService {
LOG.info("Update AccessionIdentifiers. Input dataset: {}, input accessionIdentifiers {}", dataset, accessionIdentifiers);
dataset = loadDataset(dataset);
dataset.getAccessionIdentifiers().clear();
dataset.setAccessionIdentifiers(new HashSet<>(accessionIdentifiers));
dataset.setAccessions(lookupMatchingAccessions(dataset.getAccessionIdentifiers()));
dataset.setAccessionIdentifiers(lookupMatchingAccessions(new HashSet<>(accessionIdentifiers)));
return lazyLoad(datasetRepository.save(dataset));
}
......@@ -324,7 +322,7 @@ public class DatasetServiceImpl implements DatasetService {
*/
@Override
public List<Dataset> listByAccession(final AccessionId accessionId) {
final Iterable<Dataset> datasets = datasetRepository.findAll(dataset.accessions.contains(accessionId).and(dataset.state.in(PublishState.PUBLISHED)));
final Iterable<Dataset> datasets = datasetRepository.findAll(dataset.accessionIdentifiers.any().accession.eq(accessionId).and(dataset.state.in(PublishState.PUBLISHED)));
return Lists.newArrayList(datasets);
}
......@@ -443,8 +441,7 @@ public class DatasetServiceImpl implements DatasetService {
throw new NotFoundElement("Dataset doesn't exist");
}
loadedDataset.setState(PublishState.DRAFT);
loadedDataset.setAccessionIdentifiers(new HashSet<>(accessionIdentifiers));
loadedDataset.setAccessions(lookupMatchingAccessions(loadedDataset.getAccessionIdentifiers()));
loadedDataset.setAccessionIdentifiers(lookupMatchingAccessions(new HashSet<>(accessionIdentifiers)));
return lazyLoad(datasetRepository.save(loadedDataset));
}
......@@ -608,31 +605,45 @@ public class DatasetServiceImpl implements DatasetService {
}
/**
* Looking for matching Accession
* Looking for matching Accession and then sets that to AccessionIdentifier
*
* @param identifiers the accessionIdentifiers
* @return list of matching accessions from Genesys
* @param accessionIdentifiers the accessionIdentifiers
* @return accessionIdentifiers with matching accessions from Genesys
*/
private List<AccessionId> lookupMatchingAccessions(final Set<AccessionIdentifier> identifiers) {
final List<AccessionId> accessions = new ArrayList<>();
if (!identifiers.isEmpty()) {
identifiers.forEach(accei -> {
Accession foundAccession = null;
if (!StringUtils.isBlank(accei.getDoi())) {
foundAccession = genesysService.getAccessionByDOI(accei.getDoi());
} else {
try {
foundAccession = genesysService.getAccession(accei.getInstCode(), accei.getDoi(), accei.getAcceNumb(), accei.getGenus());
} catch (final NonUniqueAccessionException e) {
LOG.warn(e.getMessage());
}
}
if (foundAccession != null) {
accessions.add(foundAccession.getAccessionId());
}
});
}
return accessions.stream().distinct().collect(Collectors.toList());
private Set<AccessionIdentifier> lookupMatchingAccessions(final Set<AccessionIdentifier> accessionIdentifiers) {
List<AccessionIdentifier> list = new ArrayList<AccessionIdentifier>(accessionIdentifiers);
List<Accession> foundAccessions = accessionRepository.findById(list);
LOG.info("Found {} matches for {} identifiers", foundAccessions.size(), accessionIdentifiers.size());
accessionIdentifiers.forEach(accei -> {
Accession foundAccession = foundAccessions.stream().filter(a -> {
return StringUtils.equalsIgnoreCase(a.getInstCode(), accei.getInstCode())
&& (
// when DOI provided
(accei.getDoi() != null && a.getDoi() != null && StringUtils.equals(a.getDoi(), accei.getDoi()))
// OR when without DOI
||
(StringUtils.equalsIgnoreCase(a.getAccessionNumber(), accei.getAcceNumb()) && StringUtils.equalsIgnoreCase(a.getGenus(), accei.getGenus()))
);
}).findFirst().orElse(null);
accei.setAccession(foundAccession == null ? null : foundAccession.getAccessionId());
if (foundAccession == null) {
LOG.debug("No match for {}", accei);
}
});
return accessionIdentifiers;
}
@Override
@Transactional(readOnly = false)
public void rematchDatasetAccessions() {
datasetRepository.findAll().forEach(dataset -> {
LOG.warn("Linking {} accessions with dataset {}", dataset.getAccessionCount(), dataset.getId());
dataset.setAccessionIdentifiers(lookupMatchingAccessions(new HashSet<>(dataset.getAccessionIdentifiers())));
datasetRepository.save(dataset);
});
}
}
......@@ -241,6 +241,10 @@ public abstract class AccessionData extends AuditedVersionedModel implements IdU
this.taxonomy = taxonomy;
}
public String getGenus() {
return genus;
}
public String getAcquisitionSource() {
return this.acquisitionSource;
}
......
......@@ -30,6 +30,7 @@ import org.genesys.catalog.persistence.PartnerRepository;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.persistence.traits.DescriptorListRepository;
import org.genesys.catalog.persistence.traits.DescriptorRepository;
import org.genesys.catalog.service.DatasetService;
import org.genesys2.server.model.UserRole;
import org.genesys2.server.model.impl.User;
import org.genesys2.server.persistence.UserRepository;
......@@ -110,7 +111,10 @@ public class ApplicationStartup implements InitializingBean, ApplicationListener
*/
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
rematchDatasetAccessions();
aclService.cleanupAcl();
if (reindexElastic) {
reindexElasticsearch();
}
......@@ -129,6 +133,7 @@ public class ApplicationStartup implements InitializingBean, ApplicationListener
ensure1OAuthClient();
}
@Autowired
private PasswordEncoder passwordEncoder;
......@@ -212,4 +217,12 @@ public class ApplicationStartup implements InitializingBean, ApplicationListener
private void reindexElasticsearch() {
elasticsearch.reindexAll();
}
@Autowired
private DatasetService datasetService;
private void rematchDatasetAccessions() {
datasetService.rematchDatasetAccessions();
}
}
......@@ -307,6 +307,7 @@ accession.breederCode=Breeder Code
accession.pedigree=Pedigree
accession.collecting=Collecting information
accession.datasets=List of Datasets
accession.datasets.traits={0} traits observed
accession.collecting.site=Location of collecting site
accession.collecting.institute=Collecting institute
accession.collecting.number=Collecting number
......
......@@ -3739,64 +3739,3 @@ databaseChangeLog:
- sql:
sql: >-
update accession_historic ah right join acce ac on ac.id = ah.id right join accession_geo geo on geo.id = ac.geoId set ah.tileIndex = geo.tileIndex;
- changeSet:
id: 1534922647579-1
author: mborodenko
changes:
- dropForeignKeyConstraint:
baseTableName: dataset_accessions
constraintName: FK_ekss0g19xg9h2tsp1zl8g9h2t
- dropColumn:
tableName: dataset_accessions
columnName: accessionId
- changeSet:
id: 1534922647579-2
author: mborodenko
changes:
- createTable:
columns:
- column:
constraints:
nullable: false
name: datasetId
type: BIGINT
- column:
constraints:
nullable: false
name: acce_id
type: BIGINT
tableName: dataset_acce
- createIndex:
columns:
- column:
name: datasetId
indexName: FK_17w5b5xt9tbw0l6eefqayh33q
tableName: dataset_acce
- createIndex:
columns:
- column:
name: acce_id
indexName: FK_7jyg2if8kyxyw8toq5qyy5vj2
tableName: dataset_acce
- addForeignKeyConstraint:
baseColumnNames: acce_id
baseTableName: dataset_acce
constraintName: FK_7jyg2if8kyxyw8toq5qyy5vj2
deferrable: false
initiallyDeferred: false
onDelete: NO ACTION
onUpdate: NO ACTION
referencedColumnNames: id
referencedTableName: acce
- addForeignKeyConstraint:
baseColumnNames: datasetId
baseTableName: dataset_acce
constraintName: FK_17w5b5xt9tbw0l6eefqayh33q
deferrable: false
initiallyDeferred: false
onDelete: NO ACTION
onUpdate: NO ACTION
referencedColumnNames: id
referencedTableName: dataset
......@@ -1079,15 +1079,15 @@
<div class="terms-table">
<c:forEach items="${accessionDatasets}" var="dataset" varStatus="status">
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-8">
<div class="col-md-8 col-sm-12">
<p>
<a class="" title="Dataset" href="<c:url value='${genesysCatalogUrl}/datasets/${dataset.uuid}' />">
<c:out value="${dataset.title}" />
</a>
</p>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-4 text-left">
<p><fmt:formatNumber value="${dataset.descriptorCount}" /></p>
<div class="col-md-4 col-sm-12 text-right">
<p><spring:message code="accession.datasets.traits" arguments="${dataset.descriptorCount}" /></p>
</div>
</div>
</c:forEach>
......
......@@ -16,9 +16,7 @@
package org.genesys.test.catalog.services;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileInputStream;
......@@ -155,6 +153,11 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(input, not(nullValue()));
assertThat(input.getAccessionIdentifiers().size(), is(1));
assertThat(input.getAccessionIdentifiers().iterator().next(), is(accessionIdentifiers.iterator().next()));
AccessionIdentifier accessionIdentifier = input.getAccessionIdentifiers().stream().findFirst().orElse(null);
assertThat(accessionIdentifier, notNullValue());
AccessionId accessionId = accessionIdentifier.getAccession();
assertThat(accessionId, nullValue());
}
@Test
......@@ -180,8 +183,9 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(input.getAccessionIdentifiers().size(), is(1));
assertThat(input.getAccessionIdentifiers().iterator().next(), is(accessionIdentifiers.iterator().next()));
final AccessionId accessionId = input.getAccessions().stream().findFirst().orElse(null);
assertThat(accessionId, notNullValue());
final AccessionIdentifier accessionIdentifier = input.getAccessionIdentifiers().stream().findFirst().orElse(null);
assertThat(accessionIdentifier, notNullValue());
final AccessionId accessionId = accessionIdentifier.getAccession();
assertThat(accessionId.getUuid(), is(accession.getAccessionId().getUuid()));
}
......@@ -210,10 +214,14 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
input = datasetService.updateAccessionIdentifiers(input, accessionIdentifiers);
assertThat(input, not(nullValue()));
assertThat(input.getAccessionIdentifiers().size(), is(3));
assertThat(input.getAccessions().size(), is(3));
assertThat(input.getAccessionIdentifiers().size(), is(accessionIdentifiers.size()));
input.getAccessionIdentifiers().forEach(aid -> {
assertThat(aid.getAccession(), notNullValue());
assertThat(aid.getAccession().getUuid(), anyOf(equalTo(accession1.getUuid()), equalTo(accession2.getUuid()), equalTo(accession3.getUuid())));
});
// remove one accession
// remove first accession
final Set<AccessionIdentifier> accessionIdentifiers2 = accessionIdentifiers.stream().filter(accessionIdentifier -> !accessionIdentifier.getInstCode().equals(TEST_INSTCODE)).collect(Collectors.toSet());
assertThat(accessionIdentifiers2.size(), is(2));
......@@ -223,9 +231,11 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
// test if dataset has a new set of accessions
assertThat(input.getAccessionIdentifiers().size(), is(2));
assertThat(input.getAccessions().size(), is(2));
assertFalse(input.getAccessions().stream().anyMatch(accessionId -> accessionId.getUuid() == accession2.getAccessionId().getUuid()));
input.getAccessionIdentifiers().forEach(aid -> {
assertThat(aid.getAccession(), notNullValue());
assertThat(aid.getAccession().getUuid(), anyOf(equalTo(accession2.getUuid()), equalTo(accession3.getUuid())));
});
}
@Test
......@@ -242,7 +252,10 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(input.getAccessionIdentifiers().size(), is(1));
// make sure that accession from Genesys also be added
assertThat(input.getAccessions().size(), is(1));
input.getAccessionIdentifiers().forEach(aid -> {
assertThat(aid.getAccession(), notNullValue());
assertThat(aid.getAccession().getUuid(), equalTo(accession.getUuid()));
});
List<Dataset> datasets = datasetService.listByAccession(accession.getAccessionId());
assertThat(datasets.size(), is(1));
......
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