1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.model.impl;
24  
25  import com.liferay.portlet.messageboards.model.MBMessage;
26  import com.liferay.portlet.messageboards.model.MBTreeWalker;
27  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
28  import com.liferay.util.CollectionFactory;
29  
30  import java.util.ArrayList;
31  import java.util.List;
32  import java.util.Map;
33  
34  import org.apache.commons.logging.Log;
35  import org.apache.commons.logging.LogFactory;
36  
37  /**
38   * <a href="MBTreeWalkerImpl.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   *
42   */
43  public class MBTreeWalkerImpl implements MBTreeWalker {
44  
45      public MBTreeWalkerImpl(MBMessage message) {
46          _messageIdsMap = CollectionFactory.getHashMap();
47  
48          try {
49              _messages = MBMessageLocalServiceUtil.getThreadMessages(
50                  message.getThreadId());
51  
52              for (int i = 0; i < _messages.size(); i++) {
53                  MBMessage curMessage = (MBMessage)_messages.get(i);
54  
55                  Long parentMessageIdObj = new Long(
56                      curMessage.getParentMessageId());
57  
58                  if (!curMessage.isRoot() &&
59                      !_messageIdsMap.containsKey(parentMessageIdObj)) {
60  
61                      _messageIdsMap.put(parentMessageIdObj, new Integer(i));
62                  }
63              }
64          }
65          catch (Exception e) {
66              _log.error(e);
67          }
68      }
69  
70      public MBMessage getRoot() {
71          return (MBMessage)_messages.get(0);
72      }
73  
74      public List getChildren(MBMessage message) {
75          List children = new ArrayList();
76  
77          int[] range = getChildrenRange(message);
78  
79          for (int i = range[0]; i < range[1]; i++) {
80              children.add(_messages.get(i));
81          }
82  
83          return children;
84      }
85  
86      public int[] getChildrenRange(MBMessage message) {
87          Long messageIdObj = new Long(message.getMessageId());
88  
89          Integer pos = (Integer)_messageIdsMap.get(messageIdObj);
90  
91          if (pos == null) {
92              return new int[] {0, 0};
93          }
94  
95          int[] range = new int[2];
96          range[0] = pos.intValue();
97  
98          for (int i = range[0]; i < _messages.size(); i++) {
99              MBMessage curMessage = (MBMessage)_messages.get(i);
100 
101             if (curMessage.getParentMessageId() == messageIdObj.longValue()) {
102                 range[1] = i + 1;
103             }
104             else {
105                 break;
106             }
107         }
108 
109         return range;
110     }
111 
112     public List getMessages() {
113         return _messages;
114     }
115 
116     public boolean isOdd() {
117         _odd = !_odd;
118 
119         return _odd;
120     }
121 
122     public boolean isLeaf(MBMessage message) {
123         Long messageIdObj = new Long(message.getMessageId());
124 
125         if (_messageIdsMap.containsKey(messageIdObj)) {
126             return false;
127         }
128         else {
129             return true;
130         }
131     }
132 
133     private static Log _log = LogFactory.getLog(MBTreeWalkerImpl.class);
134 
135     private List _messages;
136     private Map _messageIdsMap;
137     private boolean _odd;
138 
139 }