001    /**
002     * Copyright (c) 2000-2010 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.MBMessageLocalServiceUtil;
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(MBMessage message, int status) {
034                    _messageIdsMap = new HashMap<Long, Integer>();
035    
036                    try {
037                            _messages = MBMessageLocalServiceUtil.getThreadMessages(
038                                    message.getThreadId(), status);
039    
040                            for (int i = 0; i < _messages.size(); i++) {
041                                    MBMessage curMessage = _messages.get(i);
042    
043                                    long parentMessageId = curMessage.getParentMessageId();
044    
045                                    if (!curMessage.isRoot() &&
046                                            !_messageIdsMap.containsKey(parentMessageId)) {
047    
048                                            _messageIdsMap.put(parentMessageId, i);
049                                    }
050                            }
051                    }
052                    catch (Exception e) {
053                            _log.error(e);
054                    }
055            }
056    
057            public List<MBMessage> getChildren(MBMessage message) {
058                    List<MBMessage> children = new ArrayList<MBMessage>();
059    
060                    int[] range = getChildrenRange(message);
061    
062                    for (int i = range[0]; i < range[1]; i++) {
063                            children.add(_messages.get(i));
064                    }
065    
066                    return children;
067            }
068    
069            public int[] getChildrenRange(MBMessage message) {
070                    long messageId = message.getMessageId();
071    
072                    Integer pos = _messageIdsMap.get(messageId);
073    
074                    if (pos == null) {
075                            return new int[] {0, 0};
076                    }
077    
078                    int[] range = new int[2];
079                    range[0] = pos.intValue();
080    
081                    for (int i = range[0]; i < _messages.size(); i++) {
082                            MBMessage curMessage = _messages.get(i);
083    
084                            if (curMessage.getParentMessageId() == messageId) {
085                                    range[1] = i + 1;
086                            }
087                            else {
088                                    break;
089                            }
090                    }
091    
092                    return range;
093            }
094    
095            public List<MBMessage> getMessages() {
096                    return _messages;
097            }
098    
099            public MBMessage getRoot() {
100                    return _messages.get(0);
101            }
102    
103            public boolean isLeaf(MBMessage message) {
104                    Long messageIdObj = new Long(message.getMessageId());
105    
106                    if (_messageIdsMap.containsKey(messageIdObj)) {
107                            return false;
108                    }
109                    else {
110                            return true;
111                    }
112            }
113    
114            public boolean isOdd() {
115                    _odd = !_odd;
116    
117                    return _odd;
118            }
119    
120            private static Log _log = LogFactoryUtil.getLog(MBTreeWalkerImpl.class);
121    
122            private Map<Long, Integer> _messageIdsMap;
123            private List<MBMessage> _messages;
124            private boolean _odd;
125    
126    }