Commit 105ddcfb authored by Matija Obreza's avatar Matija Obreza
Browse files

Merge branch '273-jsp-accession-details-page'

parents 74b20ca1 9c1470c0
......@@ -15,16 +15,21 @@
*/
package org.genesys.catalog.model.dataset;
import java.io.Serializable;
import javax.persistence.Column;
import javax.persistence.Embeddable;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import java.io.Serializable;
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.
......@@ -61,6 +66,8 @@ public class AccessionIdentifier implements AccessionIdentifier3, Serializable {
/** 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;
/**
......@@ -265,4 +272,9 @@ public class AccessionIdentifier implements AccessionIdentifier3, Serializable {
}
return true;
}
@Override
public String toString() {
return "Dataset.AccessionIdentifier " + instCode + " " + acceNumb + " " + genus;
}
}
......@@ -24,6 +24,7 @@ import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.NoSuchRepositoryFileException;
import org.genesys.filerepository.model.RepositoryFile;
import org.genesys2.server.model.genesys.AccessionId;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
......@@ -112,6 +113,14 @@ public interface DatasetService {
*/
Dataset loadDataset(UUID uuid, int version);
/**
* List published datasets by accession.
*
* @param accessionId the accessionId
* @return list of Dataset
*/
List<Dataset> listByAccession(AccessionId accessionId);
/**
* List published datasets matching the filter.
*
......@@ -249,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;
......@@ -49,9 +49,10 @@ import org.genesys.filerepository.service.RepositoryService;
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;
......@@ -67,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;
......@@ -97,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;
......@@ -315,6 +317,15 @@ public class DatasetServiceImpl implements DatasetService {
return lazyLoad(dataset);
}
/**
* {@inheritDoc}
*/
@Override
public List<Dataset> listByAccession(final AccessionId accessionId) {
final Iterable<Dataset> datasets = datasetRepository.findAll(dataset.accessionIdentifiers.any().accession.eq(accessionId).and(dataset.state.in(PublishState.PUBLISHED)));
return Lists.newArrayList(datasets);
}
/**
* {@inheritDoc}
*/
......@@ -600,21 +611,39 @@ public class DatasetServiceImpl implements DatasetService {
* @return accessionIdentifiers with matching accessions from Genesys
*/
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 = 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());
}
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);
}
accei.setAccession(foundAccession != null ? foundAccession.getAccessionId() : null);
});
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;
}
......
......@@ -26,6 +26,7 @@ import java.util.UUID;
import javax.servlet.http.HttpServletResponse;
import org.genesys.blocks.auditlog.service.AuditTrailService;
import org.genesys.catalog.service.DatasetService;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.service.ImageGalleryService;
......@@ -99,6 +100,9 @@ public class AccessionController extends BaseController {
@Autowired
private AuditTrailService auditService;
@Autowired
private DatasetService datasetService;
@Value("${base.url}")
private String baseUrl;
......@@ -145,7 +149,8 @@ public class AccessionController extends BaseController {
model.addAttribute("accessionNames", genesysService.listAccessionNames(accessionId));
model.addAttribute("accessionAliases", accessionId.getAliases());
model.addAttribute("accessionDatasets", datasetService.listByAccession(accessionId));
model.addAttribute("accessionExchange", accession);
AccessionCollect accessionCollect = accessionId.getColl();
......
......@@ -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();
}
}
......@@ -306,6 +306,8 @@ accession.breeding=Breeder information
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
......
......@@ -1072,6 +1072,30 @@
</div>
</c:if>
<c:if test="${accessionDatasets ne null and accessionDatasets.size() gt 0}">
<div class="collect-info">
<h4 class="row section-heading"><spring:message code="accession.datasets"/></h4>
<div class="section-inner-content clearfix">
<div class="terms-table">
<c:forEach items="${accessionDatasets}" var="dataset" varStatus="status">
<div class="row">
<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-md-4 col-sm-12 text-right">
<p><spring:message code="accession.datasets.traits" arguments="${dataset.descriptorCount}" /></p>
</div>
</div>
</c:forEach>
</div>
</div>
</div>
</c:if>
<div class="collect-info">
<div class="section-inner-content clearfix">
<p>
......
......@@ -46,6 +46,14 @@ import org.springframework.beans.factory.annotation.Autowired;
public abstract class AbstractDatasetServiceTest extends CatalogServiceTest {
protected static final String TEST_INSTCODE = "XYZ001";
protected static final String TEST_INSTCODE2 = "XYZ002";
protected static final String TEST_INSTCODE3 = "XYZ003";
protected static final String TEST_ACCNUM = "TestAccNum1";
protected static final String TEST_ACCNUM2 = "TestAccNum2";
protected static final String TEST_ACCNUM3 = "TestAccNum3";
protected static final String TEST_GEN = "TestGen1";
protected static final String TEST_GEN2 = "TestGen2";
protected static final String TEST_GEN3= "TestGen3";
protected static final String DATASET_TITLE_1 = "Dataset title 1";
protected static final String DATASET_TITLE_2 = "Dataset title 2";
protected static final String DATASET_DESCRIPTION_1 = "Test description 1";
......
......@@ -16,8 +16,7 @@
package org.genesys.test.catalog.services;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.*;
import java.io.File;
import java.io.FileInputStream;
......@@ -190,6 +189,80 @@ public class DatasetServiceTest extends AbstractDatasetServiceTest {
assertThat(accessionId.getUuid(), is(accession.getAccessionId().getUuid()));
}
@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);
Dataset input = buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, null, PublishState.DRAFT);
input = datasetService.createDataset(input);
final Set<AccessionIdentifier> accessionIdentifiers = new HashSet<AccessionIdentifier>() {
/**
*
*/
private static final long serialVersionUID = 8866546652858324614L;
{
add(makeAccessionIdentifier(TEST_INSTCODE, TEST_ACCNUM, TEST_GEN, null));
add(makeAccessionIdentifier(TEST_INSTCODE2, TEST_ACCNUM2, TEST_GEN2, null));
add(makeAccessionIdentifier(TEST_INSTCODE3, TEST_ACCNUM3, TEST_GEN3, null));
}
};
input = datasetService.updateAccessionIdentifiers(input, accessionIdentifiers);
assertThat(input, not(nullValue()));
assertThat(input.getAccessionIdentifiers().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 first accession
final Set<AccessionIdentifier> accessionIdentifiers2 = accessionIdentifiers.stream().filter(accessionIdentifier -> !accessionIdentifier.getInstCode().equals(TEST_INSTCODE)).collect(Collectors.toSet());
assertThat(accessionIdentifiers2.size(), is(2));
// update dataset with a new list of accessions
input = datasetService.updateAccessionIdentifiers(input, accessionIdentifiers2);
assertThat(input, not(nullValue()));
// test if dataset has a new set of accessions
assertThat(input.getAccessionIdentifiers().size(), is(2));
input.getAccessionIdentifiers().forEach(aid -> {
assertThat(aid.getAccession(), notNullValue());
assertThat(aid.getAccession().getUuid(), anyOf(equalTo(accession2.getUuid()), equalTo(accession3.getUuid())));
});
}
@Test
public void testListDatasetsByAccession() {
final Accession accession = buildAndSaveAccession(TEST_ACCNUM, TEST_INSTCODE, TEST_GEN);
Dataset input = buildDataset(DATASET_TITLE_1, DATASET_DESCRIPTION_1, partner, null, PublishState.PUBLISHED);
input = datasetService.createDataset(input);
// add accessionIdentifier to dataset
final Set<AccessionIdentifier> accessionIdentifiers = new HashSet<>();
accessionIdentifiers.add(makeAccessionIdentifier(TEST_INSTCODE, TEST_ACCNUM, TEST_GEN, null));
input = datasetService.updateAccessionIdentifiers(input, accessionIdentifiers);
assertThat(input.getAccessionIdentifiers().size(), is(1));
// make sure that accession from Genesys also be added
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));
assertEquals(input.getUuid(), datasets.get(0).getUuid());
}
/**
* Fail to update Dataset when isPublished() = true
*/
......
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