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.GetInventoryLayerBucketForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.server.control.BucketControl;
022import com.echothree.model.control.inventory.server.logic.InventoryBucketTypeLogic;
023import com.echothree.model.control.inventory.server.logic.InventoryLayerLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.control.security.common.SecurityRoleGroups;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.inventory.server.entity.InventoryLayerBucket;
029import com.echothree.util.common.command.BaseResult;
030import com.echothree.util.common.message.ExecutionErrors;
031import com.echothree.util.common.validation.FieldDefinition;
032import com.echothree.util.common.validation.FieldType;
033import com.echothree.util.server.control.BaseSingleEntityCommand;
034import com.echothree.util.server.control.CommandSecurityDefinition;
035import com.echothree.util.server.control.PartyTypeDefinition;
036import com.echothree.util.server.control.SecurityRoleDefinition;
037import java.util.List;
038import javax.enterprise.context.Dependent;
039import javax.inject.Inject;
040
041@Dependent
042public class GetInventoryLayerBucketCommand
043        extends BaseSingleEntityCommand<InventoryLayerBucket, GetInventoryLayerBucketForm> {
044
045    private static final CommandSecurityDefinition COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
046            new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
047            new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
048                    new SecurityRoleDefinition(SecurityRoleGroups.InventoryLayerBucket.name(), SecurityRoles.Review.name())
049            ))
050    ));
051
052    private static final List<FieldDefinition> FORM_FIELD_DEFINITIONS = List.of(
053            new FieldDefinition("PartyName", FieldType.ENTITY_NAME, false, null, null),
054            new FieldDefinition("CompanyName", FieldType.ENTITY_NAME, false, null, null),
055            new FieldDefinition("InventoryConditionName", FieldType.ENTITY_NAME, true, null, null),
056            new FieldDefinition("InventoryLayerSequence", FieldType.UNSIGNED_INTEGER, true, null, null),
057            new FieldDefinition("ItemName", FieldType.ENTITY_NAME, true, null, null),
058            new FieldDefinition("InventoryBucketTypeName", FieldType.ENTITY_NAME, true, null, null)
059    );
060
061    @Inject
062    BucketControl bucketControl;
063
064    @Inject
065    PartyControl partyControl;
066
067    @Inject
068    InventoryBucketTypeLogic inventoryBucketTypeLogic;
069
070    @Inject
071    InventoryLayerLogic inventoryLayerLogic;
072
073    public GetInventoryLayerBucketCommand() {
074        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, true);
075    }
076
077    @Override
078    protected InventoryLayerBucket getEntity() {
079        InventoryLayerBucket inventoryLayerBucket = null;
080        var inventoryLayer = inventoryLayerLogic.getInventoryLayerByName(this, form.getCompanyName(),
081                form.getPartyName(), form.getItemName(), form.getInventoryConditionName(),
082                Integer.valueOf(form.getInventoryLayerSequence()));
083        var inventoryBucketType = inventoryBucketTypeLogic.getInventoryBucketTypeByName(this,
084                form.getInventoryBucketTypeName());
085
086        if(!hasExecutionErrors()) {
087            inventoryLayerBucket = bucketControl.getInventoryLayerBucket(inventoryLayer, inventoryBucketType);
088
089            if(inventoryLayerBucket == null) {
090                var inventoryLayerDetail = inventoryLayer.getLastDetail();
091                var inventoryCostingPoolDetail = inventoryLayerDetail.getInventoryCostingPool().getLastDetail();
092
093                addExecutionError(ExecutionErrors.UnknownInventoryLayerBucket.name(),
094                        partyControl.getPartyCompany(inventoryCostingPoolDetail.getCompanyParty()).getPartyCompanyName(),
095                        inventoryCostingPoolDetail.getItem().getLastDetail().getItemName(),
096                        inventoryCostingPoolDetail.getInventoryCondition().getLastDetail().getInventoryConditionName(),
097                        inventoryLayerDetail.getInventoryLayerSequence(),
098                        inventoryBucketType.getLastDetail().getInventoryBucketTypeName());
099            }
100        }
101
102        return inventoryLayerBucket;
103    }
104
105    @Override
106    protected BaseResult getResult(InventoryLayerBucket inventoryLayerBucket) {
107        var result = InventoryResultFactory.getGetInventoryLayerBucketResult();
108
109        if(inventoryLayerBucket != null) {
110            result.setInventoryLayerBucket(bucketControl.getInventoryLayerBucketTransfer(getUserVisit(),
111                    inventoryLayerBucket));
112        }
113
114        return result;
115    }
116
117}