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.form.CreateEntityBlobAttributeForm; 020import com.echothree.model.control.core.common.EntityAttributeTypes; 021import com.echothree.model.control.core.server.logic.EntityAttributeLogic; 022import com.echothree.model.control.core.server.logic.EntityInstanceLogic; 023import com.echothree.model.control.core.server.logic.MimeTypeLogic; 024import com.echothree.model.control.party.common.PartyTypes; 025import com.echothree.model.control.party.server.logic.LanguageLogic; 026import com.echothree.model.data.core.server.entity.EntityAttribute; 027import com.echothree.model.data.core.server.entity.EntityBlobAttribute; 028import com.echothree.model.data.core.server.entity.EntityInstance; 029import com.echothree.model.data.core.server.entity.EntityTypeDetail; 030import com.echothree.model.data.core.server.entity.MimeType; 031import com.echothree.model.data.party.server.entity.Language; 032import com.echothree.model.data.user.common.pk.UserVisitPK; 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.common.command.BaseResult; 037import com.echothree.util.common.persistence.type.ByteArray; 038import com.echothree.util.server.control.BaseSimpleCommand; 039import com.echothree.util.server.control.CommandSecurityDefinition; 040import com.echothree.util.server.control.PartyTypeDefinition; 041import java.util.Arrays; 042import java.util.Collections; 043import java.util.List; 044 045public class CreateEntityBlobAttributeCommand 046 extends BaseSimpleCommand<CreateEntityBlobAttributeForm> { 047 048 private final static CommandSecurityDefinition COMMAND_SECURITY_DEFINITION; 049 private final static List<FieldDefinition> FORM_FIELD_DEFINITIONS; 050 051 static { 052 COMMAND_SECURITY_DEFINITION = new CommandSecurityDefinition(List.of( 053 new PartyTypeDefinition(PartyTypes.UTILITY.name(), null), 054 new PartyTypeDefinition(PartyTypes.EMPLOYEE.name(), null) 055 )); 056 057 FORM_FIELD_DEFINITIONS = Collections.unmodifiableList(Arrays.asList( 058 new FieldDefinition("EntityRef", FieldType.ENTITY_REF, false, null, null), 059 new FieldDefinition("Key", FieldType.KEY, false, null, null), 060 new FieldDefinition("Guid", FieldType.GUID, false, null, null), 061 new FieldDefinition("Ulid", FieldType.ULID, false, null, null), 062 new FieldDefinition("EntityAttributeName", FieldType.ENTITY_NAME, false, null, null), 063 new FieldDefinition("EntityAttributeUlid", FieldType.ULID, false, null, null), 064 new FieldDefinition("LanguageIsoName", FieldType.ENTITY_NAME, false, null, null), 065 new FieldDefinition("LanguageUlid", FieldType.ENTITY_NAME, false, null, null), 066 new FieldDefinition("MimeTypeName", FieldType.MIME_TYPE, true, null, null) 067 )); 068 } 069 070 /** Creates a new instance of CreateEntityBlobAttributeCommand */ 071 public CreateEntityBlobAttributeCommand(UserVisitPK userVisitPK, CreateEntityBlobAttributeForm form) { 072 super(userVisitPK, form, COMMAND_SECURITY_DEFINITION, FORM_FIELD_DEFINITIONS, false); 073 } 074 075 @Override 076 protected BaseResult execute() { 077 var parameterCount = EntityInstanceLogic.getInstance().countPossibleEntitySpecs(form); 078 079 if(parameterCount == 1) { 080 var entityInstance = EntityInstanceLogic.getInstance().getEntityInstance(this, form); 081 082 if(!hasExecutionErrors()) { 083 String entityAttributeName = form.getEntityAttributeName(); 084 String entityAttributeUlid = form.getEntityAttributeUlid(); 085 086 parameterCount = (entityAttributeName == null ? 0 : 1) + (entityAttributeUlid == null ? 0 : 1); 087 088 if(parameterCount == 1) { 089 EntityAttribute entityAttribute = entityAttributeName == null ? 090 EntityAttributeLogic.getInstance().getEntityAttributeByUlid(this, entityAttributeUlid) : 091 EntityAttributeLogic.getInstance().getEntityAttributeByName(this, entityInstance.getEntityType(), entityAttributeName); 092 093 if(!hasExecutionErrors()) { 094 String entityAttributeTypeName = entityAttribute.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName(); 095 096 if(EntityAttributeTypes.BLOB.name().equals(entityAttributeTypeName)) { 097 if(entityInstance.getEntityType().equals(entityAttribute.getLastDetail().getEntityType())) { 098 String languageIsoName = form.getLanguageIsoName(); 099 String languageUlid = form.getLanguageUlid(); 100 101 parameterCount = (languageIsoName == null ? 0 : 1) + (languageUlid == null ? 0 : 1); 102 103 if(parameterCount == 1) { 104 Language language = languageIsoName == null ? 105 LanguageLogic.getInstance().getLanguageByUlid(this, languageUlid) : 106 LanguageLogic.getInstance().getLanguageByName(this, languageIsoName); 107 108 if(!hasExecutionErrors()) { 109 var coreControl = getCoreControl(); 110 EntityBlobAttribute entityBlobAttribute = coreControl.getEntityBlobAttribute(entityAttribute, entityInstance, language); 111 112 if(entityBlobAttribute == null) { 113 MimeType mimeType = MimeTypeLogic.getInstance().getMimeTypeByName(this, form.getMimeTypeName()); 114 115 if(!hasExecutionErrors()) { 116 if(mimeType.getLastDetail().getEntityAttributeType().getEntityAttributeTypeName().equals(EntityAttributeTypes.BLOB.name())) { 117 ByteArray blobAttribute = form.getBlobAttribute(); 118 119 if(blobAttribute != null) { 120 coreControl.createEntityBlobAttribute(entityAttribute, entityInstance, language, blobAttribute, mimeType, getPartyPK()); 121 } else { 122 addExecutionError(ExecutionErrors.MissingBlobAttribute.name()); 123 } 124 } else { 125 addExecutionError(ExecutionErrors.InvalidMimeType.name(), mimeType.getLastDetail().getMimeTypeName()); 126 } 127 } 128 } else { 129 addExecutionError(ExecutionErrors.DuplicateEntityBlobAttribute.name(), 130 EntityInstanceLogic.getInstance().getEntityRefFromEntityInstance(entityInstance), 131 entityAttribute.getLastDetail().getEntityAttributeName(), 132 language.getLanguageIsoName()); 133 } 134 } 135 } 136 } else { 137 EntityTypeDetail expectedEntityTypeDetail = entityAttribute.getLastDetail().getEntityType().getLastDetail(); 138 EntityTypeDetail suppliedEntityTypeDetail = entityInstance.getEntityType().getLastDetail(); 139 140 addExecutionError(ExecutionErrors.MismatchedEntityType.name(), 141 expectedEntityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), 142 expectedEntityTypeDetail.getEntityTypeName(), 143 suppliedEntityTypeDetail.getComponentVendor().getLastDetail().getComponentVendorName(), 144 suppliedEntityTypeDetail.getEntityTypeName()); 145 } 146 } else { 147 addExecutionError(ExecutionErrors.MismatchedEntityAttributeType.name(), 148 EntityAttributeTypes.BLOB.name(), entityAttributeTypeName); 149 } 150 } 151 } else { 152 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 153 } 154 } 155 } else { 156 addExecutionError(ExecutionErrors.InvalidParameterCount.name()); 157 } 158 159 return null; 160 } 161 162}