001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.Arrays;
045import java.util.Collection;
046import java.util.Collections;
047import java.util.List;
048import javax.enterprise.context.RequestScoped;
049
050@RequestScoped
051public class GetLotsCommand
052        extends BasePaginatedMultipleEntitiesCommand<Lot, GetLotsForm> {
053
054    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
055    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
056
057    static {
058        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
059                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
060                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
061                        new SecurityRoleDefinition(SecurityRoleGroups.Lot.name(), SecurityRoles.List.name())
062                )))
063        )));
064
065        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
066                new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null)
067                ));
068    }
069    
070    /** Creates a new instance of GetLotsCommand */
071    public GetLotsCommand() {
072        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
073    }
074
075    Item item;
076
077    @Override
078    protected void handleForm() {
079        var itemName = form.getItemName();
080
081        item = ItemLogic.getInstance().getItemByName(this, itemName);
082    }
083
084    @Override
085    protected Long getTotalEntities() {
086        var lotControl = Session.getModelController(LotControl.class);
087
088        return hasExecutionErrors() ? null :
089                lotControl.countLotsByItem(item);
090    }
091
092    @Override
093    protected Collection<Lot> getEntities() {
094        var lotControl = Session.getModelController(LotControl.class);
095        
096        return lotControl.getLotsByItem(item);
097    }
098    
099    @Override
100    protected BaseResult getResult(Collection<Lot> entities) {
101        var result = InventoryResultFactory.getGetLotsResult();
102
103        if(entities != null) {
104            var itemControl = Session.getModelController(ItemControl.class);
105            var lotControl = Session.getModelController(LotControl.class);
106            var userVisit = getUserVisit();
107
108            result.setItem(itemControl.getItemTransfer(userVisit, item));
109            result.setLots(lotControl.getLotTransfers(userVisit, entities));
110        }
111        return result;
112    }
113    
114}