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.GetLotAliasesForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.LotAliasControl;
022import com.echothree.model.control.inventory.server.control.LotControl;
023import com.echothree.model.control.inventory.server.logic.LotLogic;
024import com.echothree.model.control.item.server.logic.ItemLogic;
025import com.echothree.model.control.party.common.PartyTypes;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.inventory.server.entity.Lot;
029import com.echothree.model.data.inventory.server.entity.LotAlias;
030import com.echothree.model.data.inventory.server.factory.LotAliasFactory;
031import com.echothree.model.data.item.server.entity.Item;
032import com.echothree.util.common.command.BaseResult;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BasePaginatedMultipleEntitiesCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.Collection;
040import java.util.List;
041import javax.enterprise.context.Dependent;
042import javax.inject.Inject;
043
044@Dependent
045public class GetLotAliasesCommand
046        extends BasePaginatedMultipleEntitiesCommand<LotAlias, GetLotAliasesForm> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        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.LotAliasType.name(), SecurityRoles.List.name())
056                ))
057        ));
058
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("LotIdentifier", FieldType.STRING, true, 1L, 40L)
062        );
063    }
064    
065    @Inject
066    LotAliasControl lotAliasControl;
067
068    @Inject
069    LotControl lotControl;
070
071    @Inject
072    ItemLogic itemLogic;
073
074    @Inject
075    LotLogic lotLogic;
076
077    /** Creates a new instance of GetLotAliasesCommand */
078    public GetLotAliasesCommand() {
079        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
080    }
081
082    private Lot lot;
083
084    @Override
085    protected void handleForm() {
086        Item item = itemLogic.getItemByName(this, form.getItemName());
087
088        if(!hasExecutionErrors()) {
089            var lotIdentifier = form.getLotIdentifier();
090
091            lot = lotLogic.getLotByIdentifier(this, item, lotIdentifier);
092        }
093    }
094
095    @Override
096    protected Long getTotalEntities() {
097        return lot == null ? null : lotAliasControl.countLotAliasesByLot(lot);
098    }
099
100    @Override
101    protected Collection<LotAlias> getEntities() {
102        return lot == null ? null : lotAliasControl.getLotAliasesByLot(lot);
103    }
104
105    @Override
106    protected BaseResult getResult(Collection<LotAlias> entities) {
107        var result = InventoryResultFactory.getGetLotAliasesResult();
108
109        if(entities != null) {
110            var userVisit = getUserVisit();
111
112            result.setLot(lotControl.getLotTransfer(userVisit, lot));
113
114            if(session.hasLimit(LotAliasFactory.class)) {
115                result.setLotAliasCount(getTotalEntities());
116            }
117
118            result.setLotAliases(lotAliasControl.getLotAliasTransfers(userVisit, entities));
119        }
120
121        return result;
122    }
123
124}