CropTraitController.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.api.v1.impl;

import java.io.IOException;

import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.CRUDController;
import org.gringlobal.api.v1.TranslatedCRUDController;
import org.gringlobal.model.CropTrait;
import org.gringlobal.model.CropTraitAttach;
import org.gringlobal.model.CropTraitCode;
import org.gringlobal.model.CropTraitCodeAttach;
import org.gringlobal.model.CropTraitCodeLang;
import org.gringlobal.model.CropTraitLang;
import org.gringlobal.model.QCropTrait;
import org.gringlobal.service.CropTraitAttachmentService;
import org.gringlobal.service.CropTraitCodeAttachmentService;
import org.gringlobal.service.CropTraitCodeService;
import org.gringlobal.service.CropTraitCodeTranslationService;
import org.gringlobal.service.CropTraitCodeTranslationService.TranslatedCropTraitCode;
import org.gringlobal.service.CropTraitService;
import org.gringlobal.service.CropTraitTranslationService;
import org.gringlobal.service.CropTraitTranslationService.TranslatedCropTrait;
import org.gringlobal.service.filter.CropTraitCodeFilter;
import org.gringlobal.service.filter.CropTraitFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.querydsl.core.types.OrderSpecifier;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController("cropTraitApi1")
@RequestMapping(CropTraitController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "CropTrait")
public class CropTraitController extends TranslatedCRUDController<CropTrait, TranslatedCropTrait, CropTraitLang, CropTraitService, CropTraitFilter> {

	/** The Constant API_URL. */
	public static final String API_URL = ApiBaseController.APIv1_BASE + "/crop-trait";

	protected Class<CropTraitFilter> filterType() {
		return CropTraitFilter.class;
	}

	protected OrderSpecifier<?>[] defaultSort() {
		return new OrderSpecifier[] { QCropTrait.cropTrait.id.asc() };
	}

	@Autowired
	@Qualifier(value = "cropTraitAttachmentSupport")
	private CropTraitAttachmentService cropTraitAttachmentService;

	@Autowired
	private CropTraitService cropTraitService;


