OrderRequestController.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 java.io.OutputStream;
import java.util.List;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.v1.ActionController;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v1.CRUDController;
import org.gringlobal.api.v1.FilteredCRUDController;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.custom.validation.javax.CodeValueField;
import org.gringlobal.model.InventoryMaintenancePolicy;
import org.gringlobal.model.OrderRequest;
import org.gringlobal.model.OrderRequestAction;
import org.gringlobal.model.OrderRequestAttach;
import org.gringlobal.model.OrderRequestItem;
import org.gringlobal.model.OrderRequestItemAction;
import org.gringlobal.model.QOrderRequest;
import org.gringlobal.model.QOrderRequestAction;
import org.gringlobal.model.QOrderRequestItem;
import org.gringlobal.model.QOrderRequestItemAction;
import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.service.OrderRequestActionService;
import org.gringlobal.service.OrderRequestActionService.OrderRequestActionRequest;
import org.gringlobal.service.OrderRequestActionService.OrderRequestActionScheduleFilter;
import org.gringlobal.service.OrderRequestAttachmentService;
import org.gringlobal.service.OrderRequestItemActionService;
import org.gringlobal.service.OrderRequestItemActionService.OrderRequestItemActionRequest;
import org.gringlobal.service.OrderRequestItemActionService.OrderRequestItemActionScheduleFilter;
import org.gringlobal.service.OrderRequestItemService;
import org.gringlobal.service.OrderRequestService;
import org.gringlobal.service.OrderRequestService.OrderRequestedInventory;
import org.gringlobal.service.filter.OrderRequestActionFilter;
import org.gringlobal.service.filter.OrderRequestFilter;
import org.gringlobal.service.filter.OrderRequestItemActionFilter;
import org.gringlobal.service.filter.OrderRequestItemFilter;
import org.gringlobal.service.glis.impl.GlisSMTAReportingManager;
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.validation.annotation.Validated;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;

import com.querydsl.core.types.OrderSpecifier;

