InventorySymptomMapServiceImpl.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.InventorySymptomMap;
import org.gringlobal.model.community.InventorySymptomMapAttach;
import org.gringlobal.model.community.QInventorySymptomMapAttach;
import org.gringlobal.persistence.community.InventorySymptomMapRepository;
import org.gringlobal.service.InventorySymptomMapAttachmentService;
import org.gringlobal.service.InventorySymptomMapService;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

@Service
@Validated
public class InventorySymptomMapServiceImpl extends CRUDService2Impl<InventorySymptomMap, InventorySymptomMapRepository> implements InventorySymptomMapService {

	@Override
	@Transactional
	public InventorySymptomMap create(InventorySymptomMap source) {
		InventorySymptomMap saved = new InventorySymptomMap();
		saved.apply(source);
		return repository.save(saved);
	}

	@Override
	@Transactional
	public InventorySymptomMap update(InventorySymptomMap input, InventorySymptomMap target) {
		target.apply(input);
		return _lazyLoad(repository.save(target));
	}

	@Override
	@Transactional
	public InventorySymptomMap updateFast(@NotNull @Valid InventorySymptomMap updated, InventorySymptomMap target) {
		target.apply(updated);
		return repository.save(target);
	}

	@Override
	public Page<InventorySymptomMap> list(Pageable page) {
		var itemsPage = super.list(page);
		itemsPage.getContent().forEach(this::_lazyLoad);
		return itemsPage;
	}

	@Component
	protected static class AttachmentSupport extends BaseAttachmentSupport<InventorySymptomMap, InventorySymptomMapAttach, InventorySymptomMapAttachmentService.InventorySymptomMapAttachmentRequest> implements InventorySymptomMapAttachmentService {

		public AttachmentSupport() {
			super(QInventorySymptomMapAttach.inventorySymptomMapAttach.inventorySymptomMap().id, QInventorySymptomMapAttach.inventorySymptomMapAttach.id);
		}

		@Override
		@PreAuthorize("hasAuthority('GROUP_ADMINS') or @ggceSec.actionAllowed('InventorySymptomMap', 'ADMINISTRATION')")
		@Transactional
		public InventorySymptomMapAttach uploadFile(InventorySymptomMap entity, MultipartFile file, InventorySymptomMapAttachmentService.InventorySymptomMapAttachmentRequest metadata) throws IOException, InvalidRepositoryPathException, InvalidRepositoryFileDataException {
			return super.uploadFile(entity, file, metadata);
		}

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

		@Override
		public Path createRepositoryPath(InventorySymptomMap symptomMap) {
			symptomMap = owningEntityRepository.getReferenceById(symptomMap.getId());
			return Paths.get("/inventory-symptom/" + symptomMap.getId());
		}

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

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

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

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

}