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.GetInventoryLocationBucketsForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.BucketControl;
022import com.echothree.model.control.inventory.server.control.InventoryBucketTypeControl;
023import com.echothree.model.control.inventory.server.control.InventoryLocationControl;
024import com.echothree.model.control.inventory.server.logic.InventoryBucketTypeLogic;
025import com.echothree.model.control.inventory.server.logic.InventoryLocationLogic;
026import com.echothree.model.control.item.server.logic.ItemLogic;
027import com.echothree.model.control.party.common.PartyTypes;
028import com.echothree.model.control.security.common.SecurityRoleGroups;
029import com.echothree.model.control.security.common.SecurityRoles;
030import com.echothree.model.control.warehouse.server.logic.LocationLogic;
031import com.echothree.model.data.inventory.server.entity.InventoryBucketType;
032import com.echothree.model.data.inventory.server.entity.InventoryLocation;
033import com.echothree.model.data.inventory.server.entity.InventoryLocationBucket;
034import com.echothree.model.data.inventory.server.factory.InventoryLocationBucketFactory;
035import com.echothree.util.common.command.BaseResult;
036import com.echothree.util.common.message.ExecutionErrors;
037import com.echothree.util.common.validation.FieldDefinition;
038import com.echothree.util.common.validation.FieldType;
039import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
040import com.echothree.util.server.control.CommandSecurityDefinition;
041import com.echothree.util.server.control.PartyTypeDefinition;
042import com.echothree.util.server.control.SecurityRoleDefinition;
043import java.util.Collection;
044import java.util.List;
045import javax.enterprise.context.Dependent;
046import javax.inject.Inject;
047
048@Dependent
049public class GetInventoryLocationBucketsCommand
050        extends BasePaginatedMultipleEntitiesCommand<InventoryLocationBucket, GetInventoryLocationBucketsForm> {
051
052    private static final CommandSecurityDefinition COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053            new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054            new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                    new SecurityRoleDefinition(SecurityRoleGroups.InventoryLocationBucket.name(), SecurityRoles.List.name())
056            ))
057    ));
058
059    private static final List<FieldDefinition> FORM_FIELD_DEFINITIONS = List.of(
060            new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
061            new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null),
062            new FieldDefinition("LocationName", FieldType.ENTITY_NAME, false, null, null),
063            new FieldDefinition("ItemName", FieldType.ENTITY_NAME, false, null, null),
064            new FieldDefinition("InventoryBucketTypeName", FieldType.ENTITY_NAME, false, null, null)
065    );
066
067    @Inject
068    BucketControl bucketControl;
069
070    @Inject
071    InventoryLocationControl inventoryLocationControl;
072
073    @Inject
074    InventoryBucketTypeControl inventoryBucketTypeControl;
075
076    @Inject
077    LocationLogic locationLogic;
078
079    @Inject
080    ItemLogic itemLogic;
081
082    @Inject
083    InventoryLocationLogic inventoryLocationLogic;
084
085    @Inject
086    InventoryBucketTypeLogic inventoryBucketTypeLogic;
087
088    private InventoryLocation inventoryLocation;
089    private InventoryBucketType inventoryBucketType;
090
091    public GetInventoryLocationBucketsCommand() {
092        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
093    }
094
095    @Override
096    protected void handleForm() {
097        var partyName = form.getPartyName();
098        var warehouseName = form.getWarehouseName();
099        var locationName = form.getLocationName();
100        var itemName = form.getItemName();
101        var inventoryBucketTypeName = form.getInventoryBucketTypeName();
102        var warehouseParameterCount = (partyName == null ? 0 : 1) + (warehouseName == null ? 0 : 1);
103        var hasAnyInventoryLocationParameter = warehouseParameterCount != 0 || locationName != null || itemName != null;
104        var hasCompleteInventoryLocation = warehouseParameterCount == 1 && locationName != null && itemName != null;
105        var selectorCount = (hasCompleteInventoryLocation ? 1 : 0) + (inventoryBucketTypeName == null ? 0 : 1);
106
107        if((hasAnyInventoryLocationParameter && !hasCompleteInventoryLocation) || selectorCount != 1) {
108            addExecutionError(ExecutionErrors.InvalidParameterCount.name());
109        } else if(hasCompleteInventoryLocation) {
110            var location = locationLogic.getLocation(this, partyName, warehouseName, locationName);
111            var item = itemLogic.getItemByName(this, itemName);
112
113            if(!hasExecutionErrors()) {
114                inventoryLocation = inventoryLocationLogic.getInventoryLocation(this, location, item);
115            }
116        } else {
117            inventoryBucketType = inventoryBucketTypeLogic.getInventoryBucketTypeByName(this,
118                    inventoryBucketTypeName);
119        }
120    }
121
122    @Override
123    protected Long getTotalEntities() {
124        return hasExecutionErrors() ? null : inventoryLocation != null
125                ? bucketControl.countInventoryLocationBucketsByInventoryLocation(inventoryLocation)
126                : bucketControl.countInventoryLocationBucketsByInventoryBucketType(inventoryBucketType);
127    }
128
129    @Override
130    protected Collection<InventoryLocationBucket> getEntities() {
131        return hasExecutionErrors() ? null : inventoryLocation != null
132                ? bucketControl.getInventoryLocationBucketsByInventoryLocation(inventoryLocation)
133                : bucketControl.getInventoryLocationBucketsByInventoryBucketType(inventoryBucketType);
134    }
135
136    @Override
137    protected BaseResult getResult(Collection<InventoryLocationBucket> entities) {
138        var result = InventoryResultFactory.getGetInventoryLocationBucketsResult();
139
140        if(entities != null) {
141            var userVisit = getUserVisit();
142
143            if(inventoryLocation != null) {
144                result.setInventoryLocation(inventoryLocationControl.getInventoryLocationTransfer(userVisit,
145                        inventoryLocation));
146            } else {
147                result.setInventoryBucketType(inventoryBucketTypeControl.getInventoryBucketTypeTransfer(userVisit,
148                        inventoryBucketType));
149            }
150
151            if(session.hasLimit(InventoryLocationBucketFactory.class)) {
152                result.setInventoryLocationBucketCount(getTotalEntities());
153            }
154
155            result.setInventoryLocationBuckets(bucketControl.getInventoryLocationBucketTransfers(userVisit, entities));
156        }
157
158        return result;
159    }
160
161}