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