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.model.control.core.server.transfer; 018 019import com.echothree.model.control.core.common.CoreOptions; 020import com.echothree.model.control.core.common.transfer.EntityAttributeTransfer; 021import com.echothree.model.control.core.common.transfer.EntityBlobAttributeTransfer; 022import com.echothree.model.control.core.common.transfer.EntityInstanceTransfer; 023import com.echothree.model.control.core.common.transfer.EntityTimeTransfer; 024import com.echothree.model.control.core.common.transfer.MimeTypeTransfer; 025import com.echothree.model.control.core.server.control.CoreControl; 026import com.echothree.model.control.party.common.transfer.LanguageTransfer; 027import com.echothree.model.control.party.server.control.PartyControl; 028import com.echothree.model.data.core.server.entity.EntityBlobAttribute; 029import com.echothree.model.data.core.server.entity.EntityInstance; 030import com.echothree.model.data.user.server.entity.UserVisit; 031import com.echothree.util.common.persistence.type.ByteArray; 032import com.echothree.util.server.persistence.Session; 033import java.util.Set; 034 035public class EntityBlobAttributeTransferCache 036 extends BaseCoreTransferCache<EntityBlobAttribute, EntityBlobAttributeTransfer> { 037 038 CoreControl coreControl = Session.getModelController(CoreControl.class); 039 PartyControl partyControl = Session.getModelController(PartyControl.class); 040 041 boolean includeBlob; 042 boolean includeETag; 043 044 /** Creates a new instance of EntityBlobAttributeTransferCache */ 045 public EntityBlobAttributeTransferCache(UserVisit userVisit) { 046 super(userVisit); 047 048 var options = session.getOptions(); 049 if(options != null) { 050 includeBlob = options.contains(CoreOptions.EntityBlobAttributeIncludeBlob); 051 includeETag = options.contains(CoreOptions.EntityBlobAttributeIncludeETag); 052 } 053 } 054 055 public EntityBlobAttributeTransfer getEntityBlobAttributeTransfer(EntityBlobAttribute entityBlobAttribute, EntityInstance entityInstance) { 056 EntityBlobAttributeTransfer entityBlobAttributeTransfer = get(entityBlobAttribute); 057 058 if(entityBlobAttributeTransfer == null) { 059 EntityAttributeTransfer entityAttribute = entityInstance == null ? coreControl.getEntityAttributeTransfer(userVisit, entityBlobAttribute.getEntityAttribute(), entityInstance) : null; 060 EntityInstanceTransfer entityInstanceTransfer = coreControl.getEntityInstanceTransfer(userVisit, entityBlobAttribute.getEntityInstance(), false, false, false, false, false, false); 061 LanguageTransfer language = partyControl.getLanguageTransfer(userVisit, entityBlobAttribute.getLanguage()); 062 ByteArray blobAttribute = includeBlob ? entityBlobAttribute.getBlobAttribute() : null; 063 MimeTypeTransfer mimeType = coreControl.getMimeTypeTransfer(userVisit, entityBlobAttribute.getMimeType()); 064 String eTag = null; 065 066 if(includeETag) { 067 // Item Descriptions do not have their own EntityTime, fall back on the Item's EntityTime. 068 EntityTimeTransfer entityTimeTransfer = entityInstanceTransfer.getEntityTime(); 069 Long modifiedTime = entityTimeTransfer.getUnformattedModifiedTime(); 070 long maxTime = modifiedTime == null ? entityTimeTransfer.getUnformattedCreatedTime() : modifiedTime; 071 long eTagEntityId = entityBlobAttribute.getPrimaryKey().getEntityId(); 072 int eTagSize = entityBlobAttribute.getBlobAttribute().length(); 073 074 // EntityId-Size-ModifiedTime 075 eTag = new StringBuilder(Long.toHexString(eTagEntityId)).append('-').append(Integer.toHexString(eTagSize)).append('-').append(Long.toHexString(maxTime)).toString(); 076 } 077 078 entityBlobAttributeTransfer = new EntityBlobAttributeTransfer(entityAttribute, entityInstanceTransfer, language, blobAttribute, mimeType, eTag); 079 put(entityBlobAttribute, entityBlobAttributeTransfer); 080 } 081 082 return entityBlobAttributeTransfer; 083 } 084 085}