1 21 22 package org.opensubsystems.blog.www; 23 24 import java.io.IOException ; 25 import java.security.Principal ; 26 import java.sql.Timestamp ; 27 import java.util.List ; 28 import java.util.logging.Level ; 29 import java.util.logging.Logger ; 30 31 import javax.servlet.ServletConfig ; 32 import javax.servlet.ServletException ; 33 import javax.servlet.http.HttpServletRequest ; 34 import javax.servlet.http.HttpServletResponse ; 35 import javax.servlet.http.HttpSession ; 36 import javax.transaction.UserTransaction ; 37 38 import org.opensubsystems.blog.data.Blog; 39 import org.opensubsystems.blog.data.Entry; 40 import org.opensubsystems.core.data.DataObject; 41 import org.opensubsystems.core.error.OSSConcurentModifyException; 42 import org.opensubsystems.core.error.OSSInvalidDataException; 43 import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl; 44 import org.opensubsystems.core.util.CallContext; 45 import org.opensubsystems.core.util.DateUtils; 46 import org.opensubsystems.core.util.GlobalConstants; 47 import org.opensubsystems.core.util.Log; 48 import org.opensubsystems.core.util.TransactionUtils; 49 import org.opensubsystems.core.www.WebCommonConstants; 50 import org.opensubsystems.core.www.WebSessionUtils; 51 import org.opensubsystems.core.www.WebUtils; 52 53 62 public class BlogEditServlet extends BlogBrowserServlet 63 { 64 66 69 public static final String BLOGEDIT_LOGIN = "blogedit.login.username"; 70 71 74 public static final String BLOGEDIT_PASSWORD = "blogedit.login.password"; 75 76 79 public static final String BLOGEDIT_LOGIN_PAGE = "blogedit.login.page"; 80 81 84 public static final String BLOGEDIT_LOGOUT_PAGE = "blogedit.logout.page"; 85 86 89 public static final String BLOGEDIT_NEW_BLOG_PAGE = "blogedit.blog.new.page"; 90 91 94 public static final String BLOGEDIT_CONFIRMDELETE_BLOG_PAGE 95 = "blogedit.blog.confirmdelete.page"; 96 97 100 public static final String BLOGEDIT_NEWENTRY_BLOG_PAGE = "blogedit.entry.new.page"; 101 102 105 public static final String BLOGEDIT_CONFIRMDELETE_BLOGENTRY_PAGE 106 = "blogedit.entry.confirmdelete.page"; 107 108 110 113 public static final String FORM_CREATE_BLOG_NAME = "FORM_CREATE_BLOG"; 114 115 118 public static final String FORM_EDIT_BLOG_NAME = "FORM_EDIT_BLOG"; 119 120 123 public static final String FORM_EDIT_BLOG_NAME_CREATE = "create"; 124 125 128 public static final String FORM_EDIT_BLOG_NAME_DELETE = "delete"; 129 130 133 public static final String FORM_EDIT_BLOG_NAME_CREATEENTRY = "createentry"; 134 135 138 public static final String FORM_DELETE_BLOG_NAME = "FORM_DELETE_BLOG"; 139 140 143 public static final String FORM_CREATE_BLOGENTRY_NAME = "FORM_CREATE_BLOGENTRY"; 144 145 148 public static final String FORM_EDIT_BLOGENTRY_NAME = "FORM_EDIT_BLOGENTRY"; 149 150 153 public static final String FORM_EDIT_BLOGENTRY_NAME_DELETE = "delete"; 154 155 158 public static final String FORM_DELETE_BLOGENTRY_NAME = "FORM_DELETE_BLOGENTRY"; 159 160 163 public static final String FORM_LOGIN_NAME = "FORM_LOGIN"; 164 165 168 public static final String FORM_LOGOUT_NAME = "FORM_LOGOUT"; 169 170 173 public static final int FORM_NEW_BLOG_ID = FORM_COUNT_BLOGBROWSER + 1; 174 175 178 public static final int FORM_CREATE_BLOG_ID = FORM_COUNT_BLOGBROWSER + 2; 179 180 183 public static final int FORM_EDIT_BLOG_ID = FORM_COUNT_BLOGBROWSER + 3; 184 185 188 public static final int FORM_CONFIRMDELETE_BLOG_ID = FORM_COUNT_BLOGBROWSER + 4; 189 190 193 public static final int FORM_DELETE_BLOG_ID = FORM_COUNT_BLOGBROWSER + 5; 194 195 198 public static final int FORM_NEWENTRY_BLOG_ID = FORM_COUNT_BLOGBROWSER + 6; 199 200 203 public static final int FORM_CREATE_BLOGENTRY_ID = FORM_COUNT_BLOGBROWSER + 7; 204 205 208 public static final int FORM_EDIT_BLOGENTRY_ID = FORM_COUNT_BLOGBROWSER + 8; 209 210 213 public static final int FORM_CONFIRMDELETE_BLOGENTRY_ID = FORM_COUNT_BLOGBROWSER + 9; 214 215 218 public static final int FORM_DELETE_BLOGENTRY_ID = FORM_COUNT_BLOGBROWSER + 10; 219 220 223 public static final int FORM_LOGIN_ID = FORM_COUNT_BLOGBROWSER + 11; 224 225 228 public static final int FORM_LOGOUT_ID = FORM_COUNT_BLOGBROWSER + 12; 229 230 233 public static final int FORM_COUNT_BLOGEDIT = FORM_COUNT_BLOGBROWSER + 13; 234 235 237 240 private static Logger s_logger = Log.getInstance(BlogEditServlet.class); 241 242 245 private static int s_iSessionCounter = 0; 246 247 250 protected String m_strLogin; 251 252 255 protected String m_strPassword; 256 257 259 262 private static final long serialVersionUID = 7525262325709883457L; 263 264 266 269 public void init( 270 ServletConfig scConfig 271 ) throws ServletException 272 { 273 super.init(scConfig); 274 275 m_strLogin = WebUtils.readProperty(scConfig, BLOGEDIT_LOGIN, "", true); 277 s_logger.config(BLOGEDIT_LOGIN + " = " + m_strLogin); 278 m_strPassword = WebUtils.readProperty(scConfig, BLOGEDIT_PASSWORD, "", true); 279 s_logger.config(BLOGEDIT_LOGIN + " = " + m_strPassword); 280 281 cacheUIPath(scConfig, BLOGEDIT_LOGIN_PAGE, 284 "Path to page to login is not set in property " 285 + BLOGEDIT_LOGIN_PAGE); 286 287 cacheUIPath(scConfig, BLOGEDIT_LOGOUT_PAGE, 289 "Path to page to logout is not set in property " 290 + BLOGEDIT_LOGOUT_PAGE); 291 292 cacheUIPath(scConfig, BLOGEDIT_NEW_BLOG_PAGE, 294 "Path to page to create new blog is not set in property " 295 + BLOGEDIT_NEW_BLOG_PAGE); 296 297 cacheUIPath(scConfig, BLOGEDIT_CONFIRMDELETE_BLOG_PAGE, 299 "Path to page to confirm deletion of blog is not set in property " 300 + BLOGEDIT_CONFIRMDELETE_BLOG_PAGE); 301 302 cacheUIPath(scConfig, BLOGEDIT_NEWENTRY_BLOG_PAGE, 304 "Path to page to create new blog entry is not set in property " 305 + BLOGEDIT_NEWENTRY_BLOG_PAGE); 306 307 cacheUIPath(scConfig, BLOGEDIT_CONFIRMDELETE_BLOGENTRY_PAGE, 309 "Path to page to confirm deletion of blog entry is not set in property " 310 + BLOGEDIT_CONFIRMDELETE_BLOGENTRY_PAGE); 311 } 312 313 316 public String getServletInfo( 317 ) 318 { 319 return this.getClass().getName(); 320 } 321 322 324 327 protected void doGet( 328 HttpServletRequest hsrqRequest, 329 HttpServletResponse hsrpResponse 330 ) throws ServletException , 331 IOException 332 { 333 BlogNavigator navigator = getNavigator(hsrqRequest); 334 335 if (navigator.isLoginPage()) 336 { 337 processNewLoginForm(hsrqRequest, hsrpResponse); 338 } 339 else if (navigator.isLogoutPage()) 340 { 341 processNewLogoutForm(hsrqRequest, hsrpResponse); 342 } 343 else 344 { 345 super.doGet(hsrqRequest, hsrpResponse); 347 } 348 } 349 350 353 protected void doPost( 354 HttpServletRequest hsrqRequest, 355 HttpServletResponse hsrpResponse 356 ) throws ServletException , IOException 357 { 358 switch(getFormToProcess(hsrqRequest)) 359 { 360 case FORM_LOGIN_ID : 361 { 362 processLoginForm(hsrqRequest, hsrpResponse); 363 break; 364 } 365 366 case FORM_LOGOUT_ID : 367 { 368 processLogoutForm(hsrqRequest, hsrpResponse); 369 break; 370 } 371 372 case FORM_NEW_BLOG_ID : 373 { 374 processNewBlogForm(hsrqRequest, hsrpResponse); 375 break; 376 } 377 378 case FORM_CREATE_BLOG_ID : 379 { 380 processCreateBlogForm(hsrqRequest, hsrpResponse); 381 break; 382 } 383 384 case FORM_EDIT_BLOG_ID : 385 { 386 processEditBlogForm(hsrqRequest, hsrpResponse); 387 break; 388 } 389 390 case FORM_CONFIRMDELETE_BLOG_ID : 391 { 392 processConfirmDeleteBlogForm(hsrqRequest, hsrpResponse); 393 break; 394 } 395 396 case FORM_DELETE_BLOG_ID : 397 { 398 processDeleteBlogForm(hsrqRequest, hsrpResponse); 399 break; 400 } 401 402 case FORM_NEWENTRY_BLOG_ID : 403 { 404 processNewEntryBlogForm(hsrqRequest, hsrpResponse); 405 break; 406 } 407 408 case FORM_CREATE_BLOGENTRY_ID : 409 { 410 processCreateEntryForm(hsrqRequest, hsrpResponse); 411 break; 412 } 413 414 case FORM_EDIT_BLOGENTRY_ID : 415 { 416 processEditEntryForm(hsrqRequest, hsrpResponse); 417 break; 418 } 419 420 case FORM_CONFIRMDELETE_BLOGENTRY_ID : 421 { 422 processConfirmDeleteEntryForm(hsrqRequest, hsrpResponse); 423 break; 424 } 425 426 case FORM_DELETE_BLOGENTRY_ID : 427 { 428 processDeleteEntryForm(hsrqRequest, hsrpResponse); 429 break; 430 } 431 432 default : 433 { 434 super.doPost(hsrqRequest, hsrpResponse); 435 break; 436 } 437 } 438 } 439 440 442 445 protected int getFormToProcess( 446 HttpServletRequest hsrqRequest 447 ) 448 { 449 String strFormName; 450 int iReturn = FORM_UNKNOWN_ID; 451 452 strFormName = hsrqRequest.getParameter(FORM_NAME_REQUEST_PARAM); 453 454 if (strFormName == null) 455 { 456 iReturn = super.getFormToProcess(hsrqRequest); 457 } 458 else if (strFormName.equals(FORM_LOGIN_NAME)) 459 { 460 iReturn = FORM_LOGIN_ID; 462 } 463 else if (strFormName.equals(FORM_LOGOUT_NAME)) 464 { 465 iReturn = FORM_LOGOUT_ID; 467 } 468 else if (strFormName.equals(FORM_CREATE_BLOG_NAME)) 469 { 470 iReturn = FORM_CREATE_BLOG_ID; 472 } 473 else if (strFormName.equals(FORM_EDIT_BLOG_NAME)) 474 { 475 if (hsrqRequest.getParameter(FORM_EDIT_BLOG_NAME_CREATE) != null) 477 { 478 iReturn = FORM_NEW_BLOG_ID; 480 } 481 else if (hsrqRequest.getParameter(FORM_EDIT_BLOG_NAME_DELETE) != null) 482 { 483 iReturn = FORM_CONFIRMDELETE_BLOG_ID; 485 } 486 else if (hsrqRequest.getParameter(FORM_EDIT_BLOG_NAME_CREATEENTRY) != null) 487 { 488 iReturn = FORM_NEWENTRY_BLOG_ID; 490 } 491 else 492 { 493 iReturn = FORM_EDIT_BLOG_ID; 495 } 496 } 497 else if (strFormName.equals(FORM_DELETE_BLOG_NAME)) 498 { 499 iReturn = FORM_DELETE_BLOG_ID; 501 } 502 else if (strFormName.equals(FORM_CREATE_BLOGENTRY_NAME)) 503 { 504 iReturn = FORM_CREATE_BLOGENTRY_ID; 506 } 507 else if (strFormName.equals(FORM_EDIT_BLOGENTRY_NAME)) 508 { 509 if (hsrqRequest.getParameter(FORM_EDIT_BLOGENTRY_NAME_DELETE) != null) 512 { 513 iReturn = FORM_CONFIRMDELETE_BLOGENTRY_ID; 515 } 516 else 517 { 518 iReturn = FORM_EDIT_BLOGENTRY_ID; 520 } 521 } 522 else if (strFormName.equals(FORM_DELETE_BLOGENTRY_NAME)) 523 { 524 iReturn = FORM_DELETE_BLOGENTRY_ID; 526 } 527 else 528 { 529 iReturn = super.getFormToProcess(hsrqRequest); 530 } 531 532 return iReturn; 533 } 534 535 543 protected void processNewLoginForm( 544 HttpServletRequest hsrqRequest, 545 HttpServletResponse hsrpResponse 546 ) throws IOException , 547 ServletException 548 { 549 s_logger.entering(this.getClass().getName(), "processNewLoginForm"); 550 551 try 552 { 553 hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest)); 554 displayUI(BLOGEDIT_LOGIN_PAGE, hsrqRequest, hsrpResponse); 555 } 556 catch (Exception eExc) 557 { 558 s_logger.log(Level.WARNING, 559 "An error has occured while displaying login page.", eExc); 560 messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc.getMessage(), 561 getNavigator(hsrqRequest).getRootURL(), 562 eExc.getCause()); 563 } 564 finally 565 { 566 s_logger.exiting(this.getClass().getName(), "processNewLoginForm"); 567 } 568 } 569 570 578 protected void processLoginForm( 579 HttpServletRequest hsrqRequest, 580 HttpServletResponse hsrpResponse 581 ) throws IOException , 582 ServletException 583 { 584 s_logger.entering(this.getClass().getName(), "processLoginForm"); 585 586 try 587 { 588 HttpSession hsSession; 589 Principal currentUser; 590 String strSessionId; 591 String strURL; 592 593 hsSession = hsrqRequest.getSession(false); 597 currentUser = CallContext.getInstance().getCurrentUser(); 598 if (GlobalConstants.ERROR_CHECKING) 599 { 600 assert hsSession != null : "Session must exist at this point"; 601 } 602 603 if (currentUser != null) 604 { 605 WebSessionUtils.resetSessionAndUserInfo(hsSession); 612 613 synchronized (FORM_LOGIN_NAME) 616 { 617 strSessionId = Integer.toString(++s_iSessionCounter); 618 } 619 620 WebSessionUtils.setSessionAndUserInfo(hsSession, strSessionId, currentUser); 621 strURL = getLoginRedirect(hsSession, hsrqRequest); 623 resetLoginRedirect(hsSession); 625 if ((strURL == null) || (strURL.length() == 0)) 626 { 627 strURL = getNavigator(hsrqRequest).getRootURL(); 628 } 629 hsrpResponse.sendRedirect(strURL); 630 } 631 else 632 { 633 hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest)); 635 displayUI(BLOGEDIT_LOGIN_PAGE, hsrqRequest, hsrpResponse); 636 } 637 } 638 catch (Exception eExc) 639 { 640 s_logger.log(Level.WARNING, 641 "An error has occured while processing login page.", eExc); 642 messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc.getMessage(), 643 getNavigator(hsrqRequest).getRootURL(), 644 eExc.getCause()); 645 } 646 finally 647 { 648 s_logger.exiting(this.getClass().getName(), "processLoginForm"); 649 } 650 } 651 652 660 protected void processNewLogoutForm( 661 HttpServletRequest hsrqRequest, 662 HttpServletResponse hsrpResponse 663 ) throws IOException , 664 ServletException 665 { 666 s_logger.entering(this.getClass().getName(), "processNewLogoutForm"); 667 668 try 669 { 670 hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest)); 671 displayUI(BLOGEDIT_LOGOUT_PAGE, hsrqRequest, hsrpResponse); 672 } 673 catch (Exception eExc) 674 { 675 s_logger.log(Level.WARNING, 676 "An error has occured while displaying logout page.", eExc); 677 messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc.getMessage(), 678 getNavigator(hsrqRequest).getRootURL(), 679 eExc.getCause()); 680 } 681 finally 682 { 683 s_logger.exiting(this.getClass().getName(), "processNewLogoutForm"); 684 } 685 } 686 687 695 protected void processLogoutForm( 696 HttpServletRequest hsrqRequest, 697 HttpServletResponse hsrpResponse 698 ) throws IOException , 699 ServletException 700 { 701 s_logger.entering(this.getClass().getName(), "processLogoutForm"); 702 703 try 704 { 705 HttpSession hsSession; 706 707 hsSession = hsrqRequest.getSession(false); 711 if (GlobalConstants.ERROR_CHECKING) 712 { 713 assert hsSession != null : "Session must exist at this point"; 714 } 715 716 WebSessionUtils.resetSessionAndUserInfo(hsSession); 717 718 hsrpResponse.sendRedirect(getNavigator(hsrqRequest).getRootURL()); 720 } 721 catch (Exception eExc) 722 { 723 s_logger.log(Level.WARNING, 724 "An error has occured while processing logout page.", eExc); 725 messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc.getMessage(), 726 getNavigator(hsrqRequest).getRootURL(), 727 eExc.getCause()); 728 } 729 finally 730 { 731 s_logger.exiting(this.getClass().getName(), "processLogoutForm"); 732 } 733 } 734 735 744 protected void processNewBlogForm( 745 HttpServletRequest hsrqRequest, 746 HttpServletResponse hsrpResponse 747 ) throws IOException , 748 ServletException 749 { 750 s_logger.entering(this.getClass().getName(), "processNewBlogForm"); 751 752 try 753 { 754 Blog blog; 755 756 blog = (Blog)getController().get(DataObject.NEW_ID); 759 760 hsrqRequest.setAttribute("blog", blog); 761 hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest)); 762 displayUI(BLOGEDIT_NEW_BLOG_PAGE, hsrqRequest, hsrpResponse); 763 } 764 catch (Exception eExc) 765 { 766 s_logger.log(Level.WARNING, 767 "An error has occured while retrieving information about" + 768 " new blog.", eExc); 769 messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc.getMessage(), 770 getNavigator(hsrqRequest).getRootURL(), 771 eExc.getCause()); 772 } 773 finally 774 { 775 s_logger.exiting(this.getClass().getName(), "processNewBlogForm"); 776 } 777 } 778 779 787 protected void processCreateBlogForm( 788 HttpServletRequest hsrqRequest, 789 HttpServletResponse hsrpResponse 790 ) throws IOException , 791 ServletException 792 { 793 s_logger.entering(this.getClass().getName(), "processCreateBlogForm"); 794 795 UserTransaction transaction = null; 796 Blog blog = null; 797 try 798 { 799 transaction = DatabaseTransactionFactoryImpl.getInstance().requestTransaction(); 800 801 blog = parseBlogFromRequest(hsrqRequest); 802 Blog blogUpdated = null; 803 804 transaction.begin(); 809 blogUpdated = (Blog)getController().create(blog); 810 transaction.commit(); 811 812 hsrpResponse.sendRedirect(getNavigator(hsrqRequest).getURL(blogUpdated)); 814 } 815 catch (OSSInvalidDataException ideExc) 816 { 817 TransactionUtils.rollback(transaction); 818 CallContext.getInstance().getMessages().addMessages( 819 ideExc.getErrorMessages()); 820 hsrqRequest.setAttribute("blog", blog); 822 hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest)); 823 displayUI(BLOGEDIT_NEW_BLOG_PAGE, hsrqRequest, hsrpResponse); 824 } 825 catch (Throwable thr) 826 { 827 TransactionUtils.rollback(transaction); 828 s_logger.log(Level.WARNING, "An error has occured while creating blog.", thr); 829 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 830 getNavigator(hsrqRequest).getRootURL(), 831 thr.getCause()); 832 } 833 finally 834 { 835 s_logger.exiting(this.getClass().getName(), "processCreateBlogForm"); 836 } 837 } 838 839 847 protected void processEditBlogForm( 848 HttpServletRequest hsrqRequest, 849 HttpServletResponse hsrpResponse 850 ) throws IOException , 851 ServletException 852 { 853 s_logger.entering(this.getClass().getName(), "processEditBlogForm"); 854 855 UserTransaction transaction = null; 856 Blog blog = null; 857 858 blog = parseBlogFromRequest(hsrqRequest); 859 try 860 { 861 transaction = DatabaseTransactionFactoryImpl.getInstance().requestTransaction(); 862 863 Blog blogUpdated = null; 864 865 transaction.begin(); 870 blogUpdated = (Blog)getController().save(blog); 871 assert blogUpdated != null : "Save should return not null value."; 872 transaction.commit(); 873 874 hsrpResponse.sendRedirect(getNavigator(hsrqRequest).getRootURL()); 876 } 877 catch (OSSInvalidDataException ideExc) 878 { 879 TransactionUtils.rollback(transaction); 880 CallContext.getInstance().getMessages().addMessages( 881 ideExc.getErrorMessages()); 882 processEditBlogError(hsrqRequest, hsrpResponse, blog); 884 } 885 catch (OSSConcurentModifyException ocmeExc) 886 { 887 TransactionUtils.rollback(transaction); 888 CallContext.getInstance().getMessages().addErrorMessage( 889 ocmeExc.getMessage()); 890 processEditBlogError(hsrqRequest, hsrpResponse, blog); 892 } 893 catch (Throwable thr) 894 { 895 TransactionUtils.rollback(transaction); 896 s_logger.log(Level.WARNING, "An error has occured while saving blog.", thr); 897 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 898 getNavigator(hsrqRequest).getRootURL(), thr.getCause()); 899 } 900 finally 901 { 902 s_logger.exiting(this.getClass().getName(), "processEditBlogForm"); 903 } 904 } 905 906 917 protected void processEditBlogError( 918 HttpServletRequest hsrqRequest, 919 HttpServletResponse hsrpResponse, 920 Blog blog 921 ) throws IOException , 922 ServletException 923 { 924 BlogNavigator navigator; 925 926 navigator = getNavigator(hsrqRequest); 927 928 try 929 { 930 List lstEntries; 931 932 lstEntries = getController().getEntries(blog.getId()); 933 934 hsrqRequest.setAttribute("blog", blog); 936 if (lstEntries != null) 937 { 938 hsrqRequest.setAttribute("blogentries", lstEntries); 939 } 940 hsrqRequest.setAttribute("blognavigator", navigator); 941 displayUI(BLOGBROWSER_BLOG_VIEWER_PAGE, hsrqRequest, hsrpResponse); 942 } 943 catch (Throwable thr) 944 { 945 s_logger.log(Level.WARNING, 946 "An error has occured while retrieving blog entries.", 947 thr); 948 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 949 getNavigator(hsrqRequest).getRootURL(), thr.getCause()); 950 } 951 } 952 953 961 protected void processConfirmDeleteBlogForm( 962 HttpServletRequest hsrqRequest, 963 HttpServletResponse hsrpResponse 964 ) throws IOException , 965 ServletException 966 { 967 s_logger.entering(this.getClass().getName(), "processConfirmDeleteBlogForm"); 968 969 try 970 { 971 Object objBlogIdentification; 972 Blog blog; 973 974 objBlogIdentification = getNavigator(hsrqRequest).getBlogIdentification( 976 hsrqRequest); 977 if (objBlogIdentification instanceof Integer ) 978 { 979 blog = (Blog)getController().get( 980 ((Integer )objBlogIdentification).intValue()); 981 } 982 else 983 { 984 blog = getController().get(objBlogIdentification.toString()); 985 } 986 987 if (blog != null) 988 { 989 hsrqRequest.setAttribute("blog", blog); 990 } 991 hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest)); 992 displayUI(BLOGEDIT_CONFIRMDELETE_BLOG_PAGE, hsrqRequest, hsrpResponse); 993 } 994 catch (Throwable thr) 995 { 996 s_logger.log(Level.WARNING, 997 "An error has occured while retrieving information" + 998 " about blog to delete.", thr); 999 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1000 getNavigator(hsrqRequest).getRootURL(), thr.getCause()); 1001 } 1002 finally 1003 { 1004 s_logger.exiting(this.getClass().getName(), "processConfirmDeleteBlogForm"); 1005 } 1006 } 1007 1008 1016 protected void processDeleteBlogForm( 1017 HttpServletRequest hsrqRequest, 1018 HttpServletResponse hsrpResponse 1019 ) throws IOException , 1020 ServletException 1021 { 1022 s_logger.entering(this.getClass().getName(), "processDeleteBlogForm"); 1023 1024 UserTransaction transaction = null; 1025 try 1026 { 1027 transaction = DatabaseTransactionFactoryImpl.getInstance().requestTransaction(); 1028 1029 int iId = parseBlogFromRequest(hsrqRequest).getId(); 1030 1031 transaction.begin(); 1036 getController().delete(iId); 1037 transaction.commit(); 1038 1039 hsrpResponse.sendRedirect(getNavigator(hsrqRequest).getRootURL()); 1041 } 1042 catch (Throwable thr) 1043 { 1044 TransactionUtils.rollback(transaction); 1045 s_logger.log(Level.WARNING, "An error has occured while deleting blog.", thr); 1046 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1047 getNavigator(hsrqRequest).getRootURL(), thr.getCause()); 1048 } 1049 finally 1050 { 1051 s_logger.exiting(this.getClass().getName(), "processDeleteBlogForm"); 1052 } 1053 } 1054 1055 1063 protected void processNewEntryBlogForm( 1064 HttpServletRequest hsrqRequest, 1065 HttpServletResponse hsrpResponse 1066 ) throws IOException , 1067 ServletException 1068 { 1069 s_logger.entering(this.getClass().getName(), "processNewEntryBlogForm"); 1070 1071 try 1072 { 1073 Object [] arObjects = null; 1074 1075 arObjects = getController().getWithEntry( 1077 parseBlogFromRequest(hsrqRequest).getId(), 1078 DataObject.NEW_ID); 1079 1080 if (arObjects != null) 1081 { 1082 if (arObjects[0] != null) 1083 { 1084 hsrqRequest.setAttribute("blog", arObjects[0]); 1085 } 1086 if (arObjects[1] != null) 1087 { 1088 hsrqRequest.setAttribute("blogentry", arObjects[1]); 1089 } 1090 } 1091 1092 hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest)); 1093 displayUI(BLOGEDIT_NEWENTRY_BLOG_PAGE, hsrqRequest, hsrpResponse); 1094 } 1095 catch (Throwable thr) 1096 { 1097 s_logger.log(Level.WARNING, 1098 "An error has occured while retrieving information" + 1099 " about new blog entry.", thr); 1100 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1101 getNavigator(hsrqRequest).getRootURL(), thr.getCause()); 1102 } 1103 finally 1104 { 1105 s_logger.exiting(this.getClass().getName(), "processNewEntryBlogForm"); 1106 } 1107 } 1108 1109 1117 protected void processCreateEntryForm( 1118 HttpServletRequest hsrqRequest, 1119 HttpServletResponse hsrpResponse 1120 ) throws IOException , 1121 ServletException 1122 { 1123 s_logger.entering(this.getClass().getName(), "processCreateEntryForm"); 1124 1125 UserTransaction transaction = null; 1126 try 1127 { 1128 transaction = DatabaseTransactionFactoryImpl.getInstance().requestTransaction(); 1129 1130 Object objBlogIdentification; 1131 Entry entry = parseEntryFromRequest(hsrqRequest); 1132 Entry entryUpdated = null; 1133 1134 objBlogIdentification = getNavigator(hsrqRequest).getBlogIdentification( 1136 hsrqRequest); 1137 1138 transaction.begin(); 1143 entryUpdated = (Entry)getController().create(entry); 1144 assert entryUpdated != null : "Create should return not null value."; 1145 transaction.commit(); 1146 1147 hsrpResponse.sendRedirect(getNavigator(hsrqRequest).getBlogURL( 1149 objBlogIdentification)); 1150 } 1151 catch (Throwable thr) 1152 { 1153 TransactionUtils.rollback(transaction); 1154 s_logger.log(Level.WARNING, 1155 "An error has occured while creating new blog entry.", thr); 1156 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1157 getNavigator(hsrqRequest).getBlogURL( 1158 getNavigator(hsrqRequest).getBlogIdentification(hsrqRequest)), 1159 thr.getCause()); 1160 } 1161 finally 1162 { 1163 s_logger.exiting(this.getClass().getName(), "processCreateEntryForm"); 1164 } 1165 } 1166 1167 1175 protected void processEditEntryForm( 1176 HttpServletRequest hsrqRequest, 1177 HttpServletResponse hsrpResponse 1178 ) throws IOException , 1179 ServletException 1180 { 1181 s_logger.entering(this.getClass().getName(), "processEditEntryForm"); 1182 1183 UserTransaction transaction = null; 1184 Entry entry = null; 1185 try 1186 { 1187 transaction = DatabaseTransactionFactoryImpl.getInstance().requestTransaction(); 1188 1189 Object objBlogIdentification; 1190 Entry entryUpdated = null; 1191 1192 entry = parseEntryFromRequest(hsrqRequest); 1193 objBlogIdentification = getNavigator(hsrqRequest).getBlogIdentification( 1195 hsrqRequest); 1196 1197 transaction.begin(); 1202 entryUpdated = (Entry)getController().save(entry); 1203 assert entryUpdated != null : "Save should return not null value."; 1204 transaction.commit(); 1205 1206 hsrpResponse.sendRedirect(getNavigator(hsrqRequest).getBlogURL( 1208 objBlogIdentification)); 1209 } 1210 catch (OSSInvalidDataException ideExc) 1211 { 1212 TransactionUtils.rollback(transaction); 1213 CallContext.getInstance().getMessages().addMessages( 1214 ideExc.getErrorMessages()); 1215 processEditEntryError(hsrqRequest, hsrpResponse, entry); 1217 } 1218 catch (OSSConcurentModifyException ocmeExc) 1219 { 1220 TransactionUtils.rollback(transaction); 1221 CallContext.getInstance().getMessages().addErrorMessage( 1222 ocmeExc.getMessage()); 1223 processEditEntryError(hsrqRequest, hsrpResponse, entry); 1225 } 1226 catch (Throwable thr) 1227 { 1228 TransactionUtils.rollback(transaction); 1229 s_logger.log(Level.WARNING, 1230 "An error has occured while saving blog entry.", thr); 1231 try 1232 { 1233 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1234 getNavigator(hsrqRequest).getURL( 1235 getNavigator(hsrqRequest).getBlogEntryIdentification(hsrqRequest)), 1236 thr.getCause()); 1237 } 1238 catch (Exception eExc2) 1239 { 1240 s_logger.log(Level.SEVERE, 1241 "An error has occured while generating error message.", 1242 thr); 1243 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1244 WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE, 1245 eExc2.getCause()); 1246 } 1247 } 1248 finally 1249 { 1250 s_logger.exiting(this.getClass().getName(), "processEditEntryForm"); 1251 } 1252 } 1253 1254 1265 protected void processEditEntryError( 1266 HttpServletRequest hsrqRequest, 1267 HttpServletResponse hsrpResponse, 1268 Entry entry 1269 ) throws IOException , 1270 ServletException 1271 { 1272 try 1273 { 1274 Blog blog = (Blog)getController().get(entry.getParentId()); 1275 1276 if (blog != null) 1277 { 1278 hsrqRequest.setAttribute("blog", blog); 1279 hsrqRequest.setAttribute("blogentry", entry); 1280 hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest)); 1281 displayUI(BLOGBROWSER_BLOGENTRY_VIEWER_PAGE, hsrqRequest, hsrpResponse); 1282 } 1283 else 1284 { 1285 hsrpResponse.sendError(HttpServletResponse.SC_NOT_FOUND); 1288 } 1289 } 1290 catch (Exception eExc) 1291 { 1292 s_logger.log(Level.WARNING, 1293 "An error has occured while retrieving blog entry.", 1294 eExc); 1295 messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc.getMessage(), 1296 getNavigator(hsrqRequest).getRootURL(), eExc.getCause()); 1297 } 1298 } 1299 1300 1308 protected void processConfirmDeleteEntryForm( 1309 HttpServletRequest hsrqRequest, 1310 HttpServletResponse hsrpResponse 1311 ) throws IOException , 1312 ServletException 1313 { 1314 s_logger.entering(this.getClass().getName(), "processConfirmDeleteEntryForm"); 1315 1316 try 1317 { 1318 BlogEntryIdentification entryIdentification; 1319 Object [] arObjects = null; 1320 1321 entryIdentification = getNavigator(hsrqRequest).getBlogEntryIdentification( 1323 hsrqRequest); 1324 if (entryIdentification != null) 1325 { 1326 Object objBlogIdentification; 1327 1328 objBlogIdentification= entryIdentification.getBlogIdentification(); 1329 if (objBlogIdentification instanceof Integer ) 1330 { 1331 arObjects = getController().getWithEntry( 1332 ((Integer )objBlogIdentification).intValue(), 1333 entryIdentification.getBlogEntryIdentification()); 1334 } 1335 else 1336 { 1337 arObjects = getController().getWithEntry( 1338 objBlogIdentification.toString(), 1339 entryIdentification.getBlogEntryIdentification()); 1340 } 1341 } 1342 1343 if (arObjects != null) 1344 { 1345 if (arObjects[0] != null) 1346 { 1347 hsrqRequest.setAttribute("blog", arObjects[0]); 1348 } 1349 if (arObjects[1] != null) 1350 { 1351 hsrqRequest.setAttribute("blogentry", arObjects[1]); 1352 } 1353 } 1354 1355 hsrqRequest.setAttribute("blognavigator", getNavigator(hsrqRequest)); 1356 displayUI(BLOGEDIT_CONFIRMDELETE_BLOGENTRY_PAGE, hsrqRequest, hsrpResponse); 1357 } 1358 catch (Throwable thr) 1359 { 1360 s_logger.log(Level.WARNING, 1361 "An error has occured while retrieving information" + 1362 " about blog entry to delete.", thr); 1363 try 1364 { 1365 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1366 getNavigator(hsrqRequest).getURL( 1367 getNavigator(hsrqRequest).getBlogEntryIdentification(hsrqRequest)), 1368 thr.getCause()); 1369 } 1370 catch (Exception eExc2) 1371 { 1372 s_logger.log(Level.SEVERE, 1373 "An error has occured while generating error message.", 1374 thr); 1375 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1376 WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE, 1377 eExc2.getCause()); 1378 } 1379 } 1380 finally 1381 { 1382 s_logger.exiting(this.getClass().getName(), "processConfirmDeleteEntryForm"); 1383 } 1384 } 1385 1386 1394 protected void processDeleteEntryForm( 1395 HttpServletRequest hsrqRequest, 1396 HttpServletResponse hsrpResponse 1397 ) throws IOException , 1398 ServletException 1399 { 1400 s_logger.entering(this.getClass().getName(), "processDeleteEntryForm"); 1401 1402 UserTransaction transaction = null; 1403 try 1404 { 1405 transaction = DatabaseTransactionFactoryImpl.getInstance().requestTransaction(); 1406 1407 Object objBlogIdentification; 1408 Entry entry = parseEntryFromRequest(hsrqRequest); 1409 1410 objBlogIdentification = getNavigator(hsrqRequest).getBlogIdentification( 1412 hsrqRequest); 1413 1414 transaction.begin(); 1419 getController().deleteEntry(entry.getId()); 1420 transaction.commit(); 1421 1422 hsrpResponse.sendRedirect(getNavigator(hsrqRequest).getBlogURL( 1424 objBlogIdentification)); 1425 } 1426 catch (Throwable thr) 1427 { 1428 TransactionUtils.rollback(transaction); 1429 s_logger.log(Level.WARNING, 1430 "An error has occured while deleting blog entry.", thr); 1431 try 1432 { 1433 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1434 getNavigator(hsrqRequest).getURL( 1435 getNavigator(hsrqRequest).getBlogEntryIdentification(hsrqRequest)), 1436 thr.getCause()); 1437 } 1438 catch (Exception eExc2) 1439 { 1440 s_logger.log(Level.SEVERE, 1441 "An error has occured while generating error message.", 1442 thr); 1443 messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr.getMessage(), 1444 WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE, 1445 eExc2.getCause()); 1446 } 1447 } 1448 finally 1449 { 1450 s_logger.exiting(this.getClass().getName(), "processDeleteEntryForm"); 1451 } 1452 } 1453 1454 1460 protected Blog parseBlogFromRequest( 1461 HttpServletRequest hsrqRequest 1462 ) 1463 { 1464 String strTemp; 1465 int iBlogId; 1466 int iDomainId = DataObject.NEW_ID; 1467 String strFolder; 1468 String strCaption; 1469 String strComments; 1470 Timestamp creationDate; 1471 Timestamp modificationDate; 1472 1473 strTemp = hsrqRequest.getParameter("BLOG_ID"); 1474 if (strTemp == null) 1475 { 1476 throw new IllegalArgumentException ("Blog id is not specified."); 1477 } 1478 iBlogId = Integer.parseInt(strTemp); 1479 1480 iDomainId = CallContext.getInstance().getCurrentDomainId(); 1481 1482 strFolder = hsrqRequest.getParameter("BLOG_FOLDER"); 1483 if (strFolder == null) 1484 { 1485 strFolder = ""; 1486 } 1487 strCaption = hsrqRequest.getParameter("BLOG_CAPTION"); 1488 if (strCaption == null) 1489 { 1490 strCaption = ""; 1491 } 1492 strComments = hsrqRequest.getParameter("BLOG_COMMENTS"); 1493 if (strComments == null) 1494 { 1495 strComments = ""; 1496 } 1497 strTemp = hsrqRequest.getParameter("BLOG_CREATION_DATE"); 1500 if (strTemp == null) 1501 { 1502 creationDate = null; 1503 } 1504 else 1505 { 1506 creationDate = DateUtils.parseTimestamp(strTemp); 1507 } 1508 strTemp = hsrqRequest.getParameter("BLOG_MODIFICATION_DATE"); 1511 if (strTemp == null) 1512 { 1513 modificationDate = null; 1514 } 1515 else 1516 { 1517 modificationDate = DateUtils.parseTimestamp(strTemp); 1518 } 1519 1520 return new Blog(iBlogId, iDomainId, strFolder, strCaption, strComments, 1521 creationDate, modificationDate); 1522 } 1523 1524 1530 protected Entry parseEntryFromRequest( 1531 HttpServletRequest hsrqRequest 1532 ) 1533 { 1534 String strTemp; 1535 int iEntryId; 1536 int iDomainId = DataObject.NEW_ID; 1537 int iBlogId; 1538 String strCaption; 1539 String strComments; 1540 String strImageURL; 1541 String strTargetURL; 1542 Timestamp creationDate; 1543 Timestamp modificationDate; 1544 1545 strTemp = hsrqRequest.getParameter("BLOGENTRY_ID"); 1546 if (strTemp == null) 1547 { 1548 throw new IllegalArgumentException ("Entry id is not specified."); 1549 } 1550 iEntryId = Integer.parseInt(strTemp); 1551 1552 iDomainId = CallContext.getInstance().getCurrentDomainId(); 1553 1554 strTemp = hsrqRequest.getParameter("BLOGENTRY_BLOG_ID"); 1555 if (strTemp == null) 1556 { 1557 iBlogId = DataObject.NEW_ID; 1558 } 1559 else 1560 { 1561 iBlogId = Integer.parseInt(strTemp); 1562 } 1563 strCaption = hsrqRequest.getParameter("BLOGENTRY_CAPTION"); 1564 if (strCaption == null) 1565 { 1566 strCaption = ""; 1567 } 1568 strComments = hsrqRequest.getParameter("BLOGENTRY_COMMENTS"); 1569 if (strComments == null) 1570 { 1571 strComments = ""; 1572 } 1573 strImageURL = hsrqRequest.getParameter("BLOGENTRY_IMAGEURL"); 1574 if (strImageURL == null) 1575 { 1576 strImageURL = ""; 1577 } 1578 strTargetURL = hsrqRequest.getParameter("BLOGENTRY_TARGETURL"); 1579 if (strTargetURL == null) 1580 { 1581 strTargetURL = ""; 1582 } 1583 strTemp = hsrqRequest.getParameter("BLOGENTRY_CREATION_DATE"); 1586 if (strTemp == null) 1587 { 1588 creationDate = null; 1589 } 1590 else 1591 { 1592 creationDate = DateUtils.parseTimestamp(strTemp); 1593 } 1594 strTemp = hsrqRequest.getParameter("BLOGENTRY_MODIFICATION_DATE"); 1597 if (strTemp == null) 1598 { 1599 modificationDate = null; 1600 } 1601 else 1602 { 1603 modificationDate = DateUtils.parseTimestamp(strTemp); 1604 } 1605 1606 return new Entry(iEntryId, iDomainId, iBlogId, strCaption, strComments, 1607 strImageURL, strTargetURL, creationDate, 1608 modificationDate); 1609 } 1610 1611 1614 protected Principal verifyLogin( 1615 HttpSession hsSession, 1616 HttpServletRequest hsrqRequest, 1617 HttpServletResponse hsrpResponse 1618 ) throws ServletException , 1619 IOException 1620 { 1621 Principal currentUser = null; 1622 1623 if (getFormToProcess(hsrqRequest) == FORM_LOGIN_ID) 1624 { 1625 final String strLogin; 1627 String strPassword; 1628 1629 strLogin = hsrqRequest.getParameter("LOGIN"); 1630 strPassword = hsrqRequest.getParameter("PASSWORD"); 1631 1632 if ((m_strLogin.equals(strLogin)) 1635 && (m_strPassword.equals(strPassword))) 1636 { 1637 currentUser = new Principal () 1638 { 1639 public String getName() 1640 { 1641 return strLogin; 1642 } 1643 }; 1644 } 1645 else 1646 { 1647 CallContext.getInstance().getMessages().addErrorMessage( 1648 "The user name or password is not valid."); 1649 WebSessionUtils.resetSessionAndUserInfo(hsSession); 1652 } 1653 } 1654 else 1655 { 1656 if (WebSessionUtils.isLoggedIn(hsSession)) 1658 { 1659 currentUser = WebSessionUtils.getLoggedInUserInfo(hsSession); 1660 } 1661 } 1662 1663 return currentUser; 1664 } 1665 1666 1669 protected void redirectToLogin( 1670 HttpServletRequest hsrqRequest, 1671 HttpServletResponse hsrpResponse 1672 ) throws ServletException , 1673 IOException 1674 { 1675 BlogNavigator navigator = getNavigator(hsrqRequest); 1676 1677 if (navigator.isLoginPage()) 1678 { 1679 processNewLoginForm(hsrqRequest, hsrpResponse); 1681 } 1682 else 1683 { 1684 super.redirectToLogin(hsrqRequest, hsrpResponse); 1688 } 1689 } 1690} 1691 | Popular Tags |