Commit 887342e4 authored by Matija Obreza's avatar Matija Obreza

Introducing AbstractRepositoryFolderAspects

parent b55f11c4
/*
* 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.genesys.filerepository.service.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.genesys.filerepository.FileRepositoryException;
import org.genesys.filerepository.model.RepositoryFolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* The Class AbstractRepositoryFolderAspects.
*/
@Aspect
public abstract class AbstractRepositoryFolderAspects {
/** The Constant LOG. */
protected Logger LOG = LoggerFactory.getLogger(getClass());
/**
* After repository folder save iterable.
*
* @param joinPoint the join point
* @param repositoryFolders the repository images
* @return the object
* @throws Throwable the throwable
*/
@AfterReturning(value = "execution(* org.genesys.filerepository.persistence.RepositoryFolderRepository.save(*))", returning = "repositoryFolders")
public Object afterRepositoryFolderSaveIterable(final JoinPoint joinPoint, final Iterable<RepositoryFolder> repositoryFolders) throws Throwable {
LOG.debug("Many folders were saved: {}", repositoryFolders);
if (repositoryFolders != null) {
repositoryFolders.forEach(folder -> {
try {
onSaveFolder(folder);
} catch (FileRepositoryException e) {
LOG.error("Error adding image to gallery: {}", e.getMessage(), e);
}
});
}
return repositoryFolders;
}
/**
* After repository image save.
*
* @param joinPoint the join point
* @param repositoryFolder the repository image
* @return the object
* @throws Throwable the throwable
*/
@AfterReturning(value = "execution(* org.genesys.filerepository.persistence.RepositoryFolderRepository.save(*))", returning = "repositoryFolder")
public Object afterRepositoryFolderSave(final JoinPoint joinPoint, final RepositoryFolder repositoryFolder) throws Throwable {
if (LOG.isTraceEnabled()) {
LOG.trace("1 folder was saved: {}", repositoryFolder);
}
if (repositoryFolder != null) {
onSaveFolder(repositoryFolder);
}
return repositoryFolder;
}
/**
* Around repository image delete.
*
* @param joinPoint the join point
* @param repositoryFolder the repository image
* @return the object
* @throws Throwable the throwable
*/
@Around(value = "execution(* org.genesys.filerepository.persistence.RepositoryFolderRepository.delete(*)) && args(repositoryFolder)")
public Object aroundRepositoryFolderDelete(final ProceedingJoinPoint joinPoint, final RepositoryFolder repositoryFolder) throws Throwable {
if (repositoryFolder != null) {
LOG.trace("1 folder is being deleted path={}", repositoryFolder.getPath());
}
try {
joinPoint.proceed();
onRemoveFolder(repositoryFolder);
} catch (final Throwable e) {
throw e;
}
return repositoryFolder;
}
/**
* On remove folder.
*
* @param repositoryFolder the repository folder
*/
protected abstract void onRemoveFolder(RepositoryFolder repositoryFolder) throws FileRepositoryException;
/**
* On add folder.
*
* @param repositoryFolder the repository folder
*/
protected abstract void onSaveFolder(RepositoryFolder repositoryFolder) throws FileRepositoryException;
}
\ No newline at end of file
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