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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.EntityNameAttributeEdit; 021import com.echothree.control.user.core.common.form.EditEntityNameAttributeForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.result.EditEntityNameAttributeResult; 024import com.echothree.control.user.core.common.spec.EntityNameAttributeSpec; 025import com.echothree.model.control.core.server.logic.EntityAttributeLogic; 026import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.data.core.server.entity.EntityNameAttribute; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.command.BaseResult; 031import com.echothree.util.common.command.EditMode; 032import com.echothree.util.common.message.ExecutionErrors; 033import com.echothree.util.common.validation.FieldDefinition; 034import com.echothree.util.common.validation.FieldType; 035import com.echothree.util.server.control.BaseEditCommand; 036import com.echothree.util.server.control.CommandSecurityDefinition; 037import com.echothree.util.server.control.PartyTypeDefinition; 038import com.echothree.util.server.persistence.PersistenceUtils; 039import java.util.Arrays; 040import java.util.Collections; 041import java.util.List; 042import java.util.regex.Pattern; 043 044public class EditEntityNameAttributeCommand 045 extends BaseEditCommand<EntityNameAttributeSpec, EntityNameAttributeEdit> { 046 047 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 048 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 049 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null) 055 )); 056 057 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 058 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 059 new FieldDefinition("Key", FieldType.KEY, false, null, null), 060 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 061 new FieldDefinition("Ulid", FieldType.ULID, false, null, null), 062 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("EntityAttributeUlid", FieldType.ULID, false, null, null) 064 )); 065 066 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("NameAttribute", FieldType.ENTITY_NAME, true, null, null) 068 )); 069 } 070 071 /** Creates a new instance of EditEntityNameAttributeCommand */ 072 public EditEntityNameAttributeCommand(UserVisitPK userVisitPK, EditEntityNameAttributeForm form) { 073 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 protected BaseResult execute() { 078 EditEntityNameAttributeResult result = CoreResultFactory.getEditEntityNameAttributeResult(); 079 var parameterCount = EntityInstanceLogic.getInstance().countPossibleEntitySpecs(spec); 080 081 if(parameterCount == 1) { 082 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, spec); 083 084 if(!hasExecutionErrors()) { 085 var entityAttributeName = spec.getEntityAttributeName(); 086 var entityAttributeUlid = spec.getEntityAttributeUlid(); 087 088 parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUlid == null ? 0 : 1); 089 090 if(parameterCount == 1) { 091 var entityAttribute = entityAttributeName == null ? 092 EntityAttributeLogic.getInstance().getEntityAttributeByUlid(this, entityAttributeUlid) : 093 EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName); 094 095 if(!hasExecutionErrors()) { 096 if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) { 097 var coreControl = getCoreControl(); 098 EntityNameAttribute entityNameAttribute = null; 099 var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 100 101 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 102 entityNameAttribute = coreControl.getEntityNameAttribute(entityAttribute, entityInstance); 103 104 if(entityNameAttribute != null) { 105 if(editMode.equals(EditMode.LOCK)) { 106 result.setEntityNameAttribute(coreControl.getEntityNameAttributeTransfer(getUserVisit(), 107 entityNameAttribute, entityInstance)); 108 109 if(lockEntity(basePK)) { 110 var edit = CoreEditFactory.getEntityNameAttributeEdit(); 111 112 result.setEdit(edit); 113 edit.setNameAttribute(entityNameAttribute.getNameAttribute()); 114 } else { 115 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 116 } 117 } else { // EditMode.ABANDON 118 unlockEntity(basePK); 119 basePK = null; 120 } 121 } else { 122 addExecutionError(ExecutionErrors.UnknownEntityNameAttribute.name(), 123 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 124 } 125 } else if(editMode.equals(EditMode.UPDATE)) { 126 var entityAttributeString = coreControl.getEntityAttributeString(entityAttribute); 127 var validationPattern = entityAttributeString == null ? null : entityAttributeString.getValidationPattern(); 128 var stringAttribute = edit.getNameAttribute(); 129 130 if(validationPattern != null) { 131 var pattern = Pattern.compile(validationPattern); 132 var matcher = pattern.matcher(stringAttribute); 133 134 if(!matcher.matches()) { 135 addExecutionError(ExecutionErrors.InvalidNameAttribute.name(), stringAttribute); 136 } 137 } 138 139 if(!hasExecutionErrors()) { 140 entityNameAttribute = coreControl.getEntityNameAttributeForUpdate(entityAttribute, entityInstance); 141 142 if(entityNameAttribute != null) { 143 if(lockEntityForUpdate(basePK)) { 144 try { 145 var entityNameAttributeValue = coreControl.getEntityNameAttributeValueForUpdate(entityNameAttribute); 146 147 entityNameAttributeValue.setNameAttribute(stringAttribute); 148 149 coreControl.updateEntityNameAttributeFromValue(entityNameAttributeValue, getPartyPK()); 150 } finally { 151 unlockEntity(basePK); 152 basePK = null; 153 } 154 } else { 155 addExecutionError(ExecutionErrors.EntityLockStale.name()); 156 } 157 } else { 158 addExecutionError(ExecutionErrors.UnknownEntityNameAttribute.name(), 159 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 160 } 161 } 162 } 163 164 if(basePK != null) { 165 result.setEntityLock(getEntityLockTransfer(basePK)); 166 } 167 168 if(entityNameAttribute != null) { 169 result.setEntityNameAttribute(coreControl.getEntityNameAttributeTransfer(getUserVisit(), entityNameAttribute, entityInstance)); 170 } 171 } else { 172 addExecutionError(ExecutionErrors.MismatchedEntityType.name()); 173 } 174 } 175 } else { 176 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 177 } 178 } 179 } else { 180 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 181 } 182 183 return result; 184 } 185 186}