KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openuss > presentation > enhydra > framework > BasePO


1 /**
2  * Title: OpenUSS - Open Source University Support System
3  * Description: BasePO Presentation Object
4  * Copyright: Copyright (c) B. Lofi Dewanto
5  * Company: University of Muenster
6  * @author B. Lofi Dewanto
7  * @version 1.0
8  */

9 package org.openuss.presentation.enhydra.framework;
10
11 import com.lutris.appserver.server.*;
12 import com.lutris.appserver.server.httpPresentation.*;
13 import com.lutris.appserver.server.user.User;
14
15 import com.lutris.logging.*;
16
17 import com.lutris.util.KeywordValueException;
18
19 //import com.lutris.xml.xmlc.*;
20
//import com.lutris.xml.xmlc.html.*;
21

22 import java.lang.Throwable JavaDoc;
23 import java.lang.reflect.*;
24
25 import java.util.*;
26
27 import org.w3c.dom.*;
28 import org.w3c.dom.html.HTMLElement;
29
30
31 /**
32  * The base presentation object for Enhydra.
33  *
34  * @author B. Lofi Dewanto
35  * @version 1.0
36  */

37 abstract public class BasePO {
38     /**
39      * An event must be called "event".
40      */

41     private static String JavaDoc EVENT = "event";
42
43     /**
44      * Every methods have to use the handle in the
45      * front of the method name, e.g.: handleLogin().
46      */

47     private static String JavaDoc STANDARD_METHOD_PREFIX = "handle";
48
49     /**
50      * Saved HTML input and output context.
51      */

52     protected HttpPresentationComms myComms = null;
53
54     /**
55      * This is the procedure that is called if there is no "event"
56      * HTTP parameter found. It must be overriden by the subclass to
57      * do default processing or error checking/handling.
58      *
59      * @return String The String representation of the HTML or (other format)
60      * of the document to be displayed. This method would need to be changed
61      * if you wanted to return binary data as well. It returns a String
62      * for simplicity right now.
63      */

64     abstract public String JavaDoc handleDefault() throws HttpPresentationException;
65
66     /**
67      * This method should be implemented in the subclass so that it returns
68      * true if this particular request requires the user to be logged
69      * in, otherwise false.
70      */

71     abstract protected boolean loggedInUserRequired();
72
73     /**
74      * This method should be implemented in the subclass, so that it returns
75      * true if this particular request requires the user to apply first
76      * for the access. For public means everybody can access this,
77      * otherwise he or she needs to be activated first.
78      */

79     abstract protected boolean isForPublicAccess() throws BasePOException;
80
81     /**
82      * This method should be implemented in the subclass to change
83      * the behaviour of the access list. Access list contents the current
84      * student and the current enrollment. If the the current enrollment
85      * is not for a public access, the student has to have an access list (accepted)
86      * to get into this enrollment.
87      */

88     abstract protected void checkForAccessList()
89                                         throws ClientPageRedirectException,
90                                                BasePOException;
91
92     /**
93      * @return The saved comms objects
94      * to whichever subclass needs it
95      */

96     public HttpPresentationComms getComms() {
97         return this.myComms;
98     }
99
100     /**
101      * Method to call the proper method for the incoming event
102      */

103     public void handleEvent(HttpPresentationComms comms)
104                      throws Exception JavaDoc {
105         String JavaDoc event = comms.request.getParameter(EVENT);
106         String JavaDoc returnHTML = null;
107
108         if ((event == null) || (event.length() == 0)) {
109             returnHTML = handleDefault();
110         } else {
111             returnHTML = getPageContentForEvent(event);
112         }
113
114         comms.response.writeHTML(returnHTML);
115     }
116
117     /**
118      * If an event parameter is defined then this invokes the method that
119      * handles that event.
120      */

121     public String JavaDoc getPageContentForEvent(String JavaDoc event)
122                                   throws Exception JavaDoc {
123         try {
124             // Get the methodname
125
Method method = this.getClass()
126                                 .getMethod(toMethodName(event), null);
127
128             // Execute the run method
129
String JavaDoc thePage = (String JavaDoc) method.invoke(this, null);
130
131             return thePage;
132         } catch (InvocationTargetException ex) {
133             // Rethrow the originating exception if as it should be propagated as is
134
// It could be a page redirect exception, etc.
135
if (ex.getTargetException() instanceof Exception JavaDoc) {
136                 throw (Exception JavaDoc) ex.getTargetException();
137             } else if (ex.getTargetException() instanceof Error JavaDoc) {
138                 throw (Error JavaDoc) ex.getTargetException();
139             } else {
140                 throw ex;
141             }
142         } catch (NoSuchMethodException JavaDoc ex) {
143             // The method to handle the event does not exist.
144
throw new BasePOException("No event handler found for event: " +
145                                       event, ex);
146         } catch (IllegalAccessException JavaDoc ex) {
147             // The method to handle the event does not exist.
148
throw new BasePOException(
149                     "Illegal access to event handler (is it public?): " +
150                     event, ex);
151         }
152     }
153
154     /**
155      * This sets the first letter of the event parameter value in order
156      * to adhere to Java method naming conventions.
157      *
158      * @param String event the incoming name of the event
159      * @return String the properly capitalized name
160      */

161     private String JavaDoc toMethodName(String JavaDoc event) {
162         StringBuffer JavaDoc methodName = new StringBuffer JavaDoc(STANDARD_METHOD_PREFIX);
163         methodName.append(Character.toUpperCase(event.charAt(0)));
164
165         if (event.length() > 1) {
166             methodName.append(event.substring(1));
167         }
168
169         return methodName.toString();
170     }
171
172     /**
173      * Returns the application object associated with the
174      * current request.
175      *
176      * @return the ebroker application object.
177      */

178     public StandardApplication getApplication() {
179         return (StandardApplication) Enhydra.getApplication();
180     }
181
182     /**
183      * Method to write a debugging message to the debug log
184      * channel when the DEBUG flag is turned on
185      *
186      * @param msg The message to write to the DEBUG log channel
187      */

188     public static void writeDebugMsg(String JavaDoc msg) {
189         Enhydra.getLogChannel().write(Logger.DEBUG, msg);
190     }
191 }
Popular Tags