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.edit.BatchAliasEdit;
020import com.echothree.control.user.batch.common.edit.BatchEditFactory;
021import com.echothree.control.user.batch.common.result.BatchResultFactory;
022import com.echothree.control.user.batch.common.result.EditBatchAliasResult;
023import com.echothree.control.user.batch.common.spec.BatchAliasSpec;
024import com.echothree.control.user.batch.server.command.util.BatchAliasUtil;
025import com.echothree.model.control.batch.server.control.BatchControl;
026import com.echothree.model.control.party.common.PartyTypes;
027import com.echothree.model.control.security.common.SecurityRoles;
028import com.echothree.model.data.batch.server.entity.BatchAlias;
029import com.echothree.model.data.batch.server.entity.BatchAliasType;
030import com.echothree.util.common.command.EditMode;
031import com.echothree.util.common.message.ExecutionErrors;
032import com.echothree.util.common.validation.FieldDefinition;
033import com.echothree.util.common.validation.FieldType;
034import com.echothree.util.server.control.BaseAbstractEditCommand;
035import com.echothree.util.server.control.CommandSecurityDefinition;
036import com.echothree.util.server.control.PartyTypeDefinition;
037import com.echothree.util.server.control.SecurityRoleDefinition;
038import com.echothree.util.server.persistence.Session;
039import java.util.Arrays;
040import java.util.Collections;
041import java.util.List;
042import javax.enterprise.context.RequestScoped;
043
044@RequestScoped
045public class EditBatchAliasCommand
046        extends BaseAbstractEditCommand<BatchAliasSpec, BatchAliasEdit, EditBatchAliasResult, BatchAlias, BatchAlias> {
047    
048    private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS;
049    private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS;
050    
051    static {
052        SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
053                new FieldDefinition("BatchTypeName", FieldType.ENTITY_NAME, true, null, null),
054                new FieldDefinition("BatchName", FieldType.ENTITY_NAME, true, null, null),
055                new FieldDefinition("BatchAliasTypeName", FieldType.ENTITY_NAME, true, null, null)
056                ));
057        
058        EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList(
059                new FieldDefinition("Alias", FieldType.ENTITY_NAME, true, null, null)
060                ));
061    }
062    
063    /** Creates a new instance of EditBatchAliasCommand */
064    public EditBatchAliasCommand() {
065        super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS);
066    }
067
068    @Override
069    protected CommandSecurityDefinition getCommandSecurityDefinition() {
070        return new CommandSecurityDefinition(Collections.unmodifiableList(Arrays.asList(
071                new PartyTypeDefinition(PartyTypes.UTILITY.name(), null),
072                new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), Collections.unmodifiableList(Arrays.asList(
073                        new SecurityRoleDefinition(BatchAliasUtil.getInstance().getSecurityRoleGroupNameByBatchTypeSpec(spec), SecurityRoles.Edit.name())
074                )))
075        )));
076    }
077
078    @Override
079    public EditBatchAliasResult getResult() {
080        return BatchResultFactory.getEditBatchAliasResult();
081    }
082
083    @Override
084    public BatchAliasEdit getEdit() {
085        return BatchEditFactory.getBatchAliasEdit();
086    }
087
088    BatchAliasType batchAliasType;
089    
090    @Override
091    public BatchAlias getEntity(EditBatchAliasResult result) {
092        var batchControl = Session.getModelController(BatchControl.class);
093        BatchAlias batchAlias = null;
094        var batchTypeName = spec.getBatchTypeName();
095        var batchType = batchControl.getBatchTypeByName(batchTypeName);
096
097        if(batchType != null) {
098            var batchName = spec.getBatchName();
099            var batch = batchControl.getBatchByName(batchType, batchName);
100
101            if(batch != null) {
102                var batchAliasTypeName = spec.getBatchAliasTypeName();
103
104                batchAliasType = batchControl.getBatchAliasTypeByName(batchType, batchAliasTypeName);
105
106                if(batchAliasType != null) {
107                    if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) {
108                        batchAlias = batchControl.getBatchAlias(batch, batchAliasType);
109                    } else { // EditMode.UPDATE
110                        batchAlias = batchControl.getBatchAliasForUpdate(batch, batchAliasType);
111                    }
112
113                    if(batchAlias != null) {
114                        result.setBatchAlias(batchControl.getBatchAliasTransfer(getUserVisit(), batchAlias));
115                    } else {
116                        addExecutionError(ExecutionErrors.UnknownBatchAlias.name(), batchTypeName, batchName, batchAliasTypeName);
117                    }
118                } else {
119                    addExecutionError(ExecutionErrors.UnknownBatchAliasTypeName.name(), batchTypeName, batchAliasTypeName);
120                }
121            } else {
122                addExecutionError(ExecutionErrors.UnknownBatchName.name(), batchTypeName, batchName);
123            }
124        } else {
125            addExecutionError(ExecutionErrors.UnknownBatchTypeName.name(), batchTypeName);
126        }
127
128        return batchAlias;
129    }
130
131    @Override
132    public BatchAlias getLockEntity(BatchAlias batchAlias) {
133        return batchAlias;
134    }
135
136    @Override
137    public void fillInResult(EditBatchAliasResult result, BatchAlias batchAlias) {
138        var batchControl = Session.getModelController(BatchControl.class);
139
140        result.setBatchAlias(batchControl.getBatchAliasTransfer(getUserVisit(), batchAlias));
141    }
142
143    @Override
144    public void doLock(BatchAliasEdit edit, BatchAlias batchAlias) {
145        edit.setAlias(batchAlias.getAlias());
146    }
147
148    @Override
149    public void canUpdate(BatchAlias batchAlias) {
150        var batchControl = Session.getModelController(BatchControl.class);
151        var alias = edit.getAlias();
152        var duplicateBatchAlias = batchControl.getBatchAliasByAlias(batchAliasType, alias);
153
154        if(duplicateBatchAlias != null && !batchAlias.equals(duplicateBatchAlias)) {
155            var batchAliasTypeDetail = batchAlias.getBatchAliasType().getLastDetail();
156
157            addExecutionError(ExecutionErrors.DuplicateBatchAlias.name(), batchAliasTypeDetail.getBatchType().getLastDetail().getBatchTypeName(),
158                    batchAliasTypeDetail.getBatchAliasTypeName(), alias);
159        }
160    }
161
162    @Override
163    public void doUpdate(BatchAlias batchAlias) {
164        var batchControl = Session.getModelController(BatchControl.class);
165        var batchAliasValue = batchControl.getBatchAliasValue(batchAlias);
166
167        batchAliasValue.setAlias(edit.getAlias());
168
169        batchControl.updateBatchAliasFromValue(batchAliasValue, getPartyPK());
170    }
171
172}