1 12 13 package freeforums.message; 14 15 import java.rmi.RemoteException ; 16 import javax.ejb.*; 17 import javax.naming.*; 18 import javax.rmi.*; 19 import java.rmi.ServerException ; 20 import java.util.Date ; 21 import java.util.Collection ; 22 import java.util.Vector ; 23 24 import freeforums.user.*; 25 import freeforums.util.*; 26 27 import org.jdom.*; 28 import java.util.List ; 29 30 public class MessageManagerBean implements SessionBean { 31 32 36 private Object getHome (String 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 49 public MessageManagerBean() {} 50 53 public void ejbCreate() {} 54 57 public void ejbRemove() {} 58 61 public void ejbActivate() {} 62 65 public void ejbPassivate() {} 66 69 public void setSessionContext(SessionContext sc) {} 70 71 74 75 public Document getMessage(Document message) throws RemoteException , FinderException { 76 77 Integer messageID = new Integer (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 MessageDate = existingMessage.getMessageDate(); 84 String 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 e) { 102 return (new Document(new Element("Response").addContent("Failed"))); 103 } 104 } 105 106 109 110 public Document addMessage(Document MessageInformation) throws RemoteException , FinderException { 111 112 Element RootElement = MessageInformation.getRootElement(); 113 114 List MessageListElement = RootElement.getChildren(); 115 Element [] MessageElements = (Element[])MessageListElement.toArray(new Element[0]); 116 117 String subject = MessageElements[0].getChild("Subject").getText(); 118 String description = MessageElements[0].getChild("MessageBody").getText(); 119 Integer forumID = new Integer (MessageElements[0].getChild("ForumID").getText()); 120 String userName = MessageElements[0].getChild("UserName").getText(); 121 Integer replyToID = new Integer (MessageElements[0].getChild("ReplyToID").getText()); 122 123 124 String identifier = "Message"; 125 Integer id=null; 126 127 try { 128 129 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 e) { 138 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 exp) { 148 151 } 152 153 } 154 155 if (id==null) return (new Document(new Element("Response").addContent("Failed"))); 156 157 try { 158 159 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 ()); 167 return (new Document(new Element("Response").addContent("Done"))); 168 169 } 170 catch(Exception e) { 171 System.out.println("Exception occured while creating Forum \n" + e); 173 return (new Document(new Element("Response").addContent("Failed"))); 174 } 175 176 } 178 181 182 public Document markReadMessage(Document MessageInfo) throws RemoteException , FinderException { 183 184 Element rootElement = MessageInfo.getRootElement(); 185 String userName = rootElement.getChild("UserName").getText(); 186 Integer messageID = new Integer (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 e) { 194 System.out.println("Exception while marking message read \n" + e); 196 return (new Document(new Element("Response").addContent("Failed"))); 197 } 198 } 200 203 204 public Document isReadMessage(Document MessageInfo) throws RemoteException , FinderException { 205 206 Element rootElement = MessageInfo.getRootElement(); 207 String userName = rootElement.getChild("UserName").getText(); 208 Integer messageID = new Integer (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 e) { 217 return (new Document(new Element("Response").addContent("UnRead"))); 219 } 220 } 222 223 226 227 public Document findMessagesForForum(Document ForumID) throws RemoteException , FinderException { 228 229 Integer forumID = new Integer (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 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 244 Message existingMessage = messageArray[counter]; 245 246 Date MessageDate = existingMessage.getMessageDate(); 247 248 String 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 e) { 268 System.out.println("Exception occurred while finding messages\n" + e); 270 return (new Document(new Element("Response").addContent("Done"))); 271 } 272 } 274 } | Popular Tags |