KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freeforums > jspbeans > MessageController


1 /*
2  * MessageController.java
3  *
4  * JavaBean to allow the JSP pages to perform the right
5  * user functions.
6  *
7  * @author: Devraj Mukherjee
8  *
9  * Includes XML Support
10  *
11  */

12
13 package freeforums.jspbeans;
14
15 import javax.naming.*;
16 import java.util.Hashtable JavaDoc;
17 import javax.rmi.PortableRemoteObject JavaDoc;
18 import java.util.Properties JavaDoc;
19 import java.io.FileInputStream JavaDoc;
20 import java.util.Properties JavaDoc;
21 import java.util.Date JavaDoc;
22
23 import java.util.List JavaDoc;
24
25 import freeforums.message.*;
26 import freeforums.user.*;
27
28 import org.jdom.*;
29
30 public class MessageController extends Object JavaDoc {
31
32     public MessageController() { }
33
34     /** Bridge method to addMessage **/
35
36     public Document addMessageToForum(Document ForumDetails) {
37
38         try {
39             //Get the home interface for the user management bean
40
MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean");
41             // Create the manager object
42
MessageManager manager = home.create();
43             return manager.addMessage(ForumDetails);
44         }
45         catch(Exception JavaDoc e) {
46             // do nothing much
47
return (new Document(new Element("Response").addContent("Failed")));
48         }
49
50     }
51
52     public Document markReadMessage(Document MessageDetails) {
53
54         try {
55             //Get the home interface for the user management bean
56
MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean");
57             // Create the manager object
58
MessageManager manager = home.create();
59             return manager.markReadMessage(MessageDetails);
60         }
61         catch(Exception JavaDoc e) {
62             // do nothing much
63
return (new Document(new Element("Response").addContent("Failed")));
64         }
65
66     }
67
68     public Document isReadMessage(Document MessageDetails) {
69
70         try {
71             //Get the home interface for the user management bean
72
MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean");
73             // Create the manager object
74
MessageManager manager = home.create();
75             return manager.isReadMessage(MessageDetails);
76         }
77         catch(Exception JavaDoc e) {
78             // do nothing much
79
return (new Document(new Element("Response").addContent("Failed")));
80         }
81
82     }
83
84     public Document getMessage(Document MessageDetails) {
85
86         try {
87             //Get the home interface for the user management bean
88
MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean");
89             // Create the manager object
90
MessageManager manager = home.create();
91             return manager.getMessage(MessageDetails);
92         }
93         catch(Exception JavaDoc e) {
94             // do nothing much
95
return (new Document(new Element("Response").addContent("Failed")));
96         }
97
98     }
99
100     //////////////////////////////////////////////////////////////////////////////////////////////////
101
//
102
//
103
// The following part of this program is the complex code that produces the threaded list of
104
// messages. The code has been adopted from the previous versions of the same project.
105
//
106
//
107
//////////////////////////////////////////////////////////////////////////////////////////////////
108

109     private int depth;
110
111     /**
112      *
113      * Given a Parent Message ID this will count the number of submessages
114      *
115      **/

116
117     private int countSubMessages(Element MessageElement, Document MessageList) {
118
119         String JavaDoc ParentMessageID = MessageElement.getChild("MessageID").getText();
120
121         List JavaDoc MessageListElement = MessageList.getRootElement().getChildren();
122         Element [] MessageElements = (Element[])MessageListElement.toArray(new Element[0]);
123
124         try {
125             int countMessages=0;
126             for(int cntr=0; cntr<MessageElements.length; cntr++)
127             if(MessageElements[cntr].getChild("ReplyToID").getText().equals(ParentMessageID)) countMessages++;
128             return countMessages;
129         }
130         catch(Exception JavaDoc e) {
131             System.out.println("countSubMessage() failed\n" +e);
132             return 0;
133         }
134
135     }
136
137     /**
138      *
139      * Given a Message Element this method will format it as HTML and return the Element
140      *
141      * This is done so that no web specific output is generated in the Entity or Session Beans
142      *
143      * This Class is a web specific class so we can generate some HTML here.
144      *
145      **/

146
147      private Element formatMessage(Element MessageElement, String JavaDoc UserName) {
148
149          Element RowElement = new Element("tr");
150
151          String JavaDoc MessageID = MessageElement.getChild("MessageID").getText();
152          String JavaDoc Subject = MessageElement.getChild("Subject").getText();
153          String JavaDoc Author = MessageElement.getChild("UserName").getText();
154          String JavaDoc MessageDate = MessageElement.getChild("MessageDate").getText();
155          String JavaDoc ForumID = MessageElement.getChild("ForumID").getText();
156
157          // Check if message is read
158

159          Element ReadMessageStatus = new Element("ReadMessage");
160          ReadMessageStatus.addContent(new Element("UserName").addContent(UserName));
161          ReadMessageStatus.addContent(new Element("MessageID").addContent(MessageID));
162
163          Document ReadStatusResult = isReadMessage(new Document(ReadMessageStatus));
164
165          // Make HTML tags
166

167          RowElement.addContent(new Element("td").addContent(MessageDate));
168
169          Element ReadMessage = new Element("A");
170          ReadMessage.addAttribute("HREF","read_message.jsp?message_id=" + MessageID + "&forum_id=" + ForumID);
171          ReadMessage.addContent(Subject);
172
173          Element ImageTag = new Element("IMG");
174          ImageTag.addAttribute("SRC","../images/spacer.gif");
175          ImageTag.addAttribute("WIDTH",""+depth);
176          ImageTag.addAttribute("HEIGHT","1");
177
178          Element SubjectCol = new Element("td");
179          SubjectCol.addContent(ImageTag);
180
181          if(ReadStatusResult.getRootElement().getText().equals("UnRead")) {
182
183              Element Bold = new Element("B");
184              Bold.addContent(ReadMessage);
185              SubjectCol.addContent(Bold);
186
187          }
188          else SubjectCol.addContent(ReadMessage);
189
190          RowElement.addContent(SubjectCol);
191
192          RowElement.addContent(new Element("td").addContent(Author));
193
194          Element PostMessage = new Element("A");
195          PostMessage.addAttribute("HREF","post_message.jsp?reply_to=" + MessageID + "&forum_id=" + ForumID);
196          PostMessage.addContent("Reply");
197
198          RowElement.addContent(new Element("td").addContent(PostMessage));
199
200          return RowElement;
201
202      }
203
204     /**
205      *
206      * Parent method to generate the HTML
207      *
208      **/

209
210     public Document getMessagesForForum(Document ForumUserDetails) {
211
212         Element MessageTableElement = new Element("table");
213         MessageTableElement.addAttribute("width","100%");
214         MessageTableElement.addAttribute("cellpadding","0");
215         MessageTableElement.addAttribute("cellspacing","0");
216
217         Element HeaderRow = new Element("tr");
218         HeaderRow.addAttribute("bgcolor","#FFFFCC");
219
220         Element Column1 = new Element("td");
221         Column1.addAttribute("width","10%");
222         Column1.addContent("Date");
223         HeaderRow.addContent(Column1);
224
225         Element Column2 = new Element("td");
226         Column2.addContent("Subject");
227         HeaderRow.addContent(Column2);
228
229         Element Column3 = new Element("td");
230         Column3.addAttribute("width","10%");
231         Column3.addContent("Author");
232         HeaderRow.addContent(Column3);
233
234         Element Column4 = new Element("td");
235         Column4.addAttribute("width","10%");
236         Column4.addContent("Reply");
237         HeaderRow.addContent(Column4);
238
239         MessageTableElement.addContent(HeaderRow);
240
241         String JavaDoc ForumID = ForumUserDetails.getRootElement().getChild("ForumID").getText();
242         String JavaDoc UserName = ForumUserDetails.getRootElement().getChild("UserName").getText();
243
244         try {
245             //Get the home interface for the user management bean
246
MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean");
247             // Create the manager object
248
MessageManager manager = home.create();
249             //Get the array
250
Document MessageListDocument = manager.findMessagesForForum(new Document(new Element("ForumID").addContent(ForumID)));
251
252             List JavaDoc MessageListElement = MessageListDocument.getRootElement().getChildren();
253             Element [] MessageElements = (Element[])MessageListElement.toArray(new Element[0]);
254
255             for(int cntr=0; cntr<MessageElements.length; cntr++) {
256                 Element currentMessage = MessageElements[cntr];
257                 if(currentMessage.getChild("ReplyToID").getText().equals("-1")) {
258                     depth=0;
259                     MessageTableElement.addContent(formatMessage(currentMessage, UserName));
260                     listSubMessages(MessageTableElement,MessageListDocument,currentMessage.getChild("MessageID").getText(),UserName);
261                 }
262             }
263
264             return (new Document(MessageTableElement));
265         }
266         catch(Exception JavaDoc e) {
267             //something went wrong while making the HTML
268
//quite unlikely that we would get this exception
269
System.out.println("getMessageForForum failed \n" + e);
270             return (new Document(new Element("Response").addContent("Failed")));
271         }
272
273     } // end method
274

275     /**
276      *
277      * Converts the messages to HTML format
278      * This method is a helper method
279      *
280      **/

281
282     private void listSubMessages(Element MessageTableElement, Document MessageListDocument, String JavaDoc ParentMessageID, String JavaDoc UserName) {
283
284         List JavaDoc MessageListElement = MessageListDocument.getRootElement().getChildren();
285         Element [] MessageElements = (Element[])MessageListElement.toArray(new Element[0]);
286
287         try {
288             Element currentMessage=null;
289             int countMessages=0,messageIndex=0;
290             depth+=8;
291
292             for(int cntr=0; cntr<MessageElements.length; cntr++) {
293                 currentMessage = MessageElements[cntr];
294                 if(currentMessage.getChild("ReplyToID").getText().equals(ParentMessageID)) {
295                     countMessages++;
296                     messageIndex=cntr;
297                 }
298             }
299
300             if (countMessages==0) return;
301             if (countMessages==1) {
302                 currentMessage=MessageElements[messageIndex];
303                 MessageTableElement.addContent(formatMessage(currentMessage,UserName));
304                 if(countSubMessages(currentMessage, MessageListDocument)>0) listSubMessages(MessageTableElement, MessageListDocument, currentMessage.getChild("MessageID").getText(), UserName);
305                 depth-=8;
306                 return;
307             }
308             ///////////////////////////////////
309
// If more than one message then //
310
///////////////////////////////////
311
for(int cntr=0; cntr<MessageElements.length; cntr++) {
312                 currentMessage=MessageElements[cntr];
313                 if(currentMessage.getChild("ReplyToID").getText().equals(ParentMessageID)) {
314                     MessageTableElement.addContent(formatMessage(currentMessage,UserName));
315                     // List sub messages for this reply message
316
if(countSubMessages(currentMessage, MessageListDocument)>0)
317                     listSubMessages(MessageTableElement, MessageListDocument, currentMessage.getChild("MessageID").getText(), UserName);
318                 }
319             }
320             ///////////////////////////////
321
// End more than one message //
322
///////////////////////////////
323
depth-=8;
324             return;
325         }
326         catch(Exception JavaDoc e) {
327             //Something wrong happened above, quite unlikely.
328
System.out.println("listSubMessages() failed \n " + e);
329             return;
330         }
331     }
332
333 } // end class file
Popular Tags