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.GetInventoryLocationGroupsForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.InventoryControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoleGroups;
024import com.echothree.model.control.security.common.SecurityRoles;
025import com.echothree.model.control.warehouse.server.control.WarehouseControl;
026import com.echothree.model.data.inventory.server.entity.InventoryLocationGroup;
027import com.echothree.model.data.inventory.server.factory.InventoryLocationGroupFactory;
028import com.echothree.model.data.party.server.entity.Party;
029import com.echothree.model.data.warehouse.server.entity.Warehouse;
030import com.echothree.util.common.command.BaseResult;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import java.util.Collection;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class GetInventoryLocationGroupsCommand
045        extends BasePaginatedMultipleEntitiesCommand<InventoryLocationGroup, GetInventoryLocationGroupsForm> {
046
047    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
048    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
049
050    static {
051        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
052                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
053                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
054                        new SecurityRoleDefinition(SecurityRoleGroups.InventoryLocationGroup.name(), SecurityRoles.List.name())
055                ))
056        ));
057
058        FORM_FIELD_DEFINITIONS = List.of(
059                new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, true, null, null)
060        );
061    }
062
063    /** Creates a new instance of GetInventoryLocationGroupsCommand */
064    public GetInventoryLocationGroupsCommand() {
065        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
066    }
067
068    @Inject
069    InventoryControl inventoryControl;
070
071    @Inject
072    WarehouseControl warehouseControl;
073
074    Warehouse warehouse;
075    Party warehouseParty;
076
077    @Override
078    protected void handleForm() {
079        var warehouseName = form.getWarehouseName();
080
081        warehouse = warehouseControl.getWarehouseByName(warehouseName);
082
083        if(warehouse != null) {
084            warehouseParty = warehouse.getParty();
085        } else {
086            addExecutionError(ExecutionErrors.UnknownWarehouseName.name(), warehouseName);
087        }
088    }
089
090    @Override
091    protected Long getTotalEntities() {
092        return warehouseParty == null ? null : inventoryControl.countInventoryLocationGroupsByWarehouseParty(warehouseParty);
093    }
094
095    @Override
096    protected Collection<InventoryLocationGroup> getEntities() {
097        return warehouseParty == null ? null : inventoryControl.getInventoryLocationGroupsByWarehouseParty(warehouseParty);
098    }
099
100    @Override
101    protected BaseResult getResult(Collection<InventoryLocationGroup> entities) {
102        var result = InventoryResultFactory.getGetInventoryLocationGroupsResult();
103
104        if(entities != null) {
105            result.setWarehouse(warehouseControl.getWarehouseTransfer(getUserVisit(), warehouse));
106
107            if(session.hasLimit(InventoryLocationGroupFactory.class)) {
108                result.setInventoryLocationGroupCount(getTotalEntities());
109            }
110
111            result.setInventoryLocationGroups(inventoryControl.getInventoryLocationGroupTransfers(getUserVisit(), entities));
112        }
113
114        return result;
115    }
116
117}