1   /**
2    * Copyright (c) 2000-2009 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.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portlet.messageboards.model.MBMessage;
28  import com.liferay.portlet.messageboards.model.MBTreeWalker;
29  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
30  
31  import java.util.ArrayList;
32  import java.util.HashMap;
33  import java.util.List;
34  import java.util.Map;
35  
36  /**
37   * <a href="MBTreeWalkerImpl.java.html"><b><i>View Source</i></b></a>
38   *
39   * @author Brian Wing Shun Chan
40   *
41   */
42  public class MBTreeWalkerImpl implements MBTreeWalker {
43  
44      public MBTreeWalkerImpl(MBMessage message) {
45          _messageIdsMap = new HashMap<Long, Integer>();
46  
47          try {
48              _messages = MBMessageLocalServiceUtil.getThreadMessages(
49                  message.getThreadId());
50  
51              for (int i = 0; i < _messages.size(); i++) {
52                  MBMessage curMessage = _messages.get(i);
53  
54                  long parentMessageId = curMessage.getParentMessageId();
55  
56                  if (!curMessage.isRoot() &&
57                      !_messageIdsMap.containsKey(parentMessageId)) {
58  
59                      _messageIdsMap.put(parentMessageId, i);
60                  }
61              }
62          }
63          catch (Exception e) {
64              _log.error(e);
65          }
66      }
67  
68      public MBMessage getRoot() {
69          return _messages.get(0);
70      }
71  
72      public List<MBMessage> getChildren(MBMessage message) {
73          List<MBMessage> children = new ArrayList<MBMessage>();
74  
75          int[] range = getChildrenRange(message);
76  
77          for (int i = range[0]; i < range[1]; i++) {
78              children.add(_messages.get(i));
79          }
80  
81          return children;
82      }
83  
84      public int[] getChildrenRange(MBMessage message) {
85          long messageId = message.getMessageId();
86  
87          Integer pos = _messageIdsMap.get(messageId);
88  
89          if (pos == null) {
90              return new int[] {0, 0};
91          }
92  
93          int[] range = new int[2];
94          range[0] = pos.intValue();
95  
96          for (int i = range[0]; i < _messages.size(); i++) {
97              MBMessage curMessage = _messages.get(i);
98  
99              if (curMessage.getParentMessageId() == messageId) {
100                 range[1] = i + 1;
101             }
102             else {
103                 break;
104             }
105         }
106 
107         return range;
108     }
109 
110     public List<MBMessage> getMessages() {
111         return _messages;
112     }
113 
114     public boolean isOdd() {
115         _odd = !_odd;
116 
117         return _odd;
118     }
119 
120     public boolean isLeaf(MBMessage message) {
121         Long messageIdObj = new Long(message.getMessageId());
122 
123         if (_messageIdsMap.containsKey(messageIdObj)) {
124             return false;
125         }
126         else {
127             return true;
128         }
129     }
130 
131     private static Log _log = LogFactoryUtil.getLog(MBTreeWalkerImpl.class);
132 
133     private List<MBMessage> _messages;
134     private Map<Long, Integer> _messageIdsMap;
135     private boolean _odd;
136 
137 }