CropTraitCodeServiceImpl.java

/*
 * Copyright 2020 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.gringlobal.service.impl;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;

import lombok.extern.slf4j.Slf4j;
import org.gringlobal.api.exception.InvalidApiUsageException;
import org.gringlobal.api.v1.MultiOp;
import org.gringlobal.model.Crop;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitCode;
import org.gringlobal.model.CropTraitCodeAttach;
import org.gringlobal.model.CropTraitCodeLang;
import org.gringlobal.model.QCropTraitCodeAttach;
import org.gringlobal.persistence.CropTraitCodeLangRepository;
import org.gringlobal.persistence.CropTraitCodeRepository;
import org.gringlobal.service.CropTraitCodeAttachmentService;
import org.gringlobal.service.CropTraitCodeAttachmentService.CropTraitCodeAttachmentRequest;
import org.gringlobal.service.CropTraitCodeService;
import org.gringlobal.service.CropTraitCodeTranslationService;
import org.gringlobal.service.CropTraitCodeTranslationService.TranslatedCropTraitCode;
import org.gringlobal.service.filter.CropTraitCodeFilter;
import org.hibernate.Hibernate;
// import org.springframework.cache.annotation.CacheConfig;
// import org.springframework.cache.annotation.CacheEvict;
// import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;


/**
 * The Class CropTraitServiceImpl.
 */
@Service
@Transactional(readOnly = true)
@Validated
// @CacheConfig(cacheNames = { "cropTraits" })
@Slf4j
public class CropTraitCodeServiceImpl extends FilteredTranslatedCRUDServiceImpl<CropTraitCode, CropTraitCodeLang, TranslatedCropTraitCode, CropTraitCodeFilter, CropTraitCodeRepository> implements CropTraitCodeService {

	@Component(value = "cropTraitCodeAttachmentSupport")
	protected static class CropTraitCodeAttachmentSupport extends BaseAttachmentSupport<CropTraitCode, CropTraitCodeAttach, CropTraitCodeAttachmentRequest> implements CropTraitCodeAttachmentService {

		public CropTraitCodeAttachmentSupport() {
			super(QCropTraitCodeAttach.cropTraitCodeAttach.cropTraitCode().id, QCropTraitCodeAttach.cropTraitCodeAttach.id);
		}

		@Override
		protected Path createRepositoryPath(CropTraitCode cropTraitCode) {
			cropTraitCode = owningEntityRepository.getReferenceById(cropTraitCode.getId());

			Hibernate.initialize(cropTraitCode.getCropTrait());
			CropTrait cropTrait = cropTraitCode.getCropTrait();
			Hibernate.initialize(cropTrait.getCrop());
			Crop crop = cropTrait.getCrop();

			return Paths.get("/crop", crop.getName(), "trait", cropTrait.getId().toString(), "code", cropTraitCode.getCode());
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('CropTrait', 'WRITE')")
		protected CropTraitCodeAttach createAttach(CropTraitCode entity, CropTraitCodeAttach source) {
			CropTraitCodeAttach attach = new CropTraitCodeAttach();
			attach.apply(source);
			attach.setVirtualPath(source.getVirtualPath()); // SOAP uses this to create the record
			attach.setCropTraitCode(entity);
			return attach;
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('CropTrait', 'WRITE')")
		public CropTraitCodeAttach create(CropTraitCodeAttach source) {
			var owningEntity = owningEntityRepository.getReferenceById(source.getCropTraitCode().getId());

			var attach = createAttach(owningEntity, source);
			var savedAttach = repository.save(attach);
			return _lazyLoad(savedAttach);
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('CropTrait', 'WRITE')")
		public CropTraitCodeAttach update(CropTraitCodeAttach updated, CropTraitCodeAttach target) {
			target.apply(updated);
			return _lazyLoad(repository.save(target));
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('CropTrait', 'WRITE')")
		public CropTraitCodeAttach remove(CropTraitCodeAttach entity) {
			return super.remove(entity);
		}
	}

	@Component(value = "cropTraitCodeTranslationSupport")
	protected static class CropTraitCodeTranslationSupport extends BaseTranslationSupport<CropTraitCode, CropTraitCodeLang, TranslatedCropTraitCode, CropTraitCodeFilter, CropTraitCodeLangRepository> implements CropTraitCodeTranslationService {

		public CropTraitCodeTranslationSupport() {
			super();
		}

		@Override
		protected TranslatedCropTraitCode toTranslated(CropTraitCode entity, String title, String description) {
			return TranslatedCropTraitCode.from(entity, title, description);
		}

		@Override
		@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'CREATE')")
		public CropTraitCodeLang create(CropTraitCodeLang source) {
			return super.create(source);
		}

		@Override
		@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'WRITE')")
		public CropTraitCodeLang update(CropTraitCodeLang updated, CropTraitCodeLang target) {
			return super.update(updated, target);
		}

		@Override
		@Transactional
		@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'DELETE')")
		public CropTraitCodeLang remove(CropTraitCodeLang entity) {
			return super.remove(entity);
		}
	}


	@Override
	// @Cacheable(key = "'ctc-' + #id", unless = "#result == null")
	public CropTraitCode get(long id) {
		return super.get(id);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'CREATE')")
	public CropTraitCode create(CropTraitCode source) {
		assert(source.getId() == null);
		var target = new CropTraitCode();
		target.apply(source);
		return _lazyLoad(repository.save(target));
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'CREATE')")
	public CropTraitCode createFast(CropTraitCode source) {
		return repository.save(source);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'CREATE')")
	public TranslatedCropTraitCode create(TranslatedCropTraitCode source) {
		return super.create(source);
	}
	
	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'CREATE')")
	public MultiOp<CropTraitCode> create(List<CropTraitCode> inserts) {
		return super.create(inserts);
	}
	
	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'CREATE')")
	public MultiOp<CropTraitCode> createFast(List<CropTraitCode> inserts) {
		return super.createFast(inserts);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'WRITE')")
	// @CacheEvict(key = "'ctc-' + #target.id")
	public CropTraitCode update(CropTraitCode input, CropTraitCode target) {
		if (input == null) {
			throw new InvalidApiUsageException("CropTrait must be provided.");
		}

		log.debug("Update CropTraitCode. Input data {}", input);
		target.apply(input);

		CropTraitCode saved = repository.save(target);
		return _lazyLoad(saved);
	}

	@Override
	@Transactional
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'WRITE')")
	// @CacheEvict(key = "'ctc-' + #target.id")
	public CropTraitCode updateFast(CropTraitCode updated, CropTraitCode target) {
		target.apply(updated);
		return repository.save(target);
	}

	@Override
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'WRITE')")
	// @CacheEvict(allEntries = true)
	public MultiOp<CropTraitCode> update(List<CropTraitCode> updates) {
		return super.update(updates);
	}

	@Override
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'DELETE')")
	// @CacheEvict(key = "'ctc-' + #entity.id")
	public CropTraitCode remove(CropTraitCode entity) {
		return super.remove(entity);
	}

	@Override
	@PreAuthorize("@ggceSec.actionAllowed('CropTrait', 'DELETE')")
	// @CacheEvict(allEntries = true)
	public MultiOp<CropTraitCode> remove(List<CropTraitCode> deletes) {
		return super.remove(deletes);
	}

}