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.BaseSearchForm; 020import com.echothree.model.control.search.server.control.SearchControl; 021import com.echothree.model.control.search.server.logic.SearchLogic; 022import com.echothree.model.data.party.server.entity.Party; 023import com.echothree.model.data.search.server.entity.PartySearchTypePreference; 024import com.echothree.model.data.search.server.entity.PartySearchTypePreferenceDetail; 025import com.echothree.model.data.search.server.entity.SearchDefaultOperator; 026import com.echothree.model.data.search.server.entity.SearchKind; 027import com.echothree.model.data.search.server.entity.SearchSortDirection; 028import com.echothree.model.data.search.server.entity.SearchSortOrder; 029import com.echothree.model.data.search.server.entity.SearchType; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.server.control.BaseSimpleCommand; 033import com.echothree.util.server.control.CommandSecurityDefinition; 034import java.util.List; 035 036public abstract class BaseSearchCommand<F extends BaseSearchForm, R extends BaseResult> 037 extends BaseSimpleCommand<F> { 038 039 protected BaseSearchCommand(CommandSecurityDefinition COMMAND_SECURITY_DEFINITION, 040 List<FieldDefinition> FORM_FIELD_DEFINITIONS, boolean allowLimits) { 041 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, allowLimits); 042 } 043 044 protected BaseSearchCommand(CommandSecurityDefinition COMMAND_SECURITY_DEFINITION, boolean allowLimits) { 045 super(COMMAND_SECURITY_DEFINITION, allowLimits); 046 } 047 048 protected PartySearchTypePreference getPartySearchTypePreference(SearchControl searchControl, SearchType searchType) { 049 var party = getParty(); 050 051 return party == null ? null : searchControl.getPartySearchTypePreference(party, searchType); 052 } 053 054 protected SearchDefaultOperator getDefaultSearchDefaultOperator(SearchLogic searchLogic, boolean rememberPreferences, 055 PartySearchTypePreferenceDetail partySearchTypePreferenceDetail) { 056 var searchDefaultOperator = partySearchTypePreferenceDetail == null || rememberPreferences ? null 057 : partySearchTypePreferenceDetail.getSearchDefaultOperator(); 058 059 if(searchDefaultOperator == null) { 060 searchDefaultOperator = searchLogic.getDefaultSearchDefaultOperator(this); 061 } 062 063 return searchDefaultOperator; 064 } 065 066 protected SearchSortOrder getDefaultSearchSortOrder(SearchLogic searchLogic, boolean rememberPreferences, SearchKind searchKind, 067 PartySearchTypePreferenceDetail partySearchTypePreferenceDetail) { 068 var searchSortOrder = partySearchTypePreferenceDetail == null || rememberPreferences ? null 069 : partySearchTypePreferenceDetail.getSearchSortOrder(); 070 071 if(searchSortOrder == null) { 072 searchSortOrder = searchLogic.getDefaultSearchSortOrder(this, searchKind); 073 } 074 075 return searchSortOrder; 076 } 077 078 protected SearchSortDirection getDefaultSearchSortDirection(SearchLogic searchLogic, boolean rememberPreferences, 079 PartySearchTypePreferenceDetail partySearchTypePreferenceDetail) { 080 var searchSortDirection = partySearchTypePreferenceDetail == null || rememberPreferences ? null 081 : partySearchTypePreferenceDetail.getSearchSortDirection(); 082 083 if(searchSortDirection == null) { 084 searchSortDirection = searchLogic.getDefaultSearchSortDirection(this); 085 } 086 087 return searchSortDirection; 088 } 089 090 protected void updatePartySearchTypePreferences(SearchControl searchControl, SearchType searchType, PartySearchTypePreference partySearchTypePreference, 091 SearchDefaultOperator searchDefaultOperator, SearchSortOrder searchSortOrder, SearchSortDirection searchSortDirection, Party party) { 092 093 searchDefaultOperator = form.getSearchDefaultOperatorName() == null ? null : searchDefaultOperator; 094 searchSortOrder = form.getSearchSortOrderName() == null ? null : searchSortOrder; 095 searchSortDirection = form.getSearchSortDirectionName() == null ? null : searchSortDirection; 096 097 if(partySearchTypePreference == null) { 098 searchControl.createPartySearchTypePreference(party, searchType, searchDefaultOperator, searchSortOrder, searchSortDirection, party.getPrimaryKey()); 099 } else { 100 var partySearchTypePreferenceDetailValue = searchControl.getPartySearchTypePreferenceDetailValueForUpdate(partySearchTypePreference); 101 102 // Each of the following first check to see if we've fallen back onto the default for each, and will clear 103 // the value if we have. Otherwise, it'll use the new value that the user has selected. 104 partySearchTypePreferenceDetailValue.setSearchDefaultOperatorPK(searchDefaultOperator == null ? null : searchDefaultOperator.getPrimaryKey()); 105 partySearchTypePreferenceDetailValue.setSearchSortOrderPK(searchSortOrder == null ? null : searchSortOrder.getPrimaryKey()); 106 partySearchTypePreferenceDetailValue.setSearchSortDirectionPK(searchSortDirection == null ? null : searchSortDirection.getPrimaryKey()); 107 108 searchControl.updatePartySearchTypePreferenceFromValue(partySearchTypePreferenceDetailValue, party.getPrimaryKey()); 109 } 110 } 111 112}