001// --------------------------------------------------------------------------------
002// Copyright 2002-2025 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.CreateOrderAliasForm;
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 com.echothree.util.server.persistence.Session;
035import java.util.Arrays;
036import java.util.Collections;
037import java.util.List;
038import java.util.regex.Pattern;
039import javax.enterprise.context.RequestScoped;
040
041@RequestScoped
042public class CreateOrderAliasCommand
043        extends BaseSimpleCommand<CreateOrderAliasForm> {
044    
045    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
046    
047    static {
048        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
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                new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null)
053                ));
054    }
055    
056    /** Creates a new instance of CreateOrderAliasCommand */
057    public CreateOrderAliasCommand() {
058        super(null, FORM_FIELD_DEFINITIONS, false);
059    }
060
061    @Override
062    protected CommandSecurityDefinition getCommandSecurityDefinition() {
063        return new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
064                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
065                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
066                        new SecurityRoleDefinition(OrderAliasUtil.getInstance().getSecurityRoleGroupNameByOrderTypeSpec(form), SecurityRoles.Create.name())
067                )))
068        )));
069    }
070
071    @Override
072    protected BaseResult execute() {
073        var orderTypeControl = Session.getModelController(OrderTypeControl.class);
074        var orderTypeName = form.getOrderTypeName();
075        var orderType = orderTypeControl.getOrderTypeByName(orderTypeName);
076
077        if(orderType != null) {
078            var orderControl = Session.getModelController(OrderControl.class);
079            var orderName = form.getOrderName();
080            var order = orderControl.getOrderByName(orderType, orderName);
081
082            if(order != null) {
083                var orderAliasControl = Session.getModelController(OrderAliasControl.class);
084                var orderAliasTypeName = form.getOrderAliasTypeName();
085                var orderAliasType = orderAliasControl.getOrderAliasTypeByName(orderType, orderAliasTypeName);
086
087                if(orderAliasType != null) {
088                    var orderAliasTypeDetail = orderAliasType.getLastDetail();
089                    var validationPattern = orderAliasTypeDetail.getValidationPattern();
090                    var alias = form.getAlias();
091
092                    if(validationPattern != null) {
093                        var pattern = Pattern.compile(validationPattern);
094                        var m = pattern.matcher(alias);
095
096                        if(!m.matches()) {
097                            addExecutionError(ExecutionErrors.InvalidAlias.name(), alias);
098                        }
099                    }
100
101                    if(!hasExecutionErrors()) {
102                        if(orderAliasControl.getOrderAlias(order, orderAliasType) == null && orderAliasControl.getOrderAliasByAlias(orderAliasType, alias) == null) {
103                            orderAliasControl.createOrderAlias(order, orderAliasType, alias, getPartyPK());
104                        } else {
105                            addExecutionError(ExecutionErrors.DuplicateOrderAlias.name(), orderTypeName, orderName, orderAliasTypeName, alias);
106                        }
107                    }
108                } else {
109                    addExecutionError(ExecutionErrors.UnknownOrderAliasTypeName.name(), orderTypeName, orderAliasTypeName);
110                }
111            } else {
112                addExecutionError(ExecutionErrors.UnknownOrderName.name(), orderTypeName, orderName);
113            }
114        } else {
115            addExecutionError(ExecutionErrors.UnknownOrderTypeName.name(), orderTypeName);
116        }
117
118        return null;
119    }
120    
121}