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.GetOrderAliasForm;
020import com.echothree.control.user.order.common.result.OrderResultFactory;
021import com.echothree.control.user.order.server.command.util.OrderAliasUtil;
022import com.echothree.model.control.order.server.control.OrderAliasControl;
023import com.echothree.model.control.order.server.logic.OrderAliasLogic;
024import com.echothree.model.control.order.server.logic.OrderLogic;
025import com.echothree.model.control.order.server.logic.OrderTypeLogic;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.order.server.entity.OrderAlias;
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 GetOrderAliasCommand
043        extends BaseSingleEntityCommand<OrderAlias, GetOrderAliasForm> {
044    
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        FORM_FIELD_DEFINITIONS = List.of(
049                new FieldDefinition("OrderTypeName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("OrderName", FieldType.ENTITY_NAME, true, null, null),
051                new FieldDefinition("OrderAliasTypeName", FieldType.ENTITY_NAME, true, null, null)
052        );
053    }
054
055    @Inject
056    OrderAliasControl orderAliasControl;
057
058    @Inject
059    OrderAliasLogic orderAliasLogic;
060
061    @Inject
062    OrderLogic orderLogic;
063
064    @Inject
065    OrderTypeLogic orderTypeLogic;
066
067
068    /** Creates a new instance of GetOrderAliasCommand */
069    public GetOrderAliasCommand() {
070        super(null, FORM_FIELD_DEFINITIONS, false);
071    }
072
073    @Override
074    protected CommandSecurityDefinition getCommandSecurityDefinition() {
075        return new CommandSecurityDefinition(List.of(
076                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
077                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
078                        new SecurityRoleDefinition(OrderAliasUtil.getInstance().getSecurityRoleGroupNameByOrderTypeSpec(form), SecurityRoles.Review.name())
079                ))
080        ));
081    }
082
083    @Override
084    protected OrderAlias getEntity() {
085        var orderTypeName = form.getOrderTypeName();
086        var orderType = orderTypeLogic.getOrderTypeByName(this, orderTypeName);
087        OrderAlias orderAlias = null;
088
089        if(!hasExecutionErrors()) {
090            var orderName = form.getOrderName();
091            var order = orderLogic.getOrderByName(this, orderTypeName, orderName);
092
093            if(!hasExecutionErrors()) {
094                var orderAliasTypeName = form.getOrderAliasTypeName();
095                var orderAliasType = orderAliasLogic.getOrderAliasTypeByName(this, orderType, orderAliasTypeName);
096
097                if(!hasExecutionErrors()) {
098                    orderAlias = orderAliasControl.getOrderAlias(order, orderAliasType);
099
100                    if(orderAlias == null) {
101                        addExecutionError(ExecutionErrors.UnknownOrderAlias.name(), orderName, orderAliasTypeName);
102                    }
103                }
104            }
105        }
106
107        return orderAlias;
108    }
109
110    @Override
111    protected BaseResult getResult(OrderAlias orderAlias) {
112        var result = OrderResultFactory.getGetOrderAliasResult();
113
114        if(orderAlias != null) {
115            result.setOrderAlias(orderAliasControl.getOrderAliasTransfer(getUserVisit(), orderAlias));
116        }
117
118        return result;
119    }
120    
121}