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.batch.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.workflow.WorkflowForeignKeys.WORKFLOW_ENTITY_STATUSES_ENTITY_INSTANCE_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.workflow.WorkflowEntityStatuses.WorkflowEntityStatuses; 025import com.echothree.model.control.batch.server.control.BatchControl; 026import com.echothree.model.control.core.common.ComponentVendors; 027import com.echothree.model.control.core.common.EntityTypes; 028import com.echothree.model.control.core.server.control.EntityInstanceControl; 029import com.echothree.model.control.search.server.search.BaseSearchEvaluator; 030import com.echothree.model.control.search.server.search.EntityInstancePKHolder; 031import com.echothree.model.data.batch.server.entity.Batch; 032import com.echothree.model.data.batch.server.entity.BatchAliasType; 033import com.echothree.model.data.batch.server.entity.BatchType; 034import com.echothree.model.data.search.server.entity.SearchDefaultOperator; 035import com.echothree.model.data.search.server.entity.SearchSortDirection; 036import com.echothree.model.data.search.server.entity.SearchSortOrder; 037import com.echothree.model.data.search.server.entity.SearchType; 038import com.echothree.model.data.user.server.entity.UserVisit; 039import com.echothree.model.data.workflow.server.entity.WorkflowStep; 040import com.echothree.util.server.message.ExecutionErrorAccumulator; 041import com.echothree.util.server.persistence.Session; 042 043public class BatchSearchEvaluator 044 extends BaseSearchEvaluator { 045 046 protected BatchType batchType; 047 private WorkflowStep batchStatusWorkflowStep; 048 private String batchName; 049 private BatchAliasType batchAliasType; 050 private String alias; 051 052 protected BatchSearchEvaluator(UserVisit userVisit, SearchType searchType, SearchDefaultOperator searchDefaultOperator, SearchSortOrder searchSortOrder, 053 SearchSortDirection searchSortDirection, BatchType batchType) { 054 super(userVisit, searchDefaultOperator, searchType, searchSortOrder, searchSortDirection, null, ComponentVendors.ECHO_THREE.name(), 055 EntityTypes.Batch.name(), null, null, null); 056 057 this.batchType = batchType; 058 } 059 060 public WorkflowStep getBatchStatusWorkflowStep() { 061 return batchStatusWorkflowStep; 062 } 063 064 public void setBatchStatusWorkflowStep(WorkflowStep batchStatusWorkflowStep) { 065 this.batchStatusWorkflowStep = batchStatusWorkflowStep; 066 } 067 068 public String getBatchName() { 069 return batchName; 070 } 071 072 public void setBatchName(String batchName) { 073 this.batchName = batchName; 074 } 075 076 public BatchAliasType getBatchAliasType() { 077 return batchAliasType; 078 } 079 080 public void setBatchAliasType(BatchAliasType batchAliasType) { 081 this.batchAliasType = batchAliasType; 082 } 083 084 public String getAlias() { 085 return alias; 086 } 087 088 public void setAlias(String alias) { 089 this.alias = alias; 090 } 091 092 /** Counts the number of search parameters, not including batchType. */ 093 @Override 094 protected int countParameters() { 095 return super.countParameters() + (batchStatusWorkflowStep == null ? 0 : 1) + (batchName == null ? 0 : 1) + (batchAliasType == null ? 0 : 1) 096 + (alias == null ? 0 : 1); 097 } 098 099 public EntityInstancePKHolder getEntityInstancePKHolderByBatchType(BatchType batchType) { 100 var query = session.getDslContext() 101 .select(EntityInstances.ENTITY_INSTANCE) 102 .from(EntityInstances) 103 .join(Batches).on(Batches.BATCH.cast(Long.class).eq(EntityInstances.ENTITY_UNIQUE)) 104 .join(BatchDetails).onKey(BATCHES_ACTIVE_DETAIL_FK) 105 .where(BatchDetails.BATCH_TYPE.eq(batchType.getPrimaryKey()), 106 EntityInstances.ENTITY_TYPE.eq(entityType.getPrimaryKey())); 107 108 return getEntityInstancePKHolderFromQuery(query); 109 } 110 111 public EntityInstancePKHolder getEntityInstancePKHolderByBatchStatusWorkflowStep(WorkflowStep batchStatusWorkflowStep) { 112 var query = session.getDslContext() 113 .select(EntityInstances.ENTITY_INSTANCE) 114 .from(EntityInstances) 115 .join(Batches).on(Batches.BATCH.cast(Long.class).eq(EntityInstances.ENTITY_UNIQUE)) 116 .join(BatchDetails).onKey(BATCHES_ACTIVE_DETAIL_FK) 117 .join(WorkflowEntityStatuses).onKey(WORKFLOW_ENTITY_STATUSES_ENTITY_INSTANCE_FK) 118 .where(BatchDetails.BATCH_TYPE.eq(batchType.getPrimaryKey()), 119 EntityInstances.ENTITY_TYPE.eq(entityType.getPrimaryKey()), 120 WorkflowEntityStatuses.WORKFLOW_STEP.eq(batchStatusWorkflowStep.getPrimaryKey()), 121 WorkflowEntityStatuses.THRU_TIME.eq(Session.MAX_TIME)); 122 123 return getEntityInstancePKHolderFromQuery(query); 124 } 125 126 /** Subclasses should override and always call their super's executeSearch() */ 127 @Override 128 protected EntityInstancePKHolder executeSearch(final ExecutionErrorAccumulator eea) { 129 EntityInstancePKHolder resultSet = null; 130 var parameterCount = (batchName == null ? 0 : 1) + (alias == null ? 0 : 1); 131 132 if(parameterCount == 0) { 133 resultSet = super.executeSearch(eea); 134 135 if(batchStatusWorkflowStep != null && (resultSet == null || resultSet.size() > 0)) { 136 var entityInstancePKHolder = getEntityInstancePKHolderByBatchStatusWorkflowStep(batchStatusWorkflowStep); 137 138 if(resultSet == null) { 139 resultSet = entityInstancePKHolder; 140 } else { 141 resultSet.retainAll(entityInstancePKHolder); 142 } 143 } 144 145 if(countParameters() == 0 && (resultSet == null || resultSet.size() > 0)) { 146 var entityInstancePKHolder = getEntityInstancePKHolderByBatchType(batchType); 147 148 if(resultSet == null) { 149 resultSet = entityInstancePKHolder; 150 } else { 151 resultSet.retainAll(entityInstancePKHolder); 152 } 153 } 154 } else { 155 var batchControl = Session.getModelController(BatchControl.class); 156 Batch batch = null; 157 158 if(parameterCount == 1) { 159 if(batchAliasType == null) { 160 batchAliasType = batchControl.getDefaultBatchAliasType(batchType); 161 } 162 163 if(batchAliasType != null) { 164 batch = batchControl.getBatchByAlias(batchAliasType, alias); 165 } 166 167 if(batchName != null) { 168 batch = batchControl.getBatchByName(batchType, batchName); 169 } 170 171 if(batch != null) { 172 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 173 174 resultSet = new EntityInstancePKHolder(1); 175 resultSet.add(entityInstanceControl.getEntityInstanceByBasePK(batch.getPrimaryKey()).getPrimaryKey(), null); 176 } 177 } 178 } 179 180 return resultSet; 181 } 182 183}