OrderRequestService.java

/*
 * Copyright 2021 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.service;

import java.io.OutputStream;
import java.util.List;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonUnwrapped;
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.OrderRequestAttach;
import org.gringlobal.model.OrderRequestItem;
import org.gringlobal.model.community.CommunityCodeValues;
import org.gringlobal.service.filter.OrderRequestFilter;
import org.gringlobal.service.filter.OrderRequestItemFilter;
import org.gringlobal.service.glis.impl.GlisSMTAReportingManager;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import javax.validation.constraints.NotNull;

/**
 * @author Maxym Borodenko
 */
public interface OrderRequestService extends FilteredCRUDService2<OrderRequest, OrderRequestFilter> {

	/**
	 * Utility type to record requested name and taxon
	 */
	class OrderRequestedInventory {
		public OrderRequestedInventory() {
		}

		public OrderRequestedInventory(long inventoryId) {
			this.inventoryId = inventoryId;
		}

		public long inventoryId;
		public String requestedName;
		public String requestedTaxon;
	}

	/**
	 * Gets the orderRequest details.
	 *
	 * @param orderRequest the orderRequest
	 * @return the orderRequest details
	 */
	OrderRequestDetails getOrderRequestDetails(@NotNull OrderRequest orderRequest);

	class OrderRequestDetails {
		@JsonUnwrapped
		public OrderRequest orderRequest;

		public List<OrderRequestAttach> attachments;
	}

	/**
	 * Adding new inventories the order request.
	 *
	 * @param input the order request
	 * @param inventories inventories to be added as items
	 * @return added items
	 */
	List<OrderRequestItem> addInventories(OrderRequest input, List<OrderRequestedInventory> inventories);

	/**
	 * Assign default inventories to items
	 *
	 * @param orderRequest the order request
	 * @param itemIds item Ids
	 */
	List<OrderRequestItem> setDefaultInventories(OrderRequest orderRequest, Set<Long> itemIds);
	
	/**
	 * Creates a new OrderRequest with new {@link OrderRequestItem} list 
	 *
	 * @param source the source
	 * @param orderRequestItems the list of new order request items
	 * @return the order request
	 */
	OrderRequest create(OrderRequest source, List<OrderRequestItem> orderRequestItems);

	/**
	 * Renumber items of the order request.
	 *
	 * @param orderRequest the order request
	 */
	void renumberOrderRequestItems(OrderRequest orderRequest);

	/**
	 * Removing items from the order request.
	 *
	 * @param input the order request
	 * @param itemIds item Ids to be deleted from the order
	 * @return list of removed items
	 */
	List<OrderRequestItem> removeOrderItems(OrderRequest input, Set<Long> itemIds);

	/**
	 * Filter items.
	 *
	 * @param orderRequest the order request
	 * @param filter the filter
	 * @param page the page
	 * @return the page
	 * @throws SearchException the search exception
	 */
	Page<OrderRequestItem> filterItems(OrderRequest orderRequest, OrderRequestItemFilter filter, Pageable page) throws SearchException;

	/**
	 * Update item status.
	 *
	 * @param orderRequest the order request
	 * @param newStatus the new status
	 * @param itemIds the item ids
	 * @return the list of updated items
	 */
	List<OrderRequestItem> updateItemStatus(OrderRequest orderRequest, @CodeValueField(CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS) String newStatus, Set<Long> itemIds);

	/**
	 * Update item status.
	 *
	 * @param orderRequest the order request
	 * @param newStatus the new status
	 * @param orderItems the order items
	 * @return the list
	 */
	List<OrderRequestItem> updateItemStatus(OrderRequest orderRequest, @CodeValueField(CommunityCodeValues.ORDER_REQUEST_ITEM_STATUS) String newStatus, List<OrderRequestItem> orderItems);

	/**
	 * Generate inventories for each item in the given OrderRequest
	 *
	 * @param order the order request
	 * @return the order request
	 */
	OrderRequest generateInventories(OrderRequest order, InventoryMaintenancePolicy withdrawnInventoriesPolicy);
	
	/**
	 * Report on distribution of SMTA materials to the Easy-SMTA system
	 *
	 * @param order the order request
	 * @return SMTA report response
	 */
	GlisSMTAReportingManager.GlisSMTAReportResponse reportSMTA(OrderRequest order) throws Exception;


	/**
	 * Generate the SMTA document in PDF format for the provided recipient, language and type of SMTA and list of material
	 *
	 * @param order the order request
	 * @param outputStream OutputStream
	 */
	void generateSMTA(OrderRequest order, OutputStream outputStream) throws Exception;
}