001: /**
002: * Title: OpenUSS - Open Source University Support System
003: * Description: BasePO Presentation Object
004: * Copyright: Copyright (c) B. Lofi Dewanto
005: * Company: University of Muenster
006: * @author B. Lofi Dewanto
007: * @version 1.0
008: */package org.openuss.presentation.enhydra.framework;
009:
010: import com.lutris.appserver.server.*;
011: import com.lutris.appserver.server.httpPresentation.*;
012: import com.lutris.appserver.server.user.User;
013:
014: import com.lutris.logging.*;
015:
016: import com.lutris.util.KeywordValueException;
017:
018: //import com.lutris.xml.xmlc.*;
019: //import com.lutris.xml.xmlc.html.*;
020:
021: import java.lang.Throwable;
022: import java.lang.reflect.*;
023:
024: import java.util.*;
025:
026: import org.w3c.dom.*;
027: import org.w3c.dom.html.HTMLElement;
028:
029: /**
030: * The base presentation object for Enhydra.
031: *
032: * @author B. Lofi Dewanto
033: * @version 1.0
034: */
035: abstract public class BasePO {
036: /**
037: * An event must be called "event".
038: */
039: private static String EVENT = "event";
040:
041: /**
042: * Every methods have to use the handle in the
043: * front of the method name, e.g.: handleLogin().
044: */
045: private static String STANDARD_METHOD_PREFIX = "handle";
046:
047: /**
048: * Saved HTML input and output context.
049: */
050: protected HttpPresentationComms myComms = null;
051:
052: /**
053: * This is the procedure that is called if there is no "event"
054: * HTTP parameter found. It must be overriden by the subclass to
055: * do default processing or error checking/handling.
056: *
057: * @return String The String representation of the HTML or (other format)
058: * of the document to be displayed. This method would need to be changed
059: * if you wanted to return binary data as well. It returns a String
060: * for simplicity right now.
061: */
062: abstract public String handleDefault()
063: throws HttpPresentationException;
064:
065: /**
066: * This method should be implemented in the subclass so that it returns
067: * true if this particular request requires the user to be logged
068: * in, otherwise false.
069: */
070: abstract protected boolean loggedInUserRequired();
071:
072: /**
073: * This method should be implemented in the subclass, so that it returns
074: * true if this particular request requires the user to apply first
075: * for the access. For public means everybody can access this,
076: * otherwise he or she needs to be activated first.
077: */
078: abstract protected boolean isForPublicAccess()
079: throws BasePOException;
080:
081: /**
082: * This method should be implemented in the subclass to change
083: * the behaviour of the access list. Access list contents the current
084: * student and the current enrollment. If the the current enrollment
085: * is not for a public access, the student has to have an access list (accepted)
086: * to get into this enrollment.
087: */
088: abstract protected void checkForAccessList()
089: throws ClientPageRedirectException, BasePOException;
090:
091: /**
092: * @return The saved comms objects
093: * to whichever subclass needs it
094: */
095: public HttpPresentationComms getComms() {
096: return this .myComms;
097: }
098:
099: /**
100: * Method to call the proper method for the incoming event
101: */
102: public void handleEvent(HttpPresentationComms comms)
103: throws Exception {
104: String event = comms.request.getParameter(EVENT);
105: String returnHTML = null;
106:
107: if ((event == null) || (event.length() == 0)) {
108: returnHTML = handleDefault();
109: } else {
110: returnHTML = getPageContentForEvent(event);
111: }
112:
113: comms.response.writeHTML(returnHTML);
114: }
115:
116: /**
117: * If an event parameter is defined then this invokes the method that
118: * handles that event.
119: */
120: public String getPageContentForEvent(String event) throws Exception {
121: try {
122: // Get the methodname
123: Method method = this .getClass().getMethod(
124: toMethodName(event), null);
125:
126: // Execute the run method
127: String thePage = (String) method.invoke(this , null);
128:
129: return thePage;
130: } catch (InvocationTargetException ex) {
131: // Rethrow the originating exception if as it should be propagated as is
132: // It could be a page redirect exception, etc.
133: if (ex.getTargetException() instanceof Exception) {
134: throw (Exception) ex.getTargetException();
135: } else if (ex.getTargetException() instanceof Error) {
136: throw (Error) ex.getTargetException();
137: } else {
138: throw ex;
139: }
140: } catch (NoSuchMethodException ex) {
141: // The method to handle the event does not exist.
142: throw new BasePOException(
143: "No event handler found for event: " + event, ex);
144: } catch (IllegalAccessException ex) {
145: // The method to handle the event does not exist.
146: throw new BasePOException(
147: "Illegal access to event handler (is it public?): "
148: + event, ex);
149: }
150: }
151:
152: /**
153: * This sets the first letter of the event parameter value in order
154: * to adhere to Java method naming conventions.
155: *
156: * @param String event the incoming name of the event
157: * @return String the properly capitalized name
158: */
159: private String toMethodName(String event) {
160: StringBuffer methodName = new StringBuffer(
161: STANDARD_METHOD_PREFIX);
162: methodName.append(Character.toUpperCase(event.charAt(0)));
163:
164: if (event.length() > 1) {
165: methodName.append(event.substring(1));
166: }
167:
168: return methodName.toString();
169: }
170:
171: /**
172: * Returns the application object associated with the
173: * current request.
174: *
175: * @return the ebroker application object.
176: */
177: public StandardApplication getApplication() {
178: return (StandardApplication) Enhydra.getApplication();
179: }
180:
181: /**
182: * Method to write a debugging message to the debug log
183: * channel when the DEBUG flag is turned on
184: *
185: * @param msg The message to write to the DEBUG log channel
186: */
187: public static void writeDebugMsg(String msg) {
188: Enhydra.getLogChannel().write(Logger.DEBUG, msg);
189: }
190: }
|