Commit b5c514e0 authored by Matija Obreza's avatar Matija Obreza

Merge branch '399-accession-with-repositoryfolder' into 'master'

Resolve "Accession with RepositoryFolder"

Closes #399

See merge request genesys-pgr/genesys-server!323
parents 8d739fef 62edc422
/*
* Copyright 2018 Global Crop Diversity Trust
* Copyright 2019 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.
......@@ -26,19 +26,22 @@ import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.model.RepositoryImage;
import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects;
import org.genesys.filerepository.service.aspect.ImageGalleryAspectsImpl;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.service.InstituteFilesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
// TODO: Auto-generated Javadoc
/**
* The Class GenesysImageGalleryAspects.
*/
......@@ -66,6 +69,9 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private AccessionIdRepository accessionIdRepository;
/**
* Instantiates a new genesys image gallery aspects.
*/
......@@ -89,7 +95,7 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
public Object afterRepositoryImageSaveIterable(JoinPoint joinPoint, Iterable<RepositoryImage> repositoryImages) throws Throwable {
if (repositoryImages != null) {
repositoryImages.forEach(ri -> {
ensureImageGallery(ri);
handleAccessionImageSave(ri);
});
}
......@@ -102,16 +108,41 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
@Override
@AfterReturning(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.save(*))", returning = "repositoryImage")
public Object afterRepositoryImageSave(JoinPoint joinPoint, RepositoryImage repositoryImage) throws Throwable {
ensureImageGallery(repositoryImage);
handleAccessionImageSave(repositoryImage);
return super.afterRepositoryImageSave(joinPoint, repositoryImage);
}
/**
* Ensures an image gallery and updates image count for accessions
* @param repositoryImage
*/
private void handleAccessionImageSave(RepositoryImage repositoryImage) {
if (isAccessionFolder(repositoryImage.getFolder())) {
Accession accession = findMatchingAccession(repositoryImage);
if (accession != null) {
ensureImageGallery(accession, repositoryImage);
updateImageCountOfAcce(accession);
} else {
LOG.warn("No accession for repository folder {}", repositoryImage.getFolder().getPath());
throw new DataIntegrityViolationException("No unique accession in Genesys for " + repositoryImage.getFolder().getPath());
}
}
}
/* (non-Javadoc)
* @see org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects#aroundRepositoryImageDelete(org.aspectj.lang.ProceedingJoinPoint, org.genesys.filerepository.model.RepositoryImage)
*/
@Override
@Around(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.delete(*)) && args(repositoryImage)")
public Object aroundRepositoryImageDelete(ProceedingJoinPoint joinPoint, RepositoryImage repositoryImage) throws Throwable {
if (isAccessionFolder(repositoryImage.getFolder())) {
Accession accession = findMatchingAccession(repositoryImage);
if (accession != null) {
updateImageCountOfAcce(accession);
}
}
return super.aroundRepositoryImageDelete(joinPoint, repositoryImage);
}
......@@ -120,20 +151,57 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
*
* @param repositoryImage the repository image
*/
private void ensureImageGallery(RepositoryImage repositoryImage) {
LOG.trace(repositoryImage.getOriginalFilename());
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(repositoryImage.getFolder().getPath());
if (matcher.matches()) {
LOG.debug("Path {} is within institute accession images prefix instCode={} acceNumb={}", repositoryImage.getFolder().getPath(), matcher.group(1), matcher.group(2));
private void ensureImageGallery(Accession accession, RepositoryImage repositoryImage) {
try {
Accession accession = accessionRepository.findByInstituteCodeAndAccessionNumber(matcher.group(1), matcher.group(2));
if (accession != null) {
instituteFilesService.createImageGallery(accession.getInstitute(), accession);
AccessionId accessionId = accession.getAccessionId();
if (accession.getAccessionId().getRepositoryFolder() == null // New gallery
// or image gallery doesn't exist
|| imageGalleryService.loadImageGallery(accession.getAccessionId().getRepositoryFolder().getFolderPath()) == null) {
ImageGallery imageGallery = instituteFilesService.createImageGallery(accession.getInstitute(), accession);
accessionId.setRepositoryFolder(imageGallery.getFolder());
accessionIdRepository.save(accessionId);
}
}
} catch (InvalidRepositoryPathException e) {
LOG.warn("Error creating accession image gallery at {}, error is {}", repositoryImage.getFolder().getPath(), e.getMessage());
}
}
private Accession findMatchingAccession(RepositoryImage repositoryImage) {
LOG.trace(repositoryImage.getOriginalFilename());
Accession accession = null;
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(repositoryImage.getFolder().getPath());
if (matcher.matches()) {
LOG.debug("Path {} is within institute accession files prefix instCode={} acceNumb={}", repositoryImage.getFolder().getPath(), matcher.group(1), matcher.group(2));
accession = accessionRepository.findByInstituteCodeAndAccessionNumber(matcher.group(1), matcher.group(2));
}
return accession;
}
private boolean isAccessionFolder(RepositoryFolder folder) {
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(folder.getPath());
if (matcher.matches()) {
LOG.debug("Path {} is within institute accession files prefix instCode={} acceNumb={}", folder.getPath(), matcher.group(1), matcher.group(2));
return true;
} else {
return false;
}
}
private void updateImageCountOfAcce(Accession accession) {
AccessionId accessionId = accession.getAccessionId();
try {
ImageGallery imageGallery = instituteFilesService.loadImageGallery(accession.getInstitute(), accession);
if (imageGallery != null) {
accessionId.setImageCount(imageGallery.getImages().size());
accessionIdRepository.save(accessionId);
}
} catch (InvalidRepositoryPathException e) {
LOG.warn("Error updating imageCount of accession, error is {}", e.getMessage());
}
}
/* (non-Javadoc)
......
/**
* Copyright 2014 Global Crop Diversity Trust
/*
* Copyright 2019 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.
......@@ -12,7 +12,7 @@
* 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.model.genesys;
......@@ -45,6 +45,8 @@ import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.IdUUID;
import org.genesys.blocks.model.JsonViews;
import org.genesys.custom.elasticsearch.IgnoreField;
import org.genesys.filerepository.model.RepositoryFolder;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex;
......@@ -140,6 +142,15 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
@CollectionTable(name = "accession_breedername", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"))
private Set<String> breederName;
/** Number of images in the accession */
@ColumnDefault("0")
private int imageCount;
@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, optional = true, orphanRemoval = true)
@JoinColumn(name = "folderId", unique = true)
@JsonIgnore
private RepositoryFolder repositoryFolder;
@PrePersist
@PreUpdate
private void prepersist() {
......@@ -279,4 +290,20 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
public void setDuplSite(Set<String> duplSite) {
this.duplSite = duplSite;
}
public int getImageCount() {
return imageCount;
}
public void setImageCount(int imageCount) {
this.imageCount = imageCount;
}
public RepositoryFolder getRepositoryFolder() {
return repositoryFolder;
}
public void setRepositoryFolder(RepositoryFolder repositoryFolder) {
this.repositoryFolder = repositoryFolder;
}
}
......@@ -35,6 +35,7 @@ import org.genesys.blocks.security.service.CustomAclService;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.catalog.persistence.dataset.DatasetRepository;
import org.genesys.catalog.service.DatasetService;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.persistence.ImageGalleryPersistence;
......@@ -50,6 +51,7 @@ import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.genesys.QAccession;
import org.genesys2.server.model.genesys.QAccessionId;
import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.PDCIRepository;
......@@ -65,6 +67,7 @@ import org.genesys2.server.service.GeoService;
import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.impl.NonUniqueAccessionException;
import org.genesys2.server.service.worker.AccessionCounter;
import org.genesys2.server.service.worker.AccessionProcessor;
import org.genesys2.server.service.worker.ITPGRFAStatusUpdater;
......@@ -592,4 +595,40 @@ public class AdminController {
return "redirect:/admin/";
}
@Autowired
private AccessionIdRepository accessionIdRepository;
@PostMapping(value = "/fix-repo", params = { "accession-folders" })
@Transactional
public String updateAccessionFolders() throws Exception {
LOG.warn("Registering RepositoryFolders with Accessions");
repositoryService.getFolder(Paths.get("/wiews")).getChildren().forEach(wiewsFolder -> {
try {
String instCode = wiewsFolder.getName();
LOG.warn("Processing acn folders for {}", instCode);
RepositoryFolder instAccnFolders = repositoryService.getFolder(wiewsFolder.getFolderPath().resolve("acn"));
instAccnFolders.getChildren().forEach(acceFolder -> {
try {
Accession accession = genesysService.getAccession(instCode, acceFolder.getName());
if (accession != null) {
LOG.warn("Folder for accession {}:{} is {}", instCode, accession.getAccessionNumber(), acceFolder.getPath());
accession.getAccessionId().setRepositoryFolder(acceFolder);
accessionIdRepository.save(accession.getAccessionId());
} else {
LOG.warn("No accession {}:{} for folder {}", instCode, acceFolder.getName(), acceFolder.getPath());
}
} catch (NonUniqueAccessionException e) {
LOG.warn("Accession not unique {}:{}", instCode, acceFolder.getName());
}
});
} catch (InvalidRepositoryPathException e) {
LOG.warn("Invalid path {}", e.getMessage());
}
});
return "redirect:/admin/";
}
}
......@@ -4740,3 +4740,24 @@ databaseChangeLog:
- sql:
comment: Assign organization.version
sql: update organization set version = 1 where version = 0;
- changeSet:
id: 1547116106402-1
author: mborodenko
changes:
- addColumn:
columns:
- column:
constraints:
nullable: false
defaultValueNumeric: 0
name: imageCount
type: INT
- column:
name: folderId
type: BIGINT
tableName: acce
- addUniqueConstraint:
columnNames: folderId
constraintName: UK_3x3s9vul09xif4y29o2rwx939
tableName: acce
......@@ -200,5 +200,13 @@
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
<h3>Repository</h3>
<form method="post" action="<c:url value="/admin/fix-repo" />">
<button type="submit" class="btn btn-default" class="btn btn-default" name="accession-folders" value="fix">Assign folders to accessions</button>
<!-- CSRF protection -->
<input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" />
</form>
</body>
</html>
......@@ -37,6 +37,7 @@ 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.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.Taxonomy2Repository;
......@@ -87,6 +88,8 @@ public class ApiImagesDocsTest extends AbstractApiTest {
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private AccessionIdRepository accessionIdRepository;
@Autowired
private Taxonomy2Repository taxonomy2Repository;
@Autowired
private FaoInstituteRepository instituteRepository;
......@@ -137,6 +140,7 @@ public class ApiImagesDocsTest extends AbstractApiTest {
@Transactional
public void tearDown() {
accessionRepository.deleteAll();
accessionIdRepository.deleteAll();
taxonomy2Repository.deleteAll();
instituteRepository.deleteAll();
imageGalleryPersistence.deleteAll();
......
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