1   /**
2    * Copyright (c) 2000-2008 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  
29  import java.util.ArrayList;
30  import java.util.HashMap;
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 = new HashMap<Long, Integer>();
47  
48          try {
49              _messages = MBMessageLocalServiceUtil.getThreadMessages(
50                  message.getThreadId());
51  
52              for (int i = 0; i < _messages.size(); i++) {
53                  MBMessage curMessage = _messages.get(i);
54  
55                  long parentMessageId = curMessage.getParentMessageId();
56  
57                  if (!curMessage.isRoot() &&
58                      !_messageIdsMap.containsKey(parentMessageId)) {
59  
60                      _messageIdsMap.put(parentMessageId, i);
61                  }
62              }
63          }
64          catch (Exception e) {
65              _log.error(e);
66          }
67      }
68  
69      public MBMessage getRoot() {
70          return _messages.get(0);
71      }
72  
73      public List<MBMessage> getChildren(MBMessage message) {
74          List<MBMessage> children = new ArrayList<MBMessage>();
75  
76          int[] range = getChildrenRange(message);
77  
78          for (int i = range[0]; i < range[1]; i++) {
79              children.add(_messages.get(i));
80          }
81  
82          return children;
83      }
84  
85      public int[] getChildrenRange(MBMessage message) {
86          long messageId = message.getMessageId();
87  
88          Integer pos = _messageIdsMap.get(messageId);
89  
90          if (pos == null) {
91              return new int[] {0, 0};
92          }
93  
94          int[] range = new int[2];
95          range[0] = pos.intValue();
96  
97          for (int i = range[0]; i < _messages.size(); i++) {
98              MBMessage curMessage = _messages.get(i);
99  
100             if (curMessage.getParentMessageId() == messageId) {
101                 range[1] = i + 1;
102             }
103             else {
104                 break;
105             }
106         }
107 
108         return range;
109     }
110 
111     public List<MBMessage> getMessages() {
112         return _messages;
113     }
114 
115     public boolean isOdd() {
116         _odd = !_odd;
117 
118         return _odd;
119     }
120 
121     public boolean isLeaf(MBMessage message) {
122         Long messageIdObj = new Long(message.getMessageId());
123 
124         if (_messageIdsMap.containsKey(messageIdObj)) {
125             return false;
126         }
127         else {
128             return true;
129         }
130     }
131 
132     private static Log _log = LogFactory.getLog(MBTreeWalkerImpl.class);
133 
134     private List<MBMessage> _messages;
135     private Map<Long, Integer> _messageIdsMap;
136     private boolean _odd;
137 
138 }