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