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.SearchShippingMethodsForm;
020import com.echothree.control.user.search.common.result.SearchResultFactory;
021import com.echothree.control.user.search.common.result.SearchShippingMethodsResult;
022import com.echothree.model.control.party.common.PartyTypes;
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.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.control.shipping.server.search.ShippingMethodSearchEvaluator;
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.echothree.util.server.persistence.Session;
038import com.google.common.base.Splitter;
039import java.util.List;
040import javax.enterprise.context.RequestScoped;
041
042@RequestScoped
043public class SearchShippingMethodsCommand
044        extends BaseSearchCommand<SearchShippingMethodsForm, SearchShippingMethodsResult> {
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.ShippingMethod.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    /** Creates a new instance of SearchShippingMethodsCommand */
073    public SearchShippingMethodsCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
075    }
076    
077    @Override
078    protected BaseResult execute() {
079        var result = SearchResultFactory.getSearchShippingMethodsResult();
080        var searchLogic = SearchLogic.getInstance();
081        var searchKind = searchLogic.getSearchKindByName(this, SearchKinds.SHIPPING_METHOD.name());
082
083        if(!hasExecutionErrors()) {
084            var searchTypeName = form.getSearchTypeName();
085            var searchType = searchLogic.getSearchTypeByName(this, searchKind, searchTypeName);
086
087            if(!hasExecutionErrors()) {
088                var languageIsoName = form.getLanguageIsoName();
089                var language = languageIsoName == null ? null : LanguageLogic.getInstance().getLanguageByName(this, languageIsoName);
090                
091                if(!hasExecutionErrors()) {
092                    var searchControl = Session.getModelController(SearchControl.class);
093                    var partySearchTypePreference = getPartySearchTypePreference(searchControl, searchType);
094                    var partySearchTypePreferenceDetail = partySearchTypePreference == null ? null : partySearchTypePreference.getLastDetail();
095                    boolean rememberPreferences = Boolean.valueOf(form.getRememberPreferences());
096                    var searchDefaultOperatorName = form.getSearchDefaultOperatorName();
097                    var searchDefaultOperator = searchDefaultOperatorName == null
098                            ? getDefaultSearchDefaultOperator(searchLogic, rememberPreferences, partySearchTypePreferenceDetail)
099                            : searchLogic.getSearchDefaultOperatorByName(this, searchDefaultOperatorName);
100
101                    if(!hasExecutionErrors()) {
102                        var searchSortOrderName = form.getSearchSortOrderName();
103                        var searchSortOrder = searchSortOrderName == null
104                                ? getDefaultSearchSortOrder(searchLogic, rememberPreferences, searchKind, partySearchTypePreferenceDetail)
105                                : searchLogic.getSearchSortOrderByName(this, searchKind, searchSortOrderName);
106
107                        if(!hasExecutionErrors()) {
108                            var searchSortDirectionName = form.getSearchSortDirectionName();
109                            var searchSortDirection = searchSortDirectionName == null
110                                    ? getDefaultSearchSortDirection(searchLogic, rememberPreferences, partySearchTypePreferenceDetail)
111                                    : searchLogic.getSearchSortDirectionByName(this, searchSortDirectionName);
112
113                            if(!hasExecutionErrors()) {
114                                var searchUseTypeName = form.getSearchUseTypeName();
115                                var searchUseType = searchUseTypeName == null ? null : SearchLogic.getInstance().getSearchUseTypeByName(this, searchUseTypeName);
116
117                                if(!hasExecutionErrors()) {
118                                    var userVisit = getUserVisit();
119                                    var createdSince = form.getCreatedSince();
120                                    var modifiedSince = form.getModifiedSince();
121                                    var fields = form.getFields();
122
123                                    if(rememberPreferences) {
124                                        var party = getParty();
125
126                                        if(party != null) {
127                                            updatePartySearchTypePreferences(searchControl, searchType, partySearchTypePreference, searchDefaultOperator,
128                                                    searchSortOrder, searchSortDirection, party);
129                                        }
130                                    }
131
132                                    var entityListItemSearchEvaluator = new ShippingMethodSearchEvaluator(userVisit, language,
133                                            searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, searchUseType);
134
135                                    entityListItemSearchEvaluator.setQ(this, form.getQ());
136                                    entityListItemSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince));
137                                    entityListItemSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince));
138                                    entityListItemSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0]));
139
140                                    if(!hasExecutionErrors()) {
141                                        result.setCount(entityListItemSearchEvaluator.execute(this));
142                                    }
143                                }
144                            }
145                        }
146                    }
147                }
148            }
149        }
150        
151        return result;
152    }
153}