1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class MBTreeWalkerImpl implements MBTreeWalker {
42  
43      public MBTreeWalkerImpl(MBMessage message) {
44          _messageIdsMap = new HashMap<Long, Integer>();
45  
46          try {
47              _messages = MBMessageLocalServiceUtil.getThreadMessages(
48                  message.getThreadId());
49  
50              for (int i = 0; i < _messages.size(); i++) {
51                  MBMessage curMessage = _messages.get(i);
52  
53                  long parentMessageId = curMessage.getParentMessageId();
54  
55                  if (!curMessage.isRoot() &&
56                      !_messageIdsMap.containsKey(parentMessageId)) {
57  
58                      _messageIdsMap.put(parentMessageId, i);
59                  }
60              }
61          }
62          catch (Exception e) {
63              _log.error(e);
64          }
65      }
66  
67      public MBMessage getRoot() {
68          return _messages.get(0);
69      }
70  
71      public List<MBMessage> getChildren(MBMessage message) {
72          List<MBMessage> children = new ArrayList<MBMessage>();
73  
74          int[] range = getChildrenRange(message);
75  
76          for (int i = range[0]; i < range[1]; i++) {
77              children.add(_messages.get(i));
78          }
79  
80          return children;
81      }
82  
83      public int[] getChildrenRange(MBMessage message) {
84          long messageId = message.getMessageId();
85  
86          Integer pos = _messageIdsMap.get(messageId);
87  
88          if (pos == null) {
89              return new int[] {0, 0};
90          }
91  
92          int[] range = new int[2];
93          range[0] = pos.intValue();
94  
95          for (int i = range[0]; i < _messages.size(); i++) {
96              MBMessage curMessage = _messages.get(i);
97  
98              if (curMessage.getParentMessageId() == messageId) {
99                  range[1] = i + 1;
100             }
101             else {
102                 break;
103             }
104         }
105 
106         return range;
107     }
108 
109     public List<MBMessage> getMessages() {
110         return _messages;
111     }
112 
113     public boolean isOdd() {
114         _odd = !_odd;
115 
116         return _odd;
117     }
118 
119     public boolean isLeaf(MBMessage message) {
120         Long messageIdObj = new Long(message.getMessageId());
121 
122         if (_messageIdsMap.containsKey(messageIdObj)) {
123             return false;
124         }
125         else {
126             return true;
127         }
128     }
129 
130     private static Log _log = LogFactoryUtil.getLog(MBTreeWalkerImpl.class);
131 
132     private List<MBMessage> _messages;
133     private Map<Long, Integer> _messageIdsMap;
134     private boolean _odd;
135 
136 }