AccessionRepositoryFolderAspects.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.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.genesys.server.model.genesys.Accession;
import org.genesys.server.model.genesys.AccessionId;
import org.genesys.server.persistence.AccessionIdRepository;
import org.genesys.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.");
			}
		}
	}
}