001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.messageboards.model.impl;
016    
017    import com.liferay.portal.kernel.workflow.WorkflowConstants;
018    import com.liferay.portal.util.PropsValues;
019    import com.liferay.portlet.messageboards.model.MBCategory;
020    import com.liferay.portlet.messageboards.model.MBMessage;
021    import com.liferay.portlet.messageboards.model.MBMessageDisplay;
022    import com.liferay.portlet.messageboards.model.MBThread;
023    import com.liferay.portlet.messageboards.model.MBThreadConstants;
024    import com.liferay.portlet.messageboards.model.MBTreeWalker;
025    import com.liferay.portlet.messageboards.service.MBMessageLocalService;
026    
027    import java.util.Comparator;
028    
029    /**
030     * @author Brian Wing Shun Chan
031     * @author Shuyang Zhou
032     */
033    public class MBMessageDisplayImpl implements MBMessageDisplay {
034    
035            public MBMessageDisplayImpl(
036                    MBMessage message, MBMessage parentMessage, MBCategory category,
037                    MBThread thread, MBThread previousThread, MBThread nextThread,
038                    int status, String threadView,
039                    MBMessageLocalService messageLocalService,
040                    Comparator<MBMessage> comparator) {
041    
042                    _message = message;
043                    _parentMessage = parentMessage;
044                    _category = category;
045                    _thread = thread;
046    
047                    if (!threadView.equals(MBThreadConstants.THREAD_VIEW_FLAT)) {
048                            _treeWalker = new MBTreeWalkerImpl(
049                                    message.getThreadId(), status, messageLocalService, comparator);
050                    }
051                    else {
052                            _treeWalker = null;
053                    }
054    
055                    _previousThread = previousThread;
056                    _nextThread = nextThread;
057                    _threadView = threadView;
058    
059                    int dicussionMessagesCount = 0;
060    
061                    if (message.isDiscussion() &&
062                            (PropsValues.DISCUSSION_MAX_COMMENTS > 0)) {
063    
064                            dicussionMessagesCount =
065                                    messageLocalService.getDiscussionMessagesCount(
066                                            message.getClassName(), message.getClassPK(),
067                                            WorkflowConstants.STATUS_APPROVED);
068                    }
069    
070                    _discussionMessagesCount = dicussionMessagesCount;
071            }
072    
073            @Override
074            public MBCategory getCategory() {
075                    return _category;
076            }
077    
078            @Override
079            public MBMessage getMessage() {
080                    return _message;
081            }
082    
083            @Override
084            public MBThread getNextThread() {
085                    return _nextThread;
086            }
087    
088            @Override
089            public MBMessage getParentMessage() {
090                    return _parentMessage;
091            }
092    
093            @Override
094            public MBThread getPreviousThread() {
095                    return _previousThread;
096            }
097    
098            @Override
099            public MBThread getThread() {
100                    return _thread;
101            }
102    
103            @Override
104            public String getThreadView() {
105                    return _threadView;
106            }
107    
108            @Override
109            public MBTreeWalker getTreeWalker() {
110                    return _treeWalker;
111            }
112    
113            @Override
114            public boolean isDiscussionMaxComments() {
115                    if (_message.isDiscussion() &&
116                            (PropsValues.DISCUSSION_MAX_COMMENTS > 0) &&
117                            (PropsValues.DISCUSSION_MAX_COMMENTS <= _discussionMessagesCount)) {
118    
119                            return true;
120                    }
121    
122                    return false;
123            }
124    
125            private final MBCategory _category;
126            private final int _discussionMessagesCount;
127            private final MBMessage _message;
128            private final MBThread _nextThread;
129            private final MBMessage _parentMessage;
130            private final MBThread _previousThread;
131            private final MBThread _thread;
132            private final String _threadView;
133            private final MBTreeWalker _treeWalker;
134    
135    }