InventoryController.java

/*
 * Copyright 2024 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 lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.commons.lang3.ArrayUtils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.ApiBaseController;
import org.gringlobal.api.FilteredPage;
import org.gringlobal.api.Pagination;
import org.gringlobal.api.model.AccessionInvAttachDTO;
import org.gringlobal.api.model.AuditLogDTO;
import org.gringlobal.api.model.InventoryActionDTO;
import org.gringlobal.api.model.InventoryActionRequestDTO;
import org.gringlobal.api.model.InventoryDTO;
import org.gringlobal.api.model.InventoryDetailsDTO;
import org.gringlobal.api.model.InventoryQualityStatusAttachDTO;
import org.gringlobal.api.model.InventoryQualityStatusDTO;
import org.gringlobal.api.model.OrderRequestDTO;
import org.gringlobal.api.v2.ActionController;
import org.gringlobal.api.v2.CRUDController;
import org.gringlobal.api.v2.FilteredCRUDController;
import org.gringlobal.api.v2.facade.InventoryActionApiService;
import org.gringlobal.api.v2.facade.InventoryApiService;
import org.gringlobal.api.v2.facade.InventoryAttachmentApiService;
import org.gringlobal.api.v2.facade.InventoryQualityStatusApiService;
import org.gringlobal.api.v2.facade.InventoryApiService.InventoryHarvestDTO;
import org.gringlobal.api.v2.facade.InventoryApiService.InventoryHarvestRequest;
import org.gringlobal.api.v2.facade.InventoryQualityStatusAttachmentApiService;
import org.gringlobal.api.v2.facade.InventoryQualityStatusAttachmentApiService.InventoryQualityStatusAttachRequestDTO;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.AccessionInvAttach;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.InventoryAction;
import org.gringlobal.model.InventoryQualityStatus;
import org.gringlobal.model.QInventory;
import org.gringlobal.model.QInventoryAction;
import org.gringlobal.model.community.InventoryQualityStatusAttach;
import org.gringlobal.service.CRUDService;
import org.gringlobal.service.InventoryActionService;
import org.gringlobal.service.filter.AccessionFilter;
import org.gringlobal.service.filter.AccessionInvAttachFilter;
import org.gringlobal.service.filter.InventoryActionFilter;
import org.gringlobal.service.filter.InventoryFilter;
import org.gringlobal.service.filter.InventoryQualityStatusFilter;
import org.gringlobal.service.glis.impl.GlisDOIRegistrationManager;
import org.gringlobal.spring.CSVMessageConverter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;

import com.querydsl.core.types.OrderSpecifier;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;

@RestController("inventoryApi2")
@RequestMapping(InventoryController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Inventory")
@Slf4j
public class InventoryController extends FilteredCRUDController<InventoryDTO, Inventory, InventoryApiService, InventoryFilter> {

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

	@Override
	protected OrderSpecifier<?>[] defaultSort() {
		return new OrderSpecifier[] { QInventory.inventory.id.desc() };
	}

	@Autowired
	private InventoryAttachmentApiService inventoryAttachmentApiService;

	@Autowired
	private GlisDOIRegistrationManager glisDOIRegistrationManager;

	@RestController("inventoryActionApi2")
	@RequestMapping(InventoryActionController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Inventory")
	public static class InventoryActionController extends ActionController<InventoryActionDTO, InventoryAction, InventoryActionFilter, InventoryActionRequestDTO, InventoryActionService.InventoryActionScheduleFilter, InventoryActionApiService> {
		public static final String API_URL = InventoryController.API_URL;

		@Override
		protected OrderSpecifier<?>[] defaultSort() {
			return new OrderSpecifier[] { QInventoryAction.inventoryAction.id.desc() };
		}
	}

	@RestController("accessionInvAttachApi2")
	@RequestMapping(AccessionInvAttachController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Inventory")
	public static class AccessionInvAttachController extends FilteredCRUDController<AccessionInvAttachDTO, AccessionInvAttach, InventoryAttachmentApiService, AccessionInvAttachFilter> {
		/** The Constant API_URL. */
		public static final String API_URL = InventoryController.API_URL + "/attach/meta";

		@Override
		@Operation(operationId = "createAccessionInvAttach", description = "Create AccessionInvAttach", summary = "Create")
		public AccessionInvAttachDTO create(@RequestBody AccessionInvAttachDTO entity) {
			return super.create(entity);
		}

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

		@Override
		@GetMapping(value = ENDPOINT_ID, produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(operationId = "getAccessionInvAttach", description = "Get record by ID", summary = "Get")
		public AccessionInvAttachDTO get(@PathVariable long id) {
			return super.get(id);
		}

		@Override
		@DeleteMapping(value = ENDPOINT_ID, produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(operationId = "deleteAccessionInvAttach", description = "Delete existing record by ID", summary = "Delete")
		public AccessionInvAttachDTO remove(@PathVariable long id) {
			return super.remove(id);
		}
	}

	@PostMapping(value = "/attach/{inventoryId}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "uploadFile", description = "Attach accession file", summary = "Attach file")
	public AccessionInvAttachDTO uploadFile(@PathVariable(name = "inventoryId") final Long inventoryId, @RequestPart(name = "file") final MultipartFile file,
		@RequestPart(name = "metadata") final InventoryAttachmentApiService.InventoryAttachmentRequestDTO metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

		return inventoryAttachmentApiService.uploadFile(inventoryId, file, metadata);
	}

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

	@PostMapping(value = "/attach/{attachId}/share", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "shareAttachment", description = "Share attachment to inventories", summary = "Share attachment file")
	public void shareAttachment(@PathVariable(name = "attachId") final Long attachId, @RequestBody @NotEmpty List<Long> inventoryIds) {
		serviceFacade.shareAttachment(attachId, inventoryIds);
	}

	@RestController("inventoryQualityStatusApi2")
	@RequestMapping(InventoryQualityStatusController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Inventory")
	public static class InventoryQualityStatusController extends FilteredCRUDController<InventoryQualityStatusDTO, InventoryQualityStatus, InventoryQualityStatusApiService, InventoryQualityStatusFilter> {
		public static final String API_URL = InventoryController.API_URL + "/quality";

		@Autowired
		private InventoryQualityStatusAttachmentApiService attachFacade;

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

			return attachFacade.uploadFile(statusId, file, metadata);
		}

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

		@RestController("inventoryQualityStatusAttachApi2")
		@RequestMapping(InventoryQualityStatusController.InventoryQualityStatusAttachController.API_URL)
		@PreAuthorize("isAuthenticated()")
		@Tag(name = "PlantHealth")
		public static class InventoryQualityStatusAttachController extends CRUDController<InventoryQualityStatusAttachDTO, InventoryQualityStatusAttach, InventoryQualityStatusAttachmentApiService> {
			/** The Constant API_URL. */
			public static final String API_URL = InventoryQualityStatusController.API_URL + "/attach/meta";

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

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

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

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

	@Override
	@PostMapping(value = FilteredCRUDController.ENDPOINT_LIST, produces = { MediaType.APPLICATION_JSON_VALUE, CSVMessageConverter.TEXT_CSV_VALUE })
	public FilteredPage<InventoryDTO, InventoryFilter> list(@ParameterObject final Pagination page, @RequestBody InventoryFilter filter) throws SearchException, IOException {
		return super.list(page, filter);
	}

	@Override
	public FilteredPage<InventoryDTO, InventoryFilter> filter(@RequestParam(name = "f", required = false) String filterCode, @ParameterObject final Pagination page,
		@RequestBody(required = false) InventoryFilter filter) throws IOException, SearchException {
		return super.filter(filterCode, page, filter);
	}

	/**
	 * Print the same PDF label for specified <code>ids</code>.
	 */
	@PostMapping(value = "/generate-labels-pdf")
	@Operation(method = "generateLabelsPdf", summary = "Generate PDF labels for selected IDs", description = "Use the same label configuration for each record.")
	@ApiResponses(value = @ApiResponse(responseCode = "200", content = @Content(mediaType = MediaType.APPLICATION_PDF_VALUE)))
	public ResponseEntity<StreamingResponseBody> generateLabelsPdf(
		@ParameterObject CRUDService.LabelConfig labelConfig,
		@RequestBody List<Long> ids,
		HttpServletResponse response
	) throws Exception {
		return serviceFacade.generateLabelsPDF(labelConfig, ids, response);
	}

	/**
	 * Print the same label for specified <code>ids</code>.
	 */
	@PostMapping(value = "/generate-labels")
	@Operation(method = "generateLabels", summary = "Generate labels for selected IDs", description = "Use the same label configuration for each record.")
	@ApiResponses(value = @ApiResponse(responseCode = "200", content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE, schema = @Schema(type = "string"))))
	public ResponseEntity<StreamingResponseBody> generateLabels(
		@ParameterObject CRUDService.LabelConfig labelConfig,
		@RequestBody List<Long> ids
	) {
		return serviceFacade.generateLabels(labelConfig, ids);
	}

	/**
	 * Print the different labels for <code>ids</code>.
	 */
	@PostMapping(value = "/generate-labels-custom")
	@Operation(method = "generateLabelCustom", summary = "Generate labels for selected IDs", description = "Use different label configuration for each record. Map keys are record IDs.")
	@ApiResponses(value = @ApiResponse(responseCode = "200", content = @Content(mediaType = MediaType.TEXT_PLAIN_VALUE, schema = @Schema(type = "string"))))
	public ResponseEntity<StreamingResponseBody> generateLabelsAdvanced(@RequestBody LinkedHashMap<Long, CRUDService.LabelConfig> labelConfig) {
		return serviceFacade.generateLabels(labelConfig);
	}

	/**
	 * Retrieve inventory details by id
	 *
	 * @param id the id
	 * @return the inventory details
	 */
	@GetMapping(value = "/details/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "getDetails", description = "Retrieve inventory details by ID", summary = "Details")
	public InventoryDetailsDTO details(@PathVariable("id") final long id) {
		return serviceFacade.getInventoryDetails(id);
	}

	/**
	 * Retrieve inventory details by barcode
	 *
	 * @param barcode the barcode
	 * @return the inventory details
	 */
	@GetMapping(value = "/details", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "getDetails", description = "Retrieve inventory details by barcode", summary = "Details")
	public InventoryDetailsDTO details(@RequestParam("barcode") final String barcode) {
		return serviceFacade.getInventoryDetails(barcode);
	}

	@PostMapping("/quantity")
	@Operation(summary = "Set inventory quantity on hand", description = "Update current quantity on hand.")
	public InventoryDTO setInventoryQuantity(@RequestBody @Valid InventoryApiService.InventoryQuantityRequestDTO inventoryQuantity) {
		return serviceFacade.setInventoryQuantity(inventoryQuantity);
	}

	@PostMapping("/discard-material")
	@Operation(summary = "Update inventory quantity on hand", description = "Update current quantity on hand.")
	public ResponseEntity<HttpStatus> discardMaterial(@RequestBody final Map<Long, Integer> discardQuantities) {
		serviceFacade.discardMaterial(discardQuantities);
		return ResponseEntity.ok().build();
	}

	@PostMapping(value = "/assign-location", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "assignLocation", description = "Assign location to inventories", summary = "Assign location")
	public List<InventoryDTO> assignLocation(@RequestBody InventoryApiService.AssignLocationRequestDTO assignLocationRequest) {
		return serviceFacade.assignLocation(assignLocationRequest);
	}

	@PostMapping(value = "/assign-locations", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "assignLocations", description = "Assign multiple location to inventories", summary = "Assign locations")
	public List<InventoryDTO> assignLocations(@RequestBody List<InventoryApiService.AssignLocationRequestDTO> assignLocationRequests) {
		return serviceFacade.assignLocations(assignLocationRequests);
	}

	@PostMapping("/site/compare")
	@Operation(summary = "Compare sites", description = "Compare the number of inventories across sites")
	public Page<InventoryApiService.ComparedSitesResponseDTO> compareSites
		(@ParameterObject final Pagination page, @RequestBody @Valid InventoryApiService.CompareSitesRequestDTO request) {

		return serviceFacade.compareSites(request, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	/**
	 * Generate and assign the barcode for Inventory based on a template
	 *
	 * @param inventoryId the ID of inventory
	 * @return the barcode
	 */
	@PostMapping(value = "/{id}/barcode", produces = { MediaType.TEXT_PLAIN_VALUE })
	public String assignBarcode(@PathVariable(value = "id") final Long inventoryId) {
		return serviceFacade.assignBarcode(inventoryId);
	}

	/**
	 * Retrieve a list of audit logs for the specified inventory
	 *
	 * @param inventoryId the inventoryId
	 * @param page the page request
	 * @return the list of all log entries
	 */
	@GetMapping("/auditlog/{id}")
	public Page<AuditLogDTO> inventoryAuditLogs(@PathVariable(value = "id") final Long inventoryId, @ParameterObject final Pagination page) {
		return serviceFacade.listAuditLogs(inventoryId, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	@PostMapping(value = "/split", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "split", description = "Split inventory", summary = "split")
	public List<InventoryDTO> splitInventory(@RequestBody @Valid InventoryApiService.SplitInventoryRequestDTO request) {
		return serviceFacade.splitInventory(request);
	}

	@PostMapping(value = "/overview/{groupBy:.+}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "inventoryOverview", description = "Get inventory statistics", summary = "Overview")
	public Map<?, ?> inventoryOverview(@PathVariable(name = "groupBy", required = true) String groupBy, @RequestBody InventoryFilter filter) {
		return serviceFacade.inventoryOverview(groupBy, filter);
	}

	@PostMapping(value = "/aggregate-quantity", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "aggregateQuantity", description = "Returns the aggregate of inventory quantities.", summary = "Sum inventory quantities.")
	public FilteredPage<InventoryApiService.AggregatedInventoryQuantityDTO, InventoryFilter> aggregateQuantity(@ParameterObject final Pagination page, @RequestBody InventoryFilter filter) throws IOException {
		InventoryFilter normalizedFilter = shortFilterService.normalizeFilter(filter, filterType());
		Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, QInventory.inventory.formTypeCode.asc()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
		return new FilteredPage<>(normalizedFilter, serviceFacade.aggregateQuantity(filter, pageable));
	}

	@PostMapping("/multiplication/order")
	@Operation(summary = "Create multiplication OrderRequest", description = "Create new OrderRequest for multiplication")
	public OrderRequestDTO multiplicationOrder(@RequestBody @Valid InventoryApiService.MultiplicationOrderRequestDTO request) {
		return serviceFacade.multiplicationOrder(request);
	}

	@Override
	@DeleteMapping(value = ENDPOINT_ID, produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "remove", description = "Delete existing record by ID and clear all related data.", summary = "Delete inventory and related data.")
	public InventoryDTO remove(@PathVariable("id") final long id) {
		return super.remove(id);
	}

	@PostMapping(value = "/harvest/list", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "listInventoryHarvest", description = "Get inventories with HARVEST action, and the resulting harvested inventory.", summary = "Get planted inventories with harvest data.")
	public FilteredPage<InventoryHarvestDTO, InventoryActionFilter> listInventoryHarvest(
		@RequestBody(required = false) InventoryActionFilter filter,
		@ParameterObject final Pagination page
	) {
		return serviceFacade.listInventoryHarvest(filter, page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE));
	}

	@PostMapping(value = "/harvest/create", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "createHarvestedInventory", description = "Create a new harvested inventory based on the planted inventory.", summary = "Create a new harvested inventory.")
	public InventoryHarvestDTO createHarvestedInventory(@RequestBody @Valid InventoryHarvestRequest request) {
		return serviceFacade.createHarvestedInventory(request);
	}

	/**
	 * Update GLIS DOI Registration Service for one inventory
	 *
	 * @param id Inventory ID
	 * @return DOI Registration status
	 * @throws Throwable the problem error
	 */
	@PostMapping(value = ENDPOINT_ID + "/assign-doi")
	public GlisDOIRegistrationManager.GlisDoiResponse assignDoiToInventory(@PathVariable(name = "id") final long id) throws Throwable {
		var inventory = serviceFacade.get(id);
		return glisDOIRegistrationManager.updateInventoryDoiRegistration(null, new InventoryFilter().id(Set.of(inventory.getId()))).get(0);
	}

	/**
	 * Update GLIS DOI Registration Service for inventories by filter with propress data
	 *
	 * @param filterCode Filter code
	 * @param filter InventoryFilter
	 * @return DOI Registration progress data
	 * @throws Exception the problem error
	 */
	@PostMapping(value = "/assign-doi")
	public GlisDOIRegistrationManager.AssignDoiState assignDoiToInventories(
		@RequestParam(name = "f", required = false) String filterCode,
		@RequestBody(required = false) final InventoryFilter filter
	) throws Exception {
		return glisDOIRegistrationManager.updateInventoryDoiRegistrationWithProgress(filterCode, filter);
	}
	
	@PostMapping("/assign-doi/cancel")
	public ResponseEntity<HttpStatus> cancelDoiAssignment(@RequestParam(name = "f", required = false) String filterCode) {
		return glisDOIRegistrationManager.cancelDoiAssignment(filterCode);
	}

	@PostMapping("/assign-doi/stop")
	public void stopDoiAssignmentProcess() {
		glisDOIRegistrationManager.stopDoiAssignmentProcess();
	}
}