	@PostMapping(value = "/attach/{cropTraitId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(description = "Attach crop trait file", summary = "Attach file")
	public CropTraitAttach uploadFile(@PathVariable(name = "cropTraitId") final Long cropTraitId, @RequestPart(name = "file") final MultipartFile file,
			@RequestPart(name = "metadata") final CropTraitAttachmentService.CropTraitAttachmentRequest metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

		return cropTraitAttachmentService.uploadFile(cropTraitService.get(cropTraitId), file, metadata);
	}

	@DeleteMapping(value = "/attach/{cropTraitId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(description = "Remove attached file", summary = "Remove file")
	public CropTraitAttach removeFile(@PathVariable(name = "cropTraitId") final Long cropTraitId, @PathVariable(name = "attachmentId") final Long attachmentId) {
		return cropTraitAttachmentService.removeFile(cropTraitService.get(cropTraitId), attachmentId);
	}


	@RestController("cropTraitLangApi1")
	@RequestMapping(CropTraitLangController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "CropTrait")
	public static class CropTraitLangController extends CRUDController<CropTraitLang, CropTraitTranslationService> {
		/** The Constant API_URL. */
		public static final String API_URL = CropTraitController.API_URL + "/lang";
	}

	@RestController("cropTraitAttachApi1")
	@RequestMapping(CropTraitAttachController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "CropTrait")
	public static class CropTraitAttachController extends CRUDController<CropTraitAttach, CropTraitAttachmentService> {
		/** The Constant API_URL. */
		public static final String API_URL = CropTraitController.API_URL + "/attach/meta";

		@Override
		@Operation(operationId = "createAccessionInvAttach", description = "Create CropTraitAttach", summary = "Create")
		public CropTraitAttach create(@RequestBody CropTraitAttach entity) {
			// Throws UnsupportedOperationException
			return super.create(entity);
		}

		@Override
		@Operation(operationId = "updateCropTraitAttach", description = "Update an existing record", summary = "Update")
		public CropTraitAttach update(@RequestBody CropTraitAttach entity) {
			return super.update(entity);
		}

		@Override
		@Operation(operationId = "getCropTraitAttach", description = "Get record by ID", summary = "Get")
		public CropTraitAttach get(@PathVariable long id) {
			return super.get(id);
		}

		@Override
		@Operation(operationId = "deleteCropTraitAttach", description = "Delete existing record by ID", summary = "Delete")
		public CropTraitAttach remove(@PathVariable long id) {
			return super.remove(id);
		}
	}

	

	@RestController("cropTraitCodeApi1")
	@RequestMapping(CropTraitCodeController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "CropTrait")
	public static class CropTraitCodeController extends TranslatedCRUDController<CropTraitCode, TranslatedCropTraitCode, CropTraitCodeLang, CropTraitCodeService, CropTraitCodeFilter> {
		public static final String API_URL = CropTraitController.API_URL + "/code";

		@Autowired
//		@Qualifier(value = "cropTraitCodeAttachmentSupport")
		private CropTraitCodeAttachmentService cropTraitCodeAttachmentService;


		@RestController("cropTraitCodeLangApi1")
		@RequestMapping(CropTraitCodeLangController.API_URL)
		@PreAuthorize("isAuthenticated()")
		@Tag(name = "CropTrait")
		public static class CropTraitCodeLangController extends CRUDController<CropTraitCodeLang, CropTraitCodeTranslationService> {
			/** The Constant API_URL. */
			public static final String API_URL = CropTraitCodeController.API_URL + "/lang";
		}

		@PostMapping(value = "/attach/{cropTraitCodeId}", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(description = "Attach crop trait file", summary = "Attach file")
		public CropTraitCodeAttach uploadTraitCodeFile(@PathVariable(name = "cropTraitCodeId") final Long cropTraitCodeId, @RequestPart(name = "file") final MultipartFile file,
				@RequestPart(name = "metadata") final CropTraitCodeAttachmentService.CropTraitCodeAttachmentRequest metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

			return cropTraitCodeAttachmentService.uploadFile(translatedCrudService.get(cropTraitCodeId), file, metadata);
		}

		@DeleteMapping(value = "/attach/{cropTraitCodeId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(description = "Remove attached file", summary = "Remove file")
		public CropTraitCodeAttach removeTraitCodeFile(@PathVariable(name = "cropTraitCodeId") final Long cropTraitCodeId, @PathVariable(name = "attachmentId") final Long attachmentId) {
			return cropTraitCodeAttachmentService.removeFile(translatedCrudService.get(cropTraitCodeId), attachmentId);
		}

		@RestController("cropTraitCodeAttachApi1")
		@RequestMapping(CropTraitCodeAttachController.API_URL)
		@PreAuthorize("isAuthenticated()")
		@Tag(name = "CropTrait")
		public static class CropTraitCodeAttachController extends CRUDController<CropTraitCodeAttach, CropTraitCodeAttachmentService> {
			/** The Constant API_URL. */
			public static final String API_URL = CropTraitCodeController.API_URL + "/attach/meta";

			@Override
			@Operation(operationId = "createAccessionInvAttach", description = "Create CropTraitCodeAttach", summary = "Create")
			public CropTraitCodeAttach create(@RequestBody CropTraitCodeAttach entity) {
				// Throws UnsupportedOperationException
				return super.create(entity);
			}

			@Override
			@Operation(operationId = "updateCropTraitAttach", description = "Update an existing record", summary = "Update")
			public CropTraitCodeAttach update(@RequestBody CropTraitCodeAttach entity) {
				return super.update(entity);
			}

			@Override
			@Operation(operationId = "getCropTraitCodeAttach", description = "Get record by ID", summary = "Get")
			public CropTraitCodeAttach get(@PathVariable long id) {
				return super.get(id);
			}

			@Override
			@Operation(operationId = "deleteCropTraitAttach", description = "Delete existing record by ID", summary = "Delete")
			public CropTraitCodeAttach remove(@PathVariable long id) {
				return super.remove(id);
			}
		}

	}

}