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.core.server.command; 018 019import com.echothree.control.user.core.common.edit.CoreEditFactory; 020import com.echothree.control.user.core.common.edit.EntityStringAttributeEdit; 021import com.echothree.control.user.core.common.form.EditEntityStringAttributeForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.spec.EntityStringAttributeSpec; 024import com.echothree.model.control.core.server.logic.EntityAttributeLogic; 025import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 026import com.echothree.model.control.party.common.PartyTypes; 027import com.echothree.model.control.party.server.logic.LanguageLogic; 028import com.echothree.model.data.core.server.entity.EntityStringAttribute; 029import com.echothree.model.data.user.common.pk.UserVisitPK; 030import com.echothree.util.common.message.ExecutionErrors; 031import com.echothree.util.common.validation.FieldDefinition; 032import com.echothree.util.common.validation.FieldType; 033import com.echothree.util.common.command.BaseResult; 034import com.echothree.util.common.command.EditMode; 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.List; 040import java.util.regex.Pattern; 041import javax.enterprise.context.Dependent; 042 043@Dependent 044public class EditEntityStringAttributeCommand 045 extends BaseEditCommand<EntityStringAttributeSpec, EntityStringAttributeEdit> { 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 = List.of( 058 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 059 new FieldDefinition("Uuid", FieldType.UUID, false, null, null), 060 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 061 new FieldDefinition("EntityAttributeUuid", FieldType.UUID, false, null, null), 062 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("LanguageUuid", FieldType.ENTITY_NAME, false, null, null) 064 ); 065 066 EDIT_FIELD_DEFINITIONS = List.of( 067 new FieldDefinition("StringAttribute", FieldType.STRING, true, 1L, 512L) 068 ); 069 } 070 071 /** Creates a new instance of EditEntityStringAttributeCommand */ 072 public EditEntityStringAttributeCommand() { 073 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 protected BaseResult execute() { 078 var result = CoreResultFactory.getEditEntityStringAttributeResult(); 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 entityAttributeUuid = spec.getEntityAttributeUuid(); 087 088 parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUuid == null ? 0 : 1); 089 090 if(parameterCount == 1) { 091 var entityAttribute = entityAttributeName == null ? 092 EntityAttributeLogic.getInstance().getEntityAttributeByUuid(this, entityAttributeUuid) : 093 EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName); 094 095 if(!hasExecutionErrors()) { 096 if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) { 097 var languageIsoName = spec.getLanguageIsoName(); 098 var languageUuid = spec.getLanguageUuid(); 099 100 parameterCount = (languageIsoName == null ? 0 : 1) + (languageUuid == null ? 0 : 1); 101 102 if(parameterCount == 1) { 103 var language = languageIsoName == null ? 104 LanguageLogic.getInstance().getLanguageByUuid(this, languageUuid) : 105 LanguageLogic.getInstance().getLanguageByName(this, languageIsoName); 106 107 if(!hasExecutionErrors()) { 108 EntityStringAttribute entityStringAttribute = null; 109 var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 110 111 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 112 entityStringAttribute = coreControl.getEntityStringAttribute(entityAttribute, entityInstance, language); 113 114 if(entityStringAttribute != null) { 115 if(editMode.equals(EditMode.LOCK)) { 116 result.setEntityStringAttribute(coreControl.getEntityStringAttributeTransfer(getUserVisit(), 117 entityStringAttribute, entityInstance)); 118 119 if(lockEntity(basePK)) { 120 var edit = CoreEditFactory.getEntityStringAttributeEdit(); 121 122 result.setEdit(edit); 123 edit.setStringAttribute(entityStringAttribute.getStringAttribute()); 124 } else { 125 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 126 } 127 } else { // EditMode.ABANDON 128 unlockEntity(basePK); 129 basePK = null; 130 } 131 } else { 132 addExecutionError(ExecutionErrors.UnknownEntityStringAttribute.name(), 133 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 134 } 135 } else if(editMode.equals(EditMode.UPDATE)) { 136 var entityAttributeString = coreControl.getEntityAttributeString(entityAttribute); 137 var validationPattern = entityAttributeString == null ? null : entityAttributeString.getValidationPattern(); 138 var stringAttribute = edit.getStringAttribute(); 139 140 if(validationPattern != null) { 141 var pattern = Pattern.compile(validationPattern); 142 var m = pattern.matcher(stringAttribute); 143 144 if(!m.matches()) { 145 addExecutionError(ExecutionErrors.InvalidStringAttribute.name(), stringAttribute); 146 } 147 } 148 149 if(!hasExecutionErrors()) { 150 entityStringAttribute = coreControl.getEntityStringAttributeForUpdate(entityAttribute, entityInstance, language); 151 152 if(entityStringAttribute != null) { 153 if(lockEntityForUpdate(basePK)) { 154 try { 155 var entityStringAttributeValue = coreControl.getEntityStringAttributeValueForUpdate(entityStringAttribute); 156 157 entityStringAttributeValue.setStringAttribute(stringAttribute); 158 159 coreControl.updateEntityStringAttributeFromValue(entityStringAttributeValue, getPartyPK()); 160 } finally { 161 unlockEntity(basePK); 162 basePK = null; 163 } 164 } else { 165 addExecutionError(ExecutionErrors.EntityLockStale.name()); 166 } 167 } else { 168 addExecutionError(ExecutionErrors.UnknownEntityStringAttribute.name(), 169 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 170 } 171 } 172 } 173 174 if(basePK != null) { 175 result.setEntityLock(getEntityLockTransfer(basePK)); 176 } 177 178 if(entityStringAttribute != null) { 179 result.setEntityStringAttribute(coreControl.getEntityStringAttributeTransfer(getUserVisit(), entityStringAttribute, entityInstance)); 180 } 181 } 182 } else { 183 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 184 } 185 } else { 186 addExecutionError(ExecutionErrors.MismatchedEntityType.name()); 187 } 188 } 189 } else { 190 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 191 } 192 } 193 } else { 194 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 195 } 196 197 return result; 198 } 199 200}