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