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.batch.server.command;
018
019import com.echothree.control.user.batch.common.form.GetBatchAliasForm;
020import com.echothree.control.user.batch.common.result.BatchResultFactory;
021import com.echothree.control.user.batch.server.command.util.BatchAliasUtil;
022import com.echothree.model.control.batch.server.control.BatchControl;
023import com.echothree.model.control.batch.server.logic.BatchLogic;
024import com.echothree.model.control.party.common.PartyTypes;
025import com.echothree.model.control.security.common.SecurityRoles;
026import com.echothree.model.data.batch.server.entity.BatchAlias;
027import com.echothree.util.common.command.BaseResult;
028import com.echothree.util.common.message.ExecutionErrors;
029import com.echothree.util.common.validation.FieldDefinition;
030import com.echothree.util.common.validation.FieldType;
031import com.echothree.util.server.control.BaseSingleEntityCommand;
032import com.echothree.util.server.control.CommandSecurityDefinition;
033import com.echothree.util.server.control.PartyTypeDefinition;
034import com.echothree.util.server.control.SecurityRoleDefinition;
035import java.util.List;
036import javax.enterprise.context.Dependent;
037import javax.inject.Inject;
038
039@Dependent
040public class GetBatchAliasCommand
041        extends BaseSingleEntityCommand<BatchAlias, GetBatchAliasForm> {
042
043    private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS;
044
045    static {
046        FORM_FIELD_DEFINITIONS = List.of(
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        );
051    }
052
053    @Inject
054    BatchControl batchControl;
055
056    @Inject
057    BatchLogic batchLogic;
058
059    /** Creates a new instance of GetBatchAliasCommand */
060    public GetBatchAliasCommand() {
061        super(null, FORM_FIELD_DEFINITIONS, false);
062    }
063
064    @Override
065    protected CommandSecurityDefinition getCommandSecurityDefinition() {
066        return new CommandSecurityDefinition(List.of(
067                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
068                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of(
069                        new SecurityRoleDefinition(BatchAliasUtil.getInstance().getSecurityRoleGroupNameByBatchTypeSpec(form), SecurityRoles.Review.name())
070                ))
071        ));
072    }
073
074    @Override
075    protected BatchAlias getEntity() {
076        var batchTypeName = form.getBatchTypeName();
077        var batchType = batchLogic.getBatchTypeByName(this, batchTypeName);
078        BatchAlias batchAlias = null;
079
080        if(!hasExecutionErrors()) {
081            var batchName = form.getBatchName();
082            var batch = batchLogic.getBatchByName(this, batchType, batchName);
083
084            if(!hasExecutionErrors()) {
085                var batchAliasTypeName = form.getBatchAliasTypeName();
086                var batchAliasType = batchLogic.getBatchAliasTypeByName(this, batchType, batchAliasTypeName);
087
088                if(!hasExecutionErrors()) {
089                    batchAlias = batchControl.getBatchAlias(batch, batchAliasType);
090
091                    if(batchAlias == null) {
092                        addExecutionError(ExecutionErrors.UnknownBatchAlias.name(),
093                                batch.getLastDetail().getBatchName(), batchAliasType.getLastDetail().getBatchAliasTypeName());
094                    }
095                }
096            }
097        }
098
099        return batchAlias;
100    }
101
102    @Override
103    protected BaseResult getResult(BatchAlias batchAlias) {
104        var result = BatchResultFactory.getGetBatchAliasResult();
105
106        if(batchAlias != null) {
107            result.setBatchAlias(batchControl.getBatchAliasTransfer(getUserVisit(), batchAlias));
108        }
109
110        return result;
111    }
112
113}