Commit c8d947bf authored by Matija Obreza's avatar Matija Obreza

Merge branch 'ftp-check-accession' into 'master'

Folder names in /wiews/INSTCODE/acn must match existing accession numbers

See merge request genesys-pgr/genesys-server!359
parents a5d4f5e5 59849b3b
/*
* 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.component.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.genesys.filerepository.FileRepositoryException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys.filerepository.service.aspect.AbstractRepositoryFolderAspects;
import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.AccessionId;
import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.service.AccessionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
/**
* The Class AccessionRepositoryFolderAspects.
*/
@Aspect
public class AccessionRepositoryFolderAspects extends AbstractRepositoryFolderAspects {
/** The Constant LOG. */
public static final Logger LOG = LoggerFactory.getLogger(AccessionRepositoryFolderAspects.class);
@Autowired
private AccessionService accessionService;
@Autowired
private AccessionIdRepository accessionIdRepository;
@Override
protected void onRemoveFolder(RepositoryFolder repositoryFolder) throws FileRepositoryException {
LOG.debug("Folder removed {}", repositoryFolder.getPath());
if (GenesysImageGalleryAspects.isAccessionFolder(repositoryFolder)) {
Accession accession = accessionService.findMatchingAccession(repositoryFolder);
if (accession != null) {
LOG.info("Unlinking folder {} from accession {}", repositoryFolder.getPath(), accession.getUuid());
AccessionId accessionId = accession.getAccessionId();
accessionId.setRepositoryFolder(null);
accessionId.setImageCount(0);
accessionIdRepository.save(accessionId);
}
}
}
@Override
protected void onSaveFolder(RepositoryFolder repositoryFolder) throws FileRepositoryException {
LOG.debug("Folder saved {}", repositoryFolder.getPath());
if (GenesysImageGalleryAspects.isAccessionFolder(repositoryFolder)) {
Accession accession = accessionService.findMatchingAccession(repositoryFolder);
if (accession != null) {
AccessionId accessionId = accession.getAccessionId();
if (accessionId.getRepositoryFolder() == null) {
LOG.info("Linking folder {} to accession {}", repositoryFolder.getPath(), accession.getUuid());
accessionId.setRepositoryFolder(repositoryFolder);
accessionIdRepository.save(accessionId);
}
} else {
throw new InvalidRepositoryPathException("No such accession in Genesys. Refusing to create folder.");
}
}
}
}
...@@ -17,7 +17,6 @@ ...@@ -17,7 +17,6 @@
package org.genesys2.server.component.aspect; package org.genesys2.server.component.aspect;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.aspectj.lang.JoinPoint; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.ProceedingJoinPoint;
...@@ -34,7 +33,7 @@ import org.genesys.filerepository.service.aspect.ImageGalleryAspectsImpl; ...@@ -34,7 +33,7 @@ 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.model.genesys.AccessionId;
import org.genesys2.server.persistence.AccessionIdRepository; import org.genesys2.server.persistence.AccessionIdRepository;
import org.genesys2.server.persistence.AccessionRepository; import org.genesys2.server.service.AccessionService;
import org.genesys2.server.service.InstituteFilesService; import org.genesys2.server.service.InstituteFilesService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
...@@ -51,10 +50,6 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl ...@@ -51,10 +50,6 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
/** The Constant LOG. */ /** The Constant LOG. */
public static final Logger LOG = LoggerFactory.getLogger(GenesysImageGalleryAspects.class); public static final Logger LOG = LoggerFactory.getLogger(GenesysImageGalleryAspects.class);
/** The Constant ACCESSION_IMAGE_PATTERN. */
private static final Pattern ACCESSION_IMAGE_PATTERN = Pattern.compile(InstituteFilesService.REPOSITORY_INSTITUTE_PREFIX + "([A-Z]{3}\\d+)"
+ InstituteFilesService.REPOSITORY_INSTITUTE_ACCESSIONFILES + "/(.+)");
/** The image gallery aspect. */ /** The image gallery aspect. */
private ImageGalleryAspectsImpl imageGalleryAspect = new ImageGalleryAspectsImpl(); private ImageGalleryAspectsImpl imageGalleryAspect = new ImageGalleryAspectsImpl();
...@@ -67,7 +62,7 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl ...@@ -67,7 +62,7 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
private ImageGalleryService imageGalleryService; private ImageGalleryService imageGalleryService;
@Autowired @Autowired
private AccessionRepository accessionRepository; private AccessionService accessionService;
@Autowired @Autowired
private AccessionIdRepository accessionIdRepository; private AccessionIdRepository accessionIdRepository;
...@@ -118,14 +113,15 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl ...@@ -118,14 +113,15 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
* @param repositoryImage * @param repositoryImage
*/ */
private void handleAccessionImageSave(RepositoryImage repositoryImage) { private void handleAccessionImageSave(RepositoryImage repositoryImage) {
if (isAccessionFolder(repositoryImage.getFolder())) { RepositoryFolder folder = repositoryImage.getFolder();
Accession accession = findMatchingAccession(repositoryImage); if (isAccessionFolder(folder)) {
Accession accession = accessionService.findMatchingAccession(folder);
if (accession != null) { if (accession != null) {
ensureImageGallery(accession, repositoryImage); ensureImageGallery(accession, repositoryImage);
updateImageCountOfAcce(accession); updateImageCountOfAcce(accession);
} else { } else {
LOG.warn("No accession for repository folder {}", repositoryImage.getFolder().getPath()); LOG.warn("No accession for repository folder {}", folder.getPath());
throw new DataIntegrityViolationException("No unique accession in Genesys for " + repositoryImage.getFolder().getPath()); throw new DataIntegrityViolationException("No unique accession in Genesys for " + folder.getPath());
} }
} }
} }
...@@ -136,8 +132,9 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl ...@@ -136,8 +132,9 @@ 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 {
if (isAccessionFolder(repositoryImage.getFolder())) { RepositoryFolder repositoryFolder = repositoryImage.getFolder();
Accession accession = findMatchingAccession(repositoryImage); if (isAccessionFolder(repositoryFolder)) {
Accession accession = accessionService.findMatchingAccession(repositoryFolder);
if (accession != null) { if (accession != null) {
updateImageCountOfAcce(accession); updateImageCountOfAcce(accession);
} }
...@@ -156,35 +153,37 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl ...@@ -156,35 +153,37 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
if (accession != null) { if (accession != null) {
AccessionId accessionId = accession.getAccessionId(); AccessionId accessionId = accession.getAccessionId();
if (accession.getAccessionId().getRepositoryFolder() == null // New gallery if (accessionId.getRepositoryFolder() == null // New gallery
// or image gallery doesn't exist // or image gallery doesn't exist
|| imageGalleryService.loadImageGallery(accession.getAccessionId().getRepositoryFolder().getFolderPath()) == null) { || imageGalleryService.loadImageGallery(accessionId.getRepositoryFolder().getFolderPath()) == null) {
ImageGallery imageGallery = instituteFilesService.createImageGallery(accession.getInstitute(), accession); ImageGallery imageGallery = instituteFilesService.createImageGallery(accession.getInstitute(), accession);
if (accessionId.getRepositoryFolder() == null) {
accessionId.setRepositoryFolder(imageGallery.getFolder()); accessionId.setRepositoryFolder(imageGallery.getFolder());
accessionIdRepository.save(accessionId); accessionIdRepository.save(accessionId);
} }
} }
}
} catch (InvalidRepositoryPathException e) { } catch (InvalidRepositoryPathException e) {
LOG.warn("Error creating accession image gallery at {}, error is {}", repositoryImage.getFolder().getPath(), e.getMessage()); LOG.warn("Error creating accession image gallery at {}, error is {}", repositoryImage.getFolder().getPath(), e.getMessage());
} }
} }
private Accession findMatchingAccession(RepositoryImage repositoryImage) { public final static boolean isAccessionFolder(RepositoryFolder folder) {
LOG.trace(repositoryImage.getOriginalFilename()); Matcher matcher = AccessionService.ACCESSION_IMAGE_PATTERN.matcher(folder.getPath());
Accession accession = null;
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(repositoryImage.getFolder().getPath());
if (matcher.matches()) { if (matcher.matches()) {
LOG.debug("Path {} is within institute accession files prefix instCode={} acceNumb={}", repositoryImage.getFolder().getPath(), matcher.group(1), matcher.group(2)); LOG.debug("Path {} is within institute accession files prefix instCode={} acceNumb={}", folder.getPath(), matcher.group(1), matcher.group(2));
accession = accessionRepository.findByInstituteCodeAndAccessionNumber(matcher.group(1), matcher.group(2)); return true;
} else {
return false;
} }
return accession;
} }
private boolean isAccessionFolder(RepositoryFolder folder) { public final static boolean isInstituteFolder(RepositoryFolder folder) {
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(folder.getPath()); Matcher matcher = AccessionService.INSTITUTE_FOLDER_PATTERN.matcher(folder.getPath());
if (matcher.matches()) { if (matcher.matches()) {
LOG.debug("Path {} is within institute accession files prefix instCode={} acceNumb={}", folder.getPath(), matcher.group(1), matcher.group(2)); LOG.debug("Path {} is an institute folder instCode={}", folder.getPath(), matcher.group(1));
return true; return true;
} else { } else {
return false; return false;
......
...@@ -629,7 +629,7 @@ public class AdminController { ...@@ -629,7 +629,7 @@ public class AdminController {
try { try {
Accession accession = genesysService.getAccession(instCode, acceFolder.getName()); Accession accession = genesysService.getAccession(instCode, acceFolder.getName());
if (accession != null) { if (accession != null) {
LOG.warn("Folder for accession {}:{} is {}", instCode, accession.getAccessionNumber(), acceFolder.getPath()); LOG.debug("Folder for accession {}:{} is {}", instCode, accession.getAccessionNumber(), acceFolder.getPath());
accession.getAccessionId().setRepositoryFolder(acceFolder); accession.getAccessionId().setRepositoryFolder(acceFolder);
{ {
// Get number of images in the gallery // Get number of images in the gallery
...@@ -647,7 +647,7 @@ public class AdminController { ...@@ -647,7 +647,7 @@ public class AdminController {
} }
}); });
} else { } else {
LOG.warn("No /wiews/{}/acn folder", instCode); LOG.info("No /wiews/{}/acn folder", instCode);
} }
} catch (InvalidRepositoryPathException e) { } catch (InvalidRepositoryPathException e) {
LOG.warn("Invalid path {}", e.getMessage()); LOG.warn("Invalid path {}", e.getMessage());
......
/* /*
* 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.
...@@ -19,10 +19,12 @@ import java.util.List; ...@@ -19,10 +19,12 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Pattern;
import org.genesys.catalog.model.dataset.Dataset; import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.filerepository.model.ImageGallery; import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryFile; import org.genesys.filerepository.model.RepositoryFile;
import org.genesys.filerepository.model.RepositoryFolder;
import org.genesys2.server.model.genesys.Accession; import org.genesys2.server.model.genesys.Accession;
import org.genesys2.server.model.genesys.PDCI; import org.genesys2.server.model.genesys.PDCI;
import org.genesys2.server.model.impl.AccessionIdentifier3; import org.genesys2.server.model.impl.AccessionIdentifier3;
...@@ -34,6 +36,11 @@ import org.genesys.blocks.auditlog.model.AuditLog; ...@@ -34,6 +36,11 @@ import org.genesys.blocks.auditlog.model.AuditLog;
public interface AccessionService { public interface AccessionService {
/** The Constant ACCESSION_IMAGE_PATTERN. */
public static final Pattern INSTITUTE_FOLDER_PATTERN = Pattern.compile(InstituteFilesService.REPOSITORY_INSTITUTE_PREFIX + "([A-Z]{3,4}\\d+)");
public static final Pattern ACCESSION_IMAGE_PATTERN = Pattern.compile(InstituteFilesService.REPOSITORY_INSTITUTE_PREFIX + "([A-Z]{3,4}\\d+)"
+ InstituteFilesService.REPOSITORY_INSTITUTE_ACCESSIONFILES + "/(.+)");
/** /**
* Count accessions. Uses Elasticsearch, but counts from database when number is * Count accessions. Uses Elasticsearch, but counts from database when number is
* small enough. * small enough.
...@@ -126,6 +133,14 @@ public interface AccessionService { ...@@ -126,6 +133,14 @@ public interface AccessionService {
List<Accession> forUuids(Set<UUID> uuids); List<Accession> forUuids(Set<UUID> uuids);
/**
* Find matching accession.
*
* @param repositoryFolder the repository folder
* @return the accession
*/
Accession findMatchingAccession(RepositoryFolder repositoryFolder);
/** /**
* Executes the action on a list of accession in a * Executes the action on a list of accession in a
* Spring transaction. Spring security context not supported. * Spring transaction. Spring security context not supported.
...@@ -161,4 +176,5 @@ public interface AccessionService { ...@@ -161,4 +176,5 @@ public interface AccessionService {
public Map<String, List<AuditLog>> auditAccessionCollect; public Map<String, List<AuditLog>> auditAccessionCollect;
public Map<String, List<AuditLog>> auditAccessionGeo; public Map<String, List<AuditLog>> auditAccessionGeo;
} }
} }
...@@ -24,6 +24,7 @@ import java.util.Map; ...@@ -24,6 +24,7 @@ import java.util.Map;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.regex.Matcher;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
...@@ -346,4 +347,16 @@ public class AccessionServiceImpl implements AccessionService { ...@@ -346,4 +347,16 @@ public class AccessionServiceImpl implements AccessionService {
return accessions; return accessions;
} }
} }
@Override
public Accession findMatchingAccession(RepositoryFolder repositoryFolder) {
Accession accession = null;
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(repositoryFolder.getPath());
if (matcher.matches()) {
LOG.debug("Path {} is within institute accession files prefix instCode={} acceNumb={}", repositoryFolder.getPath(), matcher.group(1), matcher.group(2));
accession = accessionRepository.findByInstituteCodeAndAccessionNumber(matcher.group(1), matcher.group(2));
}
return accession;
}
} }
/* /*
* Copyright 2017 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.spring.config; package org.genesys2.spring.config;
...@@ -48,6 +48,7 @@ import org.genesys.filerepository.service.impl.ImageGalleryServiceImpl; ...@@ -48,6 +48,7 @@ import org.genesys.filerepository.service.impl.ImageGalleryServiceImpl;
import org.genesys.filerepository.service.impl.RepositoryServiceImpl; import org.genesys.filerepository.service.impl.RepositoryServiceImpl;
import org.genesys.filerepository.service.impl.S3StorageServiceImpl; import org.genesys.filerepository.service.impl.S3StorageServiceImpl;
import org.genesys.filerepository.service.impl.ThumbnailGenerator1; import org.genesys.filerepository.service.impl.ThumbnailGenerator1;
import org.genesys2.server.component.aspect.AccessionRepositoryFolderAspects;
import org.genesys2.server.component.aspect.GenesysImageGalleryAspects; import org.genesys2.server.component.aspect.GenesysImageGalleryAspects;
import org.genesys2.server.service.UserService; import org.genesys2.server.service.UserService;
import org.slf4j.Logger; import org.slf4j.Logger;
...@@ -165,6 +166,17 @@ public class FileRepositoryConfig implements InitializingBean { ...@@ -165,6 +166,17 @@ public class FileRepositoryConfig implements InitializingBean {
return new GenesysImageGalleryAspects(); return new GenesysImageGalleryAspects();
} }
/**
* Register accession repository folder aspects.
*
* @return the accession repository folder aspects
*/
@Bean
public AccessionRepositoryFolderAspects accessionFolderASpects() {
return new AccessionRepositoryFolderAspects();
}
/** /**
* Maintain metadata in .json in storage * Maintain metadata in .json in storage
* *
......
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