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.GetLotsForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.control.user.workflow.common.form.GetWorkflowEntrancesForm;
022import com.echothree.model.control.inventory.server.control.LotControl;
023import com.echothree.model.control.item.server.control.ItemControl;
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.control.workflow.server.control.WorkflowControl;
029import com.echothree.model.control.workflow.server.logic.WorkflowLogic;
030import com.echothree.model.data.inventory.server.entity.Lot;
031import com.echothree.model.data.item.server.entity.Item;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.model.data.workflow.server.entity.Workflow;
034import com.echothree.model.data.workflow.server.entity.WorkflowEntrance;
035import com.echothree.util.common.command.BaseResult;
036import com.echothree.util.common.validation.FieldDefinition;
037import com.echothree.util.common.validation.FieldType;
038import com.echothree.util.server.control.BaseMultipleEntitiesCommand;
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.inject.Inject;
046import javax.enterprise.context.Dependent;
047
048@Dependent
049public class GetLotsCommand
050        extends BasePaginatedMultipleEntitiesCommand<Lot, GetLotsForm> {
051
052    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
053    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
054
055    static {
056        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
057                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
058                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
059                        new SecurityRoleDefinition(SecurityRoleGroups.Lot.name(), SecurityRoles.List.name())
060                ))
061        ));
062
063        FORM_FIELD_DEFINITIONS = List.of(
064                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null)
065        );
066    }
067
068    @Inject
069    ItemControl itemControl;
070
071    @Inject
072    LotControl lotControl;
073
074    @Inject
075    WorkflowControl workflowControl;
076
077    @Inject
078    ItemLogic itemLogic;
079
080    @Inject
081    WorkflowLogic workflowLogic;
082
083    /** Creates a new instance of GetLotsCommand */
084    public GetLotsCommand() {
085        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
086    }
087
088    Item item;
089
090    @Override
091    protected void handleForm() {
092        var itemName = form.getItemName();
093
094        item = itemLogic.getItemByName(this, itemName);
095    }
096
097    @Override
098    protected Long getTotalEntities() {
099        return hasExecutionErrors() ? null :
100                lotControl.countLotsByItem(item);
101    }
102
103    @Override
104    protected Collection<Lot> getEntities() {
105        return lotControl.getLotsByItem(item);
106    }
107
108    @Override
109    protected BaseResult getResult(Collection<Lot> entities) {
110        var result = InventoryResultFactory.getGetLotsResult();
111
112        if(entities != null) {
113            var userVisit = getUserVisit();
114
115            result.setItem(itemControl.getItemTransfer(userVisit, item));
116            result.setLots(lotControl.getLotTransfers(userVisit, entities));
117        }
118        return result;
119    }
120
121}