Commit 785b9f2c authored by Maxym Borodenko's avatar Maxym Borodenko Committed by Matija Obreza

Accession with RepositoryFolder

parent c6fb463c
/*
* 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.
......@@ -31,6 +31,8 @@ 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;
......@@ -66,6 +68,9 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
@Autowired
private AccessionRepository accessionRepository;
@Autowired
private AccessionIdRepository accessionIdRepository;
/**
* Instantiates a new genesys image gallery aspects.
*/
......@@ -102,7 +107,11 @@ 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);
Accession accession = findMatchingAccession(repositoryImage);
if (accession != null) {
ensureImageGallery(repositoryImage);
updateImageCountOfAcce(accession);
}
return super.afterRepositoryImageSave(joinPoint, repositoryImage);
}
......@@ -112,6 +121,10 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
@Override
@Around(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.delete(*)) && args(repositoryImage)")
public Object aroundRepositoryImageDelete(ProceedingJoinPoint joinPoint, RepositoryImage repositoryImage) throws Throwable {
Accession accession = findMatchingAccession(repositoryImage);
if (accession != null) {
updateImageCountOfAcce(accession);
}
return super.aroundRepositoryImageDelete(joinPoint, repositoryImage);
}
......@@ -121,18 +134,40 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
* @param repositoryImage the repository image
*/
private void ensureImageGallery(RepositoryImage repositoryImage) {
Accession accession = findMatchingAccession(repositoryImage);
try {
if (accession != null) {
ImageGallery imageGallery = instituteFilesService.createImageGallery(accession.getInstitute(), accession);
AccessionId accessionId = accession.getAccessionId();
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());
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(repositoryImage.getFolder().getPath());
Accession accession = null;
if (matcher.matches()) {
LOG.debug("Path {} is within institute accession images prefix instCode={} acceNumb={}", repositoryImage.getFolder().getPath(), matcher.group(1), matcher.group(2));
try {
Accession accession = accessionRepository.findByInstituteCodeAndAccessionNumber(matcher.group(1), matcher.group(2));
if (accession != null) {
instituteFilesService.createImageGallery(accession.getInstitute(), accession);
}
} catch (InvalidRepositoryPathException e) {
LOG.warn("Error creating accession image gallery at {}, error is {}", repositoryImage.getFolder().getPath(), e.getMessage());
accession = accessionRepository.findByInstituteCodeAndAccessionNumber(matcher.group(1), matcher.group(2));
}
return accession;
}
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());
}
}
......
/**
* 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.ALL }, optional = true, orphanRemoval = true)
@JoinColumn(name = "folderId", unique = true)
@Field(type = FieldType.Object)
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;
}
}
......@@ -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
......@@ -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