0001: /*
0002: * Copyright (c) 2003 - 2007 OpenSubsystems s.r.o. Slovak Republic. All rights reserved.
0003: *
0004: * Project: OpenChronicle
0005: *
0006: * $Id: BlogEditServlet.java,v 1.3 2007/02/20 03:51:57 bastafidli Exp $
0007: *
0008: * This program is free software; you can redistribute it and/or modify
0009: * it under the terms of the GNU General Public License as published by
0010: * the Free Software Foundation; version 2 of the License.
0011: *
0012: * This program is distributed in the hope that it will be useful,
0013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0015: * GNU General Public License for more details.
0016: *
0017: * You should have received a copy of the GNU General Public License
0018: * along with this program; if not, write to the Free Software
0019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
0020: */
0021:
0022: package org.opensubsystems.blog.www;
0023:
0024: import java.io.IOException;
0025: import java.security.Principal;
0026: import java.sql.Timestamp;
0027: import java.util.logging.Level;
0028: import java.util.logging.Logger;
0029:
0030: import javax.servlet.ServletConfig;
0031: import javax.servlet.ServletException;
0032: import javax.servlet.http.HttpServletRequest;
0033: import javax.servlet.http.HttpServletResponse;
0034: import javax.servlet.http.HttpSession;
0035: import javax.transaction.UserTransaction;
0036:
0037: import org.opensubsystems.blog.data.Blog;
0038: import org.opensubsystems.blog.data.Entry;
0039: import org.opensubsystems.core.data.DataObject;
0040: import org.opensubsystems.core.error.OSSConcurentModifyException;
0041: import org.opensubsystems.core.error.OSSInvalidDataException;
0042: import org.opensubsystems.core.persist.db.DatabaseTransactionFactoryImpl;
0043: import org.opensubsystems.core.util.CallContext;
0044: import org.opensubsystems.core.util.DateUtils;
0045: import org.opensubsystems.core.util.GlobalConstants;
0046: import org.opensubsystems.core.util.Log;
0047: import org.opensubsystems.core.util.TransactionUtils;
0048: import org.opensubsystems.core.www.WebCommonConstants;
0049: import org.opensubsystems.core.www.WebSessionUtils;
0050: import org.opensubsystems.core.www.WebUtils;
0051:
0052: /**
0053: * Servlet responsible for browsing and editing of blogs and their entries.
0054: * This servlets ads functionality to the browser servlet to manipulate the data.
0055: *
0056: * @version $Id: BlogEditServlet.java,v 1.3 2007/02/20 03:51:57 bastafidli Exp $
0057: * @author Miro Halas
0058: * @code.reviewer Miro Halas
0059: * @code.reviewed 1.5 2006/07/21 05:44:29 bastafidli
0060: */
0061: public class BlogEditServlet extends BlogBrowserServlet {
0062: // Configuration settings ///////////////////////////////////////////////////
0063:
0064: /**
0065: * Name of the property for user name used to login.
0066: */
0067: public static final String BLOGEDIT_LOGIN = "blogedit.login.username";
0068:
0069: /**
0070: * Name of the property for password used to login.
0071: */
0072: public static final String BLOGEDIT_PASSWORD = "blogedit.login.password";
0073:
0074: /**
0075: * Name of the property for page to login.
0076: */
0077: public static final String BLOGEDIT_LOGIN_PAGE = "blogedit.login.page";
0078:
0079: /**
0080: * Name of the property for page to logout.
0081: */
0082: public static final String BLOGEDIT_LOGOUT_PAGE = "blogedit.logout.page";
0083:
0084: /**
0085: * Name of the property for page to create new blog.
0086: */
0087: public static final String BLOGEDIT_NEW_BLOG_PAGE = "blogedit.blog.new.page";
0088:
0089: /**
0090: * Name of the property for page to confirm deletion of blog.
0091: */
0092: public static final String BLOGEDIT_CONFIRMDELETE_BLOG_PAGE = "blogedit.blog.confirmdelete.page";
0093:
0094: /**
0095: * Name of the property for page to create new blog entry.
0096: */
0097: public static final String BLOGEDIT_NEWENTRY_BLOG_PAGE = "blogedit.entry.new.page";
0098:
0099: /**
0100: * Name of the property for page to confirm deletion of blog entry
0101: */
0102: public static final String BLOGEDIT_CONFIRMDELETE_BLOGENTRY_PAGE = "blogedit.entry.confirmdelete.page";
0103:
0104: // Constants ////////////////////////////////////////////////////////////////
0105:
0106: /**
0107: * Names of the form and action to display create blog dialog.
0108: */
0109: public static final String FORM_CREATE_BLOG_NAME = "FORM_CREATE_BLOG";
0110:
0111: /**
0112: * Names of the form and action to display edit blog dialog.
0113: */
0114: public static final String FORM_EDIT_BLOG_NAME = "FORM_EDIT_BLOG";
0115:
0116: /**
0117: * Names of the form and action to create blog.
0118: */
0119: public static final String FORM_EDIT_BLOG_NAME_CREATE = "create";
0120:
0121: /**
0122: * Names of the form and action to display delete blog dialog.
0123: */
0124: public static final String FORM_EDIT_BLOG_NAME_DELETE = "delete";
0125:
0126: /**
0127: * Names of the form and action to display create blog entry dialog.
0128: */
0129: public static final String FORM_EDIT_BLOG_NAME_CREATEENTRY = "createentry";
0130:
0131: /**
0132: * Names of the form and action to delete blog.
0133: */
0134: public static final String FORM_DELETE_BLOG_NAME = "FORM_DELETE_BLOG";
0135:
0136: /**
0137: * Names of the form and action to create blog entry.
0138: */
0139: public static final String FORM_CREATE_BLOGENTRY_NAME = "FORM_CREATE_BLOGENTRY";
0140:
0141: /**
0142: * Names of the form and action to display edit blog entry dialog.
0143: */
0144: public static final String FORM_EDIT_BLOGENTRY_NAME = "FORM_EDIT_BLOGENTRY";
0145:
0146: /**
0147: * Names of the form and action to display delete blog entry dialog.
0148: */
0149: public static final String FORM_EDIT_BLOGENTRY_NAME_DELETE = "delete";
0150:
0151: /**
0152: * Names of the form and action to delete blog entry.
0153: */
0154: public static final String FORM_DELETE_BLOGENTRY_NAME = "FORM_DELETE_BLOGENTRY";
0155:
0156: /**
0157: * Names of the form and action to login to the application.
0158: */
0159: public static final String FORM_LOGIN_NAME = "FORM_LOGIN";
0160:
0161: /**
0162: * Names of the form and action to logout from the application.
0163: */
0164: public static final String FORM_LOGOUT_NAME = "FORM_LOGOUT";
0165:
0166: /**
0167: * Constants for action to display new blog dialog.
0168: */
0169: public static final int FORM_NEW_BLOG_ID = FORM_COUNT_BLOGBROWSER + 1;
0170:
0171: /**
0172: * Constants for action to create new blog.
0173: */
0174: public static final int FORM_CREATE_BLOG_ID = FORM_COUNT_BLOGBROWSER + 2;
0175:
0176: /**
0177: * Constants for action to edit blog.
0178: */
0179: public static final int FORM_EDIT_BLOG_ID = FORM_COUNT_BLOGBROWSER + 3;
0180:
0181: /**
0182: * Constants for action to display delete blog dialog.
0183: */
0184: public static final int FORM_CONFIRMDELETE_BLOG_ID = FORM_COUNT_BLOGBROWSER + 4;
0185:
0186: /**
0187: * Constants for action to delete blog.
0188: */
0189: public static final int FORM_DELETE_BLOG_ID = FORM_COUNT_BLOGBROWSER + 5;
0190:
0191: /**
0192: * Constants for action to display create blog entry dialog.
0193: */
0194: public static final int FORM_NEWENTRY_BLOG_ID = FORM_COUNT_BLOGBROWSER + 6;
0195:
0196: /**
0197: * Constants for action to create blog entry dialog.
0198: */
0199: public static final int FORM_CREATE_BLOGENTRY_ID = FORM_COUNT_BLOGBROWSER + 7;
0200:
0201: /**
0202: * Constants for action to edit blog entry.
0203: */
0204: public static final int FORM_EDIT_BLOGENTRY_ID = FORM_COUNT_BLOGBROWSER + 8;
0205:
0206: /**
0207: * Constants for action to display delete blog entry dialog.
0208: */
0209: public static final int FORM_CONFIRMDELETE_BLOGENTRY_ID = FORM_COUNT_BLOGBROWSER + 9;
0210:
0211: /**
0212: * Constants for action to delete blog entry.
0213: */
0214: public static final int FORM_DELETE_BLOGENTRY_ID = FORM_COUNT_BLOGBROWSER + 10;
0215:
0216: /**
0217: * Constants for action to login to the application.
0218: */
0219: public static final int FORM_LOGIN_ID = FORM_COUNT_BLOGBROWSER + 11;
0220:
0221: /**
0222: * Constants for action to logout from the application.
0223: */
0224: public static final int FORM_LOGOUT_ID = FORM_COUNT_BLOGBROWSER + 12;
0225:
0226: /**
0227: * How many forms this servlet recognizes.
0228: */
0229: public static final int FORM_COUNT_BLOGEDIT = FORM_COUNT_BLOGBROWSER + 13;
0230:
0231: // Cached values ////////////////////////////////////////////////////////////
0232:
0233: /**
0234: * Logger for this class
0235: */
0236: private static Logger s_logger = Log
0237: .getInstance(BlogEditServlet.class);
0238:
0239: /**
0240: * Simple counter used to generate internal session ids for the clients.
0241: */
0242: private static int s_iSessionCounter = 0;
0243:
0244: /**
0245: * User name to use to login to the application.
0246: */
0247: protected String m_strLogin;
0248:
0249: /**
0250: * Password to use to login to the application.
0251: */
0252: protected String m_strPassword;
0253:
0254: // Attributes ///////////////////////////////////////////////////////////////
0255:
0256: /**
0257: * Generated serial version id for this class.
0258: */
0259: private static final long serialVersionUID = 7525262325709883457L;
0260:
0261: // Public methods ///////////////////////////////////////////////////////////
0262:
0263: /**
0264: * {@inheritDoc}
0265: */
0266: public void init(ServletConfig scConfig) throws ServletException {
0267: super .init(scConfig);
0268:
0269: // Allow empty user name and password if user desires so
0270: m_strLogin = WebUtils.readProperty(scConfig, BLOGEDIT_LOGIN,
0271: "", true);
0272: s_logger.config(BLOGEDIT_LOGIN + " = " + m_strLogin);
0273: m_strPassword = WebUtils.readProperty(scConfig,
0274: BLOGEDIT_PASSWORD, "", true);
0275: s_logger.config(BLOGEDIT_LOGIN + " = " + m_strPassword);
0276:
0277: // Load UI pages for this application
0278: // Page to login
0279: cacheUIPath(scConfig, BLOGEDIT_LOGIN_PAGE,
0280: "Path to page to login is not set in property "
0281: + BLOGEDIT_LOGIN_PAGE);
0282:
0283: // Page to logout
0284: cacheUIPath(scConfig, BLOGEDIT_LOGOUT_PAGE,
0285: "Path to page to logout is not set in property "
0286: + BLOGEDIT_LOGOUT_PAGE);
0287:
0288: // Page to create new blog
0289: cacheUIPath(scConfig, BLOGEDIT_NEW_BLOG_PAGE,
0290: "Path to page to create new blog is not set in property "
0291: + BLOGEDIT_NEW_BLOG_PAGE);
0292:
0293: // Page to confirm deletion of blog
0294: cacheUIPath(scConfig, BLOGEDIT_CONFIRMDELETE_BLOG_PAGE,
0295: "Path to page to confirm deletion of blog is not set in property "
0296: + BLOGEDIT_CONFIRMDELETE_BLOG_PAGE);
0297:
0298: // Page to create new blog entry
0299: cacheUIPath(scConfig, BLOGEDIT_NEWENTRY_BLOG_PAGE,
0300: "Path to page to create new blog entry is not set in property "
0301: + BLOGEDIT_NEWENTRY_BLOG_PAGE);
0302:
0303: // Page to confirm deletion of blog entry
0304: cacheUIPath(scConfig, BLOGEDIT_CONFIRMDELETE_BLOGENTRY_PAGE,
0305: "Path to page to confirm deletion of blog entry is not set in property "
0306: + BLOGEDIT_CONFIRMDELETE_BLOGENTRY_PAGE);
0307: }
0308:
0309: /**
0310: * {@inheritDoc}
0311: */
0312: public String getServletInfo() {
0313: return this .getClass().getName();
0314: }
0315:
0316: // Helper methods ///////////////////////////////////////////////////////////
0317:
0318: /**
0319: * {@inheritDoc}
0320: */
0321: protected void doGet(HttpServletRequest hsrqRequest,
0322: HttpServletResponse hsrpResponse) throws ServletException,
0323: IOException {
0324: BlogNavigator navigator = getNavigator(hsrqRequest);
0325:
0326: if (navigator.isLoginPage()) {
0327: processNewLoginForm(hsrqRequest, hsrpResponse);
0328: } else if (navigator.isLogoutPage()) {
0329: processNewLogoutForm(hsrqRequest, hsrpResponse);
0330: } else {
0331: // Now let the browser servlet process the request
0332: super .doGet(hsrqRequest, hsrpResponse);
0333: }
0334: }
0335:
0336: /**
0337: * {@inheritDoc}
0338: */
0339: protected void doPost(HttpServletRequest hsrqRequest,
0340: HttpServletResponse hsrpResponse) throws ServletException,
0341: IOException {
0342: switch (getFormToProcess(hsrqRequest)) {
0343: case FORM_LOGIN_ID: {
0344: processLoginForm(hsrqRequest, hsrpResponse);
0345: break;
0346: }
0347:
0348: case FORM_LOGOUT_ID: {
0349: processLogoutForm(hsrqRequest, hsrpResponse);
0350: break;
0351: }
0352:
0353: case FORM_NEW_BLOG_ID: {
0354: processNewBlogForm(hsrqRequest, hsrpResponse);
0355: break;
0356: }
0357:
0358: case FORM_CREATE_BLOG_ID: {
0359: processCreateBlogForm(hsrqRequest, hsrpResponse);
0360: break;
0361: }
0362:
0363: case FORM_EDIT_BLOG_ID: {
0364: processEditBlogForm(hsrqRequest, hsrpResponse);
0365: break;
0366: }
0367:
0368: case FORM_CONFIRMDELETE_BLOG_ID: {
0369: processConfirmDeleteBlogForm(hsrqRequest, hsrpResponse);
0370: break;
0371: }
0372:
0373: case FORM_DELETE_BLOG_ID: {
0374: processDeleteBlogForm(hsrqRequest, hsrpResponse);
0375: break;
0376: }
0377:
0378: case FORM_NEWENTRY_BLOG_ID: {
0379: processNewEntryBlogForm(hsrqRequest, hsrpResponse);
0380: break;
0381: }
0382:
0383: case FORM_CREATE_BLOGENTRY_ID: {
0384: processCreateEntryForm(hsrqRequest, hsrpResponse);
0385: break;
0386: }
0387:
0388: case FORM_EDIT_BLOGENTRY_ID: {
0389: processEditEntryForm(hsrqRequest, hsrpResponse);
0390: break;
0391: }
0392:
0393: case FORM_CONFIRMDELETE_BLOGENTRY_ID: {
0394: processConfirmDeleteEntryForm(hsrqRequest, hsrpResponse);
0395: break;
0396: }
0397:
0398: case FORM_DELETE_BLOGENTRY_ID: {
0399: processDeleteEntryForm(hsrqRequest, hsrpResponse);
0400: break;
0401: }
0402:
0403: default: {
0404: super .doPost(hsrqRequest, hsrpResponse);
0405: break;
0406: }
0407: }
0408: }
0409:
0410: // Helper methods ///////////////////////////////////////////////////////////
0411:
0412: /**
0413: * {@inheritDoc}
0414: */
0415: protected int getFormToProcess(HttpServletRequest hsrqRequest) {
0416: String strFormName;
0417: int iReturn = FORM_UNKNOWN_ID;
0418:
0419: strFormName = hsrqRequest.getParameter(FORM_NAME_REQUEST_PARAM);
0420:
0421: if (strFormName == null) {
0422: iReturn = super .getFormToProcess(hsrqRequest);
0423: } else if (strFormName.equals(FORM_LOGIN_NAME)) {
0424: // Login to the application
0425: iReturn = FORM_LOGIN_ID;
0426: } else if (strFormName.equals(FORM_LOGOUT_NAME)) {
0427: // Logout from the application
0428: iReturn = FORM_LOGOUT_ID;
0429: } else if (strFormName.equals(FORM_CREATE_BLOG_NAME)) {
0430: // Create new blog from already supplied details
0431: iReturn = FORM_CREATE_BLOG_ID;
0432: } else if (strFormName.equals(FORM_EDIT_BLOG_NAME)) {
0433: // Edit blog, it can mean multiple things (Create New, Save...)
0434: if (hsrqRequest.getParameter(FORM_EDIT_BLOG_NAME_CREATE) != null) {
0435: // Intention to create new blog, need to supply the details
0436: iReturn = FORM_NEW_BLOG_ID;
0437: } else if (hsrqRequest
0438: .getParameter(FORM_EDIT_BLOG_NAME_DELETE) != null) {
0439: // Intention to delete current blog, need to be confirmed
0440: iReturn = FORM_CONFIRMDELETE_BLOG_ID;
0441: } else if (hsrqRequest
0442: .getParameter(FORM_EDIT_BLOG_NAME_CREATEENTRY) != null) {
0443: // Intention to create new blog entry, need to supply the details
0444: iReturn = FORM_NEWENTRY_BLOG_ID;
0445: } else {
0446: // If nothing else is specified, then save supplied data
0447: iReturn = FORM_EDIT_BLOG_ID;
0448: }
0449: } else if (strFormName.equals(FORM_DELETE_BLOG_NAME)) {
0450: // Delete current blog
0451: iReturn = FORM_DELETE_BLOG_ID;
0452: } else if (strFormName.equals(FORM_CREATE_BLOGENTRY_NAME)) {
0453: // Create new blog entry
0454: iReturn = FORM_CREATE_BLOGENTRY_ID;
0455: } else if (strFormName.equals(FORM_EDIT_BLOGENTRY_NAME)) {
0456: // Edit blog entry, it can mean multiple things (Save, Delete...)
0457: // Create New is part of modifying blog, so it is above
0458: if (hsrqRequest
0459: .getParameter(FORM_EDIT_BLOGENTRY_NAME_DELETE) != null) {
0460: // Intention to delete current blog entry
0461: iReturn = FORM_CONFIRMDELETE_BLOGENTRY_ID;
0462: } else {
0463: // Save edited blog entry
0464: iReturn = FORM_EDIT_BLOGENTRY_ID;
0465: }
0466: } else if (strFormName.equals(FORM_DELETE_BLOGENTRY_NAME)) {
0467: // Delete current blog
0468: iReturn = FORM_DELETE_BLOGENTRY_ID;
0469: } else {
0470: iReturn = super .getFormToProcess(hsrqRequest);
0471: }
0472:
0473: return iReturn;
0474: }
0475:
0476: /**
0477: * Process form to display login dialog.
0478: *
0479: * @param hsrqRequest - the servlet request.
0480: * @param hsrpResponse - the servlet response.
0481: * @throws ServletException - an error while serving request
0482: * @throws IOException - an error while writing response
0483: */
0484: protected void processNewLoginForm(HttpServletRequest hsrqRequest,
0485: HttpServletResponse hsrpResponse) throws IOException,
0486: ServletException {
0487: s_logger.entering(this .getClass().getName(),
0488: "processNewLoginForm");
0489:
0490: try {
0491: hsrqRequest.setAttribute("blognavigator",
0492: getNavigator(hsrqRequest));
0493: displayUI(BLOGEDIT_LOGIN_PAGE, hsrqRequest, hsrpResponse);
0494: } catch (Exception eExc) {
0495: s_logger
0496: .log(
0497: Level.WARNING,
0498: "An error has occured while displaying login page.",
0499: eExc);
0500: messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc
0501: .getMessage(), getNavigator(hsrqRequest)
0502: .getRootURL(), eExc.getCause());
0503: } finally {
0504: s_logger.exiting(this .getClass().getName(),
0505: "processNewLoginForm");
0506: }
0507: }
0508:
0509: /**
0510: * Process form to login to the application.
0511: *
0512: * @param hsrqRequest - the servlet request.
0513: * @param hsrpResponse - the servlet response.
0514: * @throws ServletException - an error while serving request
0515: * @throws IOException - an error while writing response
0516: */
0517: protected void processLoginForm(HttpServletRequest hsrqRequest,
0518: HttpServletResponse hsrpResponse) throws IOException,
0519: ServletException {
0520: s_logger
0521: .entering(this .getClass().getName(), "processLoginForm");
0522:
0523: try {
0524: HttpSession hsSession;
0525: Principal currentUser;
0526: String strSessionId;
0527: String strURL;
0528:
0529: // We do not want to create new session if it doesn't exist since that
0530: // is the responsibility of the parent claees. We should have session
0531: // at this point
0532: hsSession = hsrqRequest.getSession(false);
0533: currentUser = CallContext.getInstance().getCurrentUser();
0534: if (GlobalConstants.ERROR_CHECKING) {
0535: assert hsSession != null : "Session must exist at this point";
0536: }
0537:
0538: if (currentUser != null) {
0539: // If users tries to login again using the same session (by directly
0540: // typing login URL) while it is already logged in,
0541: // the session tracking object in the application server wouldn't be
0542: // destroyed, just for that case go throught the session object and
0543: // remove all values in it, this will cause logout if it was previously
0544: // logged in
0545: WebSessionUtils.resetSessionAndUserInfo(hsSession);
0546:
0547: // This needs to by synchronized so that we generate the unique id for
0548: // each session
0549: synchronized (FORM_LOGIN_NAME) {
0550: strSessionId = Integer
0551: .toString(++s_iSessionCounter);
0552: }
0553:
0554: WebSessionUtils.setSessionAndUserInfo(hsSession,
0555: strSessionId, currentUser);
0556: // Check if there is any URL specified in the session object
0557: strURL = getLoginRedirect(hsSession, hsrqRequest);
0558: // We are doing the redirect so reset the stored value
0559: resetLoginRedirect(hsSession);
0560: if ((strURL == null) || (strURL.length() == 0)) {
0561: strURL = getNavigator(hsrqRequest).getRootURL();
0562: }
0563: hsrpResponse.sendRedirect(strURL);
0564: } else {
0565: // The login was incorrect so display the login page again
0566: hsrqRequest.setAttribute("blognavigator",
0567: getNavigator(hsrqRequest));
0568: displayUI(BLOGEDIT_LOGIN_PAGE, hsrqRequest,
0569: hsrpResponse);
0570: }
0571: } catch (Exception eExc) {
0572: s_logger
0573: .log(
0574: Level.WARNING,
0575: "An error has occured while processing login page.",
0576: eExc);
0577: messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc
0578: .getMessage(), getNavigator(hsrqRequest)
0579: .getRootURL(), eExc.getCause());
0580: } finally {
0581: s_logger.exiting(this .getClass().getName(),
0582: "processLoginForm");
0583: }
0584: }
0585:
0586: /**
0587: * Process form to display logout dialog.
0588: *
0589: * @param hsrqRequest - the servlet request.
0590: * @param hsrpResponse - the servlet response.
0591: * @throws ServletException - an error while serving request
0592: * @throws IOException - an error while writing response
0593: */
0594: protected void processNewLogoutForm(HttpServletRequest hsrqRequest,
0595: HttpServletResponse hsrpResponse) throws IOException,
0596: ServletException {
0597: s_logger.entering(this .getClass().getName(),
0598: "processNewLogoutForm");
0599:
0600: try {
0601: hsrqRequest.setAttribute("blognavigator",
0602: getNavigator(hsrqRequest));
0603: displayUI(BLOGEDIT_LOGOUT_PAGE, hsrqRequest, hsrpResponse);
0604: } catch (Exception eExc) {
0605: s_logger
0606: .log(
0607: Level.WARNING,
0608: "An error has occured while displaying logout page.",
0609: eExc);
0610: messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc
0611: .getMessage(), getNavigator(hsrqRequest)
0612: .getRootURL(), eExc.getCause());
0613: } finally {
0614: s_logger.exiting(this .getClass().getName(),
0615: "processNewLogoutForm");
0616: }
0617: }
0618:
0619: /**
0620: * Process form to logout from the application.
0621: *
0622: * @param hsrqRequest - the servlet request.
0623: * @param hsrpResponse - the servlet response.
0624: * @throws ServletException - an error while serving request
0625: * @throws IOException - an error while writing response
0626: */
0627: protected void processLogoutForm(HttpServletRequest hsrqRequest,
0628: HttpServletResponse hsrpResponse) throws IOException,
0629: ServletException {
0630: s_logger.entering(this .getClass().getName(),
0631: "processLogoutForm");
0632:
0633: try {
0634: HttpSession hsSession;
0635:
0636: // We do not want to create new session if it doesn't exist since that
0637: // is the responsibility of the parent claees. We should have session
0638: // at this point
0639: hsSession = hsrqRequest.getSession(false);
0640: if (GlobalConstants.ERROR_CHECKING) {
0641: assert hsSession != null : "Session must exist at this point";
0642: }
0643:
0644: WebSessionUtils.resetSessionAndUserInfo(hsSession);
0645:
0646: // Redirect user to the page displaying all blogs
0647: hsrpResponse.sendRedirect(getNavigator(hsrqRequest)
0648: .getRootURL());
0649: } catch (Exception eExc) {
0650: s_logger
0651: .log(
0652: Level.WARNING,
0653: "An error has occured while processing logout page.",
0654: eExc);
0655: messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc
0656: .getMessage(), getNavigator(hsrqRequest)
0657: .getRootURL(), eExc.getCause());
0658: } finally {
0659: s_logger.exiting(this .getClass().getName(),
0660: "processLogoutForm");
0661: }
0662: }
0663:
0664: /**
0665: * Process form to display dialog to create new blog.
0666: * It supplies template object with default information for new blog.
0667: *
0668: * @param hsrqRequest - the servlet request.
0669: * @param hsrpResponse - the servlet response.
0670: * @throws ServletException - an error while serving request
0671: * @throws IOException - an error while writing response
0672: */
0673: protected void processNewBlogForm(HttpServletRequest hsrqRequest,
0674: HttpServletResponse hsrpResponse) throws IOException,
0675: ServletException {
0676: s_logger.entering(this .getClass().getName(),
0677: "processNewBlogForm");
0678:
0679: try {
0680: Blog blog;
0681:
0682: // Create template object which will be used to will the form with
0683: // default values
0684: blog = (Blog) getController().get(DataObject.NEW_ID);
0685:
0686: hsrqRequest.setAttribute("blog", blog);
0687: hsrqRequest.setAttribute("blognavigator",
0688: getNavigator(hsrqRequest));
0689: displayUI(BLOGEDIT_NEW_BLOG_PAGE, hsrqRequest, hsrpResponse);
0690: } catch (Exception eExc) {
0691: s_logger.log(Level.WARNING,
0692: "An error has occured while retrieving information about"
0693: + " new blog.", eExc);
0694: messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc
0695: .getMessage(), getNavigator(hsrqRequest)
0696: .getRootURL(), eExc.getCause());
0697: } finally {
0698: s_logger.exiting(this .getClass().getName(),
0699: "processNewBlogForm");
0700: }
0701: }
0702:
0703: /**
0704: * Process form to actually create new blog from supplied information.
0705: *
0706: * @param hsrqRequest - the servlet request.
0707: * @param hsrpResponse - the servlet response.
0708: * @throws ServletException - an error while serving request
0709: * @throws IOException - an error while writing response
0710: */
0711: protected void processCreateBlogForm(
0712: HttpServletRequest hsrqRequest,
0713: HttpServletResponse hsrpResponse) throws IOException,
0714: ServletException {
0715: s_logger.entering(this .getClass().getName(),
0716: "processCreateBlogForm");
0717:
0718: UserTransaction transaction = null;
0719: Blog blog = null;
0720: try {
0721: transaction = DatabaseTransactionFactoryImpl.getInstance()
0722: .requestTransaction();
0723:
0724: blog = parseBlogFromRequest(hsrqRequest);
0725: Blog blogUpdated = null;
0726:
0727: // Since one request corresponds to user transaction and we know we are
0728: // going to modify data, start database transaction to group any
0729: // databast modification and queries we may need to execute to satisfy
0730: // this action.
0731: transaction.begin();
0732: blogUpdated = (Blog) getController().create(blog);
0733: transaction.commit();
0734:
0735: // Redirect user to the page displaying newly created blog
0736: hsrpResponse.sendRedirect(getNavigator(hsrqRequest).getURL(
0737: blogUpdated));
0738: } catch (OSSInvalidDataException ideExc) {
0739: TransactionUtils.rollback(transaction);
0740: CallContext.getInstance().getMessages().addMessages(
0741: ideExc.getErrorMessages());
0742: // Return the submitted value with the modified entries
0743: hsrqRequest.setAttribute("blog", blog);
0744: hsrqRequest.setAttribute("blognavigator",
0745: getNavigator(hsrqRequest));
0746: displayUI(BLOGEDIT_NEW_BLOG_PAGE, hsrqRequest, hsrpResponse);
0747: } catch (Throwable thr) {
0748: TransactionUtils.rollback(transaction);
0749: s_logger.log(Level.WARNING,
0750: "An error has occured while creating blog.", thr);
0751: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
0752: .getMessage(), getNavigator(hsrqRequest)
0753: .getRootURL(), thr.getCause());
0754: } finally {
0755: s_logger.exiting(this .getClass().getName(),
0756: "processCreateBlogForm");
0757: }
0758: }
0759:
0760: /**
0761: * Process form to save modified information about blog.
0762: *
0763: * @param hsrqRequest - the servlet request.
0764: * @param hsrpResponse - the servlet response.
0765: * @throws ServletException - an error while serving request
0766: * @throws IOException - an error while writing response
0767: */
0768: protected void processEditBlogForm(HttpServletRequest hsrqRequest,
0769: HttpServletResponse hsrpResponse) throws IOException,
0770: ServletException {
0771: s_logger.entering(this .getClass().getName(),
0772: "processEditBlogForm");
0773:
0774: UserTransaction transaction = null;
0775: Blog blog = null;
0776:
0777: blog = parseBlogFromRequest(hsrqRequest);
0778: try {
0779: transaction = DatabaseTransactionFactoryImpl.getInstance()
0780: .requestTransaction();
0781:
0782: Blog blogUpdated = null;
0783:
0784: // Since one request corresponds to user transaction and we know we are
0785: // going to modify data, start database transaction to group any
0786: // databast modification and queries we may need to execute to satisfy
0787: // this action.
0788: transaction.begin();
0789: blogUpdated = (Blog) getController().save(blog);
0790: assert blogUpdated != null : "Save should return not null value.";
0791: transaction.commit();
0792:
0793: // Redirect user to the page displaying list of blogs
0794: hsrpResponse.sendRedirect(getNavigator(hsrqRequest)
0795: .getRootURL());
0796: } catch (OSSInvalidDataException ideExc) {
0797: TransactionUtils.rollback(transaction);
0798: CallContext.getInstance().getMessages().addMessages(
0799: ideExc.getErrorMessages());
0800: // Return the submitted value with the modified entries
0801: processEditBlogError(hsrqRequest, hsrpResponse, blog);
0802: } catch (OSSConcurentModifyException ocmeExc) {
0803: TransactionUtils.rollback(transaction);
0804: CallContext.getInstance().getMessages().addErrorMessage(
0805: ocmeExc.getMessage());
0806: // Return the submitted value with the modified entries
0807: processEditBlogError(hsrqRequest, hsrpResponse, blog);
0808: } catch (Throwable thr) {
0809: TransactionUtils.rollback(transaction);
0810: s_logger.log(Level.WARNING,
0811: "An error has occured while saving blog.", thr);
0812: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
0813: .getMessage(), getNavigator(hsrqRequest)
0814: .getRootURL(), thr.getCause());
0815: } finally {
0816: s_logger.exiting(this .getClass().getName(),
0817: "processEditBlogForm");
0818: }
0819: }
0820:
0821: /**
0822: * Process recoverable error, which has occured while saving blog. This will
0823: * show again the edit blog page but with the modified data and explanative
0824: * error message.
0825: *
0826: * @param hsrqRequest - the servlet request.
0827: * @param hsrpResponse - the servlet response.
0828: * @param blog - blog with modified data submitted to the server
0829: * @throws ServletException - an error while serving request
0830: * @throws IOException - an error while writing response
0831: */
0832: protected void processEditBlogError(HttpServletRequest hsrqRequest,
0833: HttpServletResponse hsrpResponse, Blog blog)
0834: throws IOException, ServletException {
0835: int iPageNumber;
0836: BlogNavigator navigator;
0837:
0838: navigator = getNavigator(hsrqRequest);
0839: iPageNumber = navigator.isIndexPage();
0840: if (iPageNumber <= 0) {
0841: // If we cannot extract correct page number then just take the first
0842: // page
0843: iPageNumber = 1;
0844: }
0845: try {
0846: Object[] arObjects;
0847:
0848: arObjects = getController().getEntries(blog.getId(),
0849: iPageNumber);
0850:
0851: // Return the submitted value with the modified entries
0852: hsrqRequest.setAttribute("blog", blog);
0853: if (arObjects[0] != null) {
0854: hsrqRequest.setAttribute("blogentries", arObjects[0]);
0855: }
0856: if (arObjects[1] != null) {
0857: hsrqRequest.setAttribute("listoptions", arObjects[1]);
0858: }
0859: hsrqRequest.setAttribute("blognavigator", navigator);
0860: displayUI(BLOGBROWSER_BLOG_VIEWER_PAGE, hsrqRequest,
0861: hsrpResponse);
0862: } catch (Throwable thr) {
0863: s_logger
0864: .log(
0865: Level.WARNING,
0866: "An error has occured while retrieving blog entries.",
0867: thr);
0868: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
0869: .getMessage(), getNavigator(hsrqRequest)
0870: .getRootURL(), thr.getCause());
0871: }
0872: }
0873:
0874: /**
0875: * Process form to display dialog to confirm deletion of a blog.
0876: *
0877: * @param hsrqRequest - the servlet request.
0878: * @param hsrpResponse - the servlet response.
0879: * @throws ServletException - an error while serving request
0880: * @throws IOException - an error while writing response
0881: */
0882: protected void processConfirmDeleteBlogForm(
0883: HttpServletRequest hsrqRequest,
0884: HttpServletResponse hsrpResponse) throws IOException,
0885: ServletException {
0886: s_logger.entering(this .getClass().getName(),
0887: "processConfirmDeleteBlogForm");
0888:
0889: try {
0890: Object objBlogIdentification;
0891: Blog blog;
0892:
0893: // Get the data for blog which user wants to delete
0894: objBlogIdentification = getNavigator(hsrqRequest)
0895: .getBlogIdentification(hsrqRequest);
0896: if (objBlogIdentification instanceof Integer) {
0897: blog = (Blog) getController().get(
0898: ((Integer) objBlogIdentification).intValue());
0899: } else {
0900: blog = getController().get(
0901: objBlogIdentification.toString());
0902: }
0903:
0904: if (blog != null) {
0905: hsrqRequest.setAttribute("blog", blog);
0906: }
0907: hsrqRequest.setAttribute("blognavigator",
0908: getNavigator(hsrqRequest));
0909: displayUI(BLOGEDIT_CONFIRMDELETE_BLOG_PAGE, hsrqRequest,
0910: hsrpResponse);
0911: } catch (Throwable thr) {
0912: s_logger.log(Level.WARNING,
0913: "An error has occured while retrieving information"
0914: + " about blog to delete.", thr);
0915: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
0916: .getMessage(), getNavigator(hsrqRequest)
0917: .getRootURL(), thr.getCause());
0918: } finally {
0919: s_logger.exiting(this .getClass().getName(),
0920: "processConfirmDeleteBlogForm");
0921: }
0922: }
0923:
0924: /**
0925: * Process form to delete specified blog.
0926: *
0927: * @param hsrqRequest - the servlet request.
0928: * @param hsrpResponse - the servlet response.
0929: * @throws ServletException - an error while serving request
0930: * @throws IOException - an error while writing response
0931: */
0932: protected void processDeleteBlogForm(
0933: HttpServletRequest hsrqRequest,
0934: HttpServletResponse hsrpResponse) throws IOException,
0935: ServletException {
0936: s_logger.entering(this .getClass().getName(),
0937: "processDeleteBlogForm");
0938:
0939: UserTransaction transaction = null;
0940: try {
0941: transaction = DatabaseTransactionFactoryImpl.getInstance()
0942: .requestTransaction();
0943:
0944: int iId = parseBlogFromRequest(hsrqRequest).getId();
0945:
0946: // Since one request corresponds to user transaction and we know we are
0947: // going to modify data, start database transaction to group any
0948: // databast modification and queries we may need to execute to satisfy
0949: // this action.
0950: transaction.begin();
0951: getController().delete(iId);
0952: transaction.commit();
0953:
0954: // Redirect user to the list of blogs
0955: hsrpResponse.sendRedirect(getNavigator(hsrqRequest)
0956: .getRootURL());
0957: } catch (Throwable thr) {
0958: TransactionUtils.rollback(transaction);
0959: s_logger.log(Level.WARNING,
0960: "An error has occured while deleting blog.", thr);
0961: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
0962: .getMessage(), getNavigator(hsrqRequest)
0963: .getRootURL(), thr.getCause());
0964: } finally {
0965: s_logger.exiting(this .getClass().getName(),
0966: "processDeleteBlogForm");
0967: }
0968: }
0969:
0970: /**
0971: * Process form to display dialog to create new blog entry.
0972: *
0973: * @param hsrqRequest - the servlet request.
0974: * @param hsrpResponse - the servlet response.
0975: * @throws ServletException - an error while serving request
0976: * @throws IOException - an error while writing response
0977: */
0978: protected void processNewEntryBlogForm(
0979: HttpServletRequest hsrqRequest,
0980: HttpServletResponse hsrpResponse) throws IOException,
0981: ServletException {
0982: s_logger.entering(this .getClass().getName(),
0983: "processNewEntryBlogForm");
0984:
0985: try {
0986: Object[] arObjects = null;
0987:
0988: // Get the data for blog where user wants to create new entry
0989: arObjects = getController().getWithEntry(
0990: parseBlogFromRequest(hsrqRequest).getId(),
0991: DataObject.NEW_ID);
0992:
0993: if (arObjects != null) {
0994: if (arObjects[0] != null) {
0995: hsrqRequest.setAttribute("blog", arObjects[0]);
0996: }
0997: if (arObjects[1] != null) {
0998: hsrqRequest.setAttribute("blogentry", arObjects[1]);
0999: }
1000: }
1001:
1002: hsrqRequest.setAttribute("blognavigator",
1003: getNavigator(hsrqRequest));
1004: displayUI(BLOGEDIT_NEWENTRY_BLOG_PAGE, hsrqRequest,
1005: hsrpResponse);
1006: } catch (Throwable thr) {
1007: s_logger.log(Level.WARNING,
1008: "An error has occured while retrieving information"
1009: + " about new blog entry.", thr);
1010: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
1011: .getMessage(), getNavigator(hsrqRequest)
1012: .getRootURL(), thr.getCause());
1013: } finally {
1014: s_logger.exiting(this .getClass().getName(),
1015: "processNewEntryBlogForm");
1016: }
1017: }
1018:
1019: /**
1020: * Process form to actually create new blog entry.
1021: *
1022: * @param hsrqRequest - the servlet request.
1023: * @param hsrpResponse - the servlet response.
1024: * @throws ServletException - an error while serving request
1025: * @throws IOException - an error while writing response
1026: */
1027: protected void processCreateEntryForm(
1028: HttpServletRequest hsrqRequest,
1029: HttpServletResponse hsrpResponse) throws IOException,
1030: ServletException {
1031: s_logger.entering(this .getClass().getName(),
1032: "processCreateEntryForm");
1033:
1034: UserTransaction transaction = null;
1035: try {
1036: transaction = DatabaseTransactionFactoryImpl.getInstance()
1037: .requestTransaction();
1038:
1039: Object objBlogIdentification;
1040: Entry entry = parseEntryFromRequest(hsrqRequest);
1041: Entry entryUpdated = null;
1042:
1043: // Get the data for blog where user wants to create new entry
1044: objBlogIdentification = getNavigator(hsrqRequest)
1045: .getBlogIdentification(hsrqRequest);
1046:
1047: // Since one request corresponds to user transaction and we know we are
1048: // going to modify data, start database transaction to group any
1049: // databast modification and queries we may need to execute to satisfy
1050: // this action.
1051: transaction.begin();
1052: entryUpdated = (Entry) getController().create(entry);
1053: assert entryUpdated != null : "Create should return not null value.";
1054: transaction.commit();
1055:
1056: // Redirect user to the page displaying the blog
1057: hsrpResponse.sendRedirect(getNavigator(hsrqRequest)
1058: .getBlogURL(objBlogIdentification));
1059: } catch (Throwable thr) {
1060: TransactionUtils.rollback(transaction);
1061: s_logger
1062: .log(
1063: Level.WARNING,
1064: "An error has occured while creating new blog entry.",
1065: thr);
1066: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
1067: .getMessage(),
1068: getNavigator(hsrqRequest)
1069: .getBlogURL(
1070: getNavigator(hsrqRequest)
1071: .getBlogIdentification(
1072: hsrqRequest)), thr
1073: .getCause());
1074: } finally {
1075: s_logger.exiting(this .getClass().getName(),
1076: "processCreateEntryForm");
1077: }
1078: }
1079:
1080: /**
1081: * Process form to save modified blog entry.
1082: *
1083: * @param hsrqRequest - the servlet request.
1084: * @param hsrpResponse - the servlet response.
1085: * @throws ServletException - an error while serving request
1086: * @throws IOException - an error while writing response
1087: */
1088: protected void processEditEntryForm(HttpServletRequest hsrqRequest,
1089: HttpServletResponse hsrpResponse) throws IOException,
1090: ServletException {
1091: s_logger.entering(this .getClass().getName(),
1092: "processEditEntryForm");
1093:
1094: UserTransaction transaction = null;
1095: Entry entry = null;
1096: try {
1097: transaction = DatabaseTransactionFactoryImpl.getInstance()
1098: .requestTransaction();
1099:
1100: Object objBlogIdentification;
1101: Entry entryUpdated = null;
1102:
1103: entry = parseEntryFromRequest(hsrqRequest);
1104: // Get the data for blog where user wants to edit entry
1105: objBlogIdentification = getNavigator(hsrqRequest)
1106: .getBlogIdentification(hsrqRequest);
1107:
1108: // Since one request corresponds to user transaction and we know we are
1109: // going to modify data, start database transaction to group any
1110: // databast modification and queries we may need to execute to satisfy
1111: // this action.
1112: transaction.begin();
1113: entryUpdated = (Entry) getController().save(entry);
1114: assert entryUpdated != null : "Save should return not null value.";
1115: transaction.commit();
1116:
1117: // Redirect user to the page displaying the blog
1118: hsrpResponse.sendRedirect(getNavigator(hsrqRequest)
1119: .getBlogURL(objBlogIdentification));
1120: } catch (OSSInvalidDataException ideExc) {
1121: TransactionUtils.rollback(transaction);
1122: CallContext.getInstance().getMessages().addMessages(
1123: ideExc.getErrorMessages());
1124: // Return the submitted value
1125: processEditEntryError(hsrqRequest, hsrpResponse, entry);
1126: } catch (OSSConcurentModifyException ocmeExc) {
1127: TransactionUtils.rollback(transaction);
1128: CallContext.getInstance().getMessages().addErrorMessage(
1129: ocmeExc.getMessage());
1130: // Return the submitted value
1131: processEditEntryError(hsrqRequest, hsrpResponse, entry);
1132: } catch (Throwable thr) {
1133: TransactionUtils.rollback(transaction);
1134: s_logger.log(Level.WARNING,
1135: "An error has occured while saving blog entry.",
1136: thr);
1137: try {
1138: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
1139: .getMessage(), getNavigator(hsrqRequest)
1140: .getURL(
1141: getNavigator(hsrqRequest)
1142: .getBlogEntryIdentification(
1143: hsrqRequest)), thr
1144: .getCause());
1145: } catch (Exception eExc2) {
1146: s_logger
1147: .log(
1148: Level.SEVERE,
1149: "An error has occured while generating error message.",
1150: thr);
1151: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
1152: .getMessage(),
1153: WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE,
1154: eExc2.getCause());
1155: }
1156: } finally {
1157: s_logger.exiting(this .getClass().getName(),
1158: "processEditEntryForm");
1159: }
1160: }
1161:
1162: /**
1163: * Process recoverable error, which has occured while saving entry. This will
1164: * show again the edit entry page but with the modified data and explanative
1165: * error message.
1166: *
1167: * @param hsrqRequest - the servlet request.
1168: * @param hsrpResponse - the servlet response.
1169: * @param entry - entry with modified data submitted to the server
1170: * @throws ServletException - an error while serving request
1171: * @throws IOException - an error while writing response
1172: */
1173: protected void processEditEntryError(
1174: HttpServletRequest hsrqRequest,
1175: HttpServletResponse hsrpResponse, Entry entry)
1176: throws IOException, ServletException {
1177: try {
1178: Blog blog = (Blog) getController().get(entry.getParentId());
1179:
1180: if (blog != null) {
1181: hsrqRequest.setAttribute("blog", blog);
1182: hsrqRequest.setAttribute("blogentry", entry);
1183: hsrqRequest.setAttribute("blognavigator",
1184: getNavigator(hsrqRequest));
1185: displayUI(BLOGBROWSER_BLOGENTRY_VIEWER_PAGE,
1186: hsrqRequest, hsrpResponse);
1187: } else {
1188: // We have not found either the blog or the entry so tell the user
1189: // about it
1190: hsrpResponse
1191: .sendError(HttpServletResponse.SC_NOT_FOUND);
1192: }
1193: } catch (Exception eExc) {
1194: s_logger
1195: .log(
1196: Level.WARNING,
1197: "An error has occured while retrieving blog entry.",
1198: eExc);
1199: messageBoxPage(hsrqRequest, hsrpResponse, "Error", eExc
1200: .getMessage(), getNavigator(hsrqRequest)
1201: .getRootURL(), eExc.getCause());
1202: }
1203: }
1204:
1205: /**
1206: * Process form to display dialog to confirm deletion of a blog entry.
1207: *
1208: * @param hsrqRequest - the servlet request.
1209: * @param hsrpResponse - the servlet response.
1210: * @throws ServletException - an error while serving request
1211: * @throws IOException - an error while writing response
1212: */
1213: protected void processConfirmDeleteEntryForm(
1214: HttpServletRequest hsrqRequest,
1215: HttpServletResponse hsrpResponse) throws IOException,
1216: ServletException {
1217: s_logger.entering(this .getClass().getName(),
1218: "processConfirmDeleteEntryForm");
1219:
1220: try {
1221: BlogEntryIdentification entryIdentification;
1222: Object[] arObjects = null;
1223:
1224: // Get the information about blog and entry to delete
1225: entryIdentification = getNavigator(hsrqRequest)
1226: .getBlogEntryIdentification(hsrqRequest);
1227: if (entryIdentification != null) {
1228: Object objBlogIdentification;
1229:
1230: objBlogIdentification = entryIdentification
1231: .getBlogIdentification();
1232: if (objBlogIdentification instanceof Integer) {
1233: arObjects = getController().getWithEntry(
1234: ((Integer) objBlogIdentification)
1235: .intValue(),
1236: entryIdentification
1237: .getBlogEntryIdentification());
1238: } else {
1239: arObjects = getController().getWithEntry(
1240: objBlogIdentification.toString(),
1241: entryIdentification
1242: .getBlogEntryIdentification());
1243: }
1244: }
1245:
1246: if (arObjects != null) {
1247: if (arObjects[0] != null) {
1248: hsrqRequest.setAttribute("blog", arObjects[0]);
1249: }
1250: if (arObjects[1] != null) {
1251: hsrqRequest.setAttribute("blogentry", arObjects[1]);
1252: }
1253: }
1254:
1255: hsrqRequest.setAttribute("blognavigator",
1256: getNavigator(hsrqRequest));
1257: displayUI(BLOGEDIT_CONFIRMDELETE_BLOGENTRY_PAGE,
1258: hsrqRequest, hsrpResponse);
1259: } catch (Throwable thr) {
1260: s_logger.log(Level.WARNING,
1261: "An error has occured while retrieving information"
1262: + " about blog entry to delete.", thr);
1263: try {
1264: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
1265: .getMessage(), getNavigator(hsrqRequest)
1266: .getURL(
1267: getNavigator(hsrqRequest)
1268: .getBlogEntryIdentification(
1269: hsrqRequest)), thr
1270: .getCause());
1271: } catch (Exception eExc2) {
1272: s_logger
1273: .log(
1274: Level.SEVERE,
1275: "An error has occured while generating error message.",
1276: thr);
1277: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
1278: .getMessage(),
1279: WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE,
1280: eExc2.getCause());
1281: }
1282: } finally {
1283: s_logger.exiting(this .getClass().getName(),
1284: "processConfirmDeleteEntryForm");
1285: }
1286: }
1287:
1288: /**
1289: * Process form to delete specified blog entry.
1290: *
1291: * @param hsrqRequest - the servlet request.
1292: * @param hsrpResponse - the servlet response.
1293: * @throws ServletException - an error while serving request
1294: * @throws IOException - an error while writing response
1295: */
1296: protected void processDeleteEntryForm(
1297: HttpServletRequest hsrqRequest,
1298: HttpServletResponse hsrpResponse) throws IOException,
1299: ServletException {
1300: s_logger.entering(this .getClass().getName(),
1301: "processDeleteEntryForm");
1302:
1303: UserTransaction transaction = null;
1304: try {
1305: transaction = DatabaseTransactionFactoryImpl.getInstance()
1306: .requestTransaction();
1307:
1308: Object objBlogIdentification;
1309: Entry entry = parseEntryFromRequest(hsrqRequest);
1310:
1311: // Get the data for blog where user wants to delete new entry
1312: objBlogIdentification = getNavigator(hsrqRequest)
1313: .getBlogIdentification(hsrqRequest);
1314:
1315: // Since one request corresponds to user transaction and we know we are
1316: // going to modify data, start database transaction to group any
1317: // databast modification and queries we may need to execute to satisfy
1318: // this action.
1319: transaction.begin();
1320: getController().deleteEntry(entry.getId());
1321: transaction.commit();
1322:
1323: // Redirect user to the page displaying blog which contained now deleted entry
1324: hsrpResponse.sendRedirect(getNavigator(hsrqRequest)
1325: .getBlogURL(objBlogIdentification));
1326: } catch (Throwable thr) {
1327: TransactionUtils.rollback(transaction);
1328: s_logger.log(Level.WARNING,
1329: "An error has occured while deleting blog entry.",
1330: thr);
1331: try {
1332: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
1333: .getMessage(), getNavigator(hsrqRequest)
1334: .getURL(
1335: getNavigator(hsrqRequest)
1336: .getBlogEntryIdentification(
1337: hsrqRequest)), thr
1338: .getCause());
1339: } catch (Exception eExc2) {
1340: s_logger
1341: .log(
1342: Level.SEVERE,
1343: "An error has occured while generating error message.",
1344: thr);
1345: messageBoxPage(hsrqRequest, hsrpResponse, "Error", thr
1346: .getMessage(),
1347: WebCommonConstants.DEFAULT_DIRECTORY_WEB_PAGE,
1348: eExc2.getCause());
1349: }
1350: } finally {
1351: s_logger.exiting(this .getClass().getName(),
1352: "processDeleteEntryForm");
1353: }
1354: }
1355:
1356: /**
1357: * Create new instance of blog from a HTTP request.
1358: *
1359: * @param hsrqRequest - HTTP request from HTML form
1360: * @return Blog
1361: */
1362: protected Blog parseBlogFromRequest(HttpServletRequest hsrqRequest) {
1363: String strTemp;
1364: int iBlogId;
1365: int iDomainId = DataObject.NEW_ID;
1366: String strFolder;
1367: String strCaption;
1368: String strComments;
1369: Timestamp creationDate;
1370: Timestamp modificationDate;
1371:
1372: strTemp = hsrqRequest.getParameter("BLOG_ID");
1373: if (strTemp == null) {
1374: throw new IllegalArgumentException(
1375: "Blog id is not specified.");
1376: }
1377: iBlogId = Integer.parseInt(strTemp);
1378:
1379: iDomainId = CallContext.getInstance().getCurrentDomainId();
1380:
1381: strFolder = hsrqRequest.getParameter("BLOG_FOLDER");
1382: if (strFolder == null) {
1383: strFolder = "";
1384: }
1385: strCaption = hsrqRequest.getParameter("BLOG_CAPTION");
1386: if (strCaption == null) {
1387: strCaption = "";
1388: }
1389: strComments = hsrqRequest.getParameter("BLOG_COMMENTS");
1390: if (strComments == null) {
1391: strComments = "";
1392: }
1393: // User can never change creation date, therefore we require to store
1394: // it as the millisecond value since that is independent from the display format
1395: strTemp = hsrqRequest.getParameter("BLOG_CREATION_DATE");
1396: if (strTemp == null) {
1397: creationDate = null;
1398: } else {
1399: creationDate = DateUtils.parseTimestamp(strTemp);
1400: }
1401: // User can never change modification date, therefore we require to store
1402: // it as the millisecond value since that is independent from the display format
1403: strTemp = hsrqRequest.getParameter("BLOG_MODIFICATION_DATE");
1404: if (strTemp == null) {
1405: modificationDate = null;
1406: } else {
1407: modificationDate = DateUtils.parseTimestamp(strTemp);
1408: }
1409:
1410: return new Blog(iBlogId, iDomainId, strFolder, strCaption,
1411: strComments, creationDate, modificationDate);
1412: }
1413:
1414: /**
1415: * Create new instance of blog entry from a HTTP request..
1416: *
1417: * @param hsrqRequest - HTTP request from HTML form
1418: * @return Entry
1419: */
1420: protected Entry parseEntryFromRequest(HttpServletRequest hsrqRequest) {
1421: String strTemp;
1422: int iEntryId;
1423: int iDomainId = DataObject.NEW_ID;
1424: int iBlogId;
1425: String strCaption;
1426: String strComments;
1427: String strImageURL;
1428: String strTargetURL;
1429: Timestamp creationDate;
1430: Timestamp modificationDate;
1431:
1432: strTemp = hsrqRequest.getParameter("BLOGENTRY_ID");
1433: if (strTemp == null) {
1434: throw new IllegalArgumentException(
1435: "Entry id is not specified.");
1436: }
1437: iEntryId = Integer.parseInt(strTemp);
1438:
1439: iDomainId = CallContext.getInstance().getCurrentDomainId();
1440:
1441: strTemp = hsrqRequest.getParameter("BLOGENTRY_BLOG_ID");
1442: if (strTemp == null) {
1443: iBlogId = DataObject.NEW_ID;
1444: } else {
1445: iBlogId = Integer.parseInt(strTemp);
1446: }
1447: strCaption = hsrqRequest.getParameter("BLOGENTRY_CAPTION");
1448: if (strCaption == null) {
1449: strCaption = "";
1450: }
1451: strComments = hsrqRequest.getParameter("BLOGENTRY_COMMENTS");
1452: if (strComments == null) {
1453: strComments = "";
1454: }
1455: strImageURL = hsrqRequest.getParameter("BLOGENTRY_IMAGEURL");
1456: if (strImageURL == null) {
1457: strImageURL = "";
1458: }
1459: strTargetURL = hsrqRequest.getParameter("BLOGENTRY_TARGETURL");
1460: if (strTargetURL == null) {
1461: strTargetURL = "";
1462: }
1463: // User can never change creation date, therefore we require to store
1464: // it as the millisecond value since that is independent from the display format
1465: strTemp = hsrqRequest.getParameter("BLOGENTRY_CREATION_DATE");
1466: if (strTemp == null) {
1467: creationDate = null;
1468: } else {
1469: creationDate = DateUtils.parseTimestamp(strTemp);
1470: }
1471: // User can never change modification date, therefore we require to store
1472: // it as the millisecond value since that is independent from the display format
1473: strTemp = hsrqRequest
1474: .getParameter("BLOGENTRY_MODIFICATION_DATE");
1475: if (strTemp == null) {
1476: modificationDate = null;
1477: } else {
1478: modificationDate = DateUtils.parseTimestamp(strTemp);
1479: }
1480:
1481: return new Entry(iEntryId, iDomainId, iBlogId, strCaption,
1482: strComments, strImageURL, strTargetURL, creationDate,
1483: modificationDate);
1484: }
1485:
1486: /**
1487: * {@inheritDoc}
1488: */
1489: protected Principal verifyLogin(HttpSession hsSession,
1490: HttpServletRequest hsrqRequest,
1491: HttpServletResponse hsrpResponse) throws ServletException,
1492: IOException {
1493: Principal currentUser = null;
1494:
1495: if (getFormToProcess(hsrqRequest) == FORM_LOGIN_ID) {
1496: // User is trying to log in, so try to log him/her in
1497: final String strLogin;
1498: String strPassword;
1499:
1500: strLogin = hsrqRequest.getParameter("LOGIN");
1501: strPassword = hsrqRequest.getParameter("PASSWORD");
1502:
1503: // Verify the user name and password and if it is valid, let the
1504: // user proceed otherwise return an error message
1505: if ((m_strLogin.equals(strLogin))
1506: && (m_strPassword.equals(strPassword))) {
1507: currentUser = new Principal() {
1508: public String getName() {
1509: return strLogin;
1510: }
1511: };
1512: } else {
1513: CallContext
1514: .getInstance()
1515: .getMessages()
1516: .addErrorMessage(
1517: "The user name or password is not valid.");
1518: // User have tried to login again so reset the info from previous
1519: // login since it is no longer valid
1520: WebSessionUtils.resetSessionAndUserInfo(hsSession);
1521: }
1522: } else {
1523: // User is not trying to log in so see if he is already logged in
1524: if (WebSessionUtils.isLoggedIn(hsSession)) {
1525: currentUser = WebSessionUtils
1526: .getLoggedInUserInfo(hsSession);
1527: }
1528: }
1529:
1530: return currentUser;
1531: }
1532:
1533: /**
1534: * {@inheritDoc}
1535: */
1536: protected void redirectToLogin(HttpServletRequest hsrqRequest,
1537: HttpServletResponse hsrpResponse) throws ServletException,
1538: IOException {
1539: BlogNavigator navigator = getNavigator(hsrqRequest);
1540:
1541: if (navigator.isLoginPage()) {
1542: // User is already trying to display login page, just show it
1543: processNewLoginForm(hsrqRequest, hsrpResponse);
1544: } else {
1545: // We were asked to display login page so just let client go to the
1546: // the login page to get correct URL to the address bar and we will
1547: // most likely come back through the if above
1548: super.redirectToLogin(hsrqRequest, hsrpResponse);
1549: }
1550: }
1551: }
|