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.comment;
016    
017    import com.liferay.portal.kernel.comment.DiscussionComment;
018    import com.liferay.portal.kernel.comment.DiscussionCommentIterator;
019    import com.liferay.portal.kernel.comment.WorkflowableComment;
020    import com.liferay.portal.kernel.exception.PortalException;
021    import com.liferay.portlet.messageboards.model.MBMessage;
022    import com.liferay.portlet.messageboards.model.MBTreeWalker;
023    import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
024    import com.liferay.portlet.messageboards.util.MBUtil;
025    import com.liferay.portlet.ratings.model.RatingsEntry;
026    import com.liferay.portlet.ratings.model.RatingsStats;
027    import com.liferay.portlet.ratings.service.persistence.RatingsStatsUtil;
028    
029    import java.util.ArrayList;
030    import java.util.List;
031    
032    /**
033     * @author Adolfo P??rez
034     */
035    public class MBDiscussionCommentImpl
036            extends MBCommentImpl implements DiscussionComment, WorkflowableComment {
037    
038            public MBDiscussionCommentImpl(
039                    MBMessage message, MBTreeWalker treeWalker,
040                    List<RatingsEntry> ratingsEntries, List<RatingsStats> ratingsStats,
041                    String pathThemeImages) {
042    
043                    super(message);
044    
045                    _treeWalker = treeWalker;
046                    _ratingsEntries = ratingsEntries;
047                    _ratingsStats = ratingsStats;
048                    _pathThemeImages = pathThemeImages;
049            }
050    
051            @Override
052            public DiscussionComment getParentComment() throws PortalException {
053                    MBMessage message = getMessage();
054    
055                    long parentMessageId = message.getParentMessageId();
056    
057                    if (parentMessageId == 0) {
058                            return null;
059                    }
060    
061                    MBMessage parentMessage = MBMessageLocalServiceUtil.getMessage(
062                            parentMessageId);
063    
064                    return new MBDiscussionCommentImpl(
065                            parentMessage, _treeWalker, _ratingsEntries, _ratingsStats,
066                            _pathThemeImages);
067            }
068    
069            @Override
070            public long getParentCommentId() {
071                    MBMessage message = getMessage();
072    
073                    return message.getParentMessageId();
074            }
075    
076            @Override
077            public RatingsEntry getRatingsEntry() {
078                    long classPK = getCommentId();
079    
080                    for (RatingsEntry ratingsEntry : _ratingsEntries) {
081                            if (ratingsEntry.getClassPK() == classPK) {
082                                    return ratingsEntry;
083                            }
084                    }
085    
086                    return null;
087            }
088    
089            @Override
090            public RatingsStats getRatingsStats() {
091                    long classPK = getCommentId();
092    
093                    for (RatingsStats ratingsStats : _ratingsStats) {
094                            if (ratingsStats.getClassPK() == classPK) {
095                                    return ratingsStats;
096                            }
097                    }
098    
099                    return RatingsStatsUtil.create(0);
100            }
101    
102            @Override
103            public List<DiscussionComment> getThreadComments() {
104                    List<DiscussionComment> discussionComments = new ArrayList<>();
105    
106                    DiscussionCommentIterator discussionCommentIterator =
107                            getThreadDiscussionCommentIterator();
108    
109                    while (discussionCommentIterator.hasNext()) {
110                            discussionComments.add(discussionCommentIterator.next());
111                    }
112    
113                    return discussionComments;
114            }
115    
116            @Override
117            public int getThreadCommentsCount() {
118                    List<MBMessage> messages = _treeWalker.getMessages();
119    
120                    return messages.size();
121            }
122    
123            @Override
124            public DiscussionCommentIterator getThreadDiscussionCommentIterator() {
125                    List<MBMessage> messages = _treeWalker.getMessages();
126    
127                    int[] range = _treeWalker.getChildrenRange(getMessage());
128    
129                    return new MBDiscussionCommentIterator(
130                            messages, range[0], range[1], _treeWalker, _pathThemeImages);
131            }
132    
133            @Override
134            public DiscussionCommentIterator getThreadDiscussionCommentIterator(
135                    int from) {
136    
137                    List<MBMessage> messages = _treeWalker.getMessages();
138    
139                    int[] range = _treeWalker.getChildrenRange(getMessage());
140    
141                    return new MBDiscussionCommentIterator(
142                            messages, from, range[1], _treeWalker, _pathThemeImages);
143            }
144    
145            @Override
146            public String getTranslatedBody() {
147                    MBMessage message = getMessage();
148    
149                    if (message.isFormatBBCode()) {
150                            return MBUtil.getBBCodeHTML(getBody(), _pathThemeImages);
151                    }
152    
153                    return getBody();
154            }
155    
156            @Override
157            public boolean isRoot() {
158                    MBMessage message = getMessage();
159    
160                    return message.isRoot();
161            }
162    
163            private final String _pathThemeImages;
164            private final List<RatingsEntry> _ratingsEntries;
165            private final List<RatingsStats> _ratingsStats;
166            private final MBTreeWalker _treeWalker;
167    
168            private class MBDiscussionCommentIterator
169                    implements DiscussionCommentIterator {
170    
171                    public MBDiscussionCommentIterator(
172                            List<MBMessage> messages, int from, int to, MBTreeWalker treeWalker,
173                            String pathThemeImages) {
174    
175                            _messages = messages;
176                            _from = from;
177                            _to = to;
178                            _treeWalker = treeWalker;
179                            _pathThemeImages = pathThemeImages;
180                    }
181    
182                    @Override
183                    public int getIndexPage() {
184                            return _from;
185                    }
186    
187                    @Override
188                    public boolean hasNext() {
189                            if (_from < _to) {
190                                    return true;
191                            }
192    
193                            return false;
194                    }
195    
196                    @Override
197                    public DiscussionComment next() {
198                            DiscussionComment discussionComment = new MBDiscussionCommentImpl(
199                                    _messages.get(_from), _treeWalker, _ratingsEntries,
200                                    _ratingsStats, _pathThemeImages);
201    
202                            _from++;
203    
204                            return discussionComment;
205                    }
206    
207                    @Override
208                    public void remove() {
209                            throw new UnsupportedOperationException();
210                    }
211    
212                    private int _from;
213                    private final List<MBMessage> _messages;
214                    private final String _pathThemeImages;
215                    private final int _to;
216                    private final MBTreeWalker _treeWalker;
217    
218            }
219    
220    }