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.SearchEntityListItemsForm;
020import com.echothree.control.user.search.common.result.SearchEntityListItemsResult;
021import com.echothree.control.user.search.common.result.SearchResultFactory;
022import com.echothree.model.control.core.server.search.EntityListItemSearchEvaluator;
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.search.server.logic.SearchLogic;
027import com.echothree.model.data.user.common.pk.UserVisitPK;
028import com.echothree.util.common.command.BaseResult;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.google.common.base.Splitter;
032import java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
035
036@Dependent
037public class SearchEntityListItemsCommand
038        extends BaseSearchCommand<SearchEntityListItemsForm, SearchEntityListItemsResult> {
039
040    // No COMMAND_SECURITY_DEFINITION, anyone may execute this command.
041    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
042
043    static {
044        FORM_FIELD_DEFINITIONS = List.of(
045                new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null),
046                new FieldDefinition("SearchDefaultOperatorName", FieldType.ENTITY_NAME, false, null, null),
047                new FieldDefinition("SearchSortDirectionName", FieldType.ENTITY_NAME, false, null, null),
048                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null),
049                new FieldDefinition("SearchSortOrderName", FieldType.ENTITY_NAME, false, null, null),
050                new FieldDefinition("Q", FieldType.STRING, false, null, null),
051                new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null),
052                new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null),
053                new FieldDefinition("Fields", FieldType.STRING, false, null, null),
054                new FieldDefinition("RememberPreferences", FieldType.BOOLEAN, false, null, null),
055                new FieldDefinition("SearchUseTypeName", FieldType.ENTITY_NAME, false, null, null)
056        );
057    }
058
059    @Inject
060    SearchControl searchControl;
061
062    @Inject
063    LanguageLogic languageLogic;
064
065    @Inject
066    SearchLogic searchLogic;
067
068    /** Creates a new instance of SearchEntityListItemsCommand */
069    public SearchEntityListItemsCommand() {
070        super(null, FORM_FIELD_DEFINITIONS, false);
071    }
072    
073    @Override
074    protected BaseResult execute() {
075        var result = SearchResultFactory.getSearchEntityListItemsResult();
076        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.ENTITY_LIST_ITEM.name());
077
078        if(!hasExecutionErrors()) {
079            var searchTypeName = form.getSearchTypeName();
080            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
081
082            if(!hasExecutionErrors()) {
083                var languageIsoName = form.getLanguageIsoName();
084                var language = languageIsoName == null ? null : languageLogic.getLanguageByName(this, languageIsoName);
085                
086                if(!hasExecutionErrors()) {
087                    var partySearchTypePreference = getPartySearchTypePreference(searchControl, searchType);
088                    var partySearchTypePreferenceDetail = partySearchTypePreference == null ? null : partySearchTypePreference.getLastDetail();
089                    boolean rememberPreferences = Boolean.valueOf(form.getRememberPreferences());
090                    var searchDefaultOperatorName = form.getSearchDefaultOperatorName();
091                    var searchDefaultOperator = searchDefaultOperatorName == null
092                            ? getDefaultSearchDefaultOperator(searchLogic, rememberPreferences, partySearchTypePreferenceDetail)
093                            : searchLogic.getSearchDefaultOperatorByName(this, searchDefaultOperatorName);
094
095                    if(!hasExecutionErrors()) {
096                        var searchSortOrderName = form.getSearchSortOrderName();
097                        var searchSortOrder = searchSortOrderName == null
098                                ? getDefaultSearchSortOrder(searchLogic, rememberPreferences, searchKind, partySearchTypePreferenceDetail)
099                                : searchLogic.getSearchSortOrderByName(this, searchKind, searchSortOrderName);
100
101                        if(!hasExecutionErrors()) {
102                            var searchSortDirectionName = form.getSearchSortDirectionName();
103                            var searchSortDirection = searchSortDirectionName == null
104                                    ? getDefaultSearchSortDirection(searchLogic, rememberPreferences, partySearchTypePreferenceDetail)
105                                    : searchLogic.getSearchSortDirectionByName(this, searchSortDirectionName);
106
107                            if(!hasExecutionErrors()) {
108                                var searchUseTypeName = form.getSearchUseTypeName();
109                                var searchUseType = searchUseTypeName == null ? null : searchLogic.getSearchUseTypeByName(this, searchUseTypeName);
110
111                                if(!hasExecutionErrors()) {
112                                    var userVisit = getUserVisit();
113                                    var createdSince = form.getCreatedSince();
114                                    var modifiedSince = form.getModifiedSince();
115                                    var fields = form.getFields();
116
117                                    if(rememberPreferences) {
118                                        var party = getParty();
119
120                                        if(party != null) {
121                                            updatePartySearchTypePreferences(searchControl, searchType, partySearchTypePreference, searchDefaultOperator,
122                                                    searchSortOrder, searchSortDirection, party);
123                                        }
124                                    }
125
126                                    var entityListItemSearchEvaluator = new EntityListItemSearchEvaluator(userVisit, language,
127                                            searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, searchUseType);
128
129                                    entityListItemSearchEvaluator.setQ(this, form.getQ());
130                                    entityListItemSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
131                                    entityListItemSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
132                                    entityListItemSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
133
134                                    if(!hasExecutionErrors()) {
135                                        result.setCount(entityListItemSearchEvaluator.execute(this));
136                                    }
137                                }
138                            }
139                        }
140                    }
141                }
142            }
143        }
144        
145        return result;
146    }
147}