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.forum.common.ForumOptions;
021import com.echothree.model.control.forum.common.transfer.ForumThreadTransfer;
022import com.echothree.model.control.forum.server.control.ForumControl;
023import com.echothree.model.control.icon.server.control.IconControl;
024import com.echothree.model.data.forum.server.entity.ForumThread;
025import com.echothree.model.data.forum.server.factory.ForumMessageFactory;
026import com.echothree.model.data.user.server.entity.UserVisit;
027import com.echothree.util.common.transfer.ListWrapper;
028import javax.enterprise.context.RequestScoped;
029
030@RequestScoped
031public class ForumThreadTransferCache
032        extends BaseForumTransferCache<ForumThread, ForumThreadTransfer> {
033
034    @Inject
035    ForumControl forumControl;
036
037    @Inject
038    IconControl iconControl;
039
040    boolean includeForumMessages;
041    boolean includeForumForumThreads;
042    boolean hasForumMessageLimits;
043    
044    /** Creates a new instance of ForumThreadTransferCache */
045    protected ForumThreadTransferCache() {
046        super();
047        
048        var options = session.getOptions();
049        if(options != null) {
050            setIncludeUuid(options.contains(ForumOptions.ForumThreadIncludeUuid));
051            includeForumMessages = options.contains(ForumOptions.ForumThreadIncludeForumMessages);
052            includeForumForumThreads = options.contains(ForumOptions.ForumThreadIncludeForumForumThreads);
053            setIncludeEntityAttributeGroups(options.contains(ForumOptions.ForumThreadIncludeEntityAttributeGroups));
054            setIncludeTagScopes(options.contains(ForumOptions.ForumThreadIncludeTagScopes));
055        }
056        
057        setIncludeEntityInstance(true);
058        
059        hasForumMessageLimits = session.hasLimit(ForumMessageFactory.class);
060    }
061    
062    public ForumThreadTransfer getForumThreadTransfer(UserVisit userVisit, ForumThread forumThread) {
063        var forumThreadTransfer = get(forumThread);
064        
065        if(forumThreadTransfer == null) {
066            var forumThreadDetail = forumThread.getLastDetail();
067            var forumThreadName = forumThreadDetail.getForumThreadName();
068            var icon = forumThreadDetail.getIcon();
069            var iconTransfer = icon == null? null: iconControl.getIconTransfer(userVisit, icon);
070            var unformattedPostedTime = forumThreadDetail.getPostedTime();
071            var postedTime = formatTypicalDateTime(userVisit, unformattedPostedTime);
072            var sortOrder = forumThreadDetail.getSortOrder();
073                    
074            forumThreadTransfer = new ForumThreadTransfer(forumThreadName, iconTransfer, unformattedPostedTime, postedTime, sortOrder);
075            put(userVisit, forumThread, forumThreadTransfer);
076            
077            if(includeForumMessages) {
078                if(hasForumMessageLimits) {
079                    forumThreadTransfer.setForumMessageCount(forumControl.countForumMessagesByForumThread(forumThread));
080                }
081                
082                forumThreadTransfer.setForumMessages(new ListWrapper<>(forumControl.getForumMessageTransfersByForumThread(userVisit, forumThread)));
083            }
084            
085            if(includeForumForumThreads) {
086                forumThreadTransfer.setForumForumThreads(new ListWrapper<>(forumControl.getForumForumThreadTransfersByForumThread(userVisit, forumThread)));
087            }
088        }
089        
090        return forumThreadTransfer;
091    }
092    
093}