001// --------------------------------------------------------------------------------
002// Copyright 2002-2026 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 static com.echothree.model.jooq.server.keys.batch.BatchForeignKeys.BATCHES_ACTIVE_DETAIL_FK;
020import static com.echothree.model.jooq.server.keys.order.OrderForeignKeys.ORDER_BATCHES_BATCH_FK;
021import static com.echothree.model.jooq.server.tables.batch.BatchDetails.BatchDetails;
022import static com.echothree.model.jooq.server.tables.batch.Batches.Batches;
023import static com.echothree.model.jooq.server.tables.core.EntityInstances.EntityInstances;
024import static com.echothree.model.jooq.server.tables.order.OrderBatches.OrderBatches;
025import com.echothree.model.control.batch.server.search.BatchSearchEvaluator;
026import com.echothree.model.control.search.server.search.EntityInstancePKHolder;
027import com.echothree.model.data.accounting.server.entity.Currency;
028import com.echothree.model.data.batch.server.entity.BatchType;
029import com.echothree.model.data.search.server.entity.SearchDefaultOperator;
030import com.echothree.model.data.search.server.entity.SearchSortDirection;
031import com.echothree.model.data.search.server.entity.SearchSortOrder;
032import com.echothree.model.data.search.server.entity.SearchType;
033import com.echothree.model.data.user.server.entity.UserVisit;
034import com.echothree.util.server.message.ExecutionErrorAccumulator;
035import com.echothree.util.server.persistence.Session;
036
037public class OrderBatchSearchEvaluator
038        extends BatchSearchEvaluator {
039    
040    private Currency currency;
041    
042    /** Creates a new instance of OrderBatchSearchEvaluator */
043    public OrderBatchSearchEvaluator(UserVisit userVisit, SearchType searchType, SearchDefaultOperator searchDefaultOperator, SearchSortOrder searchSortOrder,
044            SearchSortDirection searchSortDirection, BatchType batchType) {
045        super(userVisit, searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, batchType);
046    }
047    
048    public EntityInstancePKHolder getEntityInstancePKHolderByCurrency(Currency currency) {
049        var query = session.getDslContext()
050                .select(EntityInstances.ENTITY_INSTANCE)
051                .from(EntityInstances)
052                .join(Batches).on(Batches.BATCH.cast(Long.class).eq(EntityInstances.ENTITY_UNIQUE))
053                .join(BatchDetails).onKey(BATCHES_ACTIVE_DETAIL_FK)
054                .join(OrderBatches).onKey(ORDER_BATCHES_BATCH_FK)
055                .where(BatchDetails.BATCH_TYPE.eq(batchType.getPrimaryKey()),
056                        OrderBatches.CURRENCY.eq(currency.getPrimaryKey()),
057                        OrderBatches.THRU_TIME.eq(Session.MAX_TIME),
058                        EntityInstances.ENTITY_TYPE.eq(entityType.getPrimaryKey()));
059
060        return getEntityInstancePKHolderFromQuery(query);
061    }
062    
063    public Currency getCurrency() {
064        return currency;
065    }
066    
067    public void setCurrency(Currency currency) {
068        this.currency = currency;
069    }
070    
071    /** Counts the number of search parameters. */
072    @Override
073    protected int countParameters() {
074        return super.countParameters() + (currency == null ? 0 : 1);
075    }
076
077    @Override
078    protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) {
079        var resultSet = super.executeSearch(eea);
080            
081        if(currency != null && (resultSet == null || resultSet.size() > 0)) {
082            var entityInstancePKHolder = getEntityInstancePKHolderByCurrency(currency);
083
084            if(resultSet == null) {
085                resultSet = entityInstancePKHolder;
086            } else {
087                resultSet.retainAll(entityInstancePKHolder);
088            }
089        }
090        
091        return resultSet;
092    }
093    
094}