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.EntityClobAttributeEdit; 021import com.echothree.control.user.core.common.form.EditEntityClobAttributeForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.spec.EntityClobAttributeSpec; 024import com.echothree.model.control.core.common.EntityAttributeTypes; 025import com.echothree.model.control.core.server.logic.EntityAttributeLogic; 026import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 027import com.echothree.model.control.core.server.logic.MimeTypeLogic; 028import com.echothree.model.control.party.common.PartyTypes; 029import com.echothree.model.control.party.server.logic.LanguageLogic; 030import com.echothree.model.data.core.server.entity.EntityClobAttribute; 031import com.echothree.model.data.core.server.entity.EntityTypeDetail; 032import com.echothree.model.data.core.server.entity.MimeType; 033import com.echothree.model.data.core.server.value.EntityClobAttributeValue; 034import com.echothree.model.data.user.common.pk.UserVisitPK; 035import com.echothree.util.common.command.BaseResult; 036import com.echothree.util.common.command.EditMode; 037import com.echothree.util.common.message.ExecutionErrors; 038import com.echothree.util.common.persistence.BasePK; 039import com.echothree.util.common.validation.FieldDefinition; 040import com.echothree.util.common.validation.FieldType; 041import com.echothree.util.server.control.BaseEditCommand; 042import com.echothree.util.server.control.CommandSecurityDefinition; 043import com.echothree.util.server.control.PartyTypeDefinition; 044import com.echothree.util.server.persistence.PersistenceUtils; 045import java.util.Arrays; 046import java.util.Collections; 047import java.util.List; 048 049public class EditEntityClobAttributeCommand 050 extends BaseEditCommand<EntityClobAttributeSpec, EntityClobAttributeEdit> { 051 052 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 053 private final static List<FieldDefinition> SPEC_FIELD_DEFINITIONS; 054 private final static List<FieldDefinition> EDIT_FIELD_DEFINITIONS; 055 056 static { 057 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 058 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 059 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null) 060 )); 061 062 SPEC_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 063 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 064 new FieldDefinition("Key", FieldType.KEY, false, null, null), 065 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 066 new FieldDefinition("Ulid", FieldType.ULID, false, null, null), 067 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 068 new FieldDefinition("EntityAttributeUlid", FieldType.ULID, false, null, null), 069 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 070 new FieldDefinition("LanguageUlid", FieldType.ENTITY_NAME, false, null, null) 071 )); 072 073 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 074 new FieldDefinition("ClobAttribute", FieldType.STRING, true, 1L, null), 075 new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null) 076 )); 077 } 078 079 /** Creates a new instance of EditEntityClobAttributeCommand */ 080 public EditEntityClobAttributeCommand(UserVisitPK userVisitPK, EditEntityClobAttributeForm form) { 081 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 082 } 083 084 @Override 085 protected BaseResult execute() { 086 var result = CoreResultFactory.getEditEntityClobAttributeResult(); 087 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, spec); 088 var language = LanguageLogic.getInstance().getLanguage(this, spec, spec); 089 var entityAttribute = EntityAttributeLogic.getInstance().getEntityAttribute(this, entityInstance, spec, spec, 090 EntityAttributeTypes.CLOB); 091 092 if(!hasExecutionErrors()) { 093 var coreControl = getCoreControl(); 094 EntityClobAttribute entityClobAttribute = null; 095 BasePK basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 096 097 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 098 entityClobAttribute = coreControl.getEntityClobAttribute(entityAttribute, entityInstance, language); 099 100 if(entityClobAttribute != null) { 101 if(editMode.equals(EditMode.LOCK)) { 102 result.setEntityClobAttribute(coreControl.getEntityClobAttributeTransfer(getUserVisit(), entityClobAttribute, entityInstance)); 103 104 if(lockEntity(basePK)) { 105 EntityClobAttributeEdit edit = CoreEditFactory.getEntityClobAttributeEdit(); 106 107 result.setEdit(edit); 108 edit.setClobAttribute(entityClobAttribute.getClobAttribute()); 109 edit.setMimeTypeName(entityClobAttribute.getMimeType().getLastDetail().getMimeTypeName()); 110 } else { 111 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 112 } 113 } else { // EditMode.ABANDON 114 unlockEntity(basePK); 115 basePK = null; 116 } 117 } else { 118 EntityTypeDetail entityTypeDetail = entityInstance.getEntityType().getLastDetail(); 119 120 addExecutionError(ExecutionErrors.UnknownEntityClobAttribute.name(), basePK.getEntityRef(), 121 entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), 122 entityTypeDetail.getEntityTypeName(), entityAttribute.getLastDetail().getEntityAttributeName(), 123 language.getLanguageIsoName()); 124 } 125 } else if(editMode.equals(EditMode.UPDATE)) { 126 entityClobAttribute = coreControl.getEntityClobAttributeForUpdate(entityAttribute, entityInstance, language); 127 128 if(entityClobAttribute != null) { 129 MimeType mimeType = MimeTypeLogic.getInstance().getMimeTypeByName(this, edit.getMimeTypeName()); 130 131 if(!hasExecutionErrors()) { 132 if(mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName().equals(EntityAttributeTypes.CLOB.name())) { 133 if(lockEntityForUpdate(basePK)) { 134 try { 135 EntityClobAttributeValue entityClobAttributeValue = coreControl.getEntityClobAttributeValueForUpdate(entityClobAttribute); 136 137 entityClobAttributeValue.setClobAttribute(edit.getClobAttribute()); 138 entityClobAttributeValue.setMimeTypePK(mimeType.getPrimaryKey()); 139 140 coreControl.updateEntityClobAttributeFromValue(entityClobAttributeValue, getPartyPK()); 141 } finally { 142 unlockEntity(basePK); 143 basePK = null; 144 } 145 } else { 146 addExecutionError(ExecutionErrors.EntityLockStale.name()); 147 } 148 } else { 149 addExecutionError(ExecutionErrors.InvalidMimeType.name(), mimeType.getLastDetail().getMimeTypeName()); 150 } 151 } 152 } else { 153 EntityTypeDetail entityTypeDetail = entityInstance.getEntityType().getLastDetail(); 154 155 addExecutionError(ExecutionErrors.UnknownEntityClobAttribute.name(), basePK.getEntityRef(), 156 entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), 157 entityTypeDetail.getEntityTypeName(), entityAttribute.getLastDetail().getEntityAttributeName(), 158 language.getLanguageIsoName()); 159 } 160 } 161 162 if(basePK != null) { 163 result.setEntityLock(getEntityLockTransfer(basePK)); 164 } 165 166 if(entityClobAttribute != null) { 167 result.setEntityClobAttribute(coreControl.getEntityClobAttributeTransfer(getUserVisit(), entityClobAttribute, entityInstance)); 168 } 169 } 170 171 return result; 172 } 173 174}