1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.model.impl;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portlet.messageboards.model.MBMessage;
20  import com.liferay.portlet.messageboards.model.MBTreeWalker;
21  import com.liferay.portlet.messageboards.service.MBMessageLocalServiceUtil;
22  
23  import java.util.ArrayList;
24  import java.util.HashMap;
25  import java.util.List;
26  import java.util.Map;
27  
28  /**
29   * <a href="MBTreeWalkerImpl.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class MBTreeWalkerImpl implements MBTreeWalker {
34  
35      public MBTreeWalkerImpl(MBMessage message, int status) {
36          _messageIdsMap = new HashMap<Long, Integer>();
37  
38          try {
39              _messages = MBMessageLocalServiceUtil.getThreadMessages(
40                  message.getThreadId(), status);
41  
42              for (int i = 0; i < _messages.size(); i++) {
43                  MBMessage curMessage = _messages.get(i);
44  
45                  long parentMessageId = curMessage.getParentMessageId();
46  
47                  if (!curMessage.isRoot() &&
48                      !_messageIdsMap.containsKey(parentMessageId)) {
49  
50                      _messageIdsMap.put(parentMessageId, i);
51                  }
52              }
53          }
54          catch (Exception e) {
55              _log.error(e);
56          }
57      }
58  
59      public List<MBMessage> getChildren(MBMessage message) {
60          List<MBMessage> children = new ArrayList<MBMessage>();
61  
62          int[] range = getChildrenRange(message);
63  
64          for (int i = range[0]; i < range[1]; i++) {
65              children.add(_messages.get(i));
66          }
67  
68          return children;
69      }
70  
71      public int[] getChildrenRange(MBMessage message) {
72          long messageId = message.getMessageId();
73  
74          Integer pos = _messageIdsMap.get(messageId);
75  
76          if (pos == null) {
77              return new int[] {0, 0};
78          }
79  
80          int[] range = new int[2];
81          range[0] = pos.intValue();
82  
83          for (int i = range[0]; i < _messages.size(); i++) {
84              MBMessage curMessage = _messages.get(i);
85  
86              if (curMessage.getParentMessageId() == messageId) {
87                  range[1] = i + 1;
88              }
89              else {
90                  break;
91              }
92          }
93  
94          return range;
95      }
96  
97      public List<MBMessage> getMessages() {
98          return _messages;
99      }
100 
101     public MBMessage getRoot() {
102         return _messages.get(0);
103     }
104 
105     public boolean isLeaf(MBMessage message) {
106         Long messageIdObj = new Long(message.getMessageId());
107 
108         if (_messageIdsMap.containsKey(messageIdObj)) {
109             return false;
110         }
111         else {
112             return true;
113         }
114     }
115 
116     public boolean isOdd() {
117         _odd = !_odd;
118 
119         return _odd;
120     }
121 
122     private static Log _log = LogFactoryUtil.getLog(MBTreeWalkerImpl.class);
123 
124     private Map<Long, Integer> _messageIdsMap;
125     private List<MBMessage> _messages;
126     private boolean _odd;
127 
128 }