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