OrderRepository.java

package com.tradecloud.repository;

import com.tradecloud.domain.common.Incoterm;
import com.tradecloud.domain.costing.CostingContextType;
import com.tradecloud.domain.document.invoice.ActualOrder;
import com.tradecloud.domain.event.OrderEventType;
import com.tradecloud.domain.event.OrdersEvent;
import com.tradecloud.domain.item.LineItem;
import com.tradecloud.domain.model.ordermanagement.Consignment;
import com.tradecloud.domain.model.ordermanagement.Order;
import com.tradecloud.domain.model.ordermanagement.OrderState;
import com.tradecloud.domain.model.organisationalunit.OrganisationalUnit;
import com.tradecloud.domain.shipment.Shipment;
import com.tradecloud.dto.freetext.FreeTextSearch;
import com.tradecloud.dto.order.*;
import com.tradecloud.repository.base.RepositoryBase;
import org.apache.commons.lang3.NotImplementedException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * Repository for {@link Order} objects.
 *
 * @param <T> An subclass of {@code Order}
 * @param <S> A subclass of search
 */

public interface OrderRepository<T extends Order, S extends OrderSearch> extends RepositoryBase<T, S> {

    // Free text search pair

    long countFreeText(FreeTextSearch search);

    List<T> searchFreeText(FreeTextSearch search);

    /**
     * Finds an order by the supplied reference.
     *
     * @param reference The reference of the order to search by
     * @return The order matching the supplied reference, or null if none found
     */
    // Use findAllByReference, or findByReferenceActive
    @Deprecated
    T findByReference(String reference);

    List<T> findAllByReference(String reference);

    /**
     * Finds an order by the supplied reference if it doesn't have a state of
     * DELETED.
     *
     * @param reference The reference of the order to search by
     * @return The order matching the supplied reference, or null if none found
     */
    // Use findByReferenceActive
    @Deprecated
    T findByReferenceNotDeleted(String reference);

    T findByReferenceActive(String reference);

    /**
     * Finds an order the supplied number and reference. Both fields must be
     * supplied for the search to take place. We search by both these fields as
     * there is a unique constraint on this pair of fields in the database so we
     * avail of the index by supplying both fields in the query.
     *
     * @param orderNumber    The number of the order to search by
     * @param orderReference The reference of the order to search by
     * @return The order matching the supplied params, or null if none found
     * @throws IllegalArgumentException if orderNumber or orderReference are null
     */
    T findByNumberAndReference(String orderNumber, String orderReference);

    T findByNumber(String number);

    T findByShippingReference(String shippingReference);

    /**
     * Finds the order associated with the supplied id and fetches the child
     * line items at the same time.
     *
     * @param id the order id
     * @return a T with child line items loaded, or null if no order is found
     */
    // Use searchActiveLineItems
    @Deprecated
    T findByIdWithLineItems(long id);

    Map<Order.UnlinkedOrdersType, List<T>> findAllUnlinkedOrders(S orderSearch);

    /**
     * Finds the reference of the Order associated with the supplied orderId.
     *
     * @param orderId the ID of the active Order to find the reference for
     * @return the reference of the active Order associated with the supplied orderId or null if no Order is found
     */
    String findReferenceByIdNotDeleted(long orderId);

    // not consigned

    Page<T> findAllArchivedByReference(String reference, Pageable search);

    List<OrderNotConsignedDTO> notConsignedSearch(S orderSearch);

    long notConsignedCount(S orderSearch);

    // awaiting costing

    List<T> awaitingCostingSearch(S orderSearch);

    long awaitingCostingCount(S orderSearch);

    // awaiting signoff

    List<T> awaitingSignoffSearch(S orderSearch);

    long awaitingSignoffCount(S orderSearch);

    List<Order> notPackedSearch(S orderSearch);

    long notPackedCount(S orderSearch);

    // deleted

    List<T> deletedSearch(S orderSearch);

    long deletedCount(S orderSearch);

    // orders not shipped

    List<PfOrderNotShippedSearchResult> notShippedSearch(S orderSearch);

    long notShippedCount(S orderSearch);

    //List<T> confirmationSearch(S orderSearch);

    List<OrderConfirmationSearchResult> confirmationSearchTransformed(S orderSearch);

    List<OrderConfirmationSearchResult> insuranceReportSearchTransformed(S orderSearch);

    long confirmationCount(S orderSearch);

    // orders eligable for adding to a letter of credit

    List<T> letterOfCreditSearch(S search);

    long letterOfCreditCount(S search);

    // order items not shipped

    List<T> findByState(OrderState state);

    List<T> findAllByShipment(Shipment shipment);

    T findByIdWithEventsAndPlannedSettlements(Long id);

    // Use search() with no row count
    @Deprecated
    List<T> fullSearch(S search);

    List<T> findOrdersForConsignment(Consignment consignment);

    List<T> findOrdersForLiteConsignment(Consignment consignment);

    BigDecimal getTotalPlannedSettlementAmount(T order);

    BigDecimal getTotalPlannedSettlementCostingAmount(T order);

    List<ActualOrder> findActualOrderByNumber(String number);

    /**
     * Determines if the supplied order has a sample line item associated to it.
     *
     * @param order the order to check for a sample line item
     * @param id
     * @return true if the supplied order has a sample line item associated to it, false otherwise
     */
    boolean hasSampleLineItem(T order, Long id);

    /**
     * Determines if the supplied order has a spare part line item associated to it.
     *
     * @param order  the order to check for a spare part line item
     * @param itemId
     * @return true if the supplied order has a spare part line item associated to it, false otherwise
     */
    boolean hasSparePartLineItem(T order, Long itemId);

    long countUntariffedLineItems(T order);

    /**
     * Search for the line items of an order. You normally only want the active ones
     *
     * @param order
     * @param searchMetaParams
     * @return
     */
    List<LineItem> searchActiveLineItems(T order, SearchMetaParams searchMetaParams);

    /**
     * Count the line items on an order. You normally only want the active ones
     *
     * @param order
     * @return
     */
    long countActiveLineItems(T order);

    long countByIncotermOrganisationalUnitAndOrderState(Incoterm incoterm,
                                                        OrganisationalUnit organisationalUnit,
                                                        OrderState... orderState);

    long countForAllOrgUnitsInCostDefinition(Incoterm incoterm,
                                             OrderState orderState,
                                             CostingContextType costingContextType);

    List<T> findNotInCostDefinitionOrganisationalUnits(Incoterm incoterm,
                                                       CostingContextType costingContextType,
                                                       OrderState... orderState);

    BigDecimal getTotalUnitQuantity(T order);

    List<Long> getBrotherOrderIds(Long orderId, String ordering);

    List<OrdersEvent> getOrdersEvent(Long orderId, OrderEventType orderEventType, String ordering, int maxResults);

    default List<T> orderPlannedSettlementSearch(S search) {
        throw new NotImplementedException("Not implemented");
    }

    default long orderPlannedSettlementCount(S search) {
        throw new NotImplementedException("Not implemented");
    }

    Map<T, BigDecimal> getTEUMap(List<T> orders);

    List orderSignedOffReport(S search);

    String getGeneratedOrderReference(String id);

    List orderInsuranceDeclaration(S search);

    List<ChildOrderDTO> searchChildOrders(T order, SearchMetaParams metaParams);

    long countChildOrders(T order);

    List<T> findAllByQuote(String reference);

    T retrieveWithConsignment(long id);

    boolean isOrderId(long id);

    void recycleOrder(String reference);

    long orderWithSameRefDiffNumber(String orderReference, String number);

    long orderWithSameRefQuoteDiffNumber(String quoteReference, String number);
}