1 12 13 package freeforums.jspbeans; 14 15 import javax.naming.*; 16 import java.util.Hashtable ; 17 import javax.rmi.PortableRemoteObject ; 18 import java.util.Properties ; 19 import java.io.FileInputStream ; 20 import java.util.Properties ; 21 import java.util.Date ; 22 23 import java.util.List ; 24 25 import freeforums.message.*; 26 import freeforums.user.*; 27 28 import org.jdom.*; 29 30 public class MessageController extends Object { 31 32 public MessageController() { } 33 34 35 36 public Document addMessageToForum(Document ForumDetails) { 37 38 try { 39 MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean"); 41 MessageManager manager = home.create(); 43 return manager.addMessage(ForumDetails); 44 } 45 catch(Exception e) { 46 return (new Document(new Element("Response").addContent("Failed"))); 48 } 49 50 } 51 52 public Document markReadMessage(Document MessageDetails) { 53 54 try { 55 MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean"); 57 MessageManager manager = home.create(); 59 return manager.markReadMessage(MessageDetails); 60 } 61 catch(Exception e) { 62 return (new Document(new Element("Response").addContent("Failed"))); 64 } 65 66 } 67 68 public Document isReadMessage(Document MessageDetails) { 69 70 try { 71 MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean"); 73 MessageManager manager = home.create(); 75 return manager.isReadMessage(MessageDetails); 76 } 77 catch(Exception e) { 78 return (new Document(new Element("Response").addContent("Failed"))); 80 } 81 82 } 83 84 public Document getMessage(Document MessageDetails) { 85 86 try { 87 MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean"); 89 MessageManager manager = home.create(); 91 return manager.getMessage(MessageDetails); 92 } 93 catch(Exception e) { 94 return (new Document(new Element("Response").addContent("Failed"))); 96 } 97 98 } 99 100 109 private int depth; 110 111 116 117 private int countSubMessages(Element MessageElement, Document MessageList) { 118 119 String ParentMessageID = MessageElement.getChild("MessageID").getText(); 120 121 List 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 e) { 131 System.out.println("countSubMessage() failed\n" +e); 132 return 0; 133 } 134 135 } 136 137 146 147 private Element formatMessage(Element MessageElement, String UserName) { 148 149 Element RowElement = new Element("tr"); 150 151 String MessageID = MessageElement.getChild("MessageID").getText(); 152 String Subject = MessageElement.getChild("Subject").getText(); 153 String Author = MessageElement.getChild("UserName").getText(); 154 String MessageDate = MessageElement.getChild("MessageDate").getText(); 155 String ForumID = MessageElement.getChild("ForumID").getText(); 156 157 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 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 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 ForumID = ForumUserDetails.getRootElement().getChild("ForumID").getText(); 242 String UserName = ForumUserDetails.getRootElement().getChild("UserName").getText(); 243 244 try { 245 MessageManagerHome home = (MessageManagerHome) new InitialContext().lookup("MessageManagerBean"); 247 MessageManager manager = home.create(); 249 Document MessageListDocument = manager.findMessagesForForum(new Document(new Element("ForumID").addContent(ForumID))); 251 252 List 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 e) { 267 System.out.println("getMessageForForum failed \n" + e); 270 return (new Document(new Element("Response").addContent("Failed"))); 271 } 272 273 } 275 281 282 private void listSubMessages(Element MessageTableElement, Document MessageListDocument, String ParentMessageID, String UserName) { 283 284 List 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 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 if(countSubMessages(currentMessage, MessageListDocument)>0) 317 listSubMessages(MessageTableElement, MessageListDocument, currentMessage.getChild("MessageID").getText(), UserName); 318 } 319 } 320 depth-=8; 324 return; 325 } 326 catch(Exception e) { 327 System.out.println("listSubMessages() failed \n " + e); 329 return; 330 } 331 } 332 333 } | Popular Tags |