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 org.gringlobal.api.model.InventoryActionDTO;
import org.gringlobal.api.model.InventoryActionRequestDTO;
import org.gringlobal.api.model.InventoryDTO;
import org.gringlobal.api.v1.ApiBaseController;
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.InventoryApiService;
import org.gringlobal.custom.elasticsearch.SearchException;
import org.gringlobal.model.Inventory;
import org.gringlobal.model.InventoryAction;
import org.gringlobal.model.QInventory;
import org.gringlobal.model.QInventoryAction;
import org.gringlobal.service.CRUDService;
import org.gringlobal.service.InventoryActionService;
import org.gringlobal.service.filter.InventoryActionFilter;
import org.gringlobal.service.filter.InventoryFilter;
import org.gringlobal.spring.CSVMessageConverter;
import org.springdoc.api.annotations.ParameterObject;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
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.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;

@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() };
	}

	@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.createdDate.desc() };
		}
	}


	@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 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);
	}

}