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.GetEntityListItemResultsForm; 020import com.echothree.control.user.search.common.result.SearchResultFactory; 021import com.echothree.model.control.core.server.control.EntityListItemControl; 022import com.echothree.model.control.search.common.SearchKinds; 023import com.echothree.model.control.search.server.control.SearchControl; 024import com.echothree.model.control.search.server.logic.SearchLogic; 025import com.echothree.model.data.user.common.pk.UserVisitPK; 026import com.echothree.util.common.command.BaseResult; 027import com.echothree.util.common.message.ExecutionErrors; 028import com.echothree.util.common.validation.FieldDefinition; 029import com.echothree.util.common.validation.FieldType; 030import com.echothree.util.server.control.BaseSimpleCommand; 031import com.echothree.util.server.persistence.Session; 032import java.util.List; 033import javax.enterprise.context.RequestScoped; 034 035@RequestScoped 036public class GetEntityListItemResultsCommand 037 extends BaseSimpleCommand<GetEntityListItemResultsForm> { 038 039 // No COMMAND_SECURITY_DEFINITION, anyone may execute this command. 040 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 041 042 static { 043 FORM_FIELD_DEFINITIONS = List.of( 044 new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null) 045 ); 046 } 047 048 /** Creates a new instance of GetEntityListItemResultsCommand */ 049 public GetEntityListItemResultsCommand() { 050 super(null, FORM_FIELD_DEFINITIONS, true); 051 } 052 053 @Override 054 protected BaseResult execute() { 055 var result = SearchResultFactory.getGetEntityListItemResultsResult(); 056 var searchControl = Session.getModelController(SearchControl.class); 057 var searchKind = searchControl.getSearchKindByName(SearchKinds.ENTITY_LIST_ITEM.name()); 058 059 if(searchKind != null) { 060 var searchTypeName = form.getSearchTypeName(); 061 var searchType = searchControl.getSearchTypeByName(searchKind, searchTypeName); 062 063 if(searchType != null) { 064 var userVisit = getUserVisit(); 065 var userVisitSearch = searchControl.getUserVisitSearch(userVisit, searchType); 066 067 if(userVisitSearch != null) { 068 var entityListItemControl = Session.getModelController(EntityListItemControl.class); 069 070 if(session.hasLimit(com.echothree.model.data.search.server.factory.SearchResultFactory.class)) { 071 result.setEntityListItemResultCount(SearchLogic.getInstance().countSearchResults(userVisitSearch.getSearch())); 072 } 073 074 result.setEntityListItemResults(entityListItemControl.getEntityListItemResultTransfers(userVisit, userVisitSearch)); 075 } else { 076 addExecutionError(ExecutionErrors.UnknownUserVisitSearch.name()); 077 } 078 } else { 079 addExecutionError(ExecutionErrors.UnknownSearchTypeName.name(), SearchKinds.ENTITY_LIST_ITEM.name(), searchTypeName); 080 } 081 } else { 082 addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), SearchKinds.ENTITY_LIST_ITEM.name()); 083 } 084 085 return result; 086 } 087}