OrderRequestController.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 java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.ArrayUtils;
import org.genesys.filerepository.InvalidRepositoryFileDataException;
import org.genesys.filerepository.InvalidRepositoryPathException;
import org.gringlobal.api.v2.ActionController;
import org.gringlobal.api.model.InventoryMaintenancePolicyInfo;
import org.gringlobal.api.model.OrderRequestActionDTO;
import org.gringlobal.api.model.OrderRequestActionRequestDTO;
import org.gringlobal.api.model.OrderRequestAttachDTO;
import org.gringlobal.api.model.OrderRequestDTO;
import org.gringlobal.api.model.OrderRequestDetailsDTO;
import org.gringlobal.api.model.OrderRequestItemActionDTO;
import org.gringlobal.api.model.OrderRequestItemActionRequestDTO;
import org.gringlobal.api.model.OrderRequestItemDTO;
import org.gringlobal.api.v1.ApiBaseController;
import org.gringlobal.api.v2.CRUDController;
import org.gringlobal.api.v2.FilteredCRUDController;
import org.gringlobal.api.v2.facade.OrderRequestActionApiService;
import org.gringlobal.api.v2.facade.OrderRequestApiService;
import org.gringlobal.api.v2.facade.OrderRequestAttachApiService;
import org.gringlobal.api.v2.facade.OrderRequestItemActionApiService;
import org.gringlobal.api.v2.facade.OrderRequestItemApiService;
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.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.CRUDService;
import org.gringlobal.service.OrderRequestActionService.OrderRequestActionScheduleFilter;
import org.gringlobal.service.OrderRequestAttachmentService;
import org.gringlobal.service.OrderRequestItemActionService.OrderRequestItemActionScheduleFilter;
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.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.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.RequestPart;
import org.springframework.web.bind.annotation.RestController;
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 org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.method.annotation.StreamingResponseBody;
@RestController("orderRequestApi2")
@RequestMapping(OrderRequestController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Order")
public class OrderRequestController extends FilteredCRUDController<OrderRequestDTO, OrderRequest, OrderRequestApiService, OrderRequestFilter> {
/** The Constant API_URL. */
public static final String API_URL = ApiBaseController.APIv2_BASE + "/order";
@Override
protected OrderSpecifier<?>[] defaultSort() {
return new OrderSpecifier[] { QOrderRequest.orderRequest.modifiedDate.desc() };
}
@RestController("orderRequestActionApi2")
@RequestMapping(OrderRequestActionController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Order")
public static class OrderRequestActionController extends ActionController<OrderRequestActionDTO, OrderRequestAction, OrderRequestActionFilter, OrderRequestActionRequestDTO, OrderRequestActionScheduleFilter, OrderRequestActionApiService> {
public static final String API_URL = OrderRequestController.API_URL;
@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<OrderRequestActionDTO> 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 actionServiceFacade.listByOrderRequest(orderId, pageable);
}
}
@RestController("orderRequestAttachApi2")
@RequestMapping(OrderRequestAttachController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Order")
public static class OrderRequestAttachController extends CRUDController<OrderRequestAttachDTO, OrderRequestAttach, OrderRequestAttachApiService> {
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 OrderRequestAttachDTO 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 OrderRequestAttachDTO 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 OrderRequestAttachDTO create(@RequestBody final OrderRequestAttachDTO 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 OrderRequestAttachDTO update(@RequestBody final OrderRequestAttachDTO entity) {
return super.update(entity);
}
@PostMapping(value = "/{orderId}", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(operationId = "uploadFile", description = "Attach OrderRequest file", summary = "Attach file")
public OrderRequestAttachDTO 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 serviceFacade.get(serviceFacade.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 OrderRequestAttachDTO removeFile(@PathVariable(name = "orderId") final Long inventoryId, @PathVariable(name = "attachmentId") final Long attachmentId) {
return serviceFacade.removeFile(orderRequestService.get(inventoryId), attachmentId);
}
}
@RestController("orderRequestItemActionApi2")
@RequestMapping(OrderRequestItemActionController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "Order")
@Validated
public static class OrderRequestItemActionController extends ActionController<OrderRequestItemActionDTO, OrderRequestItemAction, OrderRequestItemActionFilter, OrderRequestItemActionRequestDTO, OrderRequestItemActionScheduleFilter, OrderRequestItemActionApiService> {
public static final String API_URL = OrderRequestController.API_URL + "/items";
@Override
protected OrderSpecifier<?>[] defaultSort() {
return new OrderSpecifier[] { QOrderRequestItemAction.orderRequestItemAction.createdDate.desc() };
}
@Override
@Operation(hidden = true)
public List<OrderRequestItemActionDTO> startActions(OrderRequestItemActionRequestDTO request) {
return actionServiceFacade.startAction(request);
}
@PostMapping(value = "/{id}/action/start", produces = { MediaType.APPLICATION_JSON_VALUE })
@Operation(description = "Start item request actions", summary = "Start item actions")
public List<OrderRequestItemActionDTO> startItemActions(@PathVariable("id") final Long orderId, @RequestBody @CodeValueField(CommunityCodeValues.ORDER_REQUEST_ITEM_ACTION) String actionCode) {
return actionServiceFacade.startActions(orderId, 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<OrderRequestItemActionDTO> 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 actionServiceFacade.listByOrderRequest(orderId, pageable);
}
}
@RestController("orderRequestItemsApi2")
@RequestMapping(OrderRequestItemsController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = "OrderRequestItem")
@Validated
public static class OrderRequestItemsController extends FilteredCRUDController<OrderRequestItemDTO, OrderRequestItem, OrderRequestItemApiService, 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<OrderRequestItemDTO, 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 serviceFacade.map(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<OrderRequestItemDTO> addInventories(@PathVariable("id") final Long orderId, @RequestBody final List<OrderRequestedInventory> inventories) {
final OrderRequest orderRequest = orderRequestService.get(orderId);
return serviceFacade.get(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<OrderRequestItemDTO> 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 serviceFacade.get(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<OrderRequestItemDTO> removeOrderItems(@PathVariable("id") final Long orderId, @RequestBody final Set<Long> itemIds) {
final OrderRequest orderRequest = orderRequestService.get(orderId);
return serviceFacade.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<OrderRequestItemDTO> updateItems(@RequestBody final Set<OrderRequestItemDTO> items) {
return serviceFacade.get(serviceFacade.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);
}
/**
* 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);
}
}
@PostMapping(value = "/{id}/renumber")
@Operation(operationId = "renumber", description = "Renumber OrderRequest items", summary = "Renumber items")
public ResponseEntity<HttpStatus> renumber(@PathVariable(required = true) final long id) {
serviceFacade.renumberOrderRequestItems(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 OrderRequestDetailsDTO getDetails(@PathVariable(required = true) final long id) {
return serviceFacade.getOrderRequestDetails(id);
}
@PostMapping(value = "/{id}/generate-inventories")
@Operation(operationId = "generate-inventories", description = "Generate inventories for each item in the selected OrderRequest", summary = "Generate withdrawnInventories")
public OrderRequestDTO generateInventories(@PathVariable final long id, @RequestBody(required = false) InventoryMaintenancePolicyInfo policy) {
return serviceFacade.get(serviceFacade.generateInventories(id, policy));
}
@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 serviceFacade.reportSMTA(id);
}
/**
* 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);
}
}