1   /**
2    * Copyright (c) 2000-2009 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.MBCategory;
26  import com.liferay.portlet.messageboards.model.MBMessage;
27  import com.liferay.portlet.messageboards.model.MBMessageDisplay;
28  import com.liferay.portlet.messageboards.model.MBThread;
29  import com.liferay.portlet.messageboards.model.MBTreeWalker;
30  
31  import java.util.ArrayList;
32  import java.util.List;
33  
34  /**
35   * <a href="MBMessageDisplayImpl.java.html"><b><i>View Source</i></b></a>
36   *
37   * @author Brian Wing Shun Chan
38   *
39   */
40  public class MBMessageDisplayImpl implements MBMessageDisplay {
41  
42      public MBMessageDisplayImpl(
43          MBMessage message, MBMessage parentMessage, MBCategory category,
44          MBThread thread, MBTreeWalker treeWalker, MBThread previousThread,
45          MBThread nextThread, MBThread firstThread, MBThread lastThread) {
46  
47          _message = message;
48          _parentMessage = parentMessage;
49          _category = category;
50          _thread = thread;
51          _treeWalker = new MBTreeWalkerImpl(message);
52          _previousThread = previousThread;
53          _nextThread = nextThread;
54          _firstThread = firstThread;
55          _lastThread = lastThread;
56  
57          List<MBMessage> orderedMessages = new ArrayList<MBMessage>();
58  
59          _orderMessages(_treeWalker, treeWalker.getRoot(), orderedMessages);
60  
61          for (int i = 0; i < orderedMessages.size(); i++) {
62              MBMessage curMessage = orderedMessages.get(i);
63  
64              if (i == 0) {
65                  _firstMessage = curMessage;
66              }
67  
68              if (curMessage.equals(message)) {
69                  if ((i - 1) >= 0) {
70                      _previousMessage = orderedMessages.get(i - 1);
71                  }
72  
73                  if ((i + 1) < orderedMessages.size()) {
74                      _nextMessage = orderedMessages.get(i + 1);
75                  }
76              }
77  
78              if ((i + 1) == orderedMessages.size()) {
79                  _lastMessage = curMessage;
80              }
81          }
82      }
83  
84      public MBMessage getMessage() {
85          return _message;
86      }
87  
88      public MBMessage getParentMessage() {
89          return _parentMessage;
90      }
91  
92      public MBCategory getCategory() {
93          return _category;
94      }
95  
96      public MBThread getThread() {
97          return _thread;
98      }
99  
100     public MBTreeWalker getTreeWalker() {
101         return _treeWalker;
102     }
103 
104     public MBThread getPreviousThread() {
105         return _previousThread;
106     }
107 
108     public MBThread getNextThread() {
109         return _nextThread;
110     }
111 
112     public MBThread getFirstThread() {
113         return _firstThread;
114     }
115 
116     public MBThread getLastThread() {
117         return _lastThread;
118     }
119 
120     public boolean isFirstThread() {
121         if (_firstThread == null) {
122             return false;
123         }
124         else if (_firstThread.equals(_thread)) {
125             return true;
126         }
127         else {
128             return false;
129         }
130     }
131 
132     public boolean isLastThread() {
133         if (_lastThread == null) {
134             return false;
135         }
136         else if (_lastThread.equals(_thread)) {
137             return true;
138         }
139         else {
140             return false;
141         }
142     }
143 
144     public MBMessage getPreviousMessage() {
145         return _previousMessage;
146     }
147 
148     public MBMessage getNextMessage() {
149         return _nextMessage;
150     }
151 
152     public MBMessage getFirstMessage() {
153         return _firstMessage;
154     }
155 
156     public MBMessage getLastMessage() {
157         return _lastMessage;
158     }
159 
160     public boolean isFirstMessage() {
161         if (_firstMessage == null) {
162             return false;
163         }
164         else if (_firstMessage.equals(_message)) {
165             return true;
166         }
167         else {
168             return false;
169         }
170     }
171 
172     public boolean isLastMessage() {
173         if (_lastMessage == null) {
174             return false;
175         }
176         else if (_lastMessage.equals(_message)) {
177             return true;
178         }
179         else {
180             return false;
181         }
182     }
183 
184     private void _orderMessages(
185         MBTreeWalker treeWalker, MBMessage message,
186         List<MBMessage> orderedMessages) {
187 
188         orderedMessages.add(message);
189 
190         List<MBMessage> messages = treeWalker.getMessages();
191         int[] range = treeWalker.getChildrenRange(message);
192 
193         for (int i = range[0]; i < range[1]; i++) {
194             MBMessage curMessage = messages.get(i);
195 
196             _orderMessages(treeWalker, curMessage, orderedMessages);
197         }
198     }
199 
200     private MBMessage _message;
201     private MBMessage _parentMessage;
202     private MBCategory _category;
203     private MBThread _thread;
204     private MBTreeWalker _treeWalker;
205     private MBThread _previousThread;
206     private MBThread _nextThread;
207     private MBThread _firstThread;
208     private MBThread _lastThread;
209     private MBMessage _previousMessage;
210     private MBMessage _nextMessage;
211     private MBMessage _firstMessage;
212     private MBMessage _lastMessage;
213 
214 }