ImageGalleryAspectsImpl.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 java.util.ArrayList;
import java.util.List;

import org.aspectj.lang.annotation.Aspect;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.genesys.filerepository.model.ImageGallery;
import org.genesys.filerepository.model.RepositoryImage;
import org.genesys.filerepository.persistence.RepositoryImagePersistence;
import org.genesys.filerepository.service.ImageGalleryService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import lombok.extern.slf4j.Slf4j;

/**
 * Handles magical updates to {@link ImageGallery} when files are added to
 * {@link RepositoryImagePersistence}.
 *
 * @author matijaobreza
 */
@Aspect
@Component
@Slf4j
public class ImageGalleryAspectsImpl extends AbstractImageGalleryAspects implements InitializingBean {

	/** The image gallery service. */
	@Autowired
	private ImageGalleryService imageGalleryService;

	/**
	 * Sets the image gallery service.
	 *
	 * @param imageGalleryService the new image gallery service
	 */
	public void setImageGalleryService(final ImageGalleryService imageGalleryService) {
		this.imageGalleryService = imageGalleryService;
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
	 */
	@Override
	public void afterPropertiesSet() throws Exception {
		log.warn("Instantiating bean {}", getClass().getName());

	}

	/**
	 * Instantiates a new image gallery aspects impl.
	 */
	public ImageGalleryAspectsImpl() {
		// log it
		log.warn("Instantiating {}", getClass().getName());
	}

	/**
	 * Check if image is saved at a path of a registered {@link ImageGallery} and
	 * add it to the gallery if it exists.
	 *
	 * @param repositoryImage the repository image
	 * @throws InvalidRepositoryPathException the invalid repository path exception
	 */
	@Override
	public void addImageToGallery(final RepositoryImage repositoryImage) throws InvalidRepositoryPathException {
		if (repositoryImage == null) {
			return;
		}

		final ImageGallery imageGallery = imageGalleryService.loadImageGallery(repositoryImage.getFolder().getFolderPath());

		if (imageGallery == null) {
			log.debug("No gallery at path={}", repositoryImage.getFolder());
			return;
		}

		List<RepositoryImage> imagesList = imageGallery.getImages();
		if (imagesList == null) {
			imagesList = new ArrayList<>(1);
		}

		if (!imagesList.contains(repositoryImage)) {
			log.debug("Adding {} to image gallery at path={}", repositoryImage, imageGallery.getPath());
			imagesList.add(repositoryImage);

			imageGalleryService.saveImageOrder(imageGallery);
		}
	}

	/**
	 * Check if image is part of a gallery at the specified path and remove it.
	 *
	 * @param repositoryImage the repository image
	 * @throws InvalidRepositoryPathException the invalid repository path exception
	 */
	@Override
	public void removeImageFromGallery(final RepositoryImage repositoryImage) throws InvalidRepositoryPathException {
		if (repositoryImage == null) {
			return;
		}

		final ImageGallery imageGallery = imageGalleryService.loadImageGallery(repositoryImage.getFolder().getFolderPath());

		if (imageGallery == null) {
			log.trace("No gallery at path={}", repositoryImage.getFolder());
			return;
		}

		final List<RepositoryImage> imagesList = imageGallery.getImages();
		if (imagesList == null) {
			return;
		}

		log.trace("Images size={}", imagesList.size());
		log.trace("Removing {} from image gallery at path={}", repositoryImage, imageGallery.getPath());

		imagesList.remove(repositoryImage);

		log.trace("New images size={}", imagesList.size());

		if (imagesList.isEmpty()) {
			try {
				imageGalleryService.removeGallery(imageGallery);
			} catch (final InvalidRepositoryPathException e) {
				log.error("Failed to remove empty image gallery", e);
			}
		} else {
			imageGalleryService.saveImageOrder(imageGallery);
		}
	}

}