AbstractImageGalleryAspects.java

/*
 * Copyright 2018 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.InvalidRepositoryPathException;
import org.genesys.filerepository.model.RepositoryImage;

import lombok.extern.slf4j.Slf4j;

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

	/**
	 * After repository image save iterable.
	 *
	 * @param joinPoint the join point
	 * @param repositoryImages the repository images
	 * @return the object
	 * @throws Throwable the throwable
	 */
	@AfterReturning(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.save(*))", returning = "repositoryImages")
	public Object afterRepositoryImageSaveIterable(final JoinPoint joinPoint, final Iterable<RepositoryImage> repositoryImages) throws Throwable {

		log.debug("Many images were saved: {}", repositoryImages);

		if (repositoryImages != null) {
			repositoryImages.forEach(ri -> {
				try {
					addImageToGallery(ri);
				} catch (FileRepositoryException e) {
					log.error("Error adding image to gallery: {}", e.getMessage(), e);
				}
			});
		}

		return repositoryImages;
	}

	/**
	 * Adds the image to gallery.
	 *
	 * @param repositoryImage the repository image
	 * @throws InvalidRepositoryPathException the invalid repository path exception
	 */
	protected abstract void addImageToGallery(RepositoryImage repositoryImage) throws InvalidRepositoryPathException;

	/**
	 * After repository image save.
	 *
	 * @param joinPoint the join point
	 * @param repositoryImage the repository image
	 * @return the object
	 * @throws Throwable the throwable
	 */
	@AfterReturning(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.save(*))", returning = "repositoryImage")
	public Object afterRepositoryImageSave(final JoinPoint joinPoint, final RepositoryImage repositoryImage) throws Throwable {

		log.trace("1 image was saved: {}", repositoryImage);

		if (repositoryImage != null) {
			addImageToGallery(repositoryImage);
		}

		return repositoryImage;
	}

	/**
	 * Around repository image delete.
	 *
	 * @param joinPoint the join point
	 * @param repositoryImage the repository image
	 * @return the object
	 * @throws Throwable the throwable
	 */
	@Around(value = "execution(* org.genesys.filerepository.persistence.RepositoryImagePersistence.delete(*)) && args(repositoryImage)")
	public Object aroundRepositoryImageDelete(final ProceedingJoinPoint joinPoint, final RepositoryImage repositoryImage) throws Throwable {

		if (repositoryImage != null) {
			log.trace("1 image is being deleted path={} originalFilename={}", repositoryImage.getFolder(), repositoryImage.getOriginalFilename());
		}

		removeImageFromGallery(repositoryImage);

		try {
			joinPoint.proceed();

		} catch (final Throwable e) {
			// FIXME is this necessary? Exception should cancel transaction!
			log.info("Putting image back to gallery.");
			addImageToGallery(repositoryImage);
			throw e;
		}

		return repositoryImage;
	}

	/**
	 * Removes the image from gallery.
	 *
	 * @param repositoryImage the repository image
	 * @throws InvalidRepositoryPathException the invalid repository path exception
	 */
	protected abstract void removeImageFromGallery(RepositoryImage repositoryImage) throws InvalidRepositoryPathException;
}