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.order.server.command;
018
019import com.echothree.control.user.order.common.form.DeleteOrderAliasForm;
020import com.echothree.control.user.order.server.command.util.OrderAliasUtil;
021import com.echothree.model.control.order.server.control.OrderAliasControl;
022import com.echothree.model.control.order.server.control.OrderControl;
023import com.echothree.model.control.order.server.control.OrderTypeControl;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.util.common.command.BaseResult;
027import com.echothree.util.common.message.ExecutionErrors;
028import com.echothree.util.common.validation.FieldDefinition;
029import com.echothree.util.common.validation.FieldType;
030import com.echothree.util.server.control.BaseSimpleCommand;
031import com.echothree.util.server.control.CommandSecurityDefinition;
032import com.echothree.util.server.control.PartyTypeDefinition;
033import com.echothree.util.server.control.SecurityRoleDefinition;
034import java.util.List;
035import javax.enterprise.context.Dependent;
036import javax.inject.Inject;
037
038@Dependent
039public class DeleteOrderAliasCommand
040        extends BaseSimpleCommand<DeleteOrderAliasForm> {
041    
042    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
043    
044    static {
045        FORM_FIELD_DEFINITIONS = List.of(
046                new FieldDefinition("OrderTypeName", FieldType.ENTITY_NAME, true, null, null),
047                new FieldDefinition("OrderName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("OrderAliasTypeName", FieldType.ENTITY_NAME, true, null, null)
049        );
050    }
051
052    @Inject
053    OrderAliasControl orderAliasControl;
054
055    @Inject
056    OrderControl orderControl;
057
058    @Inject
059    OrderTypeControl orderTypeControl;
060
061    
062    /** Creates a new instance of DeleteOrderAliasCommand */
063    public DeleteOrderAliasCommand() {
064        super(null, FORM_FIELD_DEFINITIONS, false);
065    }
066
067    @Override
068    protected CommandSecurityDefinition getCommandSecurityDefinition() {
069        return new CommandSecurityDefinition(List.of(
070                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
071                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
072                        new SecurityRoleDefinition(OrderAliasUtil.getInstance().getSecurityRoleGroupNameByOrderTypeSpec(form), SecurityRoles.Delete.name())
073                ))
074        ));
075    }
076
077    @Override
078    protected BaseResult execute() {
079        var orderTypeName = form.getOrderTypeName();
080        var orderType = orderTypeControl.getOrderTypeByName(orderTypeName);
081
082        if(orderType != null) {
083            var orderName = form.getOrderName();
084            var order = orderControl.getOrderByName(orderType, orderName);
085
086            if(order != null) {
087                var orderAliasTypeName = form.getOrderAliasTypeName();
088                var orderAliasType = orderAliasControl.getOrderAliasTypeByName(orderType, orderAliasTypeName);
089
090                if(orderAliasType != null) {
091                    var orderAlias = orderAliasControl.getOrderAliasForUpdate(order, orderAliasType);
092
093                    if(orderAlias != null) {
094                        orderAliasControl.deleteOrderAlias(orderAlias, getPartyPK());
095                    } else {
096                        addExecutionError(ExecutionErrors.UnknownOrderAlias.name(), orderTypeName, orderName, orderAliasTypeName);
097                    }
098                } else {
099                    addExecutionError(ExecutionErrors.UnknownOrderAliasTypeName.name(), orderTypeName, orderAliasTypeName);
100                }
101            } else {
102                addExecutionError(ExecutionErrors.UnknownOrderName.name(), orderTypeName, orderName);
103            }
104        } else {
105            addExecutionError(ExecutionErrors.UnknownOrderTypeName.name(), orderTypeName);
106        }
107
108        return null;
109    }
110    
111}