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.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.result.EditEntityStringAttributeResult; 024import com.echothree.control.user.core.common.spec.EntityStringAttributeSpec; 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.control.party.server.logic.LanguageLogic; 029import com.echothree.model.data.core.server.entity.EntityAttribute; 030import com.echothree.model.data.core.server.entity.EntityAttributeString; 031import com.echothree.model.data.core.server.entity.EntityInstance; 032import com.echothree.model.data.core.server.entity.EntityStringAttribute; 033import com.echothree.model.data.core.server.value.EntityStringAttributeValue; 034import com.echothree.model.data.party.server.entity.Language; 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.BaseResult; 040import com.echothree.util.common.command.EditMode; 041import com.echothree.util.common.persistence.BasePK; 042import com.echothree.util.server.control.BaseEditCommand; 043import com.echothree.util.server.control.CommandSecurityDefinition; 044import com.echothree.util.server.control.PartyTypeDefinition; 045import com.echothree.util.server.persistence.PersistenceUtils; 046import java.util.Arrays; 047import java.util.Collections; 048import java.util.List; 049import java.util.regex.Matcher; 050import java.util.regex.Pattern; 051 052public class EditEntityStringAttributeCommand 053 extends BaseEditCommand<EntityStringAttributeSpec, EntityStringAttributeEdit> { 054 055 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 056 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 057 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 058 059 static { 060 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 061 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 062 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null) 063 )); 064 065 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 066 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 067 new FieldDefinition("Key", FieldType.KEY, false, null, null), 068 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 069 new FieldDefinition("Ulid", FieldType.ULID, false, null, null), 070 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 071 new FieldDefinition("EntityAttributeUlid", FieldType.ULID, false, null, null), 072 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 073 new FieldDefinition("LanguageUlid", FieldType.ENTITY_NAME, false, null, null) 074 )); 075 076 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 077 new FieldDefinition("StringAttribute", FieldType.STRING, true, 1L, 512L) 078 )); 079 } 080 081 /** Creates a new instance of EditEntityStringAttributeCommand */ 082 public EditEntityStringAttributeCommand(UserVisitPK userVisitPK, EditEntityStringAttributeForm form) { 083 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 084 } 085 086 @Override 087 protected BaseResult execute() { 088 EditEntityStringAttributeResult result = CoreResultFactory.getEditEntityStringAttributeResult(); 089 var parameterCount = EntityInstanceLogic.getInstance().countPossibleEntitySpecs(spec); 090 091 if(parameterCount == 1) { 092 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, spec); 093 094 if(!hasExecutionErrors()) { 095 String entityAttributeName = spec.getEntityAttributeName(); 096 String entityAttributeUlid = spec.getEntityAttributeUlid(); 097 098 parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUlid == null ? 0 : 1); 099 100 if(parameterCount == 1) { 101 EntityAttribute entityAttribute = entityAttributeName == null ? 102 EntityAttributeLogic.getInstance().getEntityAttributeByUlid(this, entityAttributeUlid) : 103 EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName); 104 105 if(!hasExecutionErrors()) { 106 if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) { 107 String languageIsoName = spec.getLanguageIsoName(); 108 String languageUlid = spec.getLanguageUlid(); 109 110 parameterCount = (languageIsoName == null ? 0 : 1) + (languageUlid == null ? 0 : 1); 111 112 if(parameterCount == 1) { 113 Language language = languageIsoName == null ? 114 LanguageLogic.getInstance().getLanguageByUlid(this, languageUlid) : 115 LanguageLogic.getInstance().getLanguageByName(this, languageIsoName); 116 117 if(!hasExecutionErrors()) { 118 var coreControl = getCoreControl(); 119 EntityStringAttribute entityStringAttribute = null; 120 BasePK basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 121 122 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 123 entityStringAttribute = coreControl.getEntityStringAttribute(entityAttribute, entityInstance, language); 124 125 if(entityStringAttribute != null) { 126 if(editMode.equals(EditMode.LOCK)) { 127 result.setEntityStringAttribute(coreControl.getEntityStringAttributeTransfer(getUserVisit(), 128 entityStringAttribute, entityInstance)); 129 130 if(lockEntity(basePK)) { 131 EntityStringAttributeEdit edit = CoreEditFactory.getEntityStringAttributeEdit(); 132 133 result.setEdit(edit); 134 edit.setStringAttribute(entityStringAttribute.getStringAttribute()); 135 } else { 136 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 137 } 138 } else { // EditMode.ABANDON 139 unlockEntity(basePK); 140 basePK = null; 141 } 142 } else { 143 addExecutionError(ExecutionErrors.UnknownEntityStringAttribute.name(), 144 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 145 } 146 } else if(editMode.equals(EditMode.UPDATE)) { 147 EntityAttributeString entityAttributeString = coreControl.getEntityAttributeString(entityAttribute); 148 String validationPattern = entityAttributeString == null ? null : entityAttributeString.getValidationPattern(); 149 String stringAttribute = edit.getStringAttribute(); 150 151 if(validationPattern != null) { 152 Pattern pattern = Pattern.compile(validationPattern); 153 Matcher m = pattern.matcher(stringAttribute); 154 155 if(!m.matches()) { 156 addExecutionError(ExecutionErrors.InvalidStringAttribute.name(), stringAttribute); 157 } 158 } 159 160 if(!hasExecutionErrors()) { 161 entityStringAttribute = coreControl.getEntityStringAttributeForUpdate(entityAttribute, entityInstance, language); 162 163 if(entityStringAttribute != null) { 164 if(lockEntityForUpdate(basePK)) { 165 try { 166 EntityStringAttributeValue entityStringAttributeValue = coreControl.getEntityStringAttributeValueForUpdate(entityStringAttribute); 167 168 entityStringAttributeValue.setStringAttribute(stringAttribute); 169 170 coreControl.updateEntityStringAttributeFromValue(entityStringAttributeValue, getPartyPK()); 171 } finally { 172 unlockEntity(basePK); 173 basePK = null; 174 } 175 } else { 176 addExecutionError(ExecutionErrors.EntityLockStale.name()); 177 } 178 } else { 179 addExecutionError(ExecutionErrors.UnknownEntityStringAttribute.name(), 180 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), entityAttributeName); 181 } 182 } 183 } 184 185 if(basePK != null) { 186 result.setEntityLock(getEntityLockTransfer(basePK)); 187 } 188 189 if(entityStringAttribute != null) { 190 result.setEntityStringAttribute(coreControl.getEntityStringAttributeTransfer(getUserVisit(), entityStringAttribute, entityInstance)); 191 } 192 } 193 } else { 194 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 195 } 196 } else { 197 addExecutionError(ExecutionErrors.MismatchedEntityType.name()); 198 } 199 } 200 } else { 201 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 202 } 203 } 204 } else { 205 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 206 } 207 208 return result; 209 } 210 211}