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 List<MBMessage> getChildren(MBMessage message) {
68          List<MBMessage> children = new ArrayList<MBMessage>();
69  
70          int[] range = getChildrenRange(message);
71  
72          for (int i = range[0]; i < range[1]; i++) {
73              children.add(_messages.get(i));
74          }
75  
76          return children;
77      }
78  
79      public int[] getChildrenRange(MBMessage message) {
80          long messageId = message.getMessageId();
81  
82          Integer pos = _messageIdsMap.get(messageId);
83  
84          if (pos == null) {
85              return new int[] {0, 0};
86          }
87  
88          int[] range = new int[2];
89          range[0] = pos.intValue();
90  
91          for (int i = range[0]; i < _messages.size(); i++) {
92              MBMessage curMessage = _messages.get(i);
93  
94              if (curMessage.getParentMessageId() == messageId) {
95                  range[1] = i + 1;
96              }
97              else {
98                  break;
99              }
100         }
101 
102         return range;
103     }
104 
105     public List<MBMessage> getMessages() {
106         return _messages;
107     }
108 
109     public MBMessage getRoot() {
110         return _messages.get(0);
111     }
112 
113     public boolean isLeaf(MBMessage message) {
114         Long messageIdObj = new Long(message.getMessageId());
115 
116         if (_messageIdsMap.containsKey(messageIdObj)) {
117             return false;
118         }
119         else {
120             return true;
121         }
122     }
123 
124     public boolean isOdd() {
125         _odd = !_odd;
126 
127         return _odd;
128     }
129 
130     private static Log _log = LogFactoryUtil.getLog(MBTreeWalkerImpl.class);
131 
132     private Map<Long, Integer> _messageIdsMap;
133     private List<MBMessage> _messages;
134     private boolean _odd;
135 
136 }