001    /**
002     * Copyright (c) 2000-2012 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            public List<MBMessage> getChildren(MBMessage message) {
061                    List<MBMessage> children = new ArrayList<MBMessage>();
062    
063                    int[] range = getChildrenRange(message);
064    
065                    for (int i = range[0]; i < range[1]; i++) {
066                            children.add(_messages.get(i));
067                    }
068    
069                    return children;
070            }
071    
072            public int[] getChildrenRange(MBMessage message) {
073                    long messageId = message.getMessageId();
074    
075                    Integer pos = _messageIdsMap.get(messageId);
076    
077                    if (pos == null) {
078                            return new int[] {0, 0};
079                    }
080    
081                    int[] range = new int[2];
082                    range[0] = pos.intValue();
083    
084                    for (int i = range[0]; i < _messages.size(); i++) {
085                            MBMessage curMessage = _messages.get(i);
086    
087                            if (curMessage.getParentMessageId() == messageId) {
088                                    range[1] = i + 1;
089                            }
090                            else {
091                                    break;
092                            }
093                    }
094    
095                    return range;
096            }
097    
098            public List<MBMessage> getMessages() {
099                    return _messages;
100            }
101    
102            public MBMessage getRoot() {
103                    return _messages.get(0);
104            }
105    
106            public boolean isLeaf(MBMessage message) {
107                    Long messageIdObj = new Long(message.getMessageId());
108    
109                    if (_messageIdsMap.containsKey(messageIdObj)) {
110                            return false;
111                    }
112                    else {
113                            return true;
114                    }
115            }
116    
117            public boolean isOdd() {
118                    _odd = !_odd;
119    
120                    return _odd;
121            }
122    
123            private static Log _log = LogFactoryUtil.getLog(MBTreeWalkerImpl.class);
124    
125            private Map<Long, Integer> _messageIdsMap;
126            private List<MBMessage> _messages;
127            private boolean _odd;
128    
129    }