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.message.boards.kernel.model.MBCategory;
018    import com.liferay.message.boards.kernel.model.MBMessage;
019    import com.liferay.message.boards.kernel.model.MBMessageDisplay;
020    import com.liferay.message.boards.kernel.model.MBThread;
021    import com.liferay.message.boards.kernel.model.MBThreadConstants;
022    import com.liferay.message.boards.kernel.model.MBTreeWalker;
023    import com.liferay.message.boards.kernel.service.MBMessageLocalService;
024    import com.liferay.portal.kernel.workflow.WorkflowConstants;
025    import com.liferay.portal.util.PropsValues;
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, int status, MBMessageLocalService messageLocalService,
038                    Comparator<MBMessage> comparator) {
039    
040                    _message = message;
041                    _parentMessage = parentMessage;
042                    _category = category;
043                    _thread = thread;
044    
045                    _treeWalker = new MBTreeWalkerImpl(
046                            message.getThreadId(), status, messageLocalService, comparator);
047    
048                    _previousThread = null;
049                    _nextThread = null;
050                    _threadView = MBThreadConstants.THREAD_VIEW_TREE;
051    
052                    int dicussionMessagesCount = 0;
053    
054                    if (message.isDiscussion() &&
055                            (PropsValues.DISCUSSION_MAX_COMMENTS > 0)) {
056    
057                            dicussionMessagesCount =
058                                    messageLocalService.getDiscussionMessagesCount(
059                                            message.getClassName(), message.getClassPK(),
060                                            WorkflowConstants.STATUS_APPROVED);
061                    }
062    
063                    _discussionMessagesCount = dicussionMessagesCount;
064            }
065    
066            /**
067             * @deprecated As of 7.0.0, replaced by {@link
068             *             #MBMessageDisplayImpl(MBMessage, MBMessage, MBCategory,
069             *             MBThread, int, MBMessageLocalService, Comparator)}
070             */
071            @Deprecated
072            public MBMessageDisplayImpl(
073                    MBMessage message, MBMessage parentMessage, MBCategory category,
074                    MBThread thread, MBThread previousThread, MBThread nextThread,
075                    int status, String threadView,
076                    MBMessageLocalService messageLocalService,
077                    Comparator<MBMessage> comparator) {
078    
079                    _message = message;
080                    _parentMessage = parentMessage;
081                    _category = category;
082                    _thread = thread;
083    
084                    if (!threadView.equals(MBThreadConstants.THREAD_VIEW_FLAT)) {
085                            _treeWalker = new MBTreeWalkerImpl(
086                                    message.getThreadId(), status, messageLocalService, comparator);
087                    }
088                    else {
089                            _treeWalker = null;
090                    }
091    
092                    _previousThread = previousThread;
093                    _nextThread = nextThread;
094                    _threadView = threadView;
095    
096                    int dicussionMessagesCount = 0;
097    
098                    if (message.isDiscussion() &&
099                            (PropsValues.DISCUSSION_MAX_COMMENTS > 0)) {
100    
101                            dicussionMessagesCount =
102                                    messageLocalService.getDiscussionMessagesCount(
103                                            message.getClassName(), message.getClassPK(),
104                                            WorkflowConstants.STATUS_APPROVED);
105                    }
106    
107                    _discussionMessagesCount = dicussionMessagesCount;
108            }
109    
110            @Override
111            public MBCategory getCategory() {
112                    return _category;
113            }
114    
115            @Override
116            public MBMessage getMessage() {
117                    return _message;
118            }
119    
120            /**
121             * @deprecated As of 7.0.0, with no direct replacement
122             */
123            @Deprecated
124            @Override
125            public MBThread getNextThread() {
126                    return _nextThread;
127            }
128    
129            @Override
130            public MBMessage getParentMessage() {
131                    return _parentMessage;
132            }
133    
134            /**
135             * @deprecated As of 7.0.0, with no direct replacement
136             */
137            @Deprecated
138            @Override
139            public MBThread getPreviousThread() {
140                    return _previousThread;
141            }
142    
143            @Override
144            public MBThread getThread() {
145                    return _thread;
146            }
147    
148            /**
149             * @deprecated As of 7.0.0, with no direct replacement
150             */
151            @Deprecated
152            @Override
153            public String getThreadView() {
154                    return _threadView;
155            }
156    
157            @Override
158            public MBTreeWalker getTreeWalker() {
159                    return _treeWalker;
160            }
161    
162            @Override
163            public boolean isDiscussionMaxComments() {
164                    if (_message.isDiscussion() &&
165                            (PropsValues.DISCUSSION_MAX_COMMENTS > 0) &&
166                            (PropsValues.DISCUSSION_MAX_COMMENTS <= _discussionMessagesCount)) {
167    
168                            return true;
169                    }
170    
171                    return false;
172            }
173    
174            private final MBCategory _category;
175            private final int _discussionMessagesCount;
176            private final MBMessage _message;
177            private final MBThread _nextThread;
178            private final MBMessage _parentMessage;
179            private final MBThread _previousThread;
180            private final MBThread _thread;
181            private final String _threadView;
182            private final MBTreeWalker _treeWalker;
183    
184    }