1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.messageboards.model.impl;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portlet.messageboards.model.MBMessage;
25  import com.liferay.portlet.messageboards.model.MBTreeWalker;
26  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
27  
28  import java.util.ArrayList;
29  import java.util.HashMap;
30  import java.util.List;
31  import java.util.Map;
32  
33  /**
34   * <a href="MBTreeWalkerImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   *
38   */
39  public class MBTreeWalkerImpl implements MBTreeWalker {
40  
41      public MBTreeWalkerImpl(MBMessage message) {
42          _messageIdsMap = new HashMap<Long, Integer>();
43  
44          try {
45              _messages = MBMessageLocalServiceUtil.getThreadMessages(
46                  message.getThreadId());
47  
48              for (int i = 0; i < _messages.size(); i++) {
49                  MBMessage curMessage = _messages.get(i);
50  
51                  long parentMessageId = curMessage.getParentMessageId();
52  
53                  if (!curMessage.isRoot() &&
54                      !_messageIdsMap.containsKey(parentMessageId)) {
55  
56                      _messageIdsMap.put(parentMessageId, i);
57                  }
58              }
59          }
60          catch (Exception e) {
61              _log.error(e);
62          }
63      }
64  
65      public MBMessage getRoot() {
66          return _messages.get(0);
67      }
68  
69      public List<MBMessage> getChildren(MBMessage message) {
70          List<MBMessage> children = new ArrayList<MBMessage>();
71  
72          int[] range = getChildrenRange(message);
73  
74          for (int i = range[0]; i < range[1]; i++) {
75              children.add(_messages.get(i));
76          }
77  
78          return children;
79      }
80  
81      public int[] getChildrenRange(MBMessage message) {
82          long messageId = message.getMessageId();
83  
84          Integer pos = _messageIdsMap.get(messageId);
85  
86          if (pos == null) {
87              return new int[] {0, 0};
88          }
89  
90          int[] range = new int[2];
91          range[0] = pos.intValue();
92  
93          for (int i = range[0]; i < _messages.size(); i++) {
94              MBMessage curMessage = _messages.get(i);
95  
96              if (curMessage.getParentMessageId() == messageId) {
97                  range[1] = i + 1;
98              }
99              else {
100                 break;
101             }
102         }
103 
104         return range;
105     }
106 
107     public List<MBMessage> getMessages() {
108         return _messages;
109     }
110 
111     public boolean isOdd() {
112         _odd = !_odd;
113 
114         return _odd;
115     }
116 
117     public boolean isLeaf(MBMessage message) {
118         Long messageIdObj = new Long(message.getMessageId());
119 
120         if (_messageIdsMap.containsKey(messageIdObj)) {
121             return false;
122         }
123         else {
124             return true;
125         }
126     }
127 
128     private static Log _log = LogFactoryUtil.getLog(MBTreeWalkerImpl.class);
129 
130     private List<MBMessage> _messages;
131     private Map<Long, Integer> _messageIdsMap;
132     private boolean _odd;
133 
134 }