import io.swagger.v3.oas.annotations.Operation;
import org.springdoc.api.annotations.ParameterObject;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController("orderRequestApi1")
@RequestMapping(OrderRequestController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Order")
@Slf4j
public class OrderRequestController extends FilteredCRUDController<OrderRequest, OrderRequestService, OrderRequestFilter> {

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

	@Override
	protected OrderSpecifier<?>[] defaultSort() {
		return new OrderSpecifier[] { QOrderRequest.orderRequest.modifiedDate.desc() };
	}

	@Override
	protected Class<OrderRequestFilter> filterType() {
		return OrderRequestFilter.class;
	}

	@RestController("orderRequestActionApi1")
	@RequestMapping(OrderRequestActionController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Order")
	public static class OrderRequestActionController extends ActionController<OrderRequestAction, OrderRequestActionFilter, OrderRequestActionRequest, OrderRequestActionScheduleFilter, OrderRequestActionService> {
		public static final String API_URL = OrderRequestController.API_URL;

		@Autowired
		private OrderRequestService orderRequestService;

		@Override
		protected Class<OrderRequestActionFilter> filterType() {
			return OrderRequestActionFilter.class;
		}

		@Override
		protected OrderSpecifier<?>[] defaultSort() {
			return new OrderSpecifier[] { QOrderRequestAction.orderRequestAction.createdDate.desc() };
		}

		@PostMapping(value = "/{id}/action/list", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(description = "List actions by order request", summary = "List actions by order")
		public Page<OrderRequestAction> listByOrderRequest(@PathVariable("id") final Long orderId, @ParameterObject final Pagination page) {
			Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
			return actionService.listByOrderRequest(orderRequestService.get(orderId), pageable);
		}
	}

	@RestController("orderRequestAttachApi1")
	@RequestMapping(OrderRequestAttachController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Order")
	public static class OrderRequestAttachController extends CRUDController<OrderRequestAttach, OrderRequestAttachmentService> {
		public static final String API_URL = OrderRequestController.API_URL + "/attach";
		private static final String ENDPOINT_META = "/meta";

		@Autowired
		private OrderRequestService orderRequestService;

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

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

		@Override
		@PostMapping(value = ENDPOINT_META, produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(operationId = "create", description = "Create a record", summary = "Add")
		public OrderRequestAttach create(@RequestBody final OrderRequestAttach entity) {
			return super.create(entity);
		}

		@Override
		@PutMapping(value = ENDPOINT_META, produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(operationId = "update", description = "Update an existing record", summary = "Update")
		public OrderRequestAttach update(@RequestBody final OrderRequestAttach entity) {
			return super.update(entity);
		}

		@PostMapping(value = "/{orderId}", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(operationId = "uploadFile", description = "Attach OrderRequest file", summary = "Attach file")
		public OrderRequestAttach uploadFile(@PathVariable(name = "orderId") final Long inventoryId, @RequestPart(name = "file") final MultipartFile file,
				@RequestPart(name = "metadata") final OrderRequestAttachmentService.OrderRequestAttachmentRequest metadata) throws InvalidRepositoryPathException, InvalidRepositoryFileDataException, IOException {

			return crudService.uploadFile(orderRequestService.get(inventoryId), file, metadata);
		}

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

	@RestController("orderRequestItemActionApi1")
	@RequestMapping(OrderRequestItemActionController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "Order")
	@Validated
	public static class OrderRequestItemActionController extends ActionController<OrderRequestItemAction, OrderRequestItemActionFilter, OrderRequestItemActionRequest, OrderRequestItemActionScheduleFilter, OrderRequestItemActionService> {
		public static final String API_URL = OrderRequestController.API_URL + "/items";

		@Autowired
		private OrderRequestService orderRequestService;

		@Override
		protected Class<OrderRequestItemActionFilter> filterType() {
			return OrderRequestItemActionFilter.class;
		}

		@Override
		protected OrderSpecifier<?>[] defaultSort() {
			return new OrderSpecifier[] { QOrderRequestItemAction.orderRequestItemAction.createdDate.desc() };
		}

		@Override
		@Operation(hidden = true)
		public List<OrderRequestItemAction> startActions(OrderRequestItemActionRequest request) {
			return actionService.startAction(request);
		}

		@PostMapping(value = "/{id}/action/start", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(description = "Start item request actions", summary = "Start item actions")
		public List<OrderRequestItemAction> startItemActions(@PathVariable("id") final Long orderId, @RequestBody @CodeValueField(CommunityCodeValues.ORDER_REQUEST_ITEM_ACTION) String actionCode) {
			final OrderRequest orderRequest = orderRequestService.get(orderId);
			return actionService.startActions(orderRequest, actionCode);
		}

		@PostMapping(value = "/{id}/action/list", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(description = "List actions by order request", summary = "List actions by order")
		public Page<OrderRequestItemAction> listByOrderRequest(@PathVariable("id") final Long orderId, @ParameterObject final Pagination page) {
			Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
			return actionService.listByOrderRequest(orderRequestService.get(orderId), pageable);
		}
	}

	@RestController("orderRequestItemsApi1")
	@RequestMapping(OrderRequestItemsController.API_URL)
	@PreAuthorize("isAuthenticated()")
	@Tag(name = "OrderRequestItem")
	@Validated
	public static class OrderRequestItemsController extends FilteredCRUDController<OrderRequestItem, OrderRequestItemService, OrderRequestItemFilter> {
		/** The Constant API_URL. */
		public static final String API_URL = OrderRequestController.API_URL + "/items";

		@Autowired
		private OrderRequestService orderRequestService;

		private OrderSpecifier<?>[] defaultItemSort() {
			return new OrderSpecifier[] { QOrderRequestItem.orderRequestItem.sequenceNumber.asc().nullsLast() };
		}

		@PostMapping(value = "/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(description = "Fetch items of an request", summary = "List or filter order items")
		public FilteredPage<OrderRequestItem, OrderRequestItemFilter> filterItems(@PathVariable("id") final Long orderId, @ParameterObject final Pagination page, @RequestBody(required = true) final OrderRequestItemFilter filter) throws SearchException, IOException {
			final OrderRequest orderRequest = orderRequestService.get(orderId);
			OrderRequestItemFilter normalizedFilter = shortFilterService.normalizeFilter(filter, OrderRequestItemFilter.class);
			Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, defaultItemSort()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
			return new FilteredPage<>(normalizedFilter, orderRequestService.filterItems(orderRequest, filter, pageable));
		}

		/**
		 * Adding new inventories the order request.
		 *
		 * @param orderId the order request ID
		 * @return created items
		 */
		@PostMapping(value = "/{id}/add-inventory", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(description = "Create and add items to order request", summary = "Add items")
		public List<OrderRequestItem> addInventories(@PathVariable("id") final Long orderId, @RequestBody final List<OrderRequestedInventory> inventories) {
			final OrderRequest orderRequest = orderRequestService.get(orderId);
			return orderRequestService.addInventories(orderRequest, inventories);
		}

		/**
		 * Update item status.
		 *
		 * @param orderId the order id
		 * @param newStatus the new status
		 * @param itemIds the item ids
		 * @return the list
		 */
		@PostMapping(value = "/{id}/status/{newStatus}")
		@Operation(operationId = "updateItemStatus", summary = "Change item status", description = "Update status of selected request items")
		public List<OrderRequestItem> updateItemStatus(@PathVariable("id") final Long orderId, @PathVariable("newStatus") @CodeValueField(CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS) String newStatus,
				@RequestBody Set<Long> itemIds) {

			final OrderRequest orderRequest = orderRequestService.get(orderId);
			return orderRequestService.updateItemStatus(orderRequest, newStatus, itemIds);
		}

		/**
		 * Removing items from the order request.
		 *
		 * @param orderId the order request ID
		 * @param itemIds item Ids to be deleted from the order
		 * @return list of removed items
		 */
		@PostMapping(value = "/{id}/remove", produces = { MediaType.APPLICATION_JSON_VALUE })
		@Operation(description = "Remove items from order request", summary = "Remove items")
		public List<OrderRequestItem> removeOrderItems(@PathVariable("id") final Long orderId, @RequestBody final Set<Long> itemIds) {
			final OrderRequest orderRequest = orderRequestService.get(orderId);
			return orderRequestService.removeOrderItems(orderRequest, itemIds);
		}

		/**
		 * Updating items
		 *
		 * @param items the list of items to update
		 * @return updated items
		 */
		@PutMapping(value = "/update-list")
		@Operation(operationId = "updateItems", summary = "Update items", description = "Update a list of request items")
		public List<OrderRequestItem> updateItems(@RequestBody final Set<OrderRequestItem> items) {
			return crudService.updateAll(items);
		}

		/**
		 * Assign default inventories to items
		 *
		 * @param orderId the order request ID
		 * @param itemIds item Ids
		 */
		@PostMapping(value = "/{id}/use-default")
		@Operation(operationId = "useDefaultInventories", summary = "Set default inventories", description = "Set default inventories to items")
		public List<OrderRequestItem> setDefaultInventories(@PathVariable("id") final Long orderId, @RequestBody final Set<Long> itemIds) {
			final OrderRequest orderRequest = orderRequestService.get(orderId);
			return orderRequestService.setDefaultInventories(orderRequest, itemIds);
		}
	}

	@PostMapping(value = "/{id}/renumber")
	@Operation(operationId = "renumber", description = "Renumber OrderRequest items", summary = "Renumber items")
	public ResponseEntity<HttpStatus> renumber(@PathVariable(required = true) final long id) {
		crudService.renumberOrderRequestItems(crudService.get(id));
		return ResponseEntity.ok().build();
	}

	/**
	 * Retrieve crop details by id
	 *
	 * @param id the id
	 * @return the crop details
	 */
	@GetMapping(value = "/details/{id}", produces = { MediaType.APPLICATION_JSON_VALUE })
	@Operation(operationId = "orderRequestDetails", description = "Retrieve orderRequest details by ID", summary = "Details")
	public OrderRequestService.OrderRequestDetails getDetails(@PathVariable(required = true) final long id) {
		return crudService.getOrderRequestDetails(crudService.get(id));
	}

	@PostMapping(value = "/{id}/generate-inventories")
	@Operation(operationId = "generate-inventories", description = "Generate inventories for each item in the selected OrderRequest", summary = "Generate withdrawnInventories")
	public OrderRequest generateInventories(@PathVariable final long id, @RequestBody(required = false) InventoryMaintenancePolicy policy) {
		return crudService.generateInventories(crudService.get(id), policy);
	}

	@Override
	public OrderRequest create(@RequestBody OrderRequest entity) {
		return super.create(entity);
	}

	@Override
	public OrderRequest update(@RequestBody OrderRequest entity) {
		return super.update(entity);
	}

	@Override
	public OrderRequest remove(@PathVariable long id) {
		return super.remove(id);
	}

	@Override
	public FilteredPage<OrderRequest, OrderRequestFilter> list(@ParameterObject final Pagination page, @RequestBody(required = false) OrderRequestFilter filter) throws SearchException, IOException {
		return super.list(page, filter);
	}

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

	@PostMapping(value = "/{id}/report-smta")
	@Operation(operationId = "report-SMTA", description = "Report on distribution of SMTA materials to the Easy-SMTA system", summary = "ReportSMTA")
	public GlisSMTAReportingManager.GlisSMTAReportResponse reportSMTA(@PathVariable final long id) throws Exception {
		return crudService.reportSMTA(crudService.get(id));
	}

	@RequestMapping(value = "/{id}/generate-smta", method = { RequestMethod.GET, RequestMethod.POST })
	@Operation(operationId = "generate-SMTA", description = "Download generated SMTA document in PDF format", summary = "DownloadGeneratedSMTADocument")
	public void generateSMTA(@PathVariable final long id, HttpServletResponse response) throws Exception {

		response.setContentType(MediaType.APPLICATION_PDF.toString());
		response.addHeader("Content-Disposition", String.format("attachment; filename=\"SMTA-OrderRequest#%d.pdf\"", id));

		final OutputStream outputStream = response.getOutputStream();
		try {
			crudService.generateSMTA(crudService.get(id), outputStream);
			response.flushBuffer();
		} catch (Throwable e) {
			response.resetBuffer();
			response.setContentType(MediaType.APPLICATION_JSON_VALUE);
			log.warn("Download was aborted: {}", e.getMessage());
			throw e;
		}
	}

}