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.GetInventoryLocationGroupCapacitiesForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.InventoryControl;
022import com.echothree.model.control.warehouse.server.logic.WarehouseLogic;
023import com.echothree.model.data.inventory.server.entity.InventoryLocationGroup;
024import com.echothree.model.data.inventory.server.entity.InventoryLocationGroupCapacity;
025import com.echothree.model.data.inventory.server.factory.InventoryLocationGroupCapacityFactory;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
031import java.util.Collection;
032import java.util.List;
033import javax.enterprise.context.Dependent;
034import javax.inject.Inject;
035
036@Dependent
037public class GetInventoryLocationGroupCapacitiesCommand
038        extends BasePaginatedMultipleEntitiesCommand<InventoryLocationGroupCapacity, GetInventoryLocationGroupCapacitiesForm> {
039    
040    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
041    
042    static {
043        FORM_FIELD_DEFINITIONS = List.of(
044                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null),
045                new FieldDefinition("InventoryLocationGroupName", FieldType.ENTITY_NAME, true, null, null)
046        );
047    }
048    
049    /** Creates a new instance of GetInventoryLocationGroupCapacitiesCommand */
050    public GetInventoryLocationGroupCapacitiesCommand() {
051        super(null, FORM_FIELD_DEFINITIONS, true);
052    }
053
054    @Inject
055    InventoryControl inventoryControl;
056
057    @Inject
058    WarehouseLogic warehouseLogic;
059
060    private InventoryLocationGroup inventoryLocationGroup;
061
062    @Override
063    protected void handleForm() {
064        var warehouse = warehouseLogic.getWarehouseByName(this, form.getWarehouseName(), null, null, false);
065
066        if(!hasExecutionErrors()) {
067            var inventoryLocationGroupName = form.getInventoryLocationGroupName();
068
069            inventoryLocationGroup = inventoryControl.getInventoryLocationGroupByName(warehouse.getParty(), inventoryLocationGroupName);
070
071            if(inventoryLocationGroup == null) {
072                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), warehouse.getWarehouseName(), inventoryLocationGroupName);
073            }
074        }
075    }
076
077    @Override
078    protected Long getTotalEntities() {
079        return hasExecutionErrors() ? null : inventoryControl.countInventoryLocationGroupCapacitiesByInventoryLocationGroup(inventoryLocationGroup);
080    }
081
082    @Override
083    protected Collection<InventoryLocationGroupCapacity> getEntities() {
084        return hasExecutionErrors() ? null : inventoryControl.getInventoryLocationGroupCapacitiesByInventoryLocationGroup(inventoryLocationGroup);
085    }
086
087    @Override
088    protected BaseResult getResult(Collection<InventoryLocationGroupCapacity> entities) {
089        var result = InventoryResultFactory.getGetInventoryLocationGroupCapacitiesResult();
090
091        if(entities != null) {
092            var userVisit = getUserVisit();
093
094            result.setInventoryLocationGroup(inventoryControl.getInventoryLocationGroupTransfer(userVisit, inventoryLocationGroup));
095
096            if(session.hasLimit(InventoryLocationGroupCapacityFactory.class)) {
097                result.setInventoryLocationGroupCapacityCount(getTotalEntities());
098            }
099
100            result.setInventoryLocationGroupCapacities(inventoryControl.getInventoryLocationGroupCapacityTransfersByInventoryLocationGroup(userVisit, inventoryLocationGroup));
101        }
102
103        return result;
104    }
105    
106}