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.model.control.core.server.transfer; 018 019import com.echothree.model.control.core.common.CoreOptions; 020import com.echothree.model.control.core.common.transfer.EntityClobAttributeTransfer; 021import com.echothree.model.control.core.server.control.CoreControl; 022import com.echothree.model.control.core.server.control.EntityInstanceControl; 023import com.echothree.model.control.core.server.control.MimeTypeControl; 024import com.echothree.model.control.party.server.control.PartyControl; 025import com.echothree.model.data.core.server.entity.EntityClobAttribute; 026import com.echothree.model.data.core.server.entity.EntityInstance; 027import com.echothree.model.data.user.server.entity.UserVisit; 028import com.echothree.util.server.persistence.Session; 029import javax.enterprise.context.RequestScoped; 030 031@RequestScoped 032public class EntityClobAttributeTransferCache 033 extends BaseCoreTransferCache<EntityClobAttribute, EntityClobAttributeTransfer> { 034 035 CoreControl coreControl = Session.getModelController(CoreControl.class); 036 EntityInstanceControl entityInstanceControl = Session.getModelController(EntityInstanceControl.class); 037 MimeTypeControl mimeTypeControl = Session.getModelController(MimeTypeControl.class); 038 PartyControl partyControl = Session.getModelController(PartyControl.class); 039 040 boolean includeClob; 041 boolean includeETag; 042 043 /** Creates a new instance of EntityClobAttributeTransferCache */ 044 protected EntityClobAttributeTransferCache() { 045 super(); 046 047 var options = session.getOptions(); 048 if(options != null) { 049 includeClob = options.contains(CoreOptions.EntityClobAttributeIncludeClob); 050 includeETag = options.contains(CoreOptions.EntityClobAttributeIncludeETag); 051 } 052 } 053 054 public EntityClobAttributeTransfer getEntityClobAttributeTransfer(final UserVisit userVisit, final EntityClobAttribute entityClobAttribute, final EntityInstance entityInstance) { 055 var entityClobAttributeTransfer = get(entityClobAttribute); 056 057 if(entityClobAttributeTransfer == null) { 058 var entityAttribute = entityInstance == null ? coreControl.getEntityAttributeTransfer(userVisit, entityClobAttribute.getEntityAttribute(), entityInstance) : null; 059 var entityInstanceTransfer = entityInstanceControl.getEntityInstanceTransfer(userVisit, entityClobAttribute.getEntityInstance(), false, false, false, false); 060 var language = partyControl.getLanguageTransfer(userVisit, entityClobAttribute.getLanguage()); 061 var clobAttribute = includeClob ? entityClobAttribute.getClobAttribute() : null; 062 var mimeType = mimeTypeControl.getMimeTypeTransfer(userVisit, entityClobAttribute.getMimeType()); 063 String eTag = null; 064 065 if(includeETag) { 066 // Item Descriptions do not have their own EntityTime, fall back on the Item's EntityTime. 067 var entityTimeTransfer = entityInstanceTransfer.getEntityTime(); 068 var modifiedTime = entityTimeTransfer.getUnformattedModifiedTime(); 069 long maxTime = modifiedTime == null ? entityTimeTransfer.getUnformattedCreatedTime() : modifiedTime; 070 long eTagEntityId = entityClobAttribute.getPrimaryKey().getEntityId(); 071 var eTagSize = entityClobAttribute.getClobAttribute().length(); 072 073 // EntityId-Size-ModifiedTime 074 eTag = Long.toHexString(eTagEntityId) + '-' + Integer.toHexString(eTagSize) + '-' + Long.toHexString(maxTime); 075 } 076 077 entityClobAttributeTransfer = new EntityClobAttributeTransfer(entityAttribute, entityInstanceTransfer, language, clobAttribute, mimeType, eTag); 078 put(userVisit, entityClobAttribute, entityClobAttributeTransfer); 079 } 080 081 return entityClobAttributeTransfer; 082 } 083 084}