001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.model.impl;
022:
023: import com.liferay.portlet.messageboards.model.MBMessage;
024: import com.liferay.portlet.messageboards.model.MBTreeWalker;
025: import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
026: import com.liferay.util.CollectionFactory;
027:
028: import java.util.ArrayList;
029: import java.util.List;
030: import java.util.Map;
031:
032: import org.apache.commons.logging.Log;
033: import org.apache.commons.logging.LogFactory;
034:
035: /**
036: * <a href="MBTreeWalkerImpl.java.html"><b><i>View Source</i></b></a>
037: *
038: * @author Brian Wing Shun Chan
039: *
040: */
041: public class MBTreeWalkerImpl implements MBTreeWalker {
042:
043: public MBTreeWalkerImpl(MBMessage message) {
044: _messageIdsMap = CollectionFactory.getHashMap();
045:
046: try {
047: _messages = MBMessageLocalServiceUtil
048: .getThreadMessages(message.getThreadId());
049:
050: for (int i = 0; i < _messages.size(); i++) {
051: MBMessage curMessage = (MBMessage) _messages.get(i);
052:
053: Long parentMessageIdObj = new Long(curMessage
054: .getParentMessageId());
055:
056: if (!curMessage.isRoot()
057: && !_messageIdsMap
058: .containsKey(parentMessageIdObj)) {
059:
060: _messageIdsMap.put(parentMessageIdObj, new Integer(
061: i));
062: }
063: }
064: } catch (Exception e) {
065: _log.error(e);
066: }
067: }
068:
069: public MBMessage getRoot() {
070: return (MBMessage) _messages.get(0);
071: }
072:
073: public List getChildren(MBMessage message) {
074: List children = new ArrayList();
075:
076: int[] range = getChildrenRange(message);
077:
078: for (int i = range[0]; i < range[1]; i++) {
079: children.add(_messages.get(i));
080: }
081:
082: return children;
083: }
084:
085: public int[] getChildrenRange(MBMessage message) {
086: Long messageIdObj = new Long(message.getMessageId());
087:
088: Integer pos = (Integer) _messageIdsMap.get(messageIdObj);
089:
090: if (pos == null) {
091: return new int[] { 0, 0 };
092: }
093:
094: int[] range = new int[2];
095: range[0] = pos.intValue();
096:
097: for (int i = range[0]; i < _messages.size(); i++) {
098: MBMessage curMessage = (MBMessage) _messages.get(i);
099:
100: if (curMessage.getParentMessageId() == messageIdObj
101: .longValue()) {
102: range[1] = i + 1;
103: } else {
104: break;
105: }
106: }
107:
108: return range;
109: }
110:
111: public List getMessages() {
112: return _messages;
113: }
114:
115: public boolean isOdd() {
116: _odd = !_odd;
117:
118: return _odd;
119: }
120:
121: public boolean isLeaf(MBMessage message) {
122: Long messageIdObj = new Long(message.getMessageId());
123:
124: if (_messageIdsMap.containsKey(messageIdObj)) {
125: return false;
126: } else {
127: return true;
128: }
129: }
130:
131: private static Log _log = LogFactory.getLog(MBTreeWalkerImpl.class);
132:
133: private List _messages;
134: private Map _messageIdsMap;
135: private boolean _odd;
136:
137: }
|