001    /**
002     * Copyright (c) 2000-2013 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.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portlet.messageboards.model.MBMessage;
020    import com.liferay.portlet.messageboards.model.MBTreeWalker;
021    import com.liferay.portlet.messageboards.service.MBMessageLocalService;
022    
023    import java.util.ArrayList;
024    import java.util.HashMap;
025    import java.util.List;
026    import java.util.Map;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class MBTreeWalkerImpl implements MBTreeWalker {
032    
033            public MBTreeWalkerImpl(
034                    MBMessage message, int status,
035                    MBMessageLocalService messageLocalService) {
036    
037                    _messageIdsMap = new HashMap<Long, Integer>();
038    
039                    try {
040                            _messages = messageLocalService.getThreadMessages(
041                                    message.getThreadId(), status);
042    
043                            for (int i = 0; i < _messages.size(); i++) {
044                                    MBMessage curMessage = _messages.get(i);
045    
046                                    long parentMessageId = curMessage.getParentMessageId();
047    
048                                    if (!curMessage.isRoot() &&
049                                            !_messageIdsMap.containsKey(parentMessageId)) {
050    
051                                            _messageIdsMap.put(parentMessageId, i);
052                                    }
053                            }
054                    }
055                    catch (Exception e) {
056                            _log.error(e);
057                    }
058            }
059    
060            @Override
061            public List<MBMessage> getChildren(MBMessage message) {
062                    List<MBMessage> children = new ArrayList<MBMessage>();
063    
064                    int[] range = getChildrenRange(message);
065    
066                    for (int i = range[0]; i < range[1]; i++) {
067                            children.add(_messages.get(i));
068                    }
069    
070                    return children;
071            }
072    
073            @Override
074            public int[] getChildrenRange(MBMessage message) {
075                    long messageId = message.getMessageId();
076    
077                    Integer pos = _messageIdsMap.get(messageId);
078    
079                    if (pos == null) {
080                            return new int[] {0, 0};
081                    }
082    
083                    int[] range = new int[2];
084                    range[0] = pos.intValue();
085    
086                    for (int i = range[0]; i < _messages.size(); i++) {
087                            MBMessage curMessage = _messages.get(i);
088    
089                            if (curMessage.getParentMessageId() == messageId) {
090                                    range[1] = i + 1;
091                            }
092                            else {
093                                    break;
094                            }
095                    }
096    
097                    return range;
098            }
099    
100            @Override
101            public List<MBMessage> getMessages() {
102                    return _messages;
103            }
104    
105            @Override
106            public MBMessage getRoot() {
107                    return _messages.get(0);
108            }
109    
110            @Override
111            public boolean isLeaf(MBMessage message) {
112                    Long messageIdObj = new Long(message.getMessageId());
113    
114                    if (_messageIdsMap.containsKey(messageIdObj)) {
115                            return false;
116                    }
117                    else {
118                            return true;
119                    }
120            }
121    
122            @Override
123            public boolean isOdd() {
124                    _odd = !_odd;
125    
126                    return _odd;
127            }
128    
129            private static Log _log = LogFactoryUtil.getLog(MBTreeWalkerImpl.class);
130    
131            private Map<Long, Integer> _messageIdsMap;
132            private List<MBMessage> _messages;
133            private boolean _odd;
134    
135    }