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