001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.GetSearchSortOrderChoicesForm;
020import com.echothree.control.user.search.common.result.GetSearchSortOrderChoicesResult;
021import com.echothree.control.user.search.common.result.SearchResultFactory;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.search.server.control.SearchControl;
024import com.echothree.model.control.search.server.logic.SearchLogic;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.data.party.server.entity.Party;
028import com.echothree.model.data.search.server.entity.PartySearchTypePreference;
029import com.echothree.model.data.search.server.entity.SearchKind;
030import com.echothree.model.data.search.server.entity.SearchSortOrder;
031import com.echothree.model.data.search.server.entity.SearchType;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.common.command.BaseResult;
036import com.echothree.util.server.control.BaseSimpleCommand;
037import com.echothree.util.server.control.CommandSecurityDefinition;
038import com.echothree.util.server.control.PartyTypeDefinition;
039import com.echothree.util.server.control.SecurityRoleDefinition;
040import com.echothree.util.server.persistence.Session;
041import java.util.Arrays;
042import java.util.Collections;
043import java.util.List;
044
045public class GetSearchSortOrderChoicesCommand
046        extends BaseSimpleCommand<GetSearchSortOrderChoicesForm> {
047    
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
054                    new SecurityRoleDefinition(SecurityRoleGroups.SearchSortOrder.name(), SecurityRoles.Choices.name())
055                    )))
056                )));
057
058        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
059                new FieldDefinition("SearchKindName", FieldType.ENTITY_NAME, true, null, null),
060                new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, false, null, null),
061                new FieldDefinition("DefaultSearchSortOrderChoice", FieldType.ENTITY_NAME, false, null, null),
062                new FieldDefinition("AllowNullChoice", FieldType.BOOLEAN, true, null, null)
063                ));
064    }
065    
066    /** Creates a new instance of GetSearchSortOrderChoicesCommand */
067    public GetSearchSortOrderChoicesCommand(UserVisitPK userVisitPK, GetSearchSortOrderChoicesForm form) {
068        super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
069    }
070    
071    @Override
072    protected BaseResult execute() {
073        SearchLogic searchLogic = SearchLogic.getInstance();
074        GetSearchSortOrderChoicesResult result = SearchResultFactory.getGetSearchSortOrderChoicesResult();
075        String searchKindName = form.getSearchKindName();
076        SearchKind searchKind = searchLogic.getSearchKindByName(this, searchKindName);
077        
078        if(!hasExecutionErrors()) {
079            String defaultSearchSortOrderChoice = form.getDefaultSearchSortOrderChoice();
080            Party party = getParty();
081            String searchTypeName = form.getSearchTypeName();
082            SearchType searchType = searchTypeName != null && defaultSearchSortOrderChoice == null && party != null ? SearchLogic.getInstance().getSearchTypeByName(this, searchKind, searchTypeName) : null;
083            
084            if(!hasExecutionErrors()) {
085                var searchControl = Session.getModelController(SearchControl.class);
086                boolean allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice());
087
088                if(searchType != null) {
089                    PartySearchTypePreference partySearchTypePreference = searchControl.getPartySearchTypePreference(party, searchType);
090                    
091                    if(partySearchTypePreference != null) {
092                        SearchSortOrder searchSortOrder = partySearchTypePreference.getLastDetail().getSearchSortOrder();
093                        
094                        if(searchSortOrder != null) {
095                            defaultSearchSortOrderChoice = searchSortOrder.getLastDetail().getSearchSortOrderName();
096                        }
097                    }
098                }
099                
100                result.setSearchSortOrderChoices(searchControl.getSearchSortOrderChoices(defaultSearchSortOrderChoice, getPreferredLanguage(), allowNullChoice,
101                        searchKind));
102            }
103        }
104        
105        return result;
106    }
107    
108}