KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freeforums > message > MessageManagerBean


1 /*
2  * MessageManagerBean.java
3  *
4  * Message Manager bean represent functions for
5  * managing the forums
6  *
7  * @author: Devraj Mukherjee
8  *
9  * Created: 07th July 2001
10  *
11  */

12
13  package freeforums.message;
14
15  import java.rmi.RemoteException JavaDoc;
16  import javax.ejb.*;
17  import javax.naming.*;
18  import javax.rmi.*;
19  import java.rmi.ServerException JavaDoc;
20  import java.util.Date JavaDoc;
21  import java.util.Collection JavaDoc;
22  import java.util.Vector JavaDoc;
23
24  import freeforums.user.*;
25  import freeforums.util.*;
26
27  import org.jdom.*;
28  import java.util.List JavaDoc;
29
30  public class MessageManagerBean implements SessionBean {
31
32      /**
33      Helper function to get the home interface of a specified Bean. This is used by
34      most of the methods in this class.
35      */

36      private Object JavaDoc getHome (String JavaDoc path) {
37          try {
38              return (new InitialContext().lookup(path));
39          }
40         catch (NamingException e) {
41             System.err.println("An exception occured " + e);
42             return null;
43         }
44      }
45
46      /**
47      Empty method body
48      */

49      public MessageManagerBean() {}
50      /**
51      Empty method body
52      */

53      public void ejbCreate() {}
54      /**
55      Empty method body
56      */

57      public void ejbRemove() {}
58      /**
59      Empty method body
60      */

61      public void ejbActivate() {}
62      /**
63      Empty method body
64      */

65      public void ejbPassivate() {}
66      /**
67      Empty method body
68      */

69      public void setSessionContext(SessionContext sc) {}
70
71      /**
72       Gets a message given it's message id
73       **/

74
75      public Document getMessage(Document message) throws RemoteException JavaDoc, FinderException {
76
77          Integer JavaDoc messageID = new Integer JavaDoc(message.getRootElement().getText());
78
79          try {
80              MessageHome home = (MessageHome) getHome("java:comp/env/ejb/Message");
81              Message existingMessage = home.findByPrimaryKey(messageID);
82
83              Date JavaDoc MessageDate = existingMessage.getMessageDate();
84              String JavaDoc DisplayDate = MessageDate.getDate() + "/" + MessageDate.getMonth() + "/" + (1900 + MessageDate.getYear());
85
86              Element MessageElement = new Element("Message");
87
88              MessageElement.addContent(new Element("MessageID").addContent(existingMessage.getMessageID().toString()));
89              MessageElement.addContent(new Element("Subject").addContent(existingMessage.getSubject()));
90              MessageElement.addContent(new Element("MessageBody").addContent(existingMessage.getDescription()));
91              MessageElement.addContent(new Element("MessageDate").addContent(DisplayDate));
92              MessageElement.addContent(new Element("UserName").addContent(existingMessage.getUserName()));
93              MessageElement.addContent(new Element("ForumID").addContent(existingMessage.getForumID().toString()));
94              MessageElement.addContent(new Element("ReplyToID").addContent(existingMessage.getReplyToID().toString()));
95
96              Element RootElement = new Element("MessageList");
97              RootElement.addContent(MessageElement);
98
99              return (new Document(RootElement));
100          }
101          catch(Exception JavaDoc e) {
102              return (new Document(new Element("Response").addContent("Failed")));
103          }
104      }
105
106      /**
107       Adds a new message to the collection
108       **/

109
110      public Document addMessage(Document MessageInformation) throws RemoteException JavaDoc, FinderException {
111
112          Element RootElement = MessageInformation.getRootElement();
113
114          List JavaDoc MessageListElement = RootElement.getChildren();
115          Element [] MessageElements = (Element[])MessageListElement.toArray(new Element[0]);
116
117          String JavaDoc subject = MessageElements[0].getChild("Subject").getText();
118          String JavaDoc description = MessageElements[0].getChild("MessageBody").getText();
119          Integer JavaDoc forumID = new Integer JavaDoc(MessageElements[0].getChild("ForumID").getText());
120          String JavaDoc userName = MessageElements[0].getChild("UserName").getText();
121          Integer JavaDoc replyToID = new Integer JavaDoc(MessageElements[0].getChild("ReplyToID").getText());
122
123
124          String JavaDoc identifier = "Message";
125          Integer JavaDoc id=null;
126
127          try {
128
129              /**
130               Process of creating AutoNumbers
131               **/

132              AutoNumberHome ANHome = (AutoNumberHome) getHome("java:comp/env/ejb/AutoNumber");
133              AutoNumber AutoNumberFactory = ANHome.findByPrimaryKey(identifier);
134              id = AutoNumberFactory.getAutoNumberValue();
135
136          }
137          catch (Exception JavaDoc e) {
138              /**
139               This Key was not found in the storage so
140               create a new one with this name
141               **/

142               try {
143                   AutoNumberHome ANHome = (AutoNumberHome) getHome("java:comp/env/ejb/AutoNumber");
144                   AutoNumber AutoNumberFactory = ANHome.create(identifier);
145                   id = AutoNumberFactory.getAutoNumberValue();
146               }
147               catch(Exception JavaDoc exp) {
148                   /**
149                    Could not create this primary key
150                    **/

151                }
152
153           }
154
155           if (id==null) return (new Document(new Element("Response").addContent("Failed")));
156
157           try {
158
159               /**
160               Process of creating the Forum Object
161               **/

162
163              MessageHome home = (MessageHome) getHome("java:comp/env/ejb/Message");
164              Message tempMessage = home.create(id,subject,description,forumID,userName);
165              tempMessage.setReplyToID(replyToID);
166              tempMessage.setMessageDate(new Date JavaDoc());
167              return (new Document(new Element("Response").addContent("Done")));
168
169          }
170          catch(Exception JavaDoc e) {
171              //do nothing
172
System.out.println("Exception occured while creating Forum \n" + e);
173              return (new Document(new Element("Response").addContent("Failed")));
174          }
175
176      } // end add message method
177

178     /**
179      Marks a message to be read
180      **/

181
182     public Document markReadMessage(Document MessageInfo) throws RemoteException JavaDoc, FinderException {
183
184         Element rootElement = MessageInfo.getRootElement();
185         String JavaDoc userName = rootElement.getChild("UserName").getText();
186         Integer JavaDoc messageID = new Integer JavaDoc(rootElement.getChild("MessageID").getText());
187
188         try {
189             ReadMessageHome home = (ReadMessageHome) getHome("java:comp/env/ejb/ReadMessage");
190             ReadMessage tempReadMessage = home.create(new ReadMessagePK(userName, messageID));
191             return (new Document(new Element("Response").addContent("Done")));
192          }
193          catch(Exception JavaDoc e) {
194              //do nothing
195
System.out.println("Exception while marking message read \n" + e);
196             return (new Document(new Element("Response").addContent("Failed")));
197          }
198     } // end method
199

200     /**
201      Checks to see if a message is read by a user
202      **/

203
204     public Document isReadMessage(Document MessageInfo) throws RemoteException JavaDoc, FinderException {
205
206         Element rootElement = MessageInfo.getRootElement();
207         String JavaDoc userName = rootElement.getChild("UserName").getText();
208         Integer JavaDoc messageID = new Integer JavaDoc(rootElement.getChild("MessageID").getText());
209
210         try {
211             ReadMessageHome home = (ReadMessageHome) getHome("java:comp/env/ejb/ReadMessage");
212             ReadMessage tempReadMessage = home.findByPrimaryKey(new ReadMessagePK(userName, messageID));
213             if(tempReadMessage==null) return (new Document(new Element("Response").addContent("Read")));
214             else return (new Document(new Element("Response").addContent("UnRead")));
215          }
216          catch(Exception JavaDoc e) {
217              //do nothing
218
return (new Document(new Element("Response").addContent("UnRead")));
219          }
220     } // end method
221

222
223      /**
224       finds messages for a forum id
225       **/

226
227      public Document findMessagesForForum(Document ForumID) throws RemoteException JavaDoc, FinderException {
228
229           Integer JavaDoc forumID = new Integer JavaDoc(ForumID.getRootElement().getText());
230
231           Element rootElement = new Element("MessageList");
232
233           try {
234               MessageHome home = (MessageHome) getHome("java:comp/env/ejb/Message");
235               Collection JavaDoc messages = home.findAll();
236               Message[] messageArray = (Message[]) messages.toArray(new Message[0]);
237
238               for(int counter=0; counter<messageArray.length; counter++) {
239
240                   if((messageArray[counter].getForumID()).equals(forumID)) {
241
242                       // Convert the message to a XML document
243

244                      Message existingMessage = messageArray[counter];
245
246                      Date JavaDoc MessageDate = existingMessage.getMessageDate();
247
248                      String JavaDoc DisplayDate = MessageDate.getDate() + "/" + MessageDate.getMonth() + "/" + (1900 + MessageDate.getYear());
249
250                      Element MessageElement = new Element("Message");
251                      MessageElement.addContent(new Element("MessageID").addContent(existingMessage.getMessageID().toString()));
252                      MessageElement.addContent(new Element("Subject").addContent(existingMessage.getSubject()));
253                      MessageElement.addContent(new Element("MessageBody").addContent(existingMessage.getDescription()));
254                      MessageElement.addContent(new Element("MessageDate").addContent(DisplayDate));
255                      MessageElement.addContent(new Element("UserName").addContent(existingMessage.getUserName()));
256                      MessageElement.addContent(new Element("ForumID").addContent(existingMessage.getForumID().toString()));
257                      MessageElement.addContent(new Element("ReplyToID").addContent(existingMessage.getReplyToID().toString()));
258
259                      rootElement.addContent(MessageElement);
260
261                   }
262
263               }
264
265               return (new Document(rootElement));
266           }
267           catch(Exception JavaDoc e) {
268               //something happened on the way to heaven
269
System.out.println("Exception occurred while finding messages\n" + e);
270               return (new Document(new Element("Response").addContent("Done")));
271           }
272     } // end method
273

274  } // end class file
Popular Tags