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