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.GetPartyBucketsForm;
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.BucketControl;
023import com.echothree.model.control.inventory.server.control.InventoryBucketTypeControl;
024import com.echothree.model.control.inventory.server.control.InventoryConditionControl;
025import com.echothree.model.control.inventory.server.logic.InventoryBucketTypeLogic;
026import com.echothree.model.control.inventory.server.logic.InventoryConditionLogic;
027import com.echothree.model.control.item.server.control.ItemControl;
028import com.echothree.model.control.item.server.logic.ItemLogic;
029import com.echothree.model.control.party.common.PartyTypes;
030import com.echothree.model.control.party.server.control.PartyControl;
031import com.echothree.model.control.security.common.SecurityRoleGroups;
032import com.echothree.model.control.security.common.SecurityRoles;
033import com.echothree.model.control.uom.server.control.UomControl;
034import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic;
035import com.echothree.model.data.inventory.server.entity.InventoryBucketType;
036import com.echothree.model.data.inventory.server.entity.InventoryCondition;
037import com.echothree.model.data.inventory.server.entity.PartyBucket;
038import com.echothree.model.data.inventory.server.factory.PartyBucketFactory;
039import com.echothree.model.data.item.server.entity.Item;
040import com.echothree.model.data.party.server.entity.Party;
041import com.echothree.model.data.uom.server.entity.UnitOfMeasureType;
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 GetPartyBucketsCommand
057        extends BasePaginatedMultipleEntitiesCommand<PartyBucket, GetPartyBucketsForm> {
058
059    private static final CommandSecurityDefinition COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
060            new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
061            new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
062                    new SecurityRoleDefinition(SecurityRoleGroups.PartyBucket.name(), SecurityRoles.List.name())
063            ))
064    ));
065
066    private static final List<FieldDefinition> FORM_FIELD_DEFINITIONS = List.of(
067            new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
068            new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
069            new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null),
070            new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null),
071            new FieldDefinition("UnitOfMeasureKindName", FieldType.ENTITY_NAME, false, null, null),
072            new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, false, null, null),
073            new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, false, null, null),
074            new FieldDefinition("InventoryBucketTypeName", FieldType.ENTITY_NAME, false, null, null)
075    );
076
077    @Inject
078    BucketControl bucketControl;
079
080    @Inject
081    PartyInventoryLevelUtil partyInventoryLevelUtil;
082
083    @Inject
084    ItemLogic itemLogic;
085
086    @Inject
087    UnitOfMeasureTypeLogic unitOfMeasureTypeLogic;
088
089    @Inject
090    InventoryConditionLogic inventoryConditionLogic;
091
092    @Inject
093    InventoryBucketTypeLogic inventoryBucketTypeLogic;
094
095    @Inject
096    PartyControl partyControl;
097
098    @Inject
099    ItemControl itemControl;
100
101    @Inject
102    UomControl uomControl;
103
104    @Inject
105    InventoryConditionControl inventoryConditionControl;
106
107    @Inject
108    InventoryBucketTypeControl inventoryBucketTypeControl;
109
110    private Party party;
111    private Item item;
112    private UnitOfMeasureType unitOfMeasureType;
113    private InventoryCondition inventoryCondition;
114    private InventoryBucketType inventoryBucketType;
115
116    public GetPartyBucketsCommand() {
117        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
118    }
119
120    @Override
121    protected void handleForm() {
122        var partyName = form.getPartyName();
123        var companyName = form.getCompanyName();
124        var warehouseName = form.getWarehouseName();
125        var itemName = form.getItemName();
126        var unitOfMeasureKindName = form.getUnitOfMeasureKindName();
127        var unitOfMeasureTypeName = form.getUnitOfMeasureTypeName();
128        var inventoryConditionName = form.getInventoryConditionName();
129        var inventoryBucketTypeName = form.getInventoryBucketTypeName();
130        var hasCompleteUnitOfMeasureType = unitOfMeasureKindName != null && unitOfMeasureTypeName != null;
131        var hasPartialUnitOfMeasureType = (unitOfMeasureKindName == null) != (unitOfMeasureTypeName == null);
132        var parameterCount = (partyName == null ? 0 : 1) + (companyName == null ? 0 : 1)
133                + (warehouseName == null ? 0 : 1) + (itemName == null ? 0 : 1)
134                + (hasCompleteUnitOfMeasureType ? 1 : 0) + (inventoryConditionName == null ? 0 : 1)
135                + (inventoryBucketTypeName == null ? 0 : 1);
136
137        if(parameterCount == 1 && !hasPartialUnitOfMeasureType) {
138            if(itemName != null) {
139                item = itemLogic.getItemByName(this, itemName);
140            } else if(hasCompleteUnitOfMeasureType) {
141                unitOfMeasureType = unitOfMeasureTypeLogic.getUnitOfMeasureTypeByName(this, unitOfMeasureKindName, unitOfMeasureTypeName);
142            } else if(inventoryConditionName != null) {
143                inventoryCondition = inventoryConditionLogic.getInventoryConditionByName(this, inventoryConditionName);
144            } else if(inventoryBucketTypeName != null) {
145                inventoryBucketType = inventoryBucketTypeLogic.getInventoryBucketTypeByName(this, inventoryBucketTypeName);
146            } else {
147                party = partyInventoryLevelUtil.getParty(this, partyName, companyName, warehouseName);
148            }
149        } else {
150            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
151        }
152    }
153
154    @Override
155    protected Long getTotalEntities() {
156        return hasExecutionErrors() ? null :
157                switch(party != null ? party : item != null ? item : unitOfMeasureType != null ? unitOfMeasureType
158                        : inventoryCondition != null ? inventoryCondition : inventoryBucketType) {
159                    case Party filterParty -> bucketControl.countPartyBucketsByParty(filterParty);
160                    case Item filterItem -> bucketControl.countPartyBucketsByItem(filterItem);
161                    case UnitOfMeasureType filterUnitOfMeasureType ->
162                            bucketControl.countPartyBucketsByUnitOfMeasureType(filterUnitOfMeasureType);
163                    case InventoryCondition filterInventoryCondition ->
164                            bucketControl.countPartyBucketsByInventoryCondition(filterInventoryCondition);
165                    case InventoryBucketType filterInventoryBucketType ->
166                            bucketControl.countPartyBucketsByInventoryBucketType(filterInventoryBucketType);
167                    default -> null;
168                };
169    }
170
171    @Override
172    protected Collection<PartyBucket> getEntities() {
173        return hasExecutionErrors() ? null :
174                switch(party != null ? party : item != null ? item : unitOfMeasureType != null ? unitOfMeasureType
175                        : inventoryCondition != null ? inventoryCondition : inventoryBucketType) {
176                    case Party filterParty -> bucketControl.getPartyBucketsByParty(filterParty);
177                    case Item filterItem -> bucketControl.getPartyBucketsByItem(filterItem);
178                    case UnitOfMeasureType filterUnitOfMeasureType ->
179                            bucketControl.getPartyBucketsByUnitOfMeasureType(filterUnitOfMeasureType);
180                    case InventoryCondition filterInventoryCondition ->
181                            bucketControl.getPartyBucketsByInventoryCondition(filterInventoryCondition);
182                    case InventoryBucketType filterInventoryBucketType ->
183                            bucketControl.getPartyBucketsByInventoryBucketType(filterInventoryBucketType);
184                    default -> null;
185                };
186    }
187
188    @Override
189    protected BaseResult getResult(Collection<PartyBucket> entities) {
190        var result = InventoryResultFactory.getGetPartyBucketsResult();
191
192        if(entities != null) {
193            var userVisit = getUserVisit();
194
195            if(party != null) {
196                result.setParty(partyControl.getPartyTransfer(userVisit, party));
197            } else if(item != null) {
198                result.setItem(itemControl.getItemTransfer(userVisit, item));
199            } else if(unitOfMeasureType != null) {
200                result.setUnitOfMeasureType(uomControl.getUnitOfMeasureTypeTransfer(userVisit, unitOfMeasureType));
201            } else if(inventoryCondition != null) {
202                result.setInventoryCondition(inventoryConditionControl.getInventoryConditionTransfer(userVisit, inventoryCondition));
203            } else {
204                result.setInventoryBucketType(inventoryBucketTypeControl.getInventoryBucketTypeTransfer(userVisit, inventoryBucketType));
205            }
206
207            if(session.hasLimit(PartyBucketFactory.class)) {
208                result.setPartyBucketCount(getTotalEntities());
209            }
210
211            result.setPartyBuckets(bucketControl.getPartyBucketTransfers(userVisit, entities));
212        }
213
214        return result;
215    }
216
217}