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.MVNForumConstant; 47 import com.mvnforum.admin.importexport.XMLUtil; 48 import com.mvnforum.admin.importexport.XMLWriter; 49 import com.mvnforum.auth.MVNForumPermission; 50 import com.mvnforum.db.*; 51 import net.myvietnam.mvncore.exception.*; 52 import net.myvietnam.mvncore.filter.DisableHtmlTagFilter; 53 import net.myvietnam.mvncore.filter.EnableHtmlTagFilter; 54 55 62 public class ForumXML { 63 64 private int forumID; 65 67 public int getForumID() { return forumID; } 68 69 private int parentCategoryID; 70 72 public int getParentCategoryID() { return parentCategoryID; } 73 74 public ForumXML() { 75 super(); 76 forumID=-1; 77 parentCategoryID=-1; 78 } 79 80 public void setForumID(String id) { 81 forumID=XMLUtil.stringToIntDef(id, -1); 82 } 83 84 public void setParentCategory(CategoryXML parentCategory) { 85 parentCategoryID=parentCategory.getCategoryID(); 86 } 87 88 public void setParentCategoryID(int value) { 89 if (value<0) parentCategoryID=-1; 90 else parentCategoryID=value; 91 } 92 93 121 public void addForum(String lastPostMemberName, String forumName, 122 String forumDesc, String forumCreationDate, 123 String forumModifiedDate, String forumLastPostDate, 124 String forumOrder, String forumType, 125 String forumFormatOption, String forumOption, 126 String forumStatus, String forumModerationMode, 127 String forumPassword, String forumThreadCount, 128 String forumPostCount) 129 throws CreateException, DuplicateKeyException, ObjectNotFoundException, 130 DatabaseException, ForeignKeyNotFoundException { 131 if (parentCategoryID<0) { 132 throw new CreateException("Can't create a forum, because no parent category assigned yet."); 133 } 134 if ((forumName==null) || (forumName.equals(""))) { 135 throw new CreateException("Can't create a forum with empty ForumName."); 136 } else { 137 java.sql.Timestamp forumCreationDate1; 138 java.sql.Timestamp forumModifiedDate1; 139 java.sql.Timestamp forumLastPostDate1; 140 int forumOrder1; 141 int forumType1; 142 int forumFormatOption1; 143 int forumOption1; 144 int forumStatus1; 145 int forumModerationMode1; 146 int forumThreadCount1; 147 int forumPostCount1; 148 149 try { 150 if (lastPostMemberName==null) lastPostMemberName=""; 151 if (forumDesc==null) forumDesc=""; 152 forumCreationDate1= XMLUtil.stringToSqlTimestampDefNow(forumCreationDate); 153 forumModifiedDate1= XMLUtil.stringToSqlTimestampDefNull(forumModifiedDate); 154 forumLastPostDate1= XMLUtil.stringToSqlTimestampDefNull(forumLastPostDate); 155 forumOrder1 = XMLUtil.stringToIntDef(forumOrder, 0); 156 forumType1 = XMLUtil.stringToIntDef(forumType, 0); 157 forumFormatOption1 = XMLUtil.stringToIntDef(forumFormatOption, 0); 158 forumOption1 = XMLUtil.stringToIntDef(forumOption, 0); 159 forumStatus1 = XMLUtil.stringToIntDef(forumStatus, 0); 160 forumModerationMode1 = XMLUtil.stringToIntDef(forumModerationMode, 0); 161 if (forumPassword==null) forumPassword=""; 162 forumThreadCount1 = XMLUtil.stringToIntDef(forumThreadCount, 0); 163 forumPostCount1 = XMLUtil.stringToIntDef(forumPostCount, 0); 164 } catch (NumberFormatException e) { 165 throw new CreateException("Invalid data for a forum. Expected a number."); 166 } 167 168 forumName=EnableHtmlTagFilter.filter(forumName); 169 forumDesc=EnableHtmlTagFilter.filter(forumDesc); 170 forumPassword=EnableHtmlTagFilter.filter(forumPassword); 171 172 DAOFactory.getForumDAO().create( 173 parentCategoryID, lastPostMemberName, 174 forumName, forumDesc, 175 forumCreationDate1, forumModifiedDate1, forumLastPostDate1, 176 forumOrder1, forumType1, forumFormatOption1, 177 forumOption1, forumStatus1, forumModerationMode1, 178 forumPassword, forumThreadCount1, forumPostCount1); 179 180 Collection forums=DAOFactory.getForumDAO().getForums_inCategory(parentCategoryID); 182 Iterator iter=forums.iterator(); 183 try { 184 ForumBean forum=null; 185 forumID=-1; 186 while ( (forum=(ForumBean)iter.next() )!=null) { 187 if ((forum.getForumName().equals(forumName)) && (forum.getCategoryID()==parentCategoryID)) { 188 forumID=forum.getForumID(); 189 break; 190 } 191 } 192 if (forumID<0) { 193 throw new ObjectNotFoundException("Can't find forum I've just added."); 194 } 195 } catch (NoSuchElementException e) { 196 throw new ObjectNotFoundException("Can't find forum I've just added."); 197 } 198 199 } 200 } 201 202 218 public void addMemberForumPermission(String memberName, String permission) 219 throws CreateException, DatabaseException, DuplicateKeyException, 220 ObjectNotFoundException, ForeignKeyNotFoundException { 221 222 if (forumID<0) { 223 throw new CreateException("Found member's forum-specific permission that is not assigned to any known forum."); 224 } 225 if ( (memberName==null) || (memberName.equals("")) ) { 226 throw new CreateException("Can't create a member's forum-specific permission for a member with empty MemberName."); 227 } 228 229 int permission1; 230 try { 231 permission1=XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS); 232 } catch (NumberFormatException e) { 233 throw new CreateException("Invalid data for a member forum-specific permission. Expected a number."); 234 } 235 int memberID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName); 236 try { 237 DAOFactory.getMemberForumDAO().create(memberID, forumID, permission1); 238 } catch (DuplicateKeyException e) { 239 } 241 } 242 243 259 public void addGroupForumPermission(String groupName, String permission) 260 throws CreateException, DatabaseException, ObjectNotFoundException, 261 DuplicateKeyException, ForeignKeyNotFoundException { 262 263 if (forumID < 0) { 264 throw new CreateException("Found group's forum-specific permission that is not assigned to any known forum."); 265 } 266 if ( (groupName == null) || (groupName.equals(""))) { 267 throw new CreateException("Can't create a group's forum-specific permission for a group with empty GroupName."); 268 } 269 270 int permission1; 271 try { 272 permission1 = XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS); 273 } catch (NumberFormatException e) { 274 throw new CreateException("Invalid data for a group forum-specific permission. Expected a number."); 275 } 276 int groupID = DAOFactory.getGroupsDAO().getGroupIDFromGroupName(groupName); 277 try { 278 DAOFactory.getGroupForumDAO().create(groupID, forumID, permission1); 279 } catch (DuplicateKeyException e) { 280 } 282 } 283 284 public void addGuestMemberForumPermission(String permission) 285 throws CreateException, DatabaseException, ForeignKeyNotFoundException, DuplicateKeyException { 286 287 if (forumID<0) { 288 throw new CreateException("Found guest's forum-specific permission that is not assigned to any known forum."); 289 } 290 int permission1; 291 try { 292 permission1=XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS); 293 } catch (NumberFormatException e) { 294 throw new CreateException("Invalid data for a guest member forum-specific permission. Expected a number."); 295 } 296 try { 297 DAOFactory.getMemberForumDAO().create(MVNForumConstant.MEMBER_ID_OF_GUEST, 298 forumID, permission1); 299 } catch (DuplicateKeyException e) { 300 } 302 } 303 304 public void addRegisteredMembersGroupForumPermission(String permission) 305 throws CreateException, DatabaseException, 306 DuplicateKeyException, ForeignKeyNotFoundException { 307 if (forumID<0) { 308 throw new CreateException("Found group's forum-specific permission that is not assigned to any known forum."); 309 } 310 int permission1; 311 try { 312 permission1=XMLUtil.stringToIntDef(permission, MVNForumPermission.PERMISSION_NO_PERMISSIONS); 313 } catch (NumberFormatException e) { 314 throw new CreateException("Invalid data for a group forum-specific permission. Expected a number."); 315 } 316 try { 317 DAOFactory.getGroupForumDAO().create(MVNForumConstant.GROUP_ID_OF_REGISTERED_MEMBERS, 318 forumID, permission1); 319 } catch (DuplicateKeyException e) { 320 } 322 } 323 324 346 public void addForumWatch(String memberName, 347 String watchType, String watchOption, 348 String watchStatus, String watchCreationDate, 349 String watchLastSentDate, String watchEndDate) 350 throws CreateException, DatabaseException, ObjectNotFoundException, 351 DuplicateKeyException, ForeignKeyNotFoundException { 352 353 if (forumID<0) { 354 throw new CreateException("Found forum watch that is not assigned to any known forum."); 355 } 356 357 int watchType1; 358 int watchOption1; 359 int watchStatus1; 360 java.sql.Timestamp watchCreationDate1; 361 java.sql.Timestamp watchLastSentDate1; 362 java.sql.Timestamp watchEndDate1; 363 364 try { 365 if (memberName==null) memberName=""; 366 watchType1= XMLUtil.stringToIntDef(watchType, 0); 367 watchOption1= XMLUtil.stringToIntDef(watchOption, 0); 368 watchStatus1= XMLUtil.stringToIntDef(watchStatus, 0); 369 watchCreationDate1= XMLUtil.stringToSqlTimestampDefNow(watchCreationDate); 370 watchLastSentDate1= XMLUtil.stringToSqlTimestampDefNull(watchLastSentDate); 371 watchEndDate1= XMLUtil.stringToSqlTimestampDefNull(watchEndDate); 372 } catch (NumberFormatException e) { 373 throw new CreateException("Invalid data for a forum. Expected a number."); 374 } 375 376 int memberID=0; 378 if (!memberName.equals("")) { 379 memberID=DAOFactory.getMemberDAO().getMemberIDFromMemberName(memberName); 380 } 381 DAOFactory.getWatchDAO().create( 382 memberID, 0, forumID, 0, 383 watchType1, watchOption1, watchStatus1, 384 watchCreationDate1, watchLastSentDate1, watchEndDate1); 385 } 386 387 388 public void updateLastPostMemberName(String value) 389 throws ObjectNotFoundException, DatabaseException, ForeignKeyNotFoundException { 390 if (forumID<0) { 391 throw new ObjectNotFoundException("Can't update ForumLastPostMemberName on forum that is not created yet."); 392 } 393 DAOFactory.getForumDAO().updateLastPostMemberName(forumID, value); 394 } 395 396 public void updateLastPostDate(Timestamp value) 397 throws ObjectNotFoundException, DatabaseException { 398 if (forumID<0) { 399 throw new ObjectNotFoundException("Can't update ForumLastPostDate on forum that is not created yet."); 400 } 401 DAOFactory.getForumDAO().updateLastPostDate(forumID, value); 402 } 403 404 public void increaseThreadCount() 405 throws ObjectNotFoundException, DatabaseException { 406 if (forumID<0) { 407 throw new ObjectNotFoundException("Can't update ForumThreadCount on forum that is not created yet."); 408 } 409 DAOFactory.getForumDAO().increaseThreadCount(forumID); 410 } 411 412 public void increasePostCount() 413 throws ObjectNotFoundException, DatabaseException { 414 if (forumID<0) { 415 throw new ObjectNotFoundException("Can't update ForumPostCount on forum that is not created yet."); 416 } 417 DAOFactory.getForumDAO().increasePostCount(forumID); 418 } 419 420 421 425 public static void exportForumWatchesForForum(XMLWriter xmlWriter, int forumID) 426 throws IOException , ExportException, NumberFormatException , ObjectNotFoundException, 427 DatabaseException { 428 Collection forumWatches=ExportWebHelper.execSqlQuery( 429 "SELECT MemberID, WatchType, WatchOption, WatchStatus, WatchCreationDate, WatchLastSentDate, WatchEndDate"+ 430 " FROM "+WatchDAO.TABLE_NAME+ 431 " WHERE ThreadID=0"+ " AND ForumID="+Integer.toString(forumID)); 433 Iterator iter=forumWatches.iterator(); 434 String [] forumWatch=null; 435 xmlWriter.startElement("ForumWatchList"); 437 try { 438 while ( (forumWatch=(String [])iter.next()) !=null) { 439 if (forumWatch.length!=7) { 440 throw new ExportException("Error while retrieving data about forum watch for forumID=="+forumID); 441 } 442 String memberName=DAOFactory.getMemberDAO().getMember_forPublic(Integer.parseInt(forumWatch[0])).getMemberName(); 443 xmlWriter.startElement("ForumWatch"); 444 xmlWriter.startElement("MemberName"); 445 xmlWriter.writeData(memberName); 446 xmlWriter.endElement("MemberName"); 447 xmlWriter.startElement("WatchType"); 448 xmlWriter.writeData(forumWatch[1]); 449 xmlWriter.endElement("WatchType"); 450 xmlWriter.startElement("WatchOption"); 451 xmlWriter.writeData(forumWatch[2]); 452 xmlWriter.endElement("WatchOption"); 453 xmlWriter.startElement("WatchStatus"); 454 xmlWriter.writeData(forumWatch[3]); 455 xmlWriter.endElement("WatchStatus"); 456 xmlWriter.startElement("WatchCreationDate"); 457 xmlWriter.writeData(forumWatch[4]); 458 xmlWriter.endElement("WatchCreationDate"); 459 xmlWriter.startElement("WatchLastSentDate"); 460 xmlWriter.writeData(forumWatch[5]); 461 xmlWriter.endElement("WatchLastSentDate"); 462 xmlWriter.startElement("WatchEndDate"); 463 xmlWriter.writeData(forumWatch[6]); 464 xmlWriter.endElement("WatchEndDate"); 465 xmlWriter.endElement("ForumWatch"); 466 } 467 } catch (NoSuchElementException e) { 468 } 470 xmlWriter.endElement("ForumWatchList"); 471 } 473 474 public static void exportMemberForumPermissionsForForum(XMLWriter xmlWriter, int forumID) 475 throws IOException , ExportException, NumberFormatException , ObjectNotFoundException, 476 DatabaseException { 477 Collection memberForumPermissions=ExportWebHelper.execSqlQuery( 478 "SELECT MemberID, Permission"+ 479 " FROM "+MemberForumDAO.TABLE_NAME+ 480 " WHERE ForumID="+Integer.toString(forumID)); 481 Iterator iter=memberForumPermissions.iterator(); 482 String [] memberForumPermission=null; 483 xmlWriter.startElement("MemberForumPermissionList"); 485 try { 486 while ( (memberForumPermission=(String [])iter.next()) !=null) { 487 if (memberForumPermission.length!=2) { 488 throw new ExportException("Error while retrieving data about member forum-specific permissions for forumID=="+forumID); 489 } 490 String memberName=DAOFactory.getMemberDAO().getMember_forPublic(Integer.parseInt(memberForumPermission[0])).getMemberName(); 491 xmlWriter.startElement("MemberForumPermission"); 492 xmlWriter.startElement("MemberName"); 493 xmlWriter.writeData(memberName); 494 xmlWriter.endElement("MemberName"); 495 xmlWriter.startElement("ForumPermission"); 496 xmlWriter.writeData(memberForumPermission[1]); 497 xmlWriter.endElement("ForumPermission"); 498 xmlWriter.endElement("MemberForumPermission"); 499 } 500 } catch (NoSuchElementException e) { 501 } 503 xmlWriter.endElement("MemberForumPermissionList"); 504 } 506 507 public static void exportGroupForumPermissionsForForum(XMLWriter xmlWriter, int forumID) 508 throws IOException , DatabaseException, ExportException { 509 Collection groupForumPermissions=ExportWebHelper.execSqlQuery( 510 "SELECT G.GroupName, GF.Permission"+ 511 " FROM "+GroupForumDAO.TABLE_NAME+" AS GF, "+ 512 GroupsDAO.TABLE_NAME+" AS G "+ 513 " WHERE G.GroupID=GF.GroupID AND ForumID="+Integer.toString(forumID)); 514 Iterator iter=groupForumPermissions.iterator(); 515 String [] groupForumPermission=null; 516 xmlWriter.startElement("GroupForumPermissionList"); 518 try { 519 while ( (groupForumPermission=(String [])iter.next()) !=null) { 520 if (groupForumPermission.length!=2) { 521 throw new ExportException("Error while retrieving data about group forum-specific permissions for forumID=="+forumID); 522 } 523 xmlWriter.startElement("GroupForumPermission"); 524 xmlWriter.startElement("GroupName"); 525 xmlWriter.writeData(groupForumPermission[0]); 526 xmlWriter.endElement("GroupName"); 527 xmlWriter.startElement("ForumPermission"); 528 xmlWriter.writeData(groupForumPermission[1]); 529 xmlWriter.endElement("ForumPermission"); 530 xmlWriter.endElement("GroupForumPermission"); 531 } 532 } catch (NoSuchElementException e) { 533 } 535 xmlWriter.endElement("GroupForumPermissionList"); 536 } 538 539 public static void exportForum(XMLWriter xmlWriter, int forumID) 540 throws NumberFormatException , IOException , ExportException, ObjectNotFoundException, 541 DatabaseException { 542 Collection forum1=ExportWebHelper.execSqlQuery( 543 "SELECT LastPostMemberName, ForumName,"+ 544 " ForumDesc, ForumCreationDate, ForumModifiedDate, ForumLastPostDate,"+ 545 " ForumOrder, ForumType, ForumFormatOption, ForumOption,"+ 546 " ForumStatus, ForumModerationMode, ForumPassword,"+ 547 " ForumThreadCount, ForumPostCount"+ 548 " FROM "+ForumDAO.TABLE_NAME+ 549 " WHERE ForumID="+Integer.toString(forumID)); 550 Iterator iter=forum1.iterator(); 551 String [] forum=null; 552 try { 554 if ( (forum=(String [])iter.next()) ==null) { 555 throw new ExportException("Can't find data for forumID=="+forumID); 556 } 557 if (forum.length!=15) { 558 throw new ExportException("Error while retrieving data about forum with forumID=="+forumID); 559 } 560 } catch (NoSuchElementException e) { 561 throw new ExportException("Can't find data for forumID=="+forumID); 562 } 563 564 xmlWriter.startElement("Forum"); 566 567 xmlWriter.startElement("ForumLastPostMemberName"); 568 xmlWriter.writeData(forum[0]); 569 xmlWriter.endElement("ForumLastPostMemberName"); 570 xmlWriter.startElement("ForumName"); 571 xmlWriter.writeData(DisableHtmlTagFilter.filter(forum[1])); 572 xmlWriter.endElement("ForumName"); 573 xmlWriter.startElement("ForumDesc"); 574 xmlWriter.writeData(DisableHtmlTagFilter.filter(forum[2])); 575 xmlWriter.endElement("ForumDesc"); 576 xmlWriter.startElement("ForumCreationDate"); 577 xmlWriter.writeData(forum[3]); 578 xmlWriter.endElement("ForumCreationDate"); 579 xmlWriter.startElement("ForumModifiedDate"); 580 xmlWriter.writeData(forum[4]); 581 xmlWriter.endElement("ForumModifiedDate"); 582 583 xmlWriter.startElement("ForumLastPostDate"); 584 xmlWriter.writeData(forum[5]); 585 xmlWriter.endElement("ForumLastPostDate"); 586 xmlWriter.startElement("ForumOrder"); 587 xmlWriter.writeData(forum[6]); 588 xmlWriter.endElement("ForumOrder"); 589 xmlWriter.startElement("ForumType"); 590 xmlWriter.writeData(forum[7]); 591 xmlWriter.endElement("ForumType"); 592 xmlWriter.startElement("ForumFormatOption"); 593 xmlWriter.writeData(forum[8]); 594 xmlWriter.endElement("ForumFormatOption"); 595 xmlWriter.startElement("ForumOption"); 596 xmlWriter.writeData(forum[9]); 597 xmlWriter.endElement("ForumOption"); 598 599 xmlWriter.startElement("ForumStatus"); 600 xmlWriter.writeData(forum[10]); 601 xmlWriter.endElement("ForumStatus"); 602 xmlWriter.startElement("ForumModerationMode"); 603 xmlWriter.writeData(forum[11]); 604 xmlWriter.endElement("ForumModerationMode"); 605 xmlWriter.startElement("ForumPassword"); 606 xmlWriter.writeData(DisableHtmlTagFilter.filter(forum[12])); 607 xmlWriter.endElement("ForumPassword"); 608 xmlWriter.startElement("ForumThreadCount"); 609 xmlWriter.writeData(forum[13]); 610 xmlWriter.endElement("ForumThreadCount"); 611 xmlWriter.startElement("ForumPostCount"); 612 xmlWriter.writeData(forum[14]); 613 xmlWriter.endElement("ForumPostCount"); 614 615 exportMemberForumPermissionsForForum(xmlWriter, forumID); 616 exportGroupForumPermissionsForForum(xmlWriter, forumID); 617 exportForumWatchesForForum(xmlWriter, forumID); 618 ThreadXML.exportThreadList(xmlWriter, forumID); 619 xmlWriter.endElement("Forum"); 620 } 622 623 public static void exportForumList(XMLWriter xmlWriter, int parentCategoryID) 626 throws IOException , ExportException, ObjectNotFoundException, DatabaseException { 627 Collection forumIDs=ExportWebHelper.execSqlQuery( 628 "SELECT ForumID"+ 629 " FROM "+ForumDAO.TABLE_NAME+ 630 " WHERE CategoryID="+Integer.toString(parentCategoryID)); 631 Iterator iter=forumIDs.iterator(); 632 String [] forumID=null; 633 xmlWriter.startElement("ForumList"); 635 try { 636 while ( (forumID=(String [])iter.next()) !=null) { 637 if (forumID.length!=1) { 638 throw new ExportException("Error while retrieving list of forums."); 639 } 640 try { 641 int i=Integer.parseInt(forumID[0]); 642 exportForum(xmlWriter, i); 643 } catch (NumberFormatException e) { 644 throw new ExportException("Error while retrieving list of forums."); 645 } 646 } 647 } catch (NoSuchElementException e) { 648 } 650 xmlWriter.endElement("ForumList"); 651 } 653 654 655 } 656 657 | Popular Tags |