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.inventory.server.command; 018 019import com.echothree.control.user.inventory.common.form.GetInventoryLocationsForm; 020import com.echothree.control.user.inventory.common.result.InventoryResultFactory; 021import com.echothree.model.control.inventory.server.control.InventoryConditionControl; 022import com.echothree.model.control.inventory.server.control.InventoryLocationControl; 023import com.echothree.model.control.inventory.server.logic.InventoryConditionLogic; 024import com.echothree.model.control.inventory.server.logic.InventoryLocationLogic; 025import com.echothree.model.control.item.server.control.ItemControl; 026import com.echothree.model.control.item.server.logic.ItemLogic; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.party.server.control.PartyControl; 029import com.echothree.model.control.security.common.SecurityRoleGroups; 030import com.echothree.model.control.security.common.SecurityRoles; 031import com.echothree.model.control.uom.server.control.UomControl; 032import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic; 033import com.echothree.model.control.warehouse.server.control.WarehouseControl; 034import com.echothree.model.control.warehouse.server.logic.LocationLogic; 035import com.echothree.model.data.inventory.server.entity.InventoryCondition; 036import com.echothree.model.data.inventory.server.entity.InventoryLocation; 037import com.echothree.model.data.inventory.server.factory.InventoryLocationFactory; 038import com.echothree.model.data.item.server.entity.Item; 039import com.echothree.model.data.party.server.entity.Party; 040import com.echothree.model.data.uom.server.entity.UnitOfMeasureType; 041import com.echothree.model.data.warehouse.server.entity.Location; 042import com.echothree.util.common.command.BaseResult; 043import com.echothree.util.common.message.ExecutionErrors; 044import com.echothree.util.common.validation.FieldDefinition; 045import com.echothree.util.common.validation.FieldType; 046import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 047import com.echothree.util.server.control.CommandSecurityDefinition; 048import com.echothree.util.server.control.PartyTypeDefinition; 049import com.echothree.util.server.control.SecurityRoleDefinition; 050import java.util.Collection; 051import java.util.List; 052import javax.enterprise.context.Dependent; 053import javax.inject.Inject; 054 055@Dependent 056public class GetInventoryLocationsCommand 057 extends BasePaginatedMultipleEntitiesCommand<InventoryLocation, GetInventoryLocationsForm> { 058 059 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 060 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 061 062 static { 063 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 064 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 065 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 066 new SecurityRoleDefinition(SecurityRoleGroups.InventoryLocation.name(), SecurityRoles.List.name()) 067 )) 068 )); 069 070 FORM_FIELD_DEFINITIONS = List.of( 071 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 072 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("LocationName", FieldType.ENTITY_NAME, false, null, null), 074 new FieldDefinition("OwnerPartyName", FieldType.ENTITY_NAME, false, null, null), 075 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null), 076 new FieldDefinition("UnitOfMeasureKindName", FieldType.ENTITY_NAME, false, null, null), 077 new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null), 078 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null) 079 ); 080 } 081 082 @Inject 083 InventoryConditionControl inventoryConditionControl; 084 085 @Inject 086 InventoryLocationControl inventoryLocationControl; 087 088 @Inject 089 ItemControl itemControl; 090 091 @Inject 092 PartyControl partyControl; 093 094 @Inject 095 UomControl uomControl; 096 097 @Inject 098 WarehouseControl warehouseControl; 099 100 @Inject 101 InventoryConditionLogic inventoryConditionLogic; 102 103 @Inject 104 InventoryLocationLogic inventoryLocationLogic; 105 106 @Inject 107 ItemLogic itemLogic; 108 109 @Inject 110 LocationLogic locationLogic; 111 112 @Inject 113 UnitOfMeasureTypeLogic unitOfMeasureTypeLogic; 114 115 /** Creates a new instance of GetInventoryLocationsCommand */ 116 public GetInventoryLocationsCommand() { 117 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 118 } 119 120 private Location location; 121 private Party ownerParty; 122 private Item item; 123 private UnitOfMeasureType unitOfMeasureType; 124 private InventoryCondition inventoryCondition; 125 126 @Override 127 protected void handleForm() { 128 var partyName = form.getPartyName(); 129 var warehouseName = form.getWarehouseName(); 130 var locationName = form.getLocationName(); 131 var ownerPartyName = form.getOwnerPartyName(); 132 var itemName = form.getItemName(); 133 var unitOfMeasureKindName = form.getUnitOfMeasureKindName(); 134 var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName(); 135 var inventoryConditionName = form.getInventoryConditionName(); 136 var warehouseParameterCount = (partyName == null ? 0 : 1) + (warehouseName == null ? 0 : 1); 137 var hasLocationParameters = warehouseParameterCount != 0 || locationName != null; 138 var hasCompleteLocationParameters = warehouseParameterCount == 1 && locationName != null; 139 var hasUnitOfMeasureParameters = unitOfMeasureKindName != null || unitOfMeasureTypeName != null; 140 var hasCompleteUnitOfMeasureParameters = unitOfMeasureKindName != null && unitOfMeasureTypeName != null; 141 var selectorCount = (hasCompleteLocationParameters ? 1 : 0) 142 + (ownerPartyName == null ? 0 : 1) 143 + (itemName == null ? 0 : 1) 144 + (hasCompleteUnitOfMeasureParameters ? 1 : 0) 145 + (inventoryConditionName == null ? 0 : 1); 146 147 if((hasLocationParameters && !hasCompleteLocationParameters) 148 || (hasUnitOfMeasureParameters && !hasCompleteUnitOfMeasureParameters) 149 || selectorCount != 1) { 150 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 151 } else if(hasCompleteLocationParameters) { 152 location = locationLogic.getLocation(this, partyName, warehouseName, locationName); 153 } else if(ownerPartyName != null) { 154 ownerParty = inventoryLocationLogic.getOwnerParty(this, ownerPartyName); 155 } else if(itemName != null) { 156 item = itemLogic.getItemByName(this, itemName); 157 } else if(hasCompleteUnitOfMeasureParameters) { 158 unitOfMeasureType = unitOfMeasureTypeLogic.getUnitOfMeasureTypeByName(this, unitOfMeasureKindName, unitOfMeasureTypeName); 159 } else { 160 inventoryCondition = inventoryConditionLogic.getInventoryConditionByName(this, inventoryConditionName); 161 } 162 } 163 164 @Override 165 protected Long getTotalEntities() { 166 Long total = null; 167 168 if(!hasExecutionErrors()) { 169 if(location != null) { 170 total = inventoryLocationControl.countInventoryLocationsByLocation(location); 171 } else if(ownerParty != null) { 172 total = inventoryLocationControl.countInventoryLocationsByOwnerParty(ownerParty); 173 } else if(unitOfMeasureType != null) { 174 total = inventoryLocationControl.countInventoryLocationsByUnitOfMeasureType(unitOfMeasureType); 175 } else if(item != null) { 176 total = inventoryLocationControl.countInventoryLocationsByItem(item); 177 } else if(inventoryCondition != null) { 178 total = inventoryLocationControl.countInventoryLocationsByInventoryCondition(inventoryCondition); 179 } 180 } 181 182 return total; 183 } 184 185 @Override 186 protected Collection<InventoryLocation> getEntities() { 187 Collection<InventoryLocation> entities = null; 188 189 if(!hasExecutionErrors()) { 190 if(location != null) { 191 entities = inventoryLocationControl.getInventoryLocationsByLocation(location); 192 } else if(ownerParty != null) { 193 entities = inventoryLocationControl.getInventoryLocationsByOwnerParty(ownerParty); 194 } else if(unitOfMeasureType != null) { 195 entities = inventoryLocationControl.getInventoryLocationsByUnitOfMeasureType(unitOfMeasureType); 196 } else if(item != null) { 197 entities = inventoryLocationControl.getInventoryLocationsByItem(item); 198 } else if(inventoryCondition != null) { 199 entities = inventoryLocationControl.getInventoryLocationsByInventoryCondition(inventoryCondition); 200 } 201 } 202 203 return entities; 204 } 205 206 @Override 207 protected BaseResult getResult(Collection<InventoryLocation> entities) { 208 var result = InventoryResultFactory.getGetInventoryLocationsResult(); 209 210 if(entities != null) { 211 var userVisit = getUserVisit(); 212 213 if(location != null) { 214 result.setLocation(warehouseControl.getLocationTransfer(userVisit, location)); 215 } else if(ownerParty != null) { 216 result.setOwnerParty(partyControl.getPartyTransfer(userVisit, ownerParty)); 217 } else if(unitOfMeasureType != null) { 218 result.setUnitOfMeasureType(uomControl.getUnitOfMeasureTypeTransfer(userVisit, unitOfMeasureType)); 219 } else if(item != null) { 220 result.setItem(itemControl.getItemTransfer(userVisit, item)); 221 } else if(inventoryCondition != null) { 222 result.setInventoryCondition(inventoryConditionControl.getInventoryConditionTransfer(userVisit, inventoryCondition)); 223 } 224 225 if(session.hasLimit(InventoryLocationFactory.class)) { 226 result.setInventoryLocationCount(getTotalEntities()); 227 } 228 229 result.setInventoryLocations(inventoryLocationControl.getInventoryLocationTransfers(userVisit, entities)); 230 } 231 232 return result; 233 } 234 235}