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