BaseWapPO.java :  » J2EE » Enhydra-Demos » org » openuss » presentation » enhydra » framework » Java Open Source

Java Open Source » J2EE » Enhydra Demos 
Enhydra Demos » org » openuss » presentation » enhydra » framework » BaseWapPO.java
/**
 * Title:        OpenUSS - Open Source University Support System
 * Description:  BaseWapPO Presentation Object
 * Copyright:    Copyright (c) B. Lofi Dewanto
 * Company:      University of Muenster
 * @author  B. Lofi Dewanto
 * @version 1.0
 */
package org.openuss.presentation.enhydra.framework;

import com.lutris.appserver.server.*;
import com.lutris.appserver.server.httpPresentation.*;
import com.lutris.appserver.server.user.User;

import com.lutris.logging.*;

import com.lutris.util.KeywordValueException;

//import com.lutris.xml.xmlc.*;
//import com.lutris.xml.xmlc.html.*;

import java.lang.Throwable;
import java.lang.reflect.*;

import java.util.*;

import org.w3c.dom.*;
import org.w3c.dom.html.HTMLElement;


/**
 * The base presentation object for WAP Enhydra.
 *
 * @author  B. Lofi Dewanto
 * @version 1.0
 */
abstract public class BaseWapPO {
    /**
     * An event must be called "event".
     */
    private static String EVENT = "event";

    /**
     * Every methods have to use the handle in the
     * front of the method name, e.g.: handleLogin().
     */
    private static String STANDARD_METHOD_PREFIX = "handle";

    /**
     * Saved HTML input and output context.
     */
    protected HttpPresentationComms myComms = null;

    /**
     * This is the procedure that is called if there is no "event"
     * HTTP parameter found. It must be overriden by the subclass to
     * do default processing or error checking/handling.
     *
     * @return String The String representation of the HTML or (other format)
     * of the document to be displayed. This method would need to be changed
     * if you wanted to return binary data as well. It returns a String
     * for simplicity right now.
     */
    abstract public String handleDefault() throws HttpPresentationException;

    /**
     * This method should be implemented in the subclass so that it returns
     * true if this particular request requires the user to be logged
     * in, otherwise false.
     */
    abstract protected boolean loggedInUserRequired();

    /**
     * This method should be implemented in the subclass, so that it returns
     * true if this particular request requires the user to apply first
     * for the access. For public means everybody can access this,
     * otherwise he or she needs to be activated first.
     */
    abstract protected boolean isForPublicAccess() throws BasePOException;

    /**
     * This method should be implemented in the subclass to change
     * the behaviour of the access list. Access list contents the current
     * student and the current enrollment. If the the current enrollment
     * is not for a public access, the student has to have an access list (accepted)
     * to get into this enrollment.
     */
    abstract protected void checkForAccessList()
                                        throws ClientPageRedirectException, 
                                               BasePOException;

    /**
     * @return The saved comms objects
     * to whichever subclass needs it
     */
    public HttpPresentationComms getComms() {
        return this.myComms;
    }

    /**
     * Method to call the proper method for the incoming event
     */
    public void handleEvent(HttpPresentationComms comms)
                     throws Exception {
        String event = comms.request.getParameter(EVENT);
        String returnWML = null;

        if ((event == null) || (event.length() == 0)) {
            returnWML = handleDefault();
        } else {
            returnWML = getPageContentForEvent(event);
        }

        HttpPresentationResponse response = comms.response;
        response.setContentType("text/vnd.wap.wml");

        HttpPresentationOutputStream out = response.getOutputStream();
        out.println(returnWML);
        response.flush();
    }

    /**
     * If an event parameter is defined then this invokes the method that
     * handles that event.
     */
    public String getPageContentForEvent(String event)
                                  throws Exception {
        try {
            // Get the methodname
            Method method = this.getClass()
                                .getMethod(toMethodName(event), null);

            // Execute the run method
            String thePage = (String) method.invoke(this, null);

            return thePage;
        } catch (InvocationTargetException ex) {
            // Rethrow the originating exception if as it should be propagated as is
            // It could be a page redirect exception, etc.
            if (ex.getTargetException() instanceof Exception) {
                throw (Exception) ex.getTargetException();
            } else if (ex.getTargetException() instanceof Error) {
                throw (Error) ex.getTargetException();
            } else {
                throw ex;
            }
        } catch (NoSuchMethodException ex) {
            // The method to handle the event does not exist.
            throw new BasePOException("No event handler found for event: " + 
                                      event, ex);
        } catch (IllegalAccessException ex) {
            // The method to handle the event does not exist.
            throw new BasePOException(
                    "Illegal access to event handler (is it public?): " + 
                    event, ex);
        }
    }

    /**
     * This sets the first letter of the event parameter value in order
     * to adhere to Java method naming conventions.
     *
     * @param String event the incoming name of the event
     * @return String the properly capitalized name
     */
    private String toMethodName(String event) {
        StringBuffer methodName = new StringBuffer(STANDARD_METHOD_PREFIX);
        methodName.append(Character.toUpperCase(event.charAt(0)));

        if (event.length() > 1) {
            methodName.append(event.substring(1));
        }

        return methodName.toString();
    }

    /**
     * Returns the application object associated with the
     * current request.
     *
     * @return the ebroker application object.
     */
    public StandardApplication getApplication() {
        return (StandardApplication) Enhydra.getApplication();
    }

    /**
     * Method to write a debugging message to the debug log
     * channel when the DEBUG flag is turned on
     *
     * @param msg The message to write to the DEBUG log channel
     */
    public static void writeDebugMsg(String msg) {
        Enhydra.getLogChannel().write(Logger.DEBUG, msg);
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.