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 java.util.List; 039import javax.enterprise.context.Dependent; 040import javax.inject.Inject; 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 @Inject 062 BatchControl batchControl; 063 064 065 /** Creates a new instance of EditBatchAliasCommand */ 066 public EditBatchAliasCommand() { 067 super(null, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 068 } 069 070 @Override 071 protected CommandSecurityDefinition getCommandSecurityDefinition() { 072 return new CommandSecurityDefinition(List.of( 073 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 074 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), List.of( 075 new SecurityRoleDefinition(BatchAliasUtil.getInstance().getSecurityRoleGroupNameByBatchTypeSpec(spec), SecurityRoles.Edit.name()) 076 )) 077 )); 078 } 079 080 @Override 081 public EditBatchAliasResult getResult() { 082 return BatchResultFactory.getEditBatchAliasResult(); 083 } 084 085 @Override 086 public BatchAliasEdit getEdit() { 087 return BatchEditFactory.getBatchAliasEdit(); 088 } 089 090 BatchAliasType batchAliasType; 091 092 @Override 093 public BatchAlias getEntity(EditBatchAliasResult result) { 094 BatchAlias batchAlias = null; 095 var batchTypeName = spec.getBatchTypeName(); 096 var batchType = batchControl.getBatchTypeByName(batchTypeName); 097 098 if(batchType != null) { 099 var batchName = spec.getBatchName(); 100 var batch = batchControl.getBatchByName(batchType, batchName); 101 102 if(batch != null) { 103 var batchAliasTypeName = spec.getBatchAliasTypeName(); 104 105 batchAliasType = batchControl.getBatchAliasTypeByName(batchType, batchAliasTypeName); 106 107 if(batchAliasType != null) { 108 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 109 batchAlias = batchControl.getBatchAlias(batch, batchAliasType); 110 } else { // EditMode.UPDATE 111 batchAlias = batchControl.getBatchAliasForUpdate(batch, batchAliasType); 112 } 113 114 if(batchAlias != null) { 115 result.setBatchAlias(batchControl.getBatchAliasTransfer(getUserVisit(), batchAlias)); 116 } else { 117 addExecutionError(ExecutionErrors.UnknownBatchAlias.name(), batchTypeName, batchName, batchAliasTypeName); 118 } 119 } else { 120 addExecutionError(ExecutionErrors.UnknownBatchAliasTypeName.name(), batchTypeName, batchAliasTypeName); 121 } 122 } else { 123 addExecutionError(ExecutionErrors.UnknownBatchName.name(), batchTypeName, batchName); 124 } 125 } else { 126 addExecutionError(ExecutionErrors.UnknownBatchTypeName.name(), batchTypeName); 127 } 128 129 return batchAlias; 130 } 131 132 @Override 133 public BatchAlias getLockEntity(BatchAlias batchAlias) { 134 return batchAlias; 135 } 136 137 @Override 138 public void fillInResult(EditBatchAliasResult result, BatchAlias batchAlias) { 139 result.setBatchAlias(batchControl.getBatchAliasTransfer(getUserVisit(), batchAlias)); 140 } 141 142 @Override 143 public void doLock(BatchAliasEdit edit, BatchAlias batchAlias) { 144 edit.setAlias(batchAlias.getAlias()); 145 } 146 147 @Override 148 public void canUpdate(BatchAlias batchAlias) { 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 batchAliasValue = batchControl.getBatchAliasValue(batchAlias); 163 164 batchAliasValue.setAlias(edit.getAlias()); 165 166 batchControl.updateBatchAliasFromValue(batchAliasValue, getPartyPK()); 167 } 168 169}