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