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.EntityBlobAttributeEdit; 021import com.echothree.control.user.core.common.form.EditEntityBlobAttributeForm; 022import com.echothree.control.user.core.common.result.CoreResultFactory; 023import com.echothree.control.user.core.common.spec.EntityBlobAttributeSpec; 024import com.echothree.model.control.core.common.EntityAttributeTypes; 025import com.echothree.model.control.core.server.control.EntityInstanceControl; 026import com.echothree.model.control.core.server.logic.MimeTypeLogic; 027import com.echothree.model.control.party.common.PartyTypes; 028import com.echothree.model.control.party.server.control.PartyControl; 029import com.echothree.model.data.core.server.entity.EntityBlobAttribute; 030import com.echothree.model.data.user.common.pk.UserVisitPK; 031import com.echothree.util.common.command.BaseResult; 032import com.echothree.util.common.command.EditMode; 033import com.echothree.util.common.message.ExecutionErrors; 034import com.echothree.util.common.validation.FieldDefinition; 035import com.echothree.util.common.validation.FieldType; 036import com.echothree.util.server.control.BaseEditCommand; 037import com.echothree.util.server.control.CommandSecurityDefinition; 038import com.echothree.util.server.control.PartyTypeDefinition; 039import com.echothree.util.server.persistence.PersistenceUtils; 040import com.echothree.util.server.persistence.Session; 041import java.util.Arrays; 042import java.util.Collections; 043import java.util.List; 044import javax.enterprise.context.RequestScoped; 045 046@RequestScoped 047public class EditEntityBlobAttributeCommand 048 extends BaseEditCommand<EntityBlobAttributeSpec, EntityBlobAttributeEdit> { 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, true, null, null), 062 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, true, null, null), 063 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, true, null, null) 064 )); 065 066 EDIT_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 067 new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null) 068 )); 069 } 070 071 /** Creates a new instance of EditEntityBlobAttributeCommand */ 072 public EditEntityBlobAttributeCommand() { 073 super(COMMAND_SECURITY_DEFINITION, SPEC_FIELD_DEFINITIONS, EDIT_FIELD_DEFINITIONS); 074 } 075 076 @Override 077 protected BaseResult execute() { 078 var entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 079 var result = CoreResultFactory.getEditEntityBlobAttributeResult(); 080 var entityRef = spec.getEntityRef(); 081 var entityInstance = entityInstanceControl.getEntityInstanceByEntityRef(entityRef); 082 083 if(entityInstance != null) { 084 var entityAttributeName = spec.getEntityAttributeName(); 085 var entityAttribute = coreControl.getEntityAttributeByName(entityInstance.getEntityType(), entityAttributeName); 086 087 if(entityAttribute != null) { 088 var partyControl = Session.getModelController(PartyControl.class); 089 var languageIsoName = spec.getLanguageIsoName(); 090 var language = partyControl.getLanguageByIsoName(languageIsoName); 091 092 if(language != null) { 093 EntityBlobAttribute entityBlobAttribute = null; 094 var basePK = PersistenceUtils.getInstance().getBasePKFromEntityInstance(entityInstance); 095 096 if(editMode.equals(EditMode.LOCK) || editMode.equals(EditMode.ABANDON)) { 097 entityBlobAttribute = coreControl.getEntityBlobAttribute(entityAttribute, entityInstance, language); 098 099 if(entityBlobAttribute != null) { 100 if(editMode.equals(EditMode.LOCK)) { 101 result.setEntityBlobAttribute(coreControl.getEntityBlobAttributeTransfer(getUserVisit(), 102 entityBlobAttribute, entityInstance)); 103 104 if(lockEntity(basePK)) { 105 var edit = CoreEditFactory.getEntityBlobAttributeEdit(); 106 107 result.setEdit(edit); 108 edit.setMimeTypeName(entityBlobAttribute.getMimeType().getLastDetail().getMimeTypeName()); 109 } else { 110 addExecutionError(ExecutionErrors.EntityLockFailed.name()); 111 } 112 } else { // EditMode.ABANDON 113 unlockEntity(basePK); 114 basePK = null; 115 } 116 } else { 117 var entityTypeDetail = entityInstance.getEntityType().getLastDetail(); 118 119 addExecutionError(ExecutionErrors.UnknownEntityBlobAttribute.name(), entityRef, 120 entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), 121 entityTypeDetail.getEntityTypeName(), entityAttributeName, languageIsoName); 122 } 123 } else if(editMode.equals(EditMode.UPDATE)) { 124 entityBlobAttribute = coreControl.getEntityBlobAttributeForUpdate(entityAttribute, entityInstance, language); 125 126 if(entityBlobAttribute != null) { 127 var mimeType = MimeTypeLogic.getInstance().getMimeTypeByName(this, edit.getMimeTypeName()); 128 129 if(!hasExecutionErrors()) { 130 if(mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName().equals(EntityAttributeTypes.BLOB.name())) { 131 var blobAttribute = edit.getBlobAttribute(); 132 133 if(blobAttribute != null) { 134 if(lockEntityForUpdate(basePK)) { 135 try { 136 var entityBlobAttributeValue = coreControl.getEntityBlobAttributeValueForUpdate(entityBlobAttribute); 137 138 entityBlobAttributeValue.setBlobAttribute(blobAttribute); 139 entityBlobAttributeValue.setMimeTypePK(mimeType.getPrimaryKey()); 140 141 coreControl.updateEntityBlobAttributeFromValue(entityBlobAttributeValue, getPartyPK()); 142 } finally { 143 unlockEntity(basePK); 144 basePK = null; 145 } 146 } else { 147 addExecutionError(ExecutionErrors.EntityLockStale.name()); 148 } 149 } else { 150 addExecutionError(ExecutionErrors.MissingBlobAttribute.name()); 151 } 152 } else { 153 addExecutionError(ExecutionErrors.InvalidMimeType.name(), mimeType.getLastDetail().getMimeTypeName()); 154 } 155 } 156 } else { 157 var entityTypeDetail = entityInstance.getEntityType().getLastDetail(); 158 159 addExecutionError(ExecutionErrors.UnknownEntityBlobAttribute.name(), entityRef, 160 entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), 161 entityTypeDetail.getEntityTypeName(), entityAttributeName, languageIsoName); 162 } 163 } 164 165 if(basePK != null) { 166 result.setEntityLock(getEntityLockTransfer(basePK)); 167 } 168 169 if(entityBlobAttribute != null) { 170 result.setEntityBlobAttribute(coreControl.getEntityBlobAttributeTransfer(getUserVisit(), entityBlobAttribute, entityInstance)); 171 } 172 } else { 173 addExecutionError(ExecutionErrors.UnknownLanguageIsoName.name(), languageIsoName); 174 } 175 } else { 176 var entityTypeDetail = entityInstance.getEntityType().getLastDetail(); 177 178 addExecutionError(ExecutionErrors.UnknownEntityAttributeName.name(), entityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), 179 entityTypeDetail.getEntityTypeName(), entityAttributeName); 180 } 181 } else { 182 addExecutionError(ExecutionErrors.UnknownEntityRef.name(), entityRef); 183 } 184 185 return result; 186 } 187 188}