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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -31,6 +31,8 @@ import org.genesys.filerepository.service.ImageGalleryService; ...@@ -31,6 +31,8 @@ import org.genesys.filerepository.service.ImageGalleryService;
import org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects; import org.genesys.filerepository.service.aspect.AbstractImageGalleryAspects;
import org.genesys.filerepository.service.aspect.ImageGalleryAspectsImpl; import org.genesys.filerepository.service.aspect.ImageGalleryAspectsImpl;
import org.genesys2.server.model.genesys.Accession; 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.persistence.AccessionRepository;
import org.genesys2.server.service.InstituteFilesService; import org.genesys2.server.service.InstituteFilesService;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -66,6 +68,9 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl ...@@ -66,6 +68,9 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
@Autowired @Autowired
private AccessionRepository accessionRepository; private AccessionRepository accessionRepository;
@Autowired
private AccessionIdRepository accessionIdRepository;
/** /**
* Instantiates a new genesys image gallery aspects. * Instantiates a new genesys image gallery aspects.
*/ */
...@@ -102,7 +107,11 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl ...@@ -102,7 +107,11 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
@Override @Override
@AfterReturning(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.save(*))", returning = "repositoryImage") @AfterReturning(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.save(*))", returning = "repositoryImage")
public Object afterRepositoryImageSave(JoinPoint joinPoint, RepositoryImage repositoryImage) throws Throwable { 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); return super.afterRepositoryImageSave(joinPoint, repositoryImage);
} }
...@@ -112,6 +121,10 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl ...@@ -112,6 +121,10 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
@Override @Override
@Around(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.delete(*)) && args(repositoryImage)") @Around(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.delete(*)) && args(repositoryImage)")
public Object aroundRepositoryImageDelete(ProceedingJoinPoint joinPoint, RepositoryImage repositoryImage) throws Throwable { public Object aroundRepositoryImageDelete(ProceedingJoinPoint joinPoint, RepositoryImage repositoryImage) throws Throwable {
Accession accession = findMatchingAccession(repositoryImage);
if (accession != null) {
updateImageCountOfAcce(accession);
}
return super.aroundRepositoryImageDelete(joinPoint, repositoryImage); return super.aroundRepositoryImageDelete(joinPoint, repositoryImage);
} }
...@@ -121,18 +134,40 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl ...@@ -121,18 +134,40 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
* @param repositoryImage the repository image * @param repositoryImage the repository image
*/ */
private void ensureImageGallery(RepositoryImage repositoryImage) { 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()); LOG.trace(repositoryImage.getOriginalFilename());
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(repositoryImage.getFolder().getPath()); Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(repositoryImage.getFolder().getPath());
Accession accession = null;
if (matcher.matches()) { if (matcher.matches()) {
LOG.debug("Path {} is within institute accession images prefix instCode={} acceNumb={}", repositoryImage.getFolder().getPath(), matcher.group(1), matcher.group(2)); LOG.debug("Path {} is within institute accession images prefix instCode={} acceNumb={}", repositoryImage.getFolder().getPath(), matcher.group(1), matcher.group(2));
try { accession = accessionRepository.findByInstituteCodeAndAccessionNumber(matcher.group(1), matcher.group(2));
Accession accession = accessionRepository.findByInstituteCodeAndAccessionNumber(matcher.group(1), matcher.group(2)); }
if (accession != null) { return accession;
instituteFilesService.createImageGallery(accession.getInstitute(), accession); }
}
} catch (InvalidRepositoryPathException e) { private void updateImageCountOfAcce(Accession accession) {
LOG.warn("Error creating accession image gallery at {}, error is {}", repositoryImage.getFolder().getPath(), e.getMessage()); 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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -12,7 +12,7 @@ ...@@ -12,7 +12,7 @@
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
* limitations under the License. * limitations under the License.
**/ */
package org.genesys2.server.model.genesys; package org.genesys2.server.model.genesys;
...@@ -45,6 +45,8 @@ import org.genesys.blocks.model.AuditedVersionedModel; ...@@ -45,6 +45,8 @@ import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.model.IdUUID; import org.genesys.blocks.model.IdUUID;
import org.genesys.blocks.model.JsonViews; import org.genesys.blocks.model.JsonViews;
import org.genesys.custom.elasticsearch.IgnoreField; import org.genesys.custom.elasticsearch.IgnoreField;
import org.genesys.filerepository.model.RepositoryFolder;
import org.hibernate.annotations.ColumnDefault;
import org.hibernate.annotations.Type; import org.hibernate.annotations.Type;
import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldIndex; import org.springframework.data.elasticsearch.annotations.FieldIndex;
...@@ -140,6 +142,15 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID { ...@@ -140,6 +142,15 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
@CollectionTable(name = "accession_breedername", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id")) @CollectionTable(name = "accession_breedername", joinColumns = @JoinColumn(name = "accessionId", referencedColumnName = "id"))
private Set<String> breederName; 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 @PrePersist
@PreUpdate @PreUpdate
private void prepersist() { private void prepersist() {
...@@ -279,4 +290,20 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID { ...@@ -279,4 +290,20 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
public void setDuplSite(Set<String> duplSite) { public void setDuplSite(Set<String> duplSite) {
this.duplSite = 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: ...@@ -4740,3 +4740,24 @@ databaseChangeLog:
- sql: - sql:
comment: Assign organization.version comment: Assign organization.version
sql: update organization set version = 1 where version = 0; 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; ...@@ -37,6 +37,7 @@ import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId; import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.model.genesys.Taxonomy2; import org.genesys2.server.model.genesys.Taxonomy2;
import org.genesys2.server.model.impl.FaoInstitute; import org.genesys2.server.model.impl.FaoInstitute;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository; import org.genesys2.server.persistence.AccessionRepository;
import org.genesys2.server.persistence.FaoInstituteRepository; import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.Taxonomy2Repository; import org.genesys2.server.persistence.Taxonomy2Repository;
...@@ -87,6 +88,8 @@ public class ApiImagesDocsTest extends AbstractApiTest { ...@@ -87,6 +88,8 @@ public class ApiImagesDocsTest extends AbstractApiTest {
@Autowired @Autowired
private AccessionRepository accessionRepository; private AccessionRepository accessionRepository;
@Autowired @Autowired
private AccessionIdRepository accessionIdRepository;
@Autowired
private Taxonomy2Repository taxonomy2Repository; private Taxonomy2Repository taxonomy2Repository;
@Autowired @Autowired
private FaoInstituteRepository instituteRepository; private FaoInstituteRepository instituteRepository;
...@@ -137,6 +140,7 @@ public class ApiImagesDocsTest extends AbstractApiTest { ...@@ -137,6 +140,7 @@ public class ApiImagesDocsTest extends AbstractApiTest {
@Transactional @Transactional
public void tearDown() { public void tearDown() {
accessionRepository.deleteAll(); accessionRepository.deleteAll();
accessionIdRepository.deleteAll();
taxonomy2Repository.deleteAll(); taxonomy2Repository.deleteAll();
instituteRepository.deleteAll(); instituteRepository.deleteAll();
imageGalleryPersistence.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