1 40 package com.mvnforum.admin; 41 42 import java.io.IOException ; 43 import java.sql.Timestamp ; 44 import java.util.*; 45 46 import com.mvnforum.admin.importexport.XMLUtil; 47 import com.mvnforum.admin.importexport.XMLWriter; 48 import com.mvnforum.db.*; 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 ThreadXML { 61 62 private int threadID; 63 65 public int getThreadID() { return threadID; } 66 67 private int parentForumID; 68 70 public int getParentForumID() { return parentForumID; } 71 72 private int parentCategoryID; 73 75 public int getParentCategoryID() { return parentCategoryID; } 76 77 public ThreadXML() { 78 super(); 79 threadID=-1; 80 parentForumID=-1; 81 parentCategoryID=-1; 82 } 83 84 public void setThreadID(String id) { 85 threadID=XMLUtil.stringToIntDef(id, -1); 86 } 87 88 public void setParentForum(Object o) 89 throws ForeignKeyNotFoundException { 90 if (o instanceof ForumXML) { 91 parentForumID=((ForumXML)o).getForumID(); 92 } else { 93 throw new ForeignKeyNotFoundException("Can't find parent forum's ID"); 94 } 95 } 96 97 public void setParentForumID(int value) { 98 if (value<0) parentForumID=-1; 99 else parentForumID=value; 100 } 101 102 public void setParentCategory(Object o) 103 throws ForeignKeyNotFoundException { 104 if (o instanceof ForumXML) { 105 parentCategoryID=((ForumXML)o).getParentCategoryID(); 106 } else { 107 throw new ForeignKeyNotFoundException("Can't find parent category's ID"); 108 } 109 } 110 111 public void setParentCategoryID(int value) { 112 if (value<0) parentCategoryID=-1; 113 else parentCategoryID=value; 114 } 115 116 144 public void addThread(String memberName, String lastPostMemberName, 145 String threadTopic, String threadBody, 146 String threadVoteCount, String threadVoteTotalStars, 147 String threadCreationDate, String threadLastPostDate, 148 String threadType, String threadOption, 149 String threadStatus, String threadHasPoll, 150 String threadViewCount, String threadReplyCount, 151 String threadIcon, String threadDuration, String threadAttachCount) 152 throws CreateException, ObjectNotFoundException, 153 DatabaseException, ForeignKeyNotFoundException { 154 155 if (parentForumID<0) { 156 throw new CreateException("Can't create a thread, because no parent forum assigned yet."); 157 } else if (parentCategoryID<0) { 158 throw new CreateException("Can't create a thread, because no parent category assigned yet."); 159 } else if ((threadTopic==null) || (threadBody==null)) { 160 throw new CreateException("Can't create a thread with empty ThreadTopic or empty ThreadBody."); 161 } else { 162 int threadVoteCount1; 163 int threadVoteTotalStars1; 164 java.sql.Timestamp threadCreationDate1; 165 java.sql.Timestamp threadLastPostDate1; 166 int threadType1; 167 int threadOption1; 168 int threadStatus1; 169 int threadHasPoll1; 170 int threadViewCount1; 171 int threadReplyCount1; 172 int threadDuration1; 173 int threadAttachCount1; 174 175 try { 176 if (memberName==null) memberName=""; 177 if (lastPostMemberName==null) lastPostMemberName=""; 178 threadVoteCount1= XMLUtil.stringToIntDef(threadVoteCount, 0); 179 threadVoteTotalStars1= XMLUtil.stringToIntDef(threadVoteTotalStars, 0); 180 threadCreationDate1= XMLUtil.stringToSqlTimestampDefNow(threadCreationDate); 181 threadLastPostDate1= XMLUtil.stringToSqlTimestampDefNull(threadLastPostDate); 182 threadType1 = XMLUtil.stringToIntDef(threadType, 0); 183 threadOption1 = XMLUtil.stringToIntDef(threadOption, 0); 184 threadStatus1 = XMLUtil.stringToIntDef(threadStatus, 0); 185 threadHasPoll1 = XMLUtil.stringToIntDef(threadHasPoll, 0); 186 threadViewCount1 = XMLUtil.stringToIntDef(threadViewCount, 0); 187 threadReplyCount1 = XMLUtil.stringToIntDef(threadReplyCount, 0); 188 threadAttachCount1 = XMLUtil.stringToIntDef(threadAttachCount, 0); 189 if (threadIcon==null) threadIcon=""; 190 threadDuration1 = XMLUtil.stringToIntDef(threadDuration, 0); 191 } catch (NumberFormatException e) { 192 throw new CreateException("Invalid data for a thread. Expected a number."); 193 } 194 195 threadTopic=EnableHtmlTagFilter.filter(threadTopic); 196 threadBody=EnableHtmlTagFilter.filter(threadBody); 197 threadIcon=EnableHtmlTagFilter.filter(threadIcon); 198 this.threadID = DAOFactory.getThreadDAO().createThread(parentForumID, 199 memberName, lastPostMemberName, 200 threadTopic, threadBody, 201 threadVoteCount1, threadVoteTotalStars1, 202 threadCreationDate1, threadLastPostDate1, 203 threadType1, threadOption1, threadStatus1, 204 threadHasPoll1, threadViewCount1, threadReplyCount1, 205 threadIcon, threadDuration1, threadAttachCount1); 206 } 207 } 208 209 230 public void addThreadWatch(String memberName, 231 String watchType, String watchOption, 232 String watchStatus, String watchCreationDate, 233 String watchLastSentDate, String watchEndDate) 234 throws CreateException, DatabaseException, ObjectNotFoundException, 235 DuplicateKeyException, ForeignKeyNotFoundException { 236 237 if (threadID<0) { 238 throw new CreateException("Found thread watch that is not assigned to any known thread."); 239 } 240 241 int watchType1; 242 int watchOption1; 243 int watchStatus1; 244 java.sql.Timestamp watchCreationDate1; 245 java.sql.Timestamp watchLastSentDate1; 246 java.sql.Timestamp watchEndDate1; 247 248 try { 249 if (memberName==null) memberName=""; 250 watchType1= XMLUtil.stringToIntDef(watchType, 0); 251 watchOption1= XMLUtil.stringToIntDef(watchOption, 0); 252 watchStatus1= XMLUtil.stringToIntDef(watchStatus, 0); 253 watchCreationDate1= XMLUtil.stringToSqlTimestampDefNow(watchCreationDate); 254 watchLastSentDate1= XMLUtil.stringToSqlTimestampDefNull(watchLastSentDate); 255 watchEndDate1= XMLUtil.stringToSqlTimestampDefNull(watchEndDate); 256 } catch (NumberFormatException e) { 257 throw new CreateException("Invalid data for a thread watch. Expected a number."); 258 } 259 260 int memberID=0; 262 if (!memberName.equals("")) { 263 memberID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName); 264 } 265 DAOFactory.getWatchDAO().create( 266 memberID, 0, threadID, 0, 267 watchType1, watchOption1, watchStatus1, 268 watchCreationDate1, watchLastSentDate1, watchEndDate1); 269 } 270 271 public void addFavoriteThread(String memberName, 272 String favoriteCreationDate, String favoriteType, 273 String favoriteOption, String favoriteStatus) 274 throws CreateException, DatabaseException, ObjectNotFoundException, 275 DuplicateKeyException, ForeignKeyNotFoundException { 276 277 if (threadID<0) { 278 throw new CreateException("Found favorite-thread record that is not assigned to any known thread."); 279 } else if (parentForumID<0) { 280 throw new CreateException("Can't create a favorite-thread, because no parent forum assigned yet."); 281 } 282 283 java.sql.Timestamp favoriteCreationDate1; 284 int favoriteType1; 285 int favoriteOption1; 286 int favoriteStatus1; 287 288 try { 289 if (memberName==null) memberName=""; 290 favoriteCreationDate1= XMLUtil.stringToSqlTimestampDefNow(favoriteCreationDate); 291 favoriteType1= XMLUtil.stringToIntDef(favoriteType, 0); 292 favoriteOption1= XMLUtil.stringToIntDef(favoriteOption, 0); 293 favoriteStatus1= XMLUtil.stringToIntDef(favoriteStatus, 0); 294 } catch (NumberFormatException e) { 295 throw new CreateException("Invalid data for a favorite-thread. Expected a number."); 296 } 297 298 int memberID=0; 300 if (!memberName.equals("")) { 301 memberID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName); 302 } 303 DAOFactory.getFavoriteThreadDAO().create( 304 memberID, threadID, parentForumID, 305 favoriteCreationDate1, favoriteType1, favoriteOption1, favoriteStatus1); 306 } 307 308 public void increaseReplyCount() 309 throws ObjectNotFoundException, DatabaseException { 310 311 if (threadID<0) { 312 throw new ObjectNotFoundException("Can't update ThreadReplyCount on thread that is not created yet."); 313 } 314 DAOFactory.getThreadDAO().increaseReplyCount(threadID); 315 } 316 317 public void updateLastPostMemberName(String value) 318 throws ObjectNotFoundException, DatabaseException, ForeignKeyNotFoundException { 319 if (threadID<0) { 320 throw new ObjectNotFoundException("Can't update LastPostMemberName on thread that is not created yet."); 321 } 322 DAOFactory.getThreadDAO().updateLastPostMemberName(threadID, value); 323 } 324 325 public void updateLastPostDate(Timestamp value) 326 throws ObjectNotFoundException, DatabaseException { 327 if (threadID<0) { 328 throw new ObjectNotFoundException("Can't update ThreadLastPostDate on thread that is not created yet."); 329 } 330 DAOFactory.getThreadDAO().updateLastPostDate(threadID, value); 331 } 332 333 334 public static void exportThreadWatchesForThread(XMLWriter xmlWriter, int threadID) 338 throws IOException , ExportException, NumberFormatException , ObjectNotFoundException, 339 DatabaseException { 340 Collection threadWatches=ExportWebHelper.execSqlQuery( 341 "SELECT MemberID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate"+ 342 " FROM "+WatchDAO.TABLE_NAME+ 343 " WHERE ThreadID="+Integer.toString(threadID)); Iterator iter=threadWatches.iterator(); 345 String [] threadWatch=null; 346 xmlWriter.startElement("ThreadWatchList"); 348 try { 349 while ( (threadWatch=(String [])iter.next()) !=null) { 350 if (threadWatch.length!=7) { 351 throw new ExportException("Error while retrieving data about thread watch for threadID=="+threadID); 352 } 353 String memberName=DAOFactory.getMemberDAO().getMember_forPublic(Integer.parseInt(threadWatch[0])).getMemberName(); 354 xmlWriter.startElement("ThreadWatch"); 355 xmlWriter.startElement("MemberName"); 356 xmlWriter.writeData(memberName); 357 xmlWriter.endElement("MemberName"); 358 xmlWriter.startElement("WatchType"); 359 xmlWriter.writeData(threadWatch[1]); 360 xmlWriter.endElement("WatchType"); 361 xmlWriter.startElement("WatchOption"); 362 xmlWriter.writeData(threadWatch[2]); 363 xmlWriter.endElement("WatchOption"); 364 xmlWriter.startElement("WatchStatus"); 365 xmlWriter.writeData(threadWatch[3]); 366 xmlWriter.endElement("WatchStatus"); 367 xmlWriter.startElement("WatchCreationDate"); 368 xmlWriter.writeData(threadWatch[4]); 369 xmlWriter.endElement("WatchCreationDate"); 370 xmlWriter.startElement("WatchLastSentDate"); 371 xmlWriter.writeData(threadWatch[5]); 372 xmlWriter.endElement("WatchLastSentDate"); 373 xmlWriter.startElement("WatchEndDate"); 374 xmlWriter.writeData(threadWatch[6]); 375 xmlWriter.endElement("WatchEndDate"); 376 xmlWriter.endElement("ThreadWatch"); 377 } 378 } catch (NoSuchElementException e) { 379 } 381 xmlWriter.endElement("ThreadWatchList"); 382 } 384 385 public static void exportFavoriteThreadsForThread(XMLWriter xmlWriter, int threadID) 386 throws IOException , ExportException, NumberFormatException , ObjectNotFoundException, 387 DatabaseException { 388 Collection favoriteThreads=ExportWebHelper.execSqlQuery( 389 "SELECT MemberID, FavoriteCreationDate,"+ 390 " FavoriteType, FavoriteOption, FavoriteStatus"+ 391 " FROM "+FavoriteThreadDAO.TABLE_NAME+ 392 " WHERE ThreadID="+Integer.toString(threadID)); 393 Iterator iter=favoriteThreads.iterator(); 394 String [] favoriteThread=null; 395 xmlWriter.startElement("FavoriteThreadList"); 397 try { 398 while ( (favoriteThread=(String [])iter.next()) !=null) { 399 if (favoriteThread.length!=5) { 400 throw new ExportException("Error while retrieving data about favorite-thread records for threadID=="+threadID); 401 } 402 String memberName=DAOFactory.getMemberDAO().getMember_forPublic(Integer.parseInt(favoriteThread[0])).getMemberName(); 403 xmlWriter.startElement("FavoriteThread"); 404 xmlWriter.startElement("MemberName"); 405 xmlWriter.writeData(memberName); 406 xmlWriter.endElement("MemberName"); 407 xmlWriter.startElement("FavoriteCreationDate"); 408 xmlWriter.writeData(favoriteThread[1]); 409 xmlWriter.endElement("FavoriteCreationDate"); 410 xmlWriter.startElement("FavoriteType"); 411 xmlWriter.writeData(favoriteThread[2]); 412 xmlWriter.endElement("FavoriteType"); 413 xmlWriter.startElement("FavoriteOption"); 414 xmlWriter.writeData(favoriteThread[3]); 415 xmlWriter.endElement("FavoriteOption"); 416 xmlWriter.startElement("FavoriteStatus"); 417 xmlWriter.writeData(favoriteThread[4]); 418 xmlWriter.endElement("FavoriteStatus"); 419 xmlWriter.endElement("FavoriteThread"); 420 } 421 } catch (NoSuchElementException e) { 422 } 424 xmlWriter.endElement("FavoriteThreadList"); 425 } 427 428 public static void exportThread(XMLWriter xmlWriter, int threadID) 429 throws NumberFormatException , IOException , ExportException, ObjectNotFoundException, 430 DatabaseException { 431 Collection thread1=ExportWebHelper.execSqlQuery( 432 "SELECT MemberName, LastPostMemberName,"+ 433 " ThreadTopic, ThreadBody, ThreadVoteCount, ThreadVoteTotalStars,"+ 434 " ThreadCreationDate, ThreadLastPostDate, ThreadType, ThreadOption,"+ 435 " ThreadStatus, ThreadHasPoll, ThreadViewCount, ThreadReplyCount,"+ 436 " ThreadIcon, ThreadDuration, ThreadAttachCount"+ 437 " FROM "+ThreadDAO.TABLE_NAME+ 438 " WHERE ThreadID="+Integer.toString(threadID)); 439 Iterator iter=thread1.iterator(); 440 String [] thread=null; 441 try { 443 if ( (thread=(String [])iter.next()) ==null) { 444 throw new ExportException("Can't find data for threadID=="+threadID); 445 } 446 if (thread.length!=17) { 447 throw new ExportException("Error while retrieving data about thread with threadID=="+threadID); 448 } 449 } catch (NoSuchElementException e) { 450 throw new ExportException("Can't find data for threadID=="+threadID); 451 } 452 453 xmlWriter.startElement("Thread"); 455 456 xmlWriter.startElement("MemberName"); 457 xmlWriter.writeData(thread[0]); 458 xmlWriter.endElement("MemberName"); 459 xmlWriter.startElement("ThreadLastPostMemberName"); 460 xmlWriter.writeData(thread[1]); 461 xmlWriter.endElement("ThreadLastPostMemberName"); 462 xmlWriter.startElement("ThreadTopic"); 463 xmlWriter.writeData(DisableHtmlTagFilter.filter(thread[2])); 464 xmlWriter.endElement("ThreadTopic"); 465 xmlWriter.startElement("ThreadBody"); 466 xmlWriter.writeData(DisableHtmlTagFilter.filter(thread[3])); 467 xmlWriter.endElement("ThreadBody"); 468 xmlWriter.startElement("ThreadVoteCount"); 469 xmlWriter.writeData(thread[4]); 470 xmlWriter.endElement("ThreadVoteCount"); 471 472 xmlWriter.startElement("ThreadVoteTotalStars"); 473 xmlWriter.writeData(thread[5]); 474 xmlWriter.endElement("ThreadVoteTotalStars"); 475 xmlWriter.startElement("ThreadCreationDate"); 476 xmlWriter.writeData(thread[6]); 477 xmlWriter.endElement("ThreadCreationDate"); 478 xmlWriter.startElement("ThreadLastPostDate"); 479 xmlWriter.writeData(thread[7]); 480 xmlWriter.endElement("ThreadLastPostDate"); 481 xmlWriter.startElement("ThreadType"); 482 xmlWriter.writeData(thread[8]); 483 xmlWriter.endElement("ThreadType"); 484 xmlWriter.startElement("ThreadOption"); 485 xmlWriter.writeData(thread[9]); 486 xmlWriter.endElement("ThreadOption"); 487 488 xmlWriter.startElement("ThreadStatus"); 489 xmlWriter.writeData(thread[10]); 490 xmlWriter.endElement("ThreadStatus"); 491 xmlWriter.startElement("ThreadHasPoll"); 492 xmlWriter.writeData(thread[11]); 493 xmlWriter.endElement("ThreadHasPoll"); 494 xmlWriter.startElement("ThreadViewCount"); 495 xmlWriter.writeData(thread[12]); 496 xmlWriter.endElement("ThreadViewCount"); 497 xmlWriter.startElement("ThreadReplyCount"); 498 xmlWriter.writeData(thread[13]); 499 xmlWriter.endElement("ThreadReplyCount"); 500 xmlWriter.startElement("ThreadIcon"); 501 xmlWriter.writeData(DisableHtmlTagFilter.filter(thread[14])); 502 xmlWriter.endElement("ThreadIcon"); 503 504 xmlWriter.startElement("ThreadDuration"); 505 xmlWriter.writeData(thread[15]); 506 xmlWriter.endElement("ThreadDuration"); 507 508 xmlWriter.startElement("ThreadAttachCount"); 509 xmlWriter.writeData(thread[16]); 510 xmlWriter.endElement("ThreadAttachCount"); 511 exportThreadWatchesForThread(xmlWriter, threadID); 512 exportFavoriteThreadsForThread(xmlWriter, threadID); 513 PostXML.exportPostList(xmlWriter, threadID); 514 515 xmlWriter.endElement("Thread"); 516 } 518 519 public static void exportThreadList(XMLWriter xmlWriter, int parentForumID) 522 throws IOException , ExportException, ObjectNotFoundException, DatabaseException { 523 Collection threadIDs=ExportWebHelper.execSqlQuery( 524 "SELECT ThreadID"+ 525 " FROM "+ThreadDAO.TABLE_NAME+ 526 " WHERE ForumID="+Integer.toString(parentForumID)); 527 Iterator iter=threadIDs.iterator(); 528 String [] threadID=null; 529 xmlWriter.startElement("ThreadList"); 531 try { 532 while ( (threadID=(String [])iter.next()) !=null) { 533 if (threadID.length!=1) { 534 throw new ExportException("Error while retrieving list of threads."); 535 } 536 try { 537 int i=Integer.parseInt(threadID[0]); 538 exportThread(xmlWriter, i); 539 } catch (NumberFormatException e) { 540 throw new ExportException("Error while retrieving list of threads."); 541 } 542 } 543 } catch (NoSuchElementException e) { 544 } 546 xmlWriter.endElement("ThreadList"); 547 } 549 550 551 } 552 553 | Popular Tags |