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.GetInventoryCostingPoolsForm; 020import com.echothree.control.user.inventory.common.result.InventoryResultFactory; 021import com.echothree.model.control.inventory.server.control.InventoryCostingPoolControl; 022import com.echothree.model.control.inventory.server.control.InventoryConditionControl; 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.server.logic.CompanyLogic; 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.data.inventory.server.entity.InventoryCondition; 032import com.echothree.model.data.inventory.server.entity.InventoryCostingPool; 033import com.echothree.model.data.inventory.server.factory.InventoryCostingPoolFactory; 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 com.echothree.util.server.persistence.BaseEntity; 045import java.util.Collection; 046import java.util.List; 047import javax.enterprise.context.Dependent; 048import javax.inject.Inject; 049 050@Dependent 051public class GetInventoryCostingPoolsCommand 052 extends BasePaginatedMultipleEntitiesCommand<InventoryCostingPool, GetInventoryCostingPoolsForm> { 053 054 private static final CommandSecurityDefinition COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 055 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 056 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 057 new SecurityRoleDefinition(SecurityRoleGroups.InventoryCostingPool.name(), SecurityRoles.List.name()) 058 )) 059 )); 060 061 private static final List<FieldDefinition> FORM_FIELD_DEFINITIONS = List.of( 062 new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null), 064 new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null) 066 ); 067 068 @Inject 069 InventoryConditionControl inventoryConditionControl; 070 071 @Inject 072 InventoryCostingPoolControl inventoryCostingPoolControl; 073 074 @Inject 075 ItemControl itemControl; 076 077 @Inject 078 PartyControl partyControl; 079 080 @Inject 081 CompanyLogic companyLogic; 082 083 @Inject 084 InventoryConditionLogic inventoryConditionLogic; 085 086 @Inject 087 ItemLogic itemLogic; 088 089 private BaseEntity filter; 090 091 public GetInventoryCostingPoolsCommand() { 092 super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true); 093 } 094 095 @Override 096 protected void handleForm() { 097 var companyName = form.getCompanyName(); 098 var partyName = form.getPartyName(); 099 var itemName = form.getItemName(); 100 var inventoryConditionName = form.getInventoryConditionName(); 101 var parameterCount = (companyName == null ? 0 : 1) + (partyName == null ? 0 : 1) 102 + (itemName == null ? 0 : 1) + (inventoryConditionName == null ? 0 : 1); 103 104 if(parameterCount == 1) { 105 if(itemName != null) { 106 filter = itemLogic.getItemByName(this, itemName); 107 } else if(inventoryConditionName != null) { 108 filter = inventoryConditionLogic.getInventoryConditionByName(this, inventoryConditionName); 109 } else { 110 var company = companyLogic.getPartyCompanyByName(this, companyName, partyName, null, true); 111 112 if(!hasExecutionErrors()) { 113 filter = company.getParty(); 114 } 115 } 116 } else { 117 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 118 } 119 } 120 121 @Override 122 protected Long getTotalEntities() { 123 if(hasExecutionErrors()) { 124 return null; 125 } 126 127 return switch(filter) { 128 case Party companyParty -> inventoryCostingPoolControl.countInventoryCostingPoolsByCompanyParty(companyParty); 129 case Item item -> inventoryCostingPoolControl.countInventoryCostingPoolsByItem(item); 130 case InventoryCondition inventoryCondition -> 131 inventoryCostingPoolControl.countInventoryCostingPoolsByInventoryCondition(inventoryCondition); 132 default -> throw new IllegalStateException("Unexpected inventory costing pool filter: " + filter); 133 }; 134 } 135 136 @Override 137 protected Collection<InventoryCostingPool> getEntities() { 138 if(hasExecutionErrors()) { 139 return null; 140 } 141 142 return switch(filter) { 143 case Party companyParty -> inventoryCostingPoolControl.getInventoryCostingPoolsByCompanyParty(companyParty); 144 case Item item -> inventoryCostingPoolControl.getInventoryCostingPoolsByItem(item); 145 case InventoryCondition inventoryCondition -> 146 inventoryCostingPoolControl.getInventoryCostingPoolsByInventoryCondition(inventoryCondition); 147 default -> throw new IllegalStateException("Unexpected inventory costing pool filter: " + filter); 148 }; 149 } 150 151 @Override 152 protected BaseResult getResult(Collection<InventoryCostingPool> entities) { 153 var result = InventoryResultFactory.getGetInventoryCostingPoolsResult(); 154 155 if(entities != null) { 156 var userVisit = getUserVisit(); 157 158 switch(filter) { 159 case Party companyParty -> result.setCompany(partyControl.getCompanyTransfer(userVisit, companyParty)); 160 case Item item -> result.setItem(itemControl.getItemTransfer(userVisit, item)); 161 case InventoryCondition inventoryCondition -> 162 result.setInventoryCondition(inventoryConditionControl.getInventoryConditionTransfer(userVisit, inventoryCondition)); 163 default -> throw new IllegalStateException("Unexpected inventory costing pool filter: " + filter); 164 } 165 166 if(session.hasLimit(InventoryCostingPoolFactory.class)) { 167 result.setInventoryCostingPoolCount(getTotalEntities()); 168 } 169 170 result.setInventoryCostingPools(inventoryCostingPoolControl.getInventoryCostingPoolTransfers(userVisit, entities)); 171 } 172 173 return result; 174 } 175 176}