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.GetPartyInventoryLevelsForm; 020import com.echothree.control.user.inventory.common.result.InventoryResultFactory; 021import com.echothree.control.user.inventory.server.command.common.PartyInventoryLevelUtil; 022import com.echothree.model.control.inventory.server.control.InventoryConditionControl; 023import com.echothree.model.control.inventory.server.control.InventoryLevelControl; 024import com.echothree.model.control.inventory.server.logic.InventoryConditionLogic; 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.warehouse.server.control.WarehouseControl; 032import com.echothree.model.data.inventory.server.entity.InventoryCondition; 033import com.echothree.model.data.inventory.server.entity.PartyInventoryLevel; 034import com.echothree.model.data.inventory.server.factory.PartyInventoryLevelFactory; 035import com.echothree.model.data.item.server.entity.Item; 036import com.echothree.model.data.party.server.entity.Party; 037import com.echothree.util.common.command.BaseResult; 038import com.echothree.util.common.message.ExecutionErrors; 039import com.echothree.util.common.validation.FieldDefinition; 040import com.echothree.util.common.validation.FieldType; 041import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand; 042import com.echothree.util.server.control.CommandSecurityDefinition; 043import com.echothree.util.server.control.PartyTypeDefinition; 044import com.echothree.util.server.control.SecurityRoleDefinition; 045import java.util.Collection; 046import java.util.List; 047import javax.enterprise.context.Dependent; 048import javax.inject.Inject; 049 050@Dependent 051public class GetPartyInventoryLevelsCommand 052 extends BasePaginatedMultipleEntitiesCommand<PartyInventoryLevel, GetPartyInventoryLevelsForm> { 053 054 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 055 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 056 057 static { 058 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 059 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 060 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 061 new SecurityRoleDefinition(SecurityRoleGroups.PartyInventoryLevel.name(), SecurityRoles.List.name()) 062 )) 063 )); 064 065 FORM_FIELD_DEFINITIONS = List.of( 066 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 067 new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null), 068 new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null), 069 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null), 070 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null) 071 ); 072 } 073 074 @Inject 075 InventoryConditionControl inventoryConditionControl; 076 077 @Inject 078 InventoryLevelControl inventoryLevelControl; 079 080 @Inject 081 ItemControl itemControl; 082 083 @Inject 084 PartyControl partyControl; 085 086 @Inject 087 WarehouseControl warehouseControl; 088 089 @Inject 090 InventoryConditionLogic inventoryConditionLogic; 091 092 @Inject 093 ItemLogic itemLogic; 094 095 @Inject 096 PartyInventoryLevelUtil partyInventoryLevelUtil; 097 098 /** Creates a new instance of GetPartyInventoryLevelsCommand */ 099 public GetPartyInventoryLevelsCommand() { 100 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 101 } 102 103 private Item item; 104 private InventoryCondition inventoryCondition; 105 private Party party; 106 107 @Override 108 protected void handleForm() { 109 var partyName = form.getPartyName(); 110 var companyName = form.getCompanyName(); 111 var warehouseName = form.getWarehouseName(); 112 var itemName = form.getItemName(); 113 var inventoryConditionName = form.getInventoryConditionName(); 114 var parameterCount = (partyName == null ? 0 : 1) + (companyName == null ? 0 : 1) + (warehouseName == null ? 0 : 1) + 115 (itemName == null ? 0 : 1) + (inventoryConditionName == null ? 0 : 1); 116 117 if(parameterCount == 1) { 118 if(itemName != null) { 119 item = itemLogic.getItemByName(this, itemName); 120 } else if(inventoryConditionName != null) { 121 inventoryCondition = inventoryConditionLogic.getInventoryConditionByName(this, inventoryConditionName); 122 } else { 123 party = partyInventoryLevelUtil.getParty(this, partyName, companyName, warehouseName); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 127 } 128 } 129 130 @Override 131 protected Long getTotalEntities() { 132 Long total = null; 133 134 if(!hasExecutionErrors()) { 135 if(item != null) { 136 total = inventoryLevelControl.countPartyInventoryLevelsByItem(item); 137 } else if(inventoryCondition != null) { 138 total = inventoryLevelControl.countPartyInventoryLevelsByInventoryCondition(inventoryCondition); 139 } else if(party != null) { 140 total = inventoryLevelControl.countPartyInventoryLevelsByParty(party); 141 } 142 } 143 144 return total; 145 } 146 147 @Override 148 protected Collection<PartyInventoryLevel> getEntities() { 149 Collection<PartyInventoryLevel> entities = null; 150 151 if(!hasExecutionErrors()) { 152 if(item != null) { 153 entities = inventoryLevelControl.getPartyInventoryLevelsByItem(item); 154 } else if(inventoryCondition != null) { 155 entities = inventoryLevelControl.getPartyInventoryLevelsByInventoryCondition(inventoryCondition); 156 } else if(party != null) { 157 entities = inventoryLevelControl.getPartyInventoryLevelsByParty(party); 158 } 159 } 160 161 return entities; 162 } 163 164 @Override 165 protected BaseResult getResult(Collection<PartyInventoryLevel> entities) { 166 var result = InventoryResultFactory.getGetPartyInventoryLevelsResult(); 167 168 if(entities != null) { 169 var userVisit = getUserVisit(); 170 171 if(item != null) { 172 result.setItem(itemControl.getItemTransfer(userVisit, item)); 173 } else if(inventoryCondition != null) { 174 result.setInventoryCondition(inventoryConditionControl.getInventoryConditionTransfer(userVisit, inventoryCondition)); 175 } else if(party != null) { 176 var partyType = PartyTypes.valueOf(partyInventoryLevelUtil.getPartyTypeName(party)); 177 178 switch(partyType) { 179 case COMPANY -> result.setCompany(partyControl.getCompanyTransfer(userVisit, party)); 180 case WAREHOUSE -> result.setWarehouse(warehouseControl.getWarehouseTransfer(userVisit, party)); 181 default -> {} 182 } 183 } 184 185 if(session.hasLimit(PartyInventoryLevelFactory.class)) { 186 result.setPartyInventoryLevelCount(getTotalEntities()); 187 } 188 189 result.setPartyInventoryLevels(inventoryLevelControl.getPartyInventoryLevelTransfers(userVisit, entities)); 190 } 191 192 return result; 193 } 194 195}