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.GetPartyBucketForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.control.user.inventory.server.command.common.PartyInventoryLevelUtil;
022import com.echothree.model.control.inventory.server.control.BucketControl;
023import com.echothree.model.control.inventory.server.logic.InventoryBucketTypeLogic;
024import com.echothree.model.control.inventory.server.logic.InventoryConditionLogic;
025import com.echothree.model.control.item.server.logic.ItemLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoleGroups;
028import com.echothree.model.control.security.common.SecurityRoles;
029import com.echothree.model.control.uom.server.logic.UnitOfMeasureTypeLogic;
030import com.echothree.model.data.inventory.server.entity.PartyBucket;
031import com.echothree.util.common.command.BaseResult;
032import com.echothree.util.common.message.ExecutionErrors;
033import com.echothree.util.common.validation.FieldDefinition;
034import com.echothree.util.common.validation.FieldType;
035import com.echothree.util.server.control.BaseSingleEntityCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import java.util.List;
040import javax.enterprise.context.Dependent;
041import javax.inject.Inject;
042
043@Dependent
044public class GetPartyBucketCommand
045        extends BaseSingleEntityCommand<PartyBucket, GetPartyBucketForm> {
046
047    private static final CommandSecurityDefinition COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
048            new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
049            new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
050                    new SecurityRoleDefinition(SecurityRoleGroups.PartyBucket.name(), SecurityRoles.Review.name())
051            ))
052    ));
053
054    private static final List<FieldDefinition> FORM_FIELD_DEFINITIONS = List.of(
055            new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
056            new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
057            new FieldDefinition("WarehouseName", FieldType.ENTITY_NAME, false, null, null),
058            new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
059            new FieldDefinition("UnitOfMeasureTypeName", FieldType.ENTITY_NAME, true, null, null),
060            new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
061            new FieldDefinition("InventoryBucketTypeName", FieldType.ENTITY_NAME, true, null, null)
062    );
063
064    @Inject
065    BucketControl bucketControl;
066
067    @Inject
068    PartyInventoryLevelUtil partyInventoryLevelUtil;
069
070    @Inject
071    ItemLogic itemLogic;
072
073    @Inject
074    UnitOfMeasureTypeLogic unitOfMeasureTypeLogic;
075
076    @Inject
077    InventoryConditionLogic inventoryConditionLogic;
078
079    @Inject
080    InventoryBucketTypeLogic inventoryBucketTypeLogic;
081
082    public GetPartyBucketCommand() {
083        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
084    }
085
086    @Override
087    protected PartyBucket getEntity() {
088        PartyBucket partyBucket = null;
089        var party = partyInventoryLevelUtil.getParty(this, form);
090        var item = itemLogic.getItemByName(this, form.getItemName());
091        var inventoryCondition = inventoryConditionLogic.getInventoryConditionByName(this, form.getInventoryConditionName());
092        var inventoryBucketType = inventoryBucketTypeLogic.getInventoryBucketTypeByName(this, form.getInventoryBucketTypeName());
093
094        if(!hasExecutionErrors()) {
095            var unitOfMeasureType = unitOfMeasureTypeLogic.getUnitOfMeasureTypeByName(this,
096                    item.getLastDetail().getUnitOfMeasureKind(), form.getUnitOfMeasureTypeName());
097
098            if(!hasExecutionErrors()) {
099                partyBucket = bucketControl.getPartyBucket(party, item, unitOfMeasureType, inventoryCondition, inventoryBucketType);
100
101                if(partyBucket == null) {
102                    addExecutionError(ExecutionErrors.UnknownPartyBucket.name(), party.getLastDetail().getPartyName(),
103                            form.getItemName(), form.getUnitOfMeasureTypeName(),
104                            form.getInventoryConditionName(), form.getInventoryBucketTypeName());
105                }
106            }
107        }
108
109        return partyBucket;
110    }
111
112    @Override
113    protected BaseResult getResult(PartyBucket partyBucket) {
114        var result = InventoryResultFactory.getGetPartyBucketResult();
115
116        if(!hasExecutionErrors()) {
117            result.setPartyBucket(bucketControl.getPartyBucketTransfer(getUserVisit(), partyBucket));
118        }
119
120        return result;
121    }
122
123}