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