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