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