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