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.batch.common.BatchConstants;
020import com.echothree.model.control.batch.server.logic.BatchLogic;
021import com.echothree.model.control.search.server.search.EntityInstancePKHolder;
022import com.echothree.model.control.order.server.search.OrderBatchSearchEvaluator;
023import com.echothree.model.data.core.server.factory.EntityInstanceFactory;
024import com.echothree.model.data.payment.server.entity.PaymentMethod;
025import com.echothree.model.data.search.server.entity.SearchDefaultOperator;
026import com.echothree.model.data.search.server.entity.SearchSortDirection;
027import com.echothree.model.data.search.server.entity.SearchSortOrder;
028import com.echothree.model.data.search.server.entity.SearchType;
029import com.echothree.model.data.user.server.entity.UserVisit;
030import com.echothree.util.server.message.ExecutionErrorAccumulator;
031import com.echothree.util.server.persistence.Session;
032
033public class SalesOrderBatchSearchEvaluator
034        extends OrderBatchSearchEvaluator {
035    
036    private PaymentMethod paymentMethod;
037    
038    /** Creates a new instance of SalesOrderBatchSearchEvaluator */
039    public SalesOrderBatchSearchEvaluator(UserVisit userVisit, SearchType searchType, SearchDefaultOperator searchDefaultOperator,
040            SearchSortOrder searchSortOrder, SearchSortDirection searchSortDirection) {
041        super(userVisit, searchType, searchDefaultOperator, searchSortOrder, searchSortDirection,BatchLogic.getInstance().getBatchTypeByName(null,
042                BatchConstants.BatchType_SALES_ORDER));
043    }
044    
045    public EntityInstancePKHolder getEntityInstancePKHolderByPaymentMethod(PaymentMethod paymentMethod) {
046        return getEntityInstancePKHolderFromQuery(EntityInstanceFactory.getInstance().prepareStatement(
047                "SELECT _PK_ "
048                + "FROM entityinstances, batches, batchdetails, salesorderbatches "
049                + "WHERE btch_activedetailid = btchdt_batchdetailid "
050                + "AND btchdt_btchtyp_batchtypeid = ? "
051                + "AND btch_batchid = salordbtch_btch_batchid "
052                + "AND salordbtch_pm_paymentmethodid = ? AND salordbtch_thrutime = ? "
053                + "AND eni_ent_entitytypeid = ? AND btch_batchid = eni_entityuniqueid"),
054                batchType, paymentMethod, Session.MAX_TIME, entityType);
055    }
056    
057    public PaymentMethod getPaymentMethod() {
058        return paymentMethod;
059    }
060    
061    public void setPaymentMethod(PaymentMethod paymentMethod) {
062        this.paymentMethod = paymentMethod;
063    }
064    
065    /** Counts the number of search parameters. */
066    @Override
067    protected int countParameters() {
068        return super.countParameters() + (paymentMethod == null ? 0 : 1);
069    }
070
071    @Override
072    protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) {
073        EntityInstancePKHolder resultSet = super.executeSearch(eea);
074            
075        if(paymentMethod != null && (resultSet == null || resultSet.size() > 0)) {
076            EntityInstancePKHolder entityInstancePKHolder = getEntityInstancePKHolderByPaymentMethod(paymentMethod);
077
078            if(resultSet == null) {
079                resultSet = entityInstancePKHolder;
080            } else {
081                resultSet.retainAll(entityInstancePKHolder);
082            }
083        }
084        
085        return resultSet;
086    }
087    
088}