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.batch.server.command;
018
019import com.echothree.control.user.batch.common.form.CreateBatchAliasForm;
020import com.echothree.control.user.batch.server.command.util.BatchAliasUtil;
021import com.echothree.model.control.batch.server.control.BatchControl;
022import com.echothree.model.control.party.common.PartyTypes;
023import com.echothree.model.control.security.common.SecurityRoles;
024import com.echothree.util.common.command.BaseResult;
025import com.echothree.util.common.message.ExecutionErrors;
026import com.echothree.util.common.validation.FieldDefinition;
027import com.echothree.util.common.validation.FieldType;
028import com.echothree.util.server.control.BaseSimpleCommand;
029import com.echothree.util.server.control.CommandSecurityDefinition;
030import com.echothree.util.server.control.PartyTypeDefinition;
031import com.echothree.util.server.control.SecurityRoleDefinition;
032import com.echothree.util.server.persistence.Session;
033import java.util.Arrays;
034import java.util.Collections;
035import java.util.List;
036import java.util.regex.Pattern;
037import javax.enterprise.context.RequestScoped;
038
039@RequestScoped
040public class CreateBatchAliasCommand
041        extends BaseSimpleCommand<CreateBatchAliasForm> {
042    
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044    
045    static {
046        FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
047                new FieldDefinition("BatchTypeName", FieldType.ENTITY_NAME, true, null, null),
048                new FieldDefinition("BatchName", FieldType.ENTITY_NAME, true, null, null),
049                new FieldDefinition("BatchAliasTypeName", FieldType.ENTITY_NAME, true, null, null),
050                new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null)
051                ));
052    }
053    
054    /** Creates a new instance of CreateBatchAliasCommand */
055    public CreateBatchAliasCommand() {
056        super(null, FORM_FIELD_DEFINITIONS, false);
057    }
058
059    @Override
060    protected CommandSecurityDefinition getCommandSecurityDefinition() {
061        return 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(BatchAliasUtil.getInstance().getSecurityRoleGroupNameByBatchTypeSpec(form), SecurityRoles.Create.name())
065                )))
066        )));
067    }
068
069    @Override
070    protected BaseResult execute() {
071        var batchControl = Session.getModelController(BatchControl.class);
072        var batchTypeName = form.getBatchTypeName();
073        var batchType = batchControl.getBatchTypeByName(batchTypeName);
074
075        if(batchType != null) {
076            var batchName = form.getBatchName();
077            var batch = batchControl.getBatchByName(batchType, batchName);
078
079            if(batch != null) {
080                var batchAliasTypeName = form.getBatchAliasTypeName();
081                var batchAliasType = batchControl.getBatchAliasTypeByName(batchType, batchAliasTypeName);
082
083                if(batchAliasType != null) {
084                    var batchAliasTypeDetail = batchAliasType.getLastDetail();
085                    var validationPattern = batchAliasTypeDetail.getValidationPattern();
086                    var alias = form.getAlias();
087
088                    if(validationPattern != null) {
089                        var pattern = Pattern.compile(validationPattern);
090                        var m = pattern.matcher(alias);
091
092                        if(!m.matches()) {
093                            addExecutionError(ExecutionErrors.InvalidAlias.name(), alias);
094                        }
095                    }
096
097                    if(!hasExecutionErrors()) {
098                        if(batchControl.getBatchAlias(batch, batchAliasType) == null && batchControl.getBatchAliasByAlias(batchAliasType, alias) == null) {
099                            batchControl.createBatchAlias(batch, batchAliasType, alias, getPartyPK());
100                        } else {
101                            addExecutionError(ExecutionErrors.DuplicateBatchAlias.name(), batchTypeName, batchName, batchAliasTypeName, alias);
102                        }
103                    }
104                } else {
105                    addExecutionError(ExecutionErrors.UnknownBatchAliasTypeName.name(), batchTypeName, batchAliasTypeName);
106                }
107            } else {
108                addExecutionError(ExecutionErrors.UnknownBatchName.name(), batchTypeName, batchName);
109            }
110        } else {
111            addExecutionError(ExecutionErrors.UnknownBatchTypeName.name(), batchTypeName);
112        }
113
114        return null;
115    }
116    
117}