Commit 62edc422 authored by Matija Obreza's avatar Matija Obreza

AccessionId#repositoryFolder updates

- Throw exception if accession doesn't exist in the database
- Admin: assign folders
parent 785b9f2c
......@@ -26,6 +26,7 @@ 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;
......@@ -39,8 +40,8 @@ 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.
*/
......@@ -94,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);
});
}
......@@ -107,24 +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 {
Accession accession = findMatchingAccession(repositoryImage);
if (accession != null) {
ensureImageGallery(repositoryImage);
updateImageCountOfAcce(accession);
}
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 {
Accession accession = findMatchingAccession(repositoryImage);
if (accession != null) {
updateImageCountOfAcce(accession);
if (isAccessionFolder(repositoryImage.getFolder())) {
Accession accession = findMatchingAccession(repositoryImage);
if (accession != null) {
updateImageCountOfAcce(accession);
}
}
return super.aroundRepositoryImageDelete(joinPoint, repositoryImage);
}
......@@ -133,14 +151,19 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
*
* @param repositoryImage the repository image
*/
private void ensureImageGallery(RepositoryImage repositoryImage) {
Accession accession = findMatchingAccession(repositoryImage);
private void ensureImageGallery(Accession accession, RepositoryImage repositoryImage) {
try {
if (accession != null) {
ImageGallery imageGallery = instituteFilesService.createImageGallery(accession.getInstitute(), accession);
AccessionId accessionId = accession.getAccessionId();
accessionId.setRepositoryFolder(imageGallery.getFolder());
accessionIdRepository.save(accessionId);
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());
......@@ -149,15 +172,25 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
private Accession findMatchingAccession(RepositoryImage repositoryImage) {
LOG.trace(repositoryImage.getOriginalFilename());
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(repositoryImage.getFolder().getPath());
Accession accession = null;
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));
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 {
......
......@@ -146,9 +146,9 @@ public class AccessionId extends AuditedVersionedModel implements IdUUID {
@ColumnDefault("0")
private int imageCount;
@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, optional = true, orphanRemoval = true)
@OneToOne(fetch = FetchType.LAZY, cascade = { CascadeType.REMOVE }, optional = true, orphanRemoval = true)
@JoinColumn(name = "folderId", unique = true)
@Field(type = FieldType.Object)
@JsonIgnore
private RepositoryFolder repositoryFolder;
@PrePersist
......
......@@ -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/";
}
}
......@@ -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>
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