1 40 package com.mvnforum.admin; 41 42 import java.io.IOException ; 43 import java.util.*; 44 45 import com.mvnforum.admin.importexport.XMLUtil; 46 import com.mvnforum.admin.importexport.XMLWriter; 47 import com.mvnforum.db.DAOFactory; 48 import com.mvnforum.db.PostDAO; 49 import net.myvietnam.mvncore.exception.*; 50 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter; 51 import net.myvietnam.mvncore.filter.EnableHtmlTagFilter; 52 53 60 public class PostXML { 61 62 private int postID; 63 65 public int getPostID() { return postID; } 66 67 private int parentPostID; 68 71 public int getParentPostID() { return parentPostID; } 72 73 private int parentThreadID; 74 76 public int getParentThreadID() { return parentThreadID; } 77 78 private int parentForumID; 79 81 public int getParentForumID() { return parentForumID; } 82 83 private int parentCategoryID; 84 86 public int getParentCategoryID() { return parentCategoryID; } 87 88 public PostXML() { 89 super(); 90 postID=-1; 91 parentPostID=0; 92 parentThreadID=-1; 93 parentForumID=-1; 94 parentCategoryID=-1; 95 } 96 97 public void setPostID(String id) { 98 postID=XMLUtil.stringToIntDef(id, -1); 99 } 100 101 public void setParentPost(Object o) 102 throws ForeignKeyNotFoundException { 103 if (o instanceof PostXML) { 104 parentPostID=((PostXML)o).getPostID(); 105 } else { 106 throw new ForeignKeyNotFoundException("Can't find parent post's ID"); 107 } 108 } 109 110 public void setParentPostID(int value) { 111 if (value<0) parentPostID=-1; 112 else parentPostID=value; 113 } 114 115 116 120 public void setParentThread(Object o) 121 throws ForeignKeyNotFoundException { 122 if (o instanceof ThreadXML) { 123 parentThreadID=((ThreadXML)o).getThreadID(); 124 } else if (o instanceof PostXML) { 125 parentThreadID=((PostXML)o).getParentThreadID(); 126 } else { 127 throw new ForeignKeyNotFoundException("Can't find parent thread's ID"); 128 } 129 } 130 131 public void setParentThreadID(int value) { 132 if (value<0) parentThreadID=-1; 133 else parentThreadID=value; 134 } 135 136 public void setParentForum(Object o) 137 throws ForeignKeyNotFoundException { 138 if (o instanceof ThreadXML) { 139 parentForumID=((ThreadXML)o).getParentForumID(); 140 } else if (o instanceof PostXML) { 141 parentForumID=((PostXML)o).getParentForumID(); 142 } else { 143 throw new ForeignKeyNotFoundException("Can't find parent forum's ID"); 144 } 145 } 146 147 public void setParentForumID(int value) { 148 if (value<0) parentForumID=-1; 149 else parentForumID=value; 150 } 151 152 public void setParentCategory(Object o) 153 throws ForeignKeyNotFoundException { 154 if (o instanceof ThreadXML) { 155 parentCategoryID=((ThreadXML)o).getParentCategoryID(); 156 } else if (o instanceof PostXML) { 157 parentCategoryID=((PostXML)o).getParentCategoryID(); 158 } else { 159 throw new ForeignKeyNotFoundException("Can't find parent category's ID"); 160 } 161 } 162 163 public void setParentCategoryID(int value) { 164 if (value<0) parentCategoryID=-1; 165 else parentCategoryID=value; 166 } 167 168 195 public void addPost(String memberName, String lastEditMemberName, 196 String postTopic, String postBody, 197 String postCreationDate, String postLastEditDate, 198 String postCreationIP, String postLastEditIP, 199 String postEditCount, String postFormatOption, 200 String postOption, String postStatus, 201 String postIcon, String postAttachCount) 202 throws CreateException, ObjectNotFoundException,DatabaseException, ForeignKeyNotFoundException { 203 204 if (parentPostID<0) { 205 throw new CreateException("Can't create a post, because no parent post assigned yet."); 206 } else if (parentThreadID<0) { 207 throw new CreateException("Can't create a post, because no parent thread assigned yet."); 208 } else if (parentForumID<0) { 209 throw new CreateException("Can't create a post, because no parent forum assigned yet."); 210 } 211 212 if ((postTopic==null) || (postBody==null)) { 213 throw new CreateException("Can't create a post with empty PostBody."); 214 } else { 215 java.sql.Timestamp postCreationDate1; 216 java.sql.Timestamp postLastEditDate1; 217 int postEditCount1; 218 int postFormatOption1; 219 int postOption1; 220 int postStatus1; 221 int postAttachCount1; 222 223 try { 224 if (memberName==null) memberName=""; 225 if (lastEditMemberName==null) lastEditMemberName=""; 226 postCreationDate1= XMLUtil.stringToSqlTimestampDefNow(postCreationDate); 227 postLastEditDate1= XMLUtil.stringToSqlTimestampDefNull(postLastEditDate); 228 if (postCreationIP==null) postCreationIP="0.0.0.0"; 229 if (postLastEditIP==null) postLastEditIP="0.0.0.0"; 230 postEditCount1= XMLUtil.stringToIntDef(postEditCount, 0); 231 postFormatOption1= XMLUtil.stringToIntDef(postFormatOption, 0); 232 postOption1= XMLUtil.stringToIntDef(postOption, 0); 233 postStatus1= XMLUtil.stringToIntDef(postStatus, 0); 234 if (postIcon==null) postIcon=""; 235 postAttachCount1= XMLUtil.stringToIntDef(postAttachCount, 0); 236 } catch (NumberFormatException e) { 237 throw new CreateException("Invalid data for a post. Expected a number."); 238 } 239 240 int memberID=0; 241 if (!memberName.equals("")) { 242 memberID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName); 243 } 244 245 postTopic=EnableHtmlTagFilter.filter(postTopic); 246 postBody=EnableHtmlTagFilter.filter(postBody); 247 postIcon=EnableHtmlTagFilter.filter(postIcon); 248 this.postID = DAOFactory.getPostDAO().createPost(parentPostID, 249 parentForumID, parentThreadID, 250 memberID, memberName, lastEditMemberName, 251 postTopic, postBody, postCreationDate1, postLastEditDate1, 252 postCreationIP, postLastEditIP, 253 postEditCount1, postFormatOption1, postOption1, 254 postStatus1, postIcon, postAttachCount1); 255 } 256 } 257 258 259 public void increasePostAttachCount() 260 throws ObjectNotFoundException, AssertionException, DatabaseException { 261 if (postID<0) { 262 throw new ObjectNotFoundException("Can't update PostAttachCount on post that is not created yet."); 263 } 264 int attachCount = DAOFactory.getAttachmentDAO().getNumberOfAttachments_inPost(postID); 266 DAOFactory.getPostDAO().updateAttachCount(postID, attachCount); 267 } 268 269 270 public static void exportPost(XMLWriter xmlWriter, int postID) 274 throws NumberFormatException , IOException , ExportException, ObjectNotFoundException, 275 DatabaseException { 276 Collection post1=ExportWebHelper.execSqlQuery( 277 "SELECT MemberName, LastEditMemberName,"+ 278 " PostTopic, PostBody, PostCreationDate, PostLastEditDate,"+ 279 " PostCreationIP, PostLastEditIP, PostEditCount, PostFormatOption,"+ 280 " PostOption, PostStatus, PostIcon, PostAttachCount"+ 281 " FROM "+PostDAO.TABLE_NAME+ 282 " WHERE PostID="+Integer.toString(postID)); 283 Iterator iter=post1.iterator(); 284 String [] post=null; 285 try { 287 if ( (post=(String [])iter.next()) ==null) { 288 throw new ExportException("Can't find data for postID="+postID); 289 } 290 if (post.length!=14) { 291 throw new ExportException("Error while retrieving data about post with postID="+postID); 292 } 293 } catch (NoSuchElementException e) { 294 throw new ExportException("Can't find data for postID=="+postID); 295 } 296 297 xmlWriter.startElement("Post"); 299 300 xmlWriter.startElement("MemberName"); 301 xmlWriter.writeData(post[0]); 302 xmlWriter.endElement("MemberName"); 303 xmlWriter.startElement("LastEditMemberName"); 304 xmlWriter.writeData(post[1]); 305 xmlWriter.endElement("LastEditMemberName"); 306 xmlWriter.startElement("PostTopic"); 307 xmlWriter.writeData(DisableHtmlTagFilter.filter(post[2])); 308 xmlWriter.endElement("PostTopic"); 309 xmlWriter.startElement("PostBody"); 310 xmlWriter.writeData(DisableHtmlTagFilter.filter(post[3])); 311 xmlWriter.endElement("PostBody"); 312 xmlWriter.startElement("PostCreationDate"); 313 xmlWriter.writeData(post[4]); 314 xmlWriter.endElement("PostCreationDate"); 315 316 xmlWriter.startElement("PostLastEditDate"); 317 xmlWriter.writeData(post[5]); 318 xmlWriter.endElement("PostLastEditDate"); 319 xmlWriter.startElement("PostCreationIP"); 320 xmlWriter.writeData(post[6]); 321 xmlWriter.endElement("PostCreationIP"); 322 xmlWriter.startElement("PostLastEditIP"); 323 xmlWriter.writeData(post[7]); 324 xmlWriter.endElement("PostLastEditIP"); 325 xmlWriter.startElement("PostEditCount"); 326 xmlWriter.writeData(post[8]); 327 xmlWriter.endElement("PostEditCount"); 328 xmlWriter.startElement("PostFormatOption"); 329 xmlWriter.writeData(post[9]); 330 xmlWriter.endElement("PostFormatOption"); 331 332 xmlWriter.startElement("PostOption"); 333 xmlWriter.writeData(post[10]); 334 xmlWriter.endElement("PostOption"); 335 xmlWriter.startElement("PostStatus"); 336 xmlWriter.writeData(post[11]); 337 xmlWriter.endElement("PostStatus"); 338 xmlWriter.startElement("PostIcon"); 339 xmlWriter.writeData(DisableHtmlTagFilter.filter(post[12])); 340 xmlWriter.endElement("PostIcon"); 341 xmlWriter.startElement("PostAttachCount"); 342 xmlWriter.writeData(post[13]); 343 xmlWriter.endElement("PostAttachCount"); 344 345 AttachmentXML.exportAttachmentList(xmlWriter, postID); 346 exportPostList_Replies(xmlWriter, postID); 347 xmlWriter.endElement("Post"); 348 } 350 351 public static void exportPostList_FirstPosts(XMLWriter xmlWriter, int parentThreadID) 352 throws IOException , ExportException, ObjectNotFoundException, DatabaseException { 353 Collection postIDs=ExportWebHelper.execSqlQuery( 354 "SELECT PostID"+ 355 " FROM "+PostDAO.TABLE_NAME+ 356 " WHERE ThreadID="+Integer.toString(parentThreadID)+ 357 " AND ParentPostID=0"); 358 Iterator iter=postIDs.iterator(); 359 String [] postID=null; 360 xmlWriter.startElement("PostList"); 362 try { 363 while ( (postID=(String [])iter.next()) !=null) { 364 if (postID.length!=1) { 365 throw new ExportException("Error while retrieving list of posts in threadID="+parentThreadID+"."); 366 } 367 try { 368 int i=Integer.parseInt(postID[0]); 369 exportPost(xmlWriter, i); 370 } catch (NumberFormatException e) { 371 throw new ExportException("Error while retrieving list of posts in threadID="+parentThreadID+"."); 372 } 373 } 374 } catch (NoSuchElementException e) { 375 } 377 xmlWriter.endElement("PostList"); 378 } 380 381 public static void exportPostList_Replies(XMLWriter xmlWriter, int parentPostID) 382 throws IOException , ExportException, ObjectNotFoundException, DatabaseException { 383 Collection postIDs=ExportWebHelper.execSqlQuery( 384 "SELECT PostID"+ 385 " FROM "+PostDAO.TABLE_NAME+ 386 " WHERE ParentPostID="+Integer.toString(parentPostID)); 387 Iterator iter=postIDs.iterator(); 388 String [] postID=null; 389 xmlWriter.startElement("PostList"); 391 try { 392 while ( (postID=(String [])iter.next()) !=null) { 393 if (postID.length!=1) { 394 throw new ExportException("Error while retrieving list of replies to postID="+parentPostID+"."); 395 } 396 try { 397 int i=Integer.parseInt(postID[0]); 398 exportPost(xmlWriter, i); 399 } catch (NumberFormatException e) { 400 throw new ExportException("Error while retrieving list of replies to postID="+parentPostID+"."); 401 } 402 } 403 } catch (NoSuchElementException e) { 404 } 406 xmlWriter.endElement("PostList"); 407 } 409 410 public static void exportPostList(XMLWriter xmlWriter, int parentThreadID) 413 throws IOException , ExportException, ObjectNotFoundException, DatabaseException { 414 417 exportPostList_FirstPosts(xmlWriter, parentThreadID); 418 } 419 420 } 421 | Popular Tags |