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.GetSearchSortDirectionChoicesForm; 020import com.echothree.control.user.search.common.result.GetSearchSortDirectionChoicesResult; 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.SearchSortDirection; 030import com.echothree.model.data.search.server.entity.SearchType; 031import com.echothree.model.data.user.common.pk.UserVisitPK; 032import com.echothree.util.common.message.ExecutionErrors; 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 GetSearchSortDirectionChoicesCommand 046 extends BaseSimpleCommand<GetSearchSortDirectionChoicesForm> { 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.SearchSortDirection.name(), SecurityRoles.Choices.name()) 055 ))) 056 ))); 057 058 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 059 new FieldDefinition("SearchKindName", FieldType.ENTITY_NAME, false, null, null), 060 new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("DefaultSearchSortDirectionChoice", FieldType.ENTITY_NAME, false, null, null), 062 new FieldDefinition("AllowNullChoice", FieldType.BOOLEAN, true, null, null) 063 )); 064 } 065 066 /** Creates a new instance of GetSearchSortDirectionChoicesCommand */ 067 public GetSearchSortDirectionChoicesCommand(UserVisitPK userVisitPK, GetSearchSortDirectionChoicesForm form) { 068 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 069 } 070 071 @Override 072 protected BaseResult execute() { 073 GetSearchSortDirectionChoicesResult result = SearchResultFactory.getGetSearchSortDirectionChoicesResult(); 074 String searchKindName = form.getSearchKindName(); 075 String searchTypeName = form.getSearchTypeName(); 076 var parameterCount = (searchKindName == null ? 0 : 1) + (searchTypeName == null ? 0 : 1); 077 078 if(parameterCount == 0 || parameterCount == 2) { 079 String defaultSearchSortDirectionChoice = form.getDefaultSearchSortDirectionChoice(); 080 Party party = getParty(); 081 SearchType searchType = defaultSearchSortDirectionChoice == null && party != null ? SearchLogic.getInstance().getSearchTypeByName(this, searchKindName, searchTypeName) : null; 082 083 if(!hasExecutionErrors()) { 084 var searchControl = Session.getModelController(SearchControl.class); 085 boolean allowNullChoice = Boolean.parseBoolean(form.getAllowNullChoice()); 086 087 if(searchType != null) { 088 PartySearchTypePreference partySearchTypePreference = searchControl.getPartySearchTypePreference(party, searchType); 089 090 if(partySearchTypePreference != null) { 091 SearchSortDirection searchSortDirection = partySearchTypePreference.getLastDetail().getSearchSortDirection(); 092 093 if(searchSortDirection != null) { 094 defaultSearchSortDirectionChoice = searchSortDirection.getLastDetail().getSearchSortDirectionName(); 095 } 096 } 097 } 098 099 result.setSearchSortDirectionChoices(searchControl.getSearchSortDirectionChoices(defaultSearchSortDirectionChoice, getPreferredLanguage(), allowNullChoice)); 100 } 101 } else { 102 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 103 } 104 105 return result; 106 } 107 108}