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