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