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.common.MimeTypes;
021import com.echothree.model.control.core.server.control.MimeTypeControl;
022import com.echothree.model.control.forum.common.ForumOptions;
023import com.echothree.model.control.forum.common.transfer.ForumMessagePartTransfer;
024import com.echothree.model.control.forum.server.control.ForumControl;
025import com.echothree.model.control.party.server.control.PartyControl;
026import com.echothree.model.data.forum.server.entity.ForumMessagePart;
027import com.echothree.model.data.user.server.entity.UserVisit;
028import com.echothree.util.common.persistence.type.ByteArray;
029import com.echothree.util.common.string.StringUtils;
030import javax.enterprise.context.RequestScoped;
031
032@RequestScoped
033public class ForumMessagePartTransferCache
034        extends BaseForumTransferCache<ForumMessagePart, ForumMessagePartTransfer> {
035
036    @Inject
037    ForumControl forumControl;
038
039    @Inject
040    MimeTypeControl mimeTypeControl;
041
042    @Inject
043    PartyControl partyControl;
044
045    boolean includeBlob;
046    boolean includeClob;
047    boolean includeString;
048    
049    /** Creates a new instance of ForumMessagePartTransferCache */
050    protected ForumMessagePartTransferCache() {
051        super();
052        
053        var options = session.getOptions();
054        if(options != null) {
055            includeBlob = options.contains(ForumOptions.ForumMessagePartIncludeBlob);
056            includeClob = options.contains(ForumOptions.ForumMessagePartIncludeClob);
057            includeString = options.contains(ForumOptions.ForumMessagePartIncludeString);
058        }
059    }
060    
061    public ForumMessagePartTransfer getForumMessagePartTransfer(UserVisit userVisit, ForumMessagePart forumMessagePart) {
062        var forumMessagePartTransfer = get(forumMessagePart);
063        
064        if(forumMessagePartTransfer == null) {
065            var forumMessagePartDetail = forumMessagePart.getLastDetail();
066            var forumMessageTransfer = forumControl.getForumMessageTransfer(userVisit, forumMessagePartDetail.getForumMessage());
067            var forumMessagePartTypeTransfer = forumControl.getForumMessagePartTypeTransfer(userVisit, forumMessagePartDetail.getForumMessagePartType());
068            var languageTransfer = partyControl.getLanguageTransfer(userVisit, forumMessagePartDetail.getLanguage());
069            var mimeType = forumMessagePartDetail.getMimeType();
070            ByteArray blobMessagePart = null;
071            String clobMessagePart = null;
072            String stringMessagePart = null;
073            
074            if(includeBlob) {
075                var forumBlobMessagePart = forumControl.getForumBlobMessagePart(forumMessagePart);
076                
077                if(forumBlobMessagePart != null) {
078                    blobMessagePart = forumBlobMessagePart.getBlob();
079                }
080            }
081            
082            if(includeClob) {
083                var forumClobMessagePart = forumControl.getForumClobMessagePart(forumMessagePart);
084                
085                if(forumClobMessagePart != null) {
086                    var preferredClobMimeType = session.getPreferredClobMimeType();
087                    
088                    clobMessagePart = forumClobMessagePart.getClob();
089                    
090                    if(preferredClobMimeType != null) {
091                        var preferredClobMimeTypeName = preferredClobMimeType.getLastDetail().getMimeTypeName();
092                        
093                        if(preferredClobMimeTypeName.contains(MimeTypes.TEXT_HTML.mimeTypeName())) {
094                            clobMessagePart = StringUtils.getInstance().convertToHtml(clobMessagePart, mimeType.getLastDetail().getMimeTypeName());
095                            mimeType = preferredClobMimeType;
096                        }
097                    }
098                }
099            }
100            
101            if(includeString) {
102                var forumStringMessagePart = forumControl.getForumStringMessagePart(forumMessagePart);
103                
104                if(forumStringMessagePart != null) {
105                    stringMessagePart = forumStringMessagePart.getString();
106                }
107            }
108
109            var mimeTypeTransfer = mimeType == null ? null : mimeTypeControl.getMimeTypeTransfer(userVisit, mimeType);
110            
111            forumMessagePartTransfer = new ForumMessagePartTransfer(forumMessageTransfer, forumMessagePartTypeTransfer,
112                    languageTransfer, mimeTypeTransfer, blobMessagePart, clobMessagePart, stringMessagePart);
113            put(userVisit, forumMessagePart, forumMessagePartTransfer);
114        }
115        
116        return forumMessagePartTransfer;
117    }
118    
119}