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 com.echothree.util.server.persistence.Session;
044import java.util.Collection;
045import java.util.List;
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    /** Creates a new instance of GetLotsCommand */
069    public GetLotsCommand() {
070        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
071    }
072
073    Item item;
074
075    @Override
076    protected void handleForm() {
077        var itemName = form.getItemName();
078
079        item = ItemLogic.getInstance().getItemByName(this, itemName);
080    }
081
082    @Override
083    protected Long getTotalEntities() {
084        var lotControl = Session.getModelController(LotControl.class);
085
086        return hasExecutionErrors() ? null :
087                lotControl.countLotsByItem(item);
088    }
089
090    @Override
091    protected Collection<Lot> getEntities() {
092        var lotControl = Session.getModelController(LotControl.class);
093        
094        return lotControl.getLotsByItem(item);
095    }
096    
097    @Override
098    protected BaseResult getResult(Collection<Lot> entities) {
099        var result = InventoryResultFactory.getGetLotsResult();
100
101        if(entities != null) {
102            var itemControl = Session.getModelController(ItemControl.class);
103            var lotControl = Session.getModelController(LotControl.class);
104            var userVisit = getUserVisit();
105
106            result.setItem(itemControl.getItemTransfer(userVisit, item));
107            result.setLots(lotControl.getLotTransfers(userVisit, entities));
108        }
109        return result;
110    }
111    
112}