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