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