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.InventoryLocationGroupControl;
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    @Inject
050    InventoryLocationGroupControl inventoryLocationGroupControl;
051
052    @Inject
053    WarehouseLogic warehouseLogic;
054
055    /** Creates a new instance of GetInventoryLocationGroupCapacitiesCommand */
056    public GetInventoryLocationGroupCapacitiesCommand() {
057        super(null, FORM_FIELD_DEFINITIONS, true);
058    }
059    private InventoryLocationGroup inventoryLocationGroup;
060
061    @Override
062    protected void handleForm() {
063        var warehouse = warehouseLogic.getWarehouseByName(this, form.getWarehouseName(), null, null, false);
064
065        if(!hasExecutionErrors()) {
066            var inventoryLocationGroupName = form.getInventoryLocationGroupName();
067
068            inventoryLocationGroup = inventoryLocationGroupControl.getInventoryLocationGroupByName(warehouse.getParty(), inventoryLocationGroupName);
069
070            if(inventoryLocationGroup == null) {
071                addExecutionError(ExecutionErrors.UnknownInventoryLocationGroupName.name(), warehouse.getWarehouseName(), inventoryLocationGroupName);
072            }
073        }
074    }
075
076    @Override
077    protected Long getTotalEntities() {
078        return hasExecutionErrors() ? null : inventoryLocationGroupControl.countInventoryLocationGroupCapacitiesByInventoryLocationGroup(inventoryLocationGroup);
079    }
080
081    @Override
082    protected Collection<InventoryLocationGroupCapacity> getEntities() {
083        return hasExecutionErrors() ? null : inventoryLocationGroupControl.getInventoryLocationGroupCapacitiesByInventoryLocationGroup(inventoryLocationGroup);
084    }
085
086    @Override
087    protected BaseResult getResult(Collection<InventoryLocationGroupCapacity> entities) {
088        var result = InventoryResultFactory.getGetInventoryLocationGroupCapacitiesResult();
089
090        if(entities != null) {
091            var userVisit = getUserVisit();
092
093            result.setInventoryLocationGroup(inventoryLocationGroupControl.getInventoryLocationGroupTransfer(userVisit, inventoryLocationGroup));
094
095            if(session.hasLimit(InventoryLocationGroupCapacityFactory.class)) {
096                result.setInventoryLocationGroupCapacityCount(getTotalEntities());
097            }
098
099            result.setInventoryLocationGroupCapacities(inventoryLocationGroupControl.getInventoryLocationGroupCapacityTransfersByInventoryLocationGroup(userVisit, inventoryLocationGroup));
100        }
101
102        return result;
103    }
104
105}