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    /** Creates a new instance of GetOrderAliasCommand */
055    public GetOrderAliasCommand() {
056        super(null, FORM_FIELD_DEFINITIONS, false);
057    }
058
059    @Inject
060    OrderAliasControl orderAliasControl;
061
062    @Inject
063    OrderLogic orderLogic;
064
065    @Inject
066    OrderTypeLogic orderTypeLogic;
067
068    @Override
069    protected CommandSecurityDefinition getCommandSecurityDefinition() {
070        return new CommandSecurityDefinition(List.of(
071                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
072                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
073                        new SecurityRoleDefinition(OrderAliasUtil.getInstance().getSecurityRoleGroupNameByOrderTypeSpec(form), SecurityRoles.Review.name())
074                ))
075        ));
076    }
077
078    @Override
079    protected OrderAlias getEntity() {
080        var orderTypeName = form.getOrderTypeName();
081        var orderType = orderTypeLogic.getOrderTypeByName(this, orderTypeName);
082        OrderAlias orderAlias = null;
083
084        if(!hasExecutionErrors()) {
085            var orderName = form.getOrderName();
086            var order = orderLogic.getOrderByName(this, orderTypeName, orderName);
087
088            if(!hasExecutionErrors()) {
089                var orderAliasTypeName = form.getOrderAliasTypeName();
090                var orderAliasType = orderLogic.getOrderAliasTypeByName(this, orderType, orderAliasTypeName);
091
092                if(!hasExecutionErrors()) {
093                    orderAlias = orderAliasControl.getOrderAlias(order, orderAliasType);
094
095                    if(orderAlias == null) {
096                        addExecutionError(ExecutionErrors.UnknownOrderAlias.name(), orderName, orderAliasTypeName);
097                    }
098                }
099            }
100        }
101
102        return orderAlias;
103    }
104
105    @Override
106    protected BaseResult getResult(OrderAlias orderAlias) {
107        var result = OrderResultFactory.getGetOrderAliasResult();
108
109        if(orderAlias != null) {
110            result.setOrderAlias(orderAliasControl.getOrderAliasTransfer(getUserVisit(), orderAlias));
111        }
112
113        return result;
114    }
115    
116}