AbstractRepositoryFolderAspects.java

/*
 * 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 lombok.extern.slf4j.Slf4j;

/**
 * The Class AbstractRepositoryFolderAspects.
 */
@Aspect
@Slf4j
public abstract class AbstractRepositoryFolderAspects {

	/**
	 * 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 {

		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
	 * @throws FileRepositoryException the file repository exception
	 */
	protected abstract void onRemoveFolder(RepositoryFolder repositoryFolder) throws FileRepositoryException;

	/**
	 * On add folder.
	 *
	 * @param repositoryFolder the repository folder
	 * @throws FileRepositoryException the file repository exception
	 */
	protected abstract void onSaveFolder(RepositoryFolder repositoryFolder) throws FileRepositoryException;
}