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.CreateInventoryCostingMethodForm;
020import com.echothree.control.user.inventory.common.result.InventoryResultFactory;
021import com.echothree.model.control.inventory.common.exception.UnknownInventoryTransactionWorkflowEntranceNameException;
022import com.echothree.model.control.inventory.common.exception.UnknownInventoryTransactionWorkflowNameException;
023import com.echothree.model.control.inventory.server.logic.InventoryCostingMethodLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoleGroups;
026import com.echothree.model.control.security.common.SecurityRoles;
027import com.echothree.model.control.sequence.server.logic.SequenceTypeLogic;
028import com.echothree.model.control.workflow.server.logic.WorkflowEntranceLogic;
029import com.echothree.model.control.workflow.server.logic.WorkflowLogic;
030import com.echothree.model.data.inventory.server.entity.InventoryCostingMethod;
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.BaseSimpleCommand;
036import com.echothree.util.server.control.CommandSecurityDefinition;
037import com.echothree.util.server.control.PartyTypeDefinition;
038import com.echothree.util.server.control.SecurityRoleDefinition;
039import com.echothree.util.server.persistence.EntityPermission;
040import java.util.List;
041import javax.inject.Inject;
042import javax.enterprise.context.Dependent;
043
044@Dependent
045public class CreateInventoryCostingMethodCommand
046        extends BaseSimpleCommand<CreateInventoryCostingMethodForm> {
047
048    private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION;
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050
051    static {
052        COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of(
053                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
054                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
055                        new SecurityRoleDefinition(SecurityRoleGroups.InventoryCostingMethod.name(), SecurityRoles.Create.name())
056                ))
057        ));
058
059        FORM_FIELD_DEFINITIONS = List.of(
060                new FieldDefinition("InventoryCostingMethodName", FieldType.ENTITY_NAME, true, null, null),
061                new FieldDefinition("IsDefault", FieldType.BOOLEAN, true, null, null),
062                new FieldDefinition("SortOrder", FieldType.SIGNED_INTEGER, true, null, null),
063                new FieldDefinition("Description", FieldType.STRING, false, 1L, 132L)
064        );
065    }
066
067    @Inject
068    InventoryCostingMethodLogic inventoryCostingMethodLogic;
069
070    @Inject
071    SequenceTypeLogic sequenceTypeLogic;
072
073    @Inject
074    WorkflowEntranceLogic workflowEntranceLogic;
075
076    @Inject
077    WorkflowLogic workflowLogic;
078
079    /** Creates a new instance of CreateInventoryCostingMethodCommand */
080    public CreateInventoryCostingMethodCommand() {
081        super(COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false);
082    }
083
084    @Override
085    protected BaseResult execute() {
086        var result = InventoryResultFactory.getCreateInventoryCostingMethodResult();
087        var inventoryCostingMethodName = form.getInventoryCostingMethodName();
088        var isDefault = Boolean.valueOf(form.getIsDefault());
089        var sortOrder = Integer.valueOf(form.getSortOrder());
090        var description = form.getDescription();
091        var partyPK = getPartyPK();
092
093        var inventoryCostingMethod = inventoryCostingMethodLogic.createInventoryCostingMethod(this,
094                inventoryCostingMethodName, isDefault, sortOrder, getPreferredLanguage(), description, partyPK);
095
096        if(inventoryCostingMethod != null) {
097            result.setEntityRef(inventoryCostingMethod.getPrimaryKey().getEntityRef());
098            result.setInventoryCostingMethodName(inventoryCostingMethod.getLastDetail().getInventoryCostingMethodName());
099        }
100
101        return result;
102    }
103
104}