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