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