PathogenController.java

/*
 * Copyright 2025 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.v2.impl;

import java.io.IOException;
import java.util.List;

import javax.validation.Valid;

import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.model.OrderRequestItemPathogenMapDTO;
import org.gringlobal.api.model.PathogenAttachDTO;
import org.gringlobal.api.model.PathogenDTO;
import org.gringlobal.api.model.PathogenGeographyMapDTO;
import org.gringlobal.api.model.PathogenSymptomMapDTO;
import org.gringlobal.api.model.PathogenTaxonomySpeciesMapDTO;
import org.gringlobal.api.model.TaxonomySpeciesDTO;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.v2.CRUDController;
import org.gringlobal.api.v2.FilteredCRUDController;
import org.gringlobal.api.v2.facade.OrderRequestItemPathogenMapApiService;
import org.gringlobal.api.v2.facade.PathogenApiService;
import org.gringlobal.api.v2.facade.PathogenAttachApiService;
import org.gringlobal.api.v2.facade.PathogenAttachApiService.PathogenAttachRequestDTO;
import org.gringlobal.api.v2.facade.PathogenGeographyMapApiService;
import org.gringlobal.api.v2.facade.PathogenSymptomMapApiService;
import org.gringlobal.api.v2.facade.PathogenTaxonomySpeciesMapApiService;
import org.gringlobal.model.community.OrderRequestItemPathogenMap;
import org.gringlobal.model.community.Pathogen;
import org.gringlobal.model.community.PathogenAttach;
import org.gringlobal.model.community.PathogenGeographyMap;
import org.gringlobal.model.community.PathogenSymptomMap;
import org.gringlobal.model.community.PathogenTaxonomySpeciesMap;
import org.gringlobal.service.filter.PathogenFilter;
import org.springframework.beans.factory.annotation.Autowired;
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.GetMapping;
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 io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController("pathogenApi2")
@RequestMapping(PathogenController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "PlantHealth")
public class PathogenController extends FilteredCRUDController<PathogenDTO, Pathogen, PathogenApiService, PathogenFilter> {

	/** The Constant API_URL. */
	public static final String API_URL = ApiBaseController.APIv2_BASE + "/pathogen";

	@Autowired
	private PathogenAttachApiService pathogenAttachApiService;

	@GetMapping(value = "/{pathogenId}/species", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "getPathogenSpecies", description = "Get all pathogen species", summary = "GetPathogenSpecies")
	public List<TaxonomySpeciesDTO> getPathogenSpecies(@PathVariable(name = "pathogenId") final Long pathogenId) {
		return serviceFacade.getPathogenSpecies(pathogenId);
	}

	@PostMapping(value = "/attach/{pathogenId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "uploadFile", description = "Attach pathogen file", summary = "Attach file")
	public PathogenAttachDTO uploadFile(@PathVariable(name = "pathogenId") final Long pathogenId, @RequestPart(name = "file") final MultipartFile file,
		@RequestPart(name = "metadata") final @Valid PathogenAttachRequestDTO metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

		return pathogenAttachApiService.uploadFile(pathogenId, file, metadata);
	}

	@DeleteMapping(value = "/attach/{pathogenId}/{attachmentId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "removeFile", description = "Remove attached file", summary = "Remove file")
	public PathogenAttachDTO removeFile(@PathVariable(name = "pathogenId") final Long pathogenId, @PathVariable(name = "attachmentId") final Long attachmentId) {
		return pathogenAttachApiService.removeFile(pathogenId, attachmentId);
	}

	@RestController("pathogenAttachApi2")
	@RequestMapping(PathogenController.PathogenAttachController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "PlantHealth")
	public static class PathogenAttachController extends CRUDController<PathogenAttachDTO, PathogenAttach, PathogenAttachApiService> {
		/** The Constant API_URL. */
		public static final String API_URL = PathogenController.API_URL + "/attach/meta";

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

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

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

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

	@RestController("pathogenSymptomMapApi2")
	@RequestMapping(PathogenController.PathogenSymptomMapController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "PlantHealth")
	public static class PathogenSymptomMapController extends CRUDController<PathogenSymptomMapDTO, PathogenSymptomMap, PathogenSymptomMapApiService> {
		/** The Constant API_URL. */
		public static final String API_URL = PathogenController.API_URL + "/symptom";
	}

	@RestController("pathogenGeographyMapApi2")
	@RequestMapping(PathogenController.PathogenGeographyMapController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "PlantHealth")
	public static class PathogenGeographyMapController extends CRUDController<PathogenGeographyMapDTO, PathogenGeographyMap, PathogenGeographyMapApiService> {
		/** The Constant API_URL. */
		public static final String API_URL = PathogenController.API_URL + "/geography";
	}

	@RestController("orderRequestItemPathogenMapApi2")
	@RequestMapping(PathogenController.OrderRequestItemPathogenMapController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "PlantHealth")
	public static class OrderRequestItemPathogenMapController extends CRUDController<OrderRequestItemPathogenMapDTO, OrderRequestItemPathogenMap, OrderRequestItemPathogenMapApiService> {
		/** The Constant API_URL. */
		public static final String API_URL = PathogenController.API_URL + "/order-request-item";
	}

	@RestController("pathogenTaxonomySpeciesMapApi2")
	@RequestMapping(PathogenController.PathogenTaxonomySpeciesMapController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "PlantHealth")
	public static class PathogenTaxonomySpeciesMapController extends CRUDController<PathogenTaxonomySpeciesMapDTO, PathogenTaxonomySpeciesMap, PathogenTaxonomySpeciesMapApiService> {
		/** The Constant API_URL. */
		public static final String API_URL = PathogenController.API_URL + "/species";
	}
	
}