001// --------------------------------------------------------------------------------
002// Copyright 2002-2024 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.GetOrderAliasResult;
021import com.echothree.control.user.order.common.result.OrderResultFactory;
022import com.echothree.control.user.order.server.command.util.OrderAliasUtil;
023import com.echothree.model.control.order.server.control.OrderAliasControl;
024import com.echothree.model.control.order.server.control.OrderControl;
025import com.echothree.model.control.order.server.control.OrderTypeControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.order.server.entity.Order;
029import com.echothree.model.data.order.server.entity.OrderAlias;
030import com.echothree.model.data.order.server.entity.OrderAliasType;
031import com.echothree.model.data.order.server.entity.OrderType;
032import com.echothree.model.data.user.common.pk.UserVisitPK;
033import com.echothree.util.common.command.BaseResult;
034import com.echothree.util.common.message.ExecutionErrors;
035import com.echothree.util.common.validation.FieldDefinition;
036import com.echothree.util.common.validation.FieldType;
037import com.echothree.util.server.control.BaseSimpleCommand;
038import com.echothree.util.server.control.CommandSecurityDefinition;
039import com.echothree.util.server.control.PartyTypeDefinition;
040import com.echothree.util.server.control.SecurityRoleDefinition;
041import com.echothree.util.server.persistence.Session;
042import java.util.Arrays;
043import java.util.Collections;
044import java.util.List;
045
046public class GetOrderAliasCommand
047        extends BaseSimpleCommand<GetOrderAliasForm> {
048    
049    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
050    
051    static {
052        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
053                new FieldDefinition("OrderTypeName", FieldType.ENTITY_NAME, true, null, null),
054                new FieldDefinition("OrderName", FieldType.ENTITY_NAME, true, null, null),
055                new FieldDefinition("OrderAliasTypeName", FieldType.ENTITY_NAME, true, null, null)
056                ));
057    }
058    
059    /** Creates a new instance of GetOrderAliasCommand */
060    public GetOrderAliasCommand(UserVisitPK userVisitPK, GetOrderAliasForm form) {
061        super(userVisitPK, form, new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
062                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
063                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
064                        new SecurityRoleDefinition(OrderAliasUtil.getInstance().getSecurityRoleGroupNameByOrderTypeSpec(form), SecurityRoles.Review.name())
065                        )))
066                ))), FORM_FIELD_DEFINITIONS, false);
067    }
068    
069    @Override
070    protected BaseResult execute() {
071        var orderTypeControl = Session.getModelController(OrderTypeControl.class);
072        GetOrderAliasResult result = OrderResultFactory.getGetOrderAliasResult();
073        var orderTypeName = form.getOrderTypeName();
074        var orderType = orderTypeControl.getOrderTypeByName(orderTypeName);
075
076        if(orderType != null) {
077            var orderControl = Session.getModelController(OrderControl.class);
078            String orderName = form.getOrderName();
079            Order order = orderControl.getOrderByName(orderType, orderName);
080
081            if(order != null) {
082                var orderAliasControl = Session.getModelController(OrderAliasControl.class);
083                String orderAliasTypeName = form.getOrderAliasTypeName();
084                OrderAliasType orderAliasType = orderAliasControl.getOrderAliasTypeByName(orderType, orderAliasTypeName);
085
086                if(orderAliasType != null) {
087                    OrderAlias orderAlias = orderAliasControl.getOrderAlias(order, orderAliasType);
088
089                    if(orderAlias != null) {
090                        result.setOrderAlias(orderAliasControl.getOrderAliasTransfer(getUserVisit(), orderAlias));
091                    } else {
092                        addExecutionError(ExecutionErrors.UnknownOrderAlias.name(), orderName, orderAliasTypeName);
093                    }
094                } else {
095                    addExecutionError(ExecutionErrors.UnknownOrderAliasTypeName.name(), orderTypeName, orderAliasTypeName);
096                }
097            } else {
098                addExecutionError(ExecutionErrors.UnknownOrderName.name(), orderTypeName, orderName);
099            }
100        } else {
101            addExecutionError(ExecutionErrors.UnknownOrderTypeName.name(), orderTypeName);
102        }
103
104        return result;
105    }
106    
107}