SymptomServiceImpl.java

/*
 * Copyright 2022 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 org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.model.community.QSymptomAttach;
import org.gringlobal.model.community.Symptom;
import org.gringlobal.model.community.SymptomAttach;
import org.gringlobal.persistence.community.SymptomRepository;
import org.gringlobal.service.SymptomAttachmentService;
import org.gringlobal.service.SymptomService;
import org.gringlobal.service.filter.SymptomFilter;
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;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;


@Service
@Transactional(readOnly = true)
@Validated
public class SymptomServiceImpl extends FilteredCRUDService2Impl<Symptom, SymptomFilter, SymptomRepository> implements SymptomService {

	@Component
	protected static class AttachmentSupport extends BaseAttachmentSupport<Symptom, SymptomAttach, SymptomAttachmentService.SymptomAttachmentRequest> implements SymptomAttachmentService {
		
		public AttachmentSupport() {
			super(QSymptomAttach.symptomAttach.symptom().id, QSymptomAttach.symptomAttach.id);
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Symptom', 'ADMINISTRATION')")
		@Transactional
		public SymptomAttach uploadFile(Symptom entity, MultipartFile file, SymptomAttachmentService.SymptomAttachmentRequest metadata) throws IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException {
			return super.uploadFile(entity, file, metadata);
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Symptom', 'DELETE')")
		@Transactional
		public SymptomAttach removeFile(Symptom entity, Long attachmentId) {
			return super.removeFile(entity, attachmentId);
		}

		@Override
		public Path createRepositoryPath(Symptom symptom) {
			symptom = owningEntityRepository.getReferenceById(symptom.getId());
			return Paths.get("/symptom/" + symptom.getName());
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Symptom', 'ADMINISTRATION')")
		protected SymptomAttach createAttach(Symptom entity, SymptomAttach source) {
			SymptomAttach attach = new SymptomAttach();
			attach.apply(source);
			attach.setSymptom(entity);
			return attach;
		}

		@Override
		public SymptomAttach create(SymptomAttach source) {
			throw new UnsupportedOperationException("Create attachments by uploading a file.");
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Symptom', 'ADMINISTRATION')")
		@Transactional
		public SymptomAttach update(SymptomAttach updated, SymptomAttach target) {
			target.apply(updated);
			return _lazyLoad(repository.save(target));
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Symptom', 'DELETE')")
		@Transactional
		public SymptomAttach remove(SymptomAttach entity) {
			return super.remove(entity);
		}
	}

	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Symptom', 'CREATE')")
	public Symptom create(Symptom source) {
		Symptom saved = new Symptom();
		saved.apply(source);
		return repository.save(saved);
	}
	
	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Symptom', 'WRITE')")
	public Symptom update(Symptom input, Symptom target) {
		target.apply(input);
		return _lazyLoad(repository.save(target));
	}
	
	@Override
	@Transactional
	@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('Symptom', 'WRITE')")
	public Symptom updateFast(Symptom updated, Symptom target) {
		target.apply(updated);
		return repository.save(target);
	}
}