001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.batch.server.search;
018
019import com.echothree.model.control.batch.server.control.BatchControl;
020import com.echothree.model.control.core.common.ComponentVendors;
021import com.echothree.model.control.core.common.EntityTypes;
022import com.echothree.model.control.core.server.control.EntityInstanceControl;
023import com.echothree.model.control.search.server.search.BaseSearchEvaluator;
024import com.echothree.model.control.search.server.search.EntityInstancePKHolder;
025import com.echothree.model.data.batch.server.entity.Batch;
026import com.echothree.model.data.batch.server.entity.BatchAliasType;
027import com.echothree.model.data.batch.server.entity.BatchType;
028import com.echothree.model.data.core.server.factory.EntityInstanceFactory;
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.model.data.workflow.server.entity.WorkflowStep;
035import com.echothree.util.server.message.ExecutionErrorAccumulator;
036import com.echothree.util.server.persistence.Session;
037
038public class BatchSearchEvaluator
039        extends BaseSearchEvaluator {
040    
041    protected BatchType batchType;
042    private WorkflowStep batchStatusWorkflowStep;
043    private String batchName;
044    private BatchAliasType batchAliasType;
045    private String alias;
046    
047    protected BatchSearchEvaluator(UserVisit userVisit, SearchType searchType, SearchDefaultOperator searchDefaultOperator, SearchSortOrder searchSortOrder,
048            SearchSortDirection searchSortDirection, BatchType batchType) {
049        super(userVisit, searchDefaultOperator, searchType, searchSortOrder, searchSortDirection, null, ComponentVendors.ECHO_THREE.name(),
050                EntityTypes.Batch.name(), null, null, null);
051        
052        this.batchType = batchType;
053    }
054    
055    public WorkflowStep getBatchStatusWorkflowStep() {
056        return batchStatusWorkflowStep;
057    }
058
059    public void setBatchStatusWorkflowStep(WorkflowStep batchStatusWorkflowStep) {
060        this.batchStatusWorkflowStep = batchStatusWorkflowStep;
061    }
062    
063    public String getBatchName() {
064        return batchName;
065    }
066
067    public void setBatchName(String batchName) {
068        this.batchName = batchName;
069    }
070
071    public BatchAliasType getBatchAliasType() {
072        return batchAliasType;
073    }
074
075    public void setBatchAliasType(BatchAliasType batchAliasType) {
076        this.batchAliasType = batchAliasType;
077    }
078
079    public String getAlias() {
080        return alias;
081    }
082
083    public void setAlias(String alias) {
084        this.alias = alias;
085    }
086    
087    /** Counts the number of search parameters, not including batchType. */
088    @Override
089    protected int countParameters() {
090        return super.countParameters() + (batchStatusWorkflowStep == null ? 0 : 1) + (batchName == null ? 0 : 1) + (batchAliasType == null ? 0 : 1)
091                + (alias == null ? 0 : 1);
092    }
093
094    public EntityInstancePKHolder getEntityInstancePKHolderByBatchType(BatchType batchType) {
095        return getEntityInstancePKHolderFromQuery(EntityInstanceFactory.getInstance().prepareStatement(
096                "SELECT _PK_ "
097                + "FROM entityinstances, batches, batchdetails "
098                + "WHERE btch_activedetailid = btchdt_batchdetailid "
099                + "AND btchdt_btchtyp_batchtypeid = ? "
100                + "AND eni_ent_entitytypeid = ? AND btch_batchid = eni_entityuniqueid"),
101                batchType, entityType);
102    }
103    
104    public EntityInstancePKHolder getEntityInstancePKHolderByBatchStatusWorkflowStep(WorkflowStep batchStatusWorkflowStep) {
105            return getEntityInstancePKHolderFromQuery(EntityInstanceFactory.getInstance().prepareStatement(
106                    "SELECT _PK_ "
107                    + "FROM entityinstances, batches, batchdetails, workflowentitystatuses "
108                    + "WHERE btch_activedetailid = btchdt_batchdetailid "
109                    + "AND btchdt_btchtyp_batchtypeid = ? "
110                    + "AND eni_ent_entitytypeid = ? AND btch_batchid = eni_entityuniqueid "
111                    + "AND eni_entityinstanceid = wkfles_eni_entityinstanceid AND wkfles_wkfls_workflowstepid = ? AND wkfles_thrutime = ?"),
112                    batchType, entityType, batchStatusWorkflowStep, Session.MAX_TIME);
113    }
114 
115    /** Subclasses should override and always call their super's executeSearch() */
116    @Override
117    protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) {
118        EntityInstancePKHolder resultSet = null;
119        var parameterCount = (batchName == null ? 0 : 1) + (alias == null ? 0 : 1);
120
121        if(parameterCount == 0) {
122            resultSet = super.executeSearch(eea);
123            
124            if(batchStatusWorkflowStep != null && (resultSet == null || resultSet.size() > 0)) {
125                var entityInstancePKHolder = getEntityInstancePKHolderByBatchStatusWorkflowStep(batchStatusWorkflowStep);
126
127                if(resultSet == null) {
128                    resultSet = entityInstancePKHolder;
129                } else {
130                    resultSet.retainAll(entityInstancePKHolder);
131                }
132            }
133            
134            if(countParameters() == 0 && (resultSet == null || resultSet.size() > 0)) {
135                var entityInstancePKHolder = getEntityInstancePKHolderByBatchType(batchType);
136
137                if(resultSet == null) {
138                    resultSet = entityInstancePKHolder;
139                } else {
140                    resultSet.retainAll(entityInstancePKHolder);
141                }
142            }
143        } else {
144            var batchControl = Session.getModelController(BatchControl.class);
145            Batch batch = null;
146
147            if(parameterCount == 1) {
148                if(batchAliasType == null) {
149                    batchAliasType = batchControl.getDefaultBatchAliasType(batchType);
150                }
151
152                if(batchAliasType != null) {
153                    batch = batchControl.getBatchByAlias(batchAliasType, alias);
154                }
155
156                if(batchName != null) {
157                    batch = batchControl.getBatchByName(batchType, batchName);
158                }
159
160                if(batch != null) {
161                    var entityInstanceControl = Session.getModelController(EntityInstanceControl.class);
162
163                    resultSet = new EntityInstancePKHolder(1);
164                    resultSet.add(entityInstanceControl.getEntityInstanceByBasePK(batch.getPrimaryKey()).getPrimaryKey(), null);
165                }
166            }
167        }
168        
169        return resultSet;
170    }
171
172}