001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.SearchWarehousesForm; 020import com.echothree.control.user.search.common.result.SearchResultFactory; 021import com.echothree.model.control.party.common.PartyTypes; 022import com.echothree.model.control.party.server.control.PartyControl; 023import com.echothree.model.control.search.common.SearchKinds; 024import com.echothree.model.control.search.server.control.SearchControl; 025import com.echothree.model.control.search.server.logic.SearchLogic; 026import com.echothree.model.control.security.common.SecurityRoleGroups; 027import com.echothree.model.control.security.common.SecurityRoles; 028import com.echothree.model.control.warehouse.server.search.WarehouseSearchEvaluator; 029import com.echothree.model.data.party.server.entity.PartyAliasType; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.command.BaseResult; 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.server.control.BaseSimpleCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.control.SecurityRoleDefinition; 039import com.echothree.util.server.persistence.Session; 040import com.google.common.base.Splitter; 041import java.util.List; 042import javax.enterprise.context.Dependent; 043 044@Dependent 045public class SearchWarehousesCommand 046 extends BaseSimpleCommand<SearchWarehousesForm> { 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(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 055 new SecurityRoleDefinition(SecurityRoleGroups.Warehouse.name(), SecurityRoles.Search.name()) 056 )) 057 )); 058 059 FORM_FIELD_DEFINITIONS = List.of( 060 new FieldDefinition("SearchTypeName", FieldType.ENTITY_NAME, true, null, null), 061 new FieldDefinition("Q", FieldType.STRING, false, null, null), 062 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("PartyAliasTypeName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("Alias", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("CreatedSince", FieldType.DATE_TIME, false, null, null), 067 new FieldDefinition("ModifiedSince", FieldType.DATE_TIME, false, null, null), 068 new FieldDefinition("Fields", FieldType.STRING, false, null, null) 069 ); 070 } 071 072 /** Creates a new instance of SearchWarehousesCommand */ 073 public SearchWarehousesCommand() { 074 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 075 } 076 077 @Override 078 protected BaseResult execute() { 079 var result = SearchResultFactory.getSearchWarehousesResult(); 080 var searchControl = Session.getModelController(SearchControl.class); 081 var searchKind = searchControl.getSearchKindByName(SearchKinds.WAREHOUSE.name()); 082 083 if(searchKind != null) { 084 var searchTypeName = form.getSearchTypeName(); 085 var searchType = searchControl.getSearchTypeByName(searchKind, searchTypeName); 086 087 if(searchType != null) { 088 var partyAliasTypeName = form.getPartyAliasTypeName(); 089 var alias = form.getAlias(); 090 PartyAliasType partyAliasType = null; 091 092 if(partyAliasTypeName != null) { 093 var partyControl = Session.getModelController(PartyControl.class); 094 var partyType = partyControl.getPartyTypeByName(PartyTypes.WAREHOUSE.name()); 095 096 if(partyType != null) { 097 partyAliasType = partyControl.getPartyAliasTypeByName(partyType, partyAliasTypeName); 098 099 if(partyAliasType == null) { 100 addExecutionError(ExecutionErrors.UnknownPartyAliasTypeName.name(), PartyTypes.WAREHOUSE.name(), partyAliasTypeName); 101 } 102 } else { 103 addExecutionError(ExecutionErrors.UnknownPartyTypeName.name(), PartyTypes.WAREHOUSE.name()); 104 } 105 } 106 107 if(!hasExecutionErrors()) { 108 var searchLogic = SearchLogic.getInstance(); 109 var userVisit = getUserVisit(); 110 var warehouseSearchEvaluator = new WarehouseSearchEvaluator(userVisit, searchType, 111 searchLogic.getDefaultSearchDefaultOperator(null), searchLogic.getDefaultSearchSortOrder(null, searchKind), 112 searchLogic.getDefaultSearchSortDirection(null)); 113 var createdSince = form.getCreatedSince(); 114 var modifiedSince = form.getModifiedSince(); 115 var fields = form.getFields(); 116 117 warehouseSearchEvaluator.setQ(this, form.getQ()); 118 warehouseSearchEvaluator.setPartyAliasType(partyAliasType); 119 warehouseSearchEvaluator.setAlias(alias); 120 warehouseSearchEvaluator.setWarehouseName(form.getWarehouseName()); 121 warehouseSearchEvaluator.setPartyName(form.getPartyName()); 122 warehouseSearchEvaluator.setCreatedSince(createdSince == null ? null : Long.valueOf(createdSince)); 123 warehouseSearchEvaluator.setModifiedSince(modifiedSince == null ? null : Long.valueOf(modifiedSince)); 124 warehouseSearchEvaluator.setFields(fields == null ? null : Splitter.on(':').trimResults().omitEmptyStrings().splitToList(fields).toArray(new String[0])); 125 126 if(!hasExecutionErrors()) { 127 result.setCount(warehouseSearchEvaluator.execute(this)); 128 } 129 } 130 } else { 131 addExecutionError(ExecutionErrors.UnknownSearchTypeName.name(), SearchKinds.WAREHOUSE.name(), searchTypeName); 132 } 133 } else { 134 addExecutionError(ExecutionErrors.UnknownSearchKindName.name(), SearchKinds.WAREHOUSE.name()); 135 } 136 137 return result; 138 } 139}