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