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 @@
package org.genesys2.server.component.aspect;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
......@@ -34,7 +33,7 @@ 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.AccessionService;
import org.genesys2.server.service.InstituteFilesService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -51,10 +50,6 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
/** The Constant LOG. */
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. */
private ImageGalleryAspectsImpl imageGalleryAspect = new ImageGalleryAspectsImpl();
......@@ -65,9 +60,9 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
/** The image gallery service. */
@Autowired
private ImageGalleryService imageGalleryService;
@Autowired
private AccessionRepository accessionRepository;
private AccessionService accessionService;
@Autowired
private AccessionIdRepository accessionIdRepository;
......@@ -118,14 +113,15 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
* @param repositoryImage
*/
private void handleAccessionImageSave(RepositoryImage repositoryImage) {
if (isAccessionFolder(repositoryImage.getFolder())) {
Accession accession = findMatchingAccession(repositoryImage);
RepositoryFolder folder = repositoryImage.getFolder();
if (isAccessionFolder(folder)) {
Accession accession = accessionService.findMatchingAccession(folder);
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());
LOG.warn("No accession for repository folder {}", folder.getPath());
throw new DataIntegrityViolationException("No unique accession in Genesys for " + folder.getPath());
}
}
}
......@@ -136,8 +132,9 @@ 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 {
if (isAccessionFolder(repositoryImage.getFolder())) {
Accession accession = findMatchingAccession(repositoryImage);
RepositoryFolder repositoryFolder = repositoryImage.getFolder();
if (isAccessionFolder(repositoryFolder)) {
Accession accession = accessionService.findMatchingAccession(repositoryFolder);
if (accession != null) {
updateImageCountOfAcce(accession);
}
......@@ -156,13 +153,16 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
if (accession != null) {
AccessionId accessionId = accession.getAccessionId();
if (accession.getAccessionId().getRepositoryFolder() == null // New gallery
if (accessionId.getRepositoryFolder() == null // New gallery
// 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);
accessionId.setRepositoryFolder(imageGallery.getFolder());
accessionIdRepository.save(accessionId);
if (accessionId.getRepositoryFolder() == null) {
accessionId.setRepositoryFolder(imageGallery.getFolder());
accessionIdRepository.save(accessionId);
}
}
}
} catch (InvalidRepositoryPathException e) {
......@@ -170,21 +170,20 @@ public class GenesysImageGalleryAspects extends AbstractImageGalleryAspects impl
}
}
private Accession findMatchingAccession(RepositoryImage repositoryImage) {
LOG.trace(repositoryImage.getOriginalFilename());
Accession accession = null;
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(repositoryImage.getFolder().getPath());
public final static boolean isAccessionFolder(RepositoryFolder folder) {
Matcher matcher = AccessionService.ACCESSION_IMAGE_PATTERN.matcher(folder.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));
LOG.debug("Path {} is within institute accession files prefix instCode={} acceNumb={}", folder.getPath(), matcher.group(1), matcher.group(2));
return true;
} else {
return false;
}
return accession;
}
private boolean isAccessionFolder(RepositoryFolder folder) {
Matcher matcher = ACCESSION_IMAGE_PATTERN.matcher(folder.getPath());
public final static boolean isInstituteFolder(RepositoryFolder folder) {
Matcher matcher = AccessionService.INSTITUTE_FOLDER_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));
LOG.debug("Path {} is an institute folder instCode={}", folder.getPath(), matcher.group(1));
return true;
} else {
return false;
......
......@@ -629,7 +629,7 @@ public class AdminController {
try {
Accession accession = genesysService.getAccession(instCode, acceFolder.getName());
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);
{
// Get number of images in the gallery
......@@ -647,7 +647,7 @@ public class AdminController {
}
});
} else {
LOG.warn("No /wiews/{}/acn folder", instCode);
LOG.info("No /wiews/{}/acn folder", instCode);
}
} catch (InvalidRepositoryPathException e) {
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");
* you may not use this file except in compliance with the License.
......@@ -19,10 +19,12 @@ import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Pattern;
import org.genesys.catalog.model.dataset.Dataset;
import org.genesys.filerepository.model.ImageGallery;
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.PDCI;
import org.genesys2.server.model.impl.AccessionIdentifier3;
......@@ -34,6 +36,11 @@ import org.genesys.blocks.auditlog.model.AuditLog;
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
* small enough.
......@@ -125,7 +132,15 @@ public interface AccessionService {
Map<UUID, AccessionIdentifier3> toUUID(List<? extends AccessionIdentifier3> identifiers);
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
* Spring transaction. Spring security context not supported.
......@@ -161,4 +176,5 @@ public interface AccessionService {
public Map<String, List<AuditLog>> auditAccessionCollect;
public Map<String, List<AuditLog>> auditAccessionGeo;
}
}
......@@ -24,6 +24,7 @@ import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;
......@@ -346,4 +347,16 @@ public class AccessionServiceImpl implements AccessionService {
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");
* 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.spring.config;
......@@ -48,6 +48,7 @@ import org.genesys.filerepository.service.impl.ImageGalleryServiceImpl;
import org.genesys.filerepository.service.impl.RepositoryServiceImpl;
import org.genesys.filerepository.service.impl.S3StorageServiceImpl;
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.service.UserService;
import org.slf4j.Logger;
......@@ -164,6 +165,17 @@ public class FileRepositoryConfig implements InitializingBean {
public GenesysImageGalleryAspects imageGalleryAspects() {
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
......
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