001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 Echo Three, LLC
003//
004// Licensed under the Apache License, Version 2.0 (the "License");
005// you may not use this file except in compliance with the License.
006// You may obtain a copy of the License at
007//
008//     http://www.apache.org/licenses/LICENSE-2.0
009//
010// Unless required by applicable law or agreed to in writing, software
011// distributed under the License is distributed on an "AS IS" BASIS,
012// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013// See the License for the specific language governing permissions and
014// limitations under the License.
015// --------------------------------------------------------------------------------
016
017package com.echothree.model.control.sales.server.search;
018
019import com.echothree.model.control.order.common.OrderTypes;
020import com.echothree.model.control.order.server.logic.OrderLogic;
021import com.echothree.model.control.order.server.search.OrderSearchEvaluator;
022import com.echothree.model.control.search.server.search.EntityInstancePKHolder;
023import com.echothree.model.data.associate.server.entity.AssociateReferral;
024import com.echothree.model.data.core.server.factory.EntityInstanceFactory;
025import com.echothree.model.data.offer.server.entity.OfferUse;
026import com.echothree.model.data.search.server.entity.SearchDefaultOperator;
027import com.echothree.model.data.search.server.entity.SearchSortDirection;
028import com.echothree.model.data.search.server.entity.SearchSortOrder;
029import com.echothree.model.data.search.server.entity.SearchType;
030import com.echothree.model.data.user.server.entity.UserVisit;
031import com.echothree.util.server.message.ExecutionErrorAccumulator;
032import com.echothree.util.server.persistence.Session;
033
034public class SalesOrderSearchEvaluator
035        extends OrderSearchEvaluator {
036    
037    private OfferUse offerUse;
038    private AssociateReferral associateReferral;
039    
040    /** Creates a new instance of SalesOrderSearchEvaluator */
041    public SalesOrderSearchEvaluator(UserVisit userVisit, SearchType searchType, SearchDefaultOperator searchDefaultOperator, SearchSortOrder searchSortOrder,
042            SearchSortDirection searchSortDirection) {
043        super(userVisit, searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, OrderLogic.getInstance().getOrderTypeByName(null,
044                OrderTypes.SALES_ORDER.name()));
045    }
046    
047    public EntityInstancePKHolder getEntityInstancePKHolderByOfferUse(OfferUse offerUse) {
048        return getEntityInstancePKHolderFromQuery(EntityInstanceFactory.getInstance().prepareStatement(
049                "SELECT _PK_ "
050                + "FROM entityinstances, orders, orderdetails, salesorders "
051                + "WHERE ord_activedetailid = orddt_orderdetailid "
052                + "AND orddt_ordtyp_ordertypeid = ? "
053                + "AND ord_orderid = salord_ord_orderid "
054                + "AND salord_ofruse_offeruseid = ? AND salord_thrutime = ? "
055                + "AND eni_ent_entitytypeid = ? AND ord_orderid = eni_entityuniqueid"),
056                orderType, offerUse, Session.MAX_TIME, entityType);
057    }
058    
059    public EntityInstancePKHolder getEntityInstancePKHolderByAssociateReferral(AssociateReferral associateReferral) {
060        return getEntityInstancePKHolderFromQuery(EntityInstanceFactory.getInstance().prepareStatement(
061                "SELECT _PK_ "
062                + "FROM entityinstances, orders, orderdetails, salesorders "
063                + "WHERE ord_activedetailid = orddt_orderdetailid "
064                + "AND orddt_ordtyp_ordertypeid = ? "
065                + "AND ord_orderid = salord_ord_orderid "
066                + "AND salord_ascrfr_associatereferralid = ? AND salord_thrutime = ? "
067                + "AND eni_ent_entitytypeid = ? AND ord_orderid = eni_entityuniqueid"),
068                orderType, associateReferral, Session.MAX_TIME, entityType);
069    }
070    
071    public OfferUse getOfferUse() {
072        return offerUse;
073    }
074    
075    public void setOfferUse(OfferUse offerUse) {
076        this.offerUse = offerUse;
077    }
078    
079    /** Counts the number of search parameters. */
080    @Override
081    protected int countParameters() {
082        return super.countParameters() + (offerUse == null ? 0 : 1);
083    }
084
085    @Override
086    protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) {
087        EntityInstancePKHolder resultSet = super.executeSearch(eea);
088        
089        if(offerUse != null && (resultSet == null || resultSet.size() > 0)) {
090            EntityInstancePKHolder entityInstancePKHolder = getEntityInstancePKHolderByOfferUse(offerUse);
091
092            if(resultSet == null) {
093                resultSet = entityInstancePKHolder;
094            } else {
095                resultSet.retainAll(entityInstancePKHolder);
096            }
097        }
098        
099        if(associateReferral != null && (resultSet == null || resultSet.size() > 0)) {
100            EntityInstancePKHolder entityInstancePKHolder = getEntityInstancePKHolderByAssociateReferral(associateReferral);
101
102            if(resultSet == null) {
103                resultSet = entityInstancePKHolder;
104            } else {
105                resultSet.retainAll(entityInstancePKHolder);
106            }
107        }
108        
109        return resultSet;
110    }
111    
112}