001// -------------------------------------------------------------------------------- 002// Copyright 2002-2026 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.forum.server.transfer; 018 019import javax.inject.Inject; 020import com.echothree.model.control.core.server.control.MimeTypeControl; 021import com.echothree.model.control.forum.common.ForumOptions; 022import com.echothree.model.control.forum.common.transfer.ForumMessageAttachmentTransfer; 023import com.echothree.model.control.forum.server.control.ForumControl; 024import com.echothree.model.data.forum.server.entity.ForumMessageAttachment; 025import com.echothree.model.data.user.server.entity.UserVisit; 026import com.echothree.util.common.persistence.type.ByteArray; 027import javax.enterprise.context.RequestScoped; 028 029@RequestScoped 030public class ForumMessageAttachmentTransferCache 031 extends BaseForumTransferCache<ForumMessageAttachment, ForumMessageAttachmentTransfer> { 032 033 @Inject 034 ForumControl forumControl; 035 036 @Inject 037 MimeTypeControl mimeTypeControl; 038 039 boolean includeBlob; 040 boolean includeClob; 041 boolean includeETag; 042 043 /** Creates a new instance of ForumMessageAttachmentTransferCache */ 044 protected ForumMessageAttachmentTransferCache() { 045 super(); 046 047 var options = session.getOptions(); 048 if(options != null) { 049 includeBlob = options.contains(ForumOptions.ForumMessageAttachmentIncludeBlob); 050 includeClob = options.contains(ForumOptions.ForumMessageAttachmentIncludeClob); 051 includeETag = options.contains(ForumOptions.ForumMessageAttachmentIncludeETag); 052 } 053 054 setIncludeEntityInstance(true); 055 } 056 057 public ForumMessageAttachmentTransfer getForumMessageAttachmentTransfer(UserVisit userVisit, ForumMessageAttachment forumMessageAttachment) { 058 var forumMessageAttachmentTransfer = get(forumMessageAttachment); 059 060 if(forumMessageAttachmentTransfer == null) { 061 var forumMessageAttachmentDetail = forumMessageAttachment.getLastDetail(); 062 var forumMessage = forumControl.getForumMessageTransfer(userVisit, forumMessageAttachmentDetail.getForumMessage()); 063 var forumMessageAttachmentSequence = forumMessageAttachmentDetail.getForumMessageAttachmentSequence(); 064 var mimeType = mimeTypeControl.getMimeTypeTransfer(userVisit, forumMessageAttachmentDetail.getMimeType()); 065 var description = forumControl.getBestForumMessageAttachmentDescription(forumMessageAttachment, getLanguage(userVisit)); 066 ByteArray blob = null; 067 String clob = null; 068 String eTag = null; 069 long eTagEntityId = 0; 070 var eTagSize = 0; 071 072 if(includeBlob) { 073 var forumMessageAttachmentBlob = forumControl.getForumMessageBlobAttachment(forumMessageAttachment); 074 075 if(forumMessageAttachmentBlob != null) { 076 blob = forumMessageAttachmentBlob.getBlob(); 077 } 078 } 079 080 if(includeClob) { 081 var forumMessageAttachmentClob = forumControl.getForumMessageClobAttachment(forumMessageAttachment); 082 083 if(forumMessageAttachmentClob != null) { 084 clob = forumMessageAttachmentClob.getClob(); 085 } 086 } 087 088 if(includeETag && eTagEntityId != 0) { 089 // Forum Message Attachments do not have their own EntityTime, fall back on the Item's EntityTime. 090 var entityTimeTransfer = forumMessage.getEntityInstance().getEntityTime(); 091 var modifiedTime = entityTimeTransfer.getUnformattedModifiedTime(); 092 long maxTime = modifiedTime == null ? entityTimeTransfer.getUnformattedCreatedTime() : modifiedTime; 093 094 // EntityId-Size-ModifiedTime 095 eTag = Long.toHexString(eTagEntityId) + '-' + Integer.toHexString(eTagSize) + '-' + Long.toHexString(maxTime); 096 } 097 098 forumMessageAttachmentTransfer = new ForumMessageAttachmentTransfer(forumMessage, forumMessageAttachmentSequence, mimeType, description, blob, clob, 099 eTag); 100 put(userVisit, forumMessageAttachment, forumMessageAttachmentTransfer); 101 } 102 103 return forumMessageAttachmentTransfer; 104 } 105 106}