InventoryViabilityController.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.Map;
import java.util.Set;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import org.apache.commons.lang3.ArrayUtils;
import org.gringlobal.api.model.InventoryActionDTO;
import org.gringlobal.api.model.InventoryViabilityActionDTO;
import org.gringlobal.api.model.InventoryViabilityActionRequestDTO;
import org.gringlobal.api.model.InventoryViabilityDTO;
import org.gringlobal.api.model.InventoryViabilityDataDTO;
import org.gringlobal.api.model.InventoryViabilityDetailsDTO;
import org.gringlobal.api.model.OrderRequestDTO;
import org.gringlobal.api.v1.FilteredPage;
import org.gringlobal.api.v1.Pagination;
import org.gringlobal.api.v2.ActionController;
import org.gringlobal.api.v2.FilteredCRUDController;
import org.gringlobal.api.v2.facade.InventoryActionApiService;
import org.gringlobal.api.v2.facade.InventoryViabilityActionApiService;
import org.gringlobal.api.v2.facade.InventoryViabilityApiService;
import org.gringlobal.api.v2.facade.InventoryViabilityDataApiService;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.AbstractAction.ActionState;
import org.gringlobal.model.Cooperator;
import org.gringlobal.model.InventoryViability;
import org.gringlobal.model.InventoryViabilityAction;
import org.gringlobal.model.InventoryViabilityData;
import org.gringlobal.model.QInventoryAction;
import org.gringlobal.model.QInventoryViability;
import org.gringlobal.model.QInventoryViabilityAction;
import org.gringlobal.model.QInventoryViabilityData;
import org.gringlobal.model.Site;
import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.service.CRUDService;
import org.gringlobal.service.InventoryViabilityActionService.InventoryViabilityActionScheduleFilter;
import org.gringlobal.service.filter.InventoryActionFilter;
import org.gringlobal.service.filter.InventoryFilter;
import org.gringlobal.service.filter.InventoryViabilityActionFilter;
import org.gringlobal.service.filter.InventoryViabilityDataFilter;
import org.gringlobal.service.filter.InventoryViabilityFilter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
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.RestController;
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 lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
@RestController("inventoryViabilityApi2")
@RequestMapping(InventoryViabilityController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = InventoryViabilityController.API_TAG)
public class InventoryViabilityController extends FilteredCRUDController<InventoryViabilityDTO, InventoryViability, InventoryViabilityApiService, InventoryViabilityFilter> {
/** The Constant API_URL. */
public static final String API_URL = InventoryController.API_URL + "/viability";
public static final String API_TAG = "InventoryViability";
@Autowired
private InventoryActionApiService inventoryActionService;
@NoArgsConstructor
@SuperBuilder
public static class ViabilityOrderRequest {
public Site site;
@NotNull
public Cooperator cooperator;
@NotNull
@Size(min = 1)
public Map<Long, Long> inventories;
}
@Override
protected OrderSpecifier<?>[] defaultSort() {
return new OrderSpecifier[] { QInventoryViability.inventoryViability.testedDate.desc() };
}
@PostMapping("/scheduled")
public FilteredPage<InventoryActionDTO, InventoryFilter> listScheduledInventories(@ParameterObject final Pagination page, @RequestBody(required = false) final InventoryFilter inventoryFilter) throws SearchException, IOException {
InventoryFilter normalizedFilter = shortFilterService.normalizeFilter(inventoryFilter, InventoryFilter.class);
InventoryActionFilter actionFilter = (InventoryActionFilter) new InventoryActionFilter()
.inventory(normalizedFilter)
.actionNameCode(Set.of(CommunityCodeValues.INVENTORY_ACTION_VABILITYTEST.value))
.states(Set.of(ActionState.PENDING, ActionState.SCHEDULED));
Pageable pageable = ArrayUtils.isEmpty(page.getS()) ? page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE, QInventoryAction.inventoryAction.notBeforeDate.asc().nullsFirst()) : page.toPageRequest(MAX_PAGE_SIZE, DEFAULT_PAGE_SIZE);
return new FilteredPage<>(normalizedFilter, inventoryActionService.listActions(actionFilter, pageable));
}
@PostMapping("/order")
public OrderRequestDTO orderViabilityTest(@RequestBody @Valid ViabilityOrderRequest viabilityOrderRequest) {
return serviceFacade.orderViabilityTest(viabilityOrderRequest);
}
/**
* Calculate results across all replicates.
*
* @param id the id of InventoryViability
* @return object of InventoryViability (not persisted) with calculated results
*/
@PostMapping("/{id:\\d+}/calculate")
public InventoryViabilityDetailsDTO calculate(@PathVariable("id") final long id, @RequestBody(required = false) List<Integer> selectedReplicationNumbers) {
return serviceFacade.calculateResult(id, selectedReplicationNumbers);
}
/**
* Returns fully loaded viability + data records
* @param id InventoryViability#id
* @return fully loaded viability
*/
@GetMapping(ENDPOINT_ID + "/details")
public InventoryViabilityDetailsDTO getDetails(@PathVariable("id") final long id) {
return serviceFacade.loadDetails(id);
}
/**
* Print the same label for specified <code>ids</code>.
*/
@PostMapping(value = "/generate-labels")
@Operation(method = "generateLabels", summary = "Generate replicate labels for selected viability test IDs", description = "Generates a separate label for each replicate in the test. 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 replicate labels for selected viability test IDs", description = "Generates a separate label for each replicate in the test with 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);
}
@RestController("inventoryViabilityActionApi2")
@RequestMapping(InventoryViabilityActionController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = InventoryViabilityController.API_TAG)
public static class InventoryViabilityActionController extends ActionController<InventoryViabilityActionDTO, InventoryViabilityAction, InventoryViabilityActionFilter, InventoryViabilityActionRequestDTO, InventoryViabilityActionScheduleFilter, InventoryViabilityActionApiService> {
public static final String API_URL = InventoryViabilityController.API_URL;
@Override
protected OrderSpecifier<?>[] defaultSort() {
return new OrderSpecifier[] { QInventoryViabilityAction.inventoryViabilityAction.startedDate.desc() };
}
}
@RestController("inventoryViabilityDataApi2")
@RequestMapping(InventoryViabilityDataController.API_URL)
@PreAuthorize("isAuthenticated()")
@Tag(name = InventoryViabilityController.API_TAG)
public static class InventoryViabilityDataController extends FilteredCRUDController<InventoryViabilityDataDTO, InventoryViabilityData, InventoryViabilityDataApiService, InventoryViabilityDataFilter> {
public static final String API_URL = InventoryViabilityController.API_URL + "/data";
@Override
protected OrderSpecifier<?>[] defaultSort() {
return new OrderSpecifier[] { QInventoryViabilityData.inventoryViabilityData.countDate.desc() };
}
}
}