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