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