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.control.user.search.server.command; 018 019import com.echothree.control.user.search.common.form.SearchItemsForm; 020import com.echothree.control.user.search.common.result.SearchItemsResult; 021import com.echothree.control.user.search.common.result.SearchResultFactory; 022import com.echothree.model.control.item.server.logic.ItemLogic; 023import com.echothree.model.control.party.server.logic.LanguageLogic; 024import com.echothree.model.control.search.common.SearchKinds; 025import com.echothree.model.control.search.server.control.SearchControl; 026import com.echothree.model.control.item.server.search.ItemSearchEvaluator; 027import com.echothree.model.control.search.server.logic.SearchLogic; 028import com.echothree.model.control.item.common.workflow.ItemStatusConstants; 029import com.echothree.model.control.workflow.server.logic.WorkflowLogic; 030import com.echothree.model.control.workflow.server.logic.WorkflowStepLogic; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.model.data.workflow.server.entity.WorkflowStep; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.common.command.BaseResult; 037import com.google.common.base.Splitter; 038import java.util.ArrayList; 039import java.util.List; 040import javax.enterprise.context.Dependent; 041import javax.inject.Inject; 042 043@Dependent 044public class SearchItemsCommand 045 extends BaseSearchCommand<SearchItemsForm, SearchItemsResult> { 046 047 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 048 049 static { 050 FORM_FIELD_DEFINITIONS = List.of( 051 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 052 new FieldDefinition("SearchDefaultOperatorName", FieldType.ENTITY_NAME, false, null, null), 053 new FieldDefinition("SearchSortDirectionName", FieldType.ENTITY_NAME, false, null, null), 054 new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null), 055 new FieldDefinition("SearchSortOrderName", FieldType.ENTITY_NAME, false, null, null), 056 new FieldDefinition("ItemNameOrAlias", FieldType.ENTITY_NAME, false, null, null), 057 new FieldDefinition("Description", FieldType.STRING, false, null, null), 058 new FieldDefinition("ItemTypeName", FieldType.ENTITY_NAME, false, null, null), 059 new FieldDefinition("ItemUseTypeName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("ItemStatusChoice", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("ItemStatusChoices", FieldType.ENTITY_NAMES, false, null, null), 062 new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null), 063 new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null), 064 new FieldDefinition("Fields", FieldType.STRING, false, null, null), 065 new FieldDefinition("RememberPreferences", FieldType.BOOLEAN, false, null, null), 066 new FieldDefinition("SearchUseTypeName", FieldType.ENTITY_NAME, false, null, null) 067 ); 068 } 069 070 @Inject 071 SearchControl searchControl; 072 073 @Inject 074 ItemLogic itemLogic; 075 076 @Inject 077 LanguageLogic languageLogic; 078 079 @Inject 080 SearchLogic searchLogic; 081 082 @Inject 083 WorkflowLogic workflowLogic; 084 085 @Inject 086 WorkflowStepLogic workflowStepLogic; 087 088 /** Creates a new instance of SearchItemsCommand */ 089 public SearchItemsCommand() { 090 super(null, FORM_FIELD_DEFINITIONS, false); 091 } 092 093 @Override 094 protected BaseResult execute() { 095 var result = SearchResultFactory.getSearchItemsResult(); 096 var itemStatusChoice = form.getItemStatusChoice(); 097 var itemStatusChoices = form.getItemStatusChoices(); 098 var parameterCount = (itemStatusChoice == null ? 0 : 1) + (itemStatusChoices == null ? 0 : 1); 099 100 if(parameterCount < 2) { 101 var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.ITEM.name()); 102 103 if(!hasExecutionErrors()) { 104 var searchTypeName = form.getSearchTypeName(); 105 var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName); 106 107 if(!hasExecutionErrors()) { 108 var languageIsoName = form.getLanguageIsoName(); 109 var language = languageIsoName == null ? null : languageLogic.getLanguageByName(this, languageIsoName); 110 111 if(!hasExecutionErrors()) { 112 var partySearchTypePreference = getPartySearchTypePreference(searchControl, searchType); 113 var partySearchTypePreferenceDetail = partySearchTypePreference == null ? null : partySearchTypePreference.getLastDetail(); 114 boolean rememberPreferences = Boolean.valueOf(form.getRememberPreferences()); 115 var searchDefaultOperatorName = form.getSearchDefaultOperatorName(); 116 var searchDefaultOperator = searchDefaultOperatorName == null 117 ? getDefaultSearchDefaultOperator(searchLogic, rememberPreferences, partySearchTypePreferenceDetail) 118 : searchLogic.getSearchDefaultOperatorByName(this, searchDefaultOperatorName); 119 120 if(!hasExecutionErrors()) { 121 var searchSortOrderName = form.getSearchSortOrderName(); 122 var searchSortOrder = searchSortOrderName == null 123 ? getDefaultSearchSortOrder(searchLogic, rememberPreferences, searchKind, partySearchTypePreferenceDetail) 124 : searchLogic.getSearchSortOrderByName(this, searchKind, searchSortOrderName); 125 126 if(!hasExecutionErrors()) { 127 var searchSortDirectionName = form.getSearchSortDirectionName(); 128 var searchSortDirection = searchSortDirectionName == null 129 ? getDefaultSearchSortDirection(searchLogic, rememberPreferences, partySearchTypePreferenceDetail) 130 : searchLogic.getSearchSortDirectionByName(this, searchSortDirectionName); 131 132 if(!hasExecutionErrors()) { 133 var searchUseTypeName = form.getSearchUseTypeName(); 134 var searchUseType = searchUseTypeName == null ? null : searchLogic.getSearchUseTypeByName(this, searchUseTypeName); 135 136 if(!hasExecutionErrors()) { 137 var itemTypeName = form.getItemTypeName(); 138 var itemType = itemTypeName == null ? null : itemLogic.getItemTypeByName(this, itemTypeName); 139 140 if(!hasExecutionErrors()) { 141 var itemUseTypeName = form.getItemUseTypeName(); 142 var itemUseType = itemUseTypeName == null ? null : itemLogic.getItemUseTypeByName(this, itemUseTypeName); 143 144 if(!hasExecutionErrors()) { 145 WorkflowStep itemStatusWorkflowStep = null; 146 List<WorkflowStep> itemStatusWorkflowSteps = null; 147 148 if(itemStatusChoice != null || itemStatusChoices != null) { 149 var workflow = workflowLogic.getWorkflowByName(this, ItemStatusConstants.Workflow_ITEM_STATUS); 150 151 if(!hasExecutionErrors()) { 152 if(itemStatusChoice != null) { 153 itemStatusWorkflowStep = workflowStepLogic.getWorkflowStepByName(this, workflow, itemStatusChoice); 154 } else { 155 var workflowStepNames = Splitter.on(':').trimResults().omitEmptyStrings().splitToList(itemStatusChoices).toArray(new String[0]); 156 157 itemStatusWorkflowSteps = new ArrayList<>(workflowStepNames.length); 158 for(var i = 0; i < workflowStepNames.length; i++) { 159 var workflowStep = workflowStepLogic.getWorkflowStepByName(this, workflow, workflowStepNames[i]); 160 161 if(!hasExecutionErrors()) { 162 itemStatusWorkflowSteps.add(workflowStep); 163 } 164 } 165 } 166 } 167 } 168 169 if(!hasExecutionErrors()) { 170 var userVisit = getUserVisit(); 171 var createdSince = form.getCreatedSince(); 172 var modifiedSince = form.getModifiedSince(); 173 var fields = form.getFields(); 174 175 if(rememberPreferences) { 176 var party = getParty(); 177 178 if(party != null) { 179 updatePartySearchTypePreferences(searchControl, searchType, partySearchTypePreference, searchDefaultOperator, 180 searchSortOrder, searchSortDirection, party); 181 } 182 } 183 184 var itemSearchEvaluator = new ItemSearchEvaluator(userVisit, language, searchType, 185 searchDefaultOperator, searchSortOrder, searchSortDirection, searchUseType); 186 187 itemSearchEvaluator.setItemNameOrAlias(form.getItemNameOrAlias()); 188 itemSearchEvaluator.setQ(this, form.getDescription()); 189 itemSearchEvaluator.setItemType(itemType); 190 itemSearchEvaluator.setItemUseType(itemUseType); 191 itemSearchEvaluator.setItemStatusWorkflowStep(itemStatusWorkflowStep); 192 itemSearchEvaluator.setItemStatusWorkflowSteps(itemStatusWorkflowSteps); 193 itemSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince)); 194 itemSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince)); 195 itemSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0])); 196 197 if(!hasExecutionErrors()) { 198 result.setCount(itemSearchEvaluator.execute(this)); 199 } 200 } else { 201 addExecutionError(ExecutionErrors.UnknownItemStatusChoice.name(), itemStatusChoice); 202 } 203 } 204 } 205 } 206 } 207 } 208 } 209 } 210 } 211 } 212 } else { 213 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 214 } 215 216 return result; 217 } 218}