KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > projectmanagement > presentation > BasePO


1 package projectmanagement.presentation;
2
3 import com.lutris.appserver.server.httpPresentation.*;
4 import com.lutris.appserver.server.session.*;
5 import com.lutris.appserver.server.Enhydra;
6 //import com.lutris.xml.xmlc.*;
7
//import com.lutris.xml.xmlc.html.*;
8
import com.lutris.logging.*;
9 import com.lutris.util.KeywordValueException;
10 import com.lutris.appserver.server.user.User;
11
12 import org.enhydra.xml.xmlc.XMLObject;
13 import org.w3c.dom.*;
14 import org.w3c.dom.html.HTMLElement;
15
16 import java.lang.reflect.*;
17 import java.util.*;
18 import java.lang.Throwable JavaDoc;
19
20 import projectmanagement.*;
21 import projectmanagement.spec.employee.Employee;
22
23 /**
24  * This is the parent Presentation object. All presentation objects
25  * should extend this class.
26  *
27  * The run method looks for an event parameter and then calls
28  * handle<EventName>. If the "event" Parameter is not defined then
29  * the handleDefault() method is called in your child class.
30  *
31  * @author Sasa Bojanic
32  * @version 1.0
33  */

34 public abstract class BasePO implements HttpPresentation {
35    private static String JavaDoc AUTHORIZATION_ERROR_PAGE = "/AuthorizationError.po";
36    private static String JavaDoc LOGIN_PAGE = "/Login.po";
37    private static String JavaDoc EVENT = "event";
38    private static String JavaDoc STANDARD_METHOD_PREFIX = "handle";
39
40    /**
41     * This is the procedure that is called if there is no "event"
42     * HTTP parameter found. It must be overriden by the subclass to
43     * do default processing or error checking/handling.
44     *
45     * @return String The String representation of the HTML or (other format)
46     * of the document to be displayed. This method would need to be changed
47     * if you wanted to return binary data as well. It returns a String
48     * for simplicity right now.
49     */

50    public abstract XMLObject handleDefault() throws HttpPresentationException;
51
52    /**
53     * This method should be implemented in the subclass so that it returns
54     * true if this particular request requires the user to be logged
55     * in, otherwise false.
56     */

57    protected abstract int getRequiredAuthLevel();
58
59    /**
60     * Saved input and output context, and session data
61     */

62    protected HttpPresentationComms myComms = null;
63    protected ProjectManagementSessionData mySessionData = null;
64
65    /**
66     * Gets HttpPresentation object
67     *
68     * @return The saved comms objects
69     * to whichever subclass needs it
70     */

71    public HttpPresentationComms getComms() {
72       return this.myComms;
73    }
74
75    /**
76     * Gets the session data
77     *
78     * @return session data
79     */

80    public ProjectManagementSessionData getSessionData() {
81       return this.mySessionData;
82    }
83
84    /**
85     * Sets the user into the session
86     *
87     * @param theEmployee, the employee to be set in the session
88     * @exception ProjectManagementPresentationException
89     */

90    public void setUser(Employee theEmployee)
91          throws ProjectManagementPresentationException {
92       this.getSessionData().setUser(theEmployee);
93    }
94
95    /**
96     * Gets the user from the session
97     *
98     * @return the employee object in the session
99     */

100    public Employee getUser() {
101       return this.getSessionData().getUser();
102    }
103
104    /**
105     * Method to remove the current user from the session
106     */

107    public void removeUserFromSession() {
108       this.getSessionData().removeUser();
109    }
110
111    /**
112     * This implements the run method in HttpPresentation.
113     *
114     * @param HttpPresentationComms
115     * @exception Exception
116     */

117    public void run(HttpPresentationComms comms)
118       throws Exception JavaDoc {
119       // Initialize new or get the existing session data
120
initSessionData(comms);
121
122       // Check if the user can access the given page
123
checkAuthLevel();
124
125       try {
126          // Handle the incoming event request
127
handleEvent(comms);
128       } catch (Exception JavaDoc ex) {
129          throw new Exception JavaDoc("Exception in run "+ex);
130       }
131    }
132
133    /**
134     * Method to get or create the AgSessionData object from the user session
135     * This object is saved in the EbrokerPresentation object
136     *
137     * @param HttpPresentationComms
138     * @exception Exception
139     */

140    protected void initSessionData(HttpPresentationComms comms)
141          throws ProjectManagementPresentationException {
142       this.myComms = comms;
143       try {
144          Object JavaDoc obj = comms.sessionData.get(ProjectManagementSessionData.SESSION_KEY);
145          // If we found the session data, save it in a private data member
146
if(null != obj) {
147             this.mySessionData = (ProjectManagementSessionData)obj;
148          } else {
149             // If no session data was found, create a new session data instance
150
this.mySessionData = new ProjectManagementSessionData();
151             comms.sessionData.set(ProjectManagementSessionData.SESSION_KEY, this.mySessionData);
152          }
153       } catch(KeywordValueException ex) {
154          writeDebugMsg("Problem getting session data from session: " +
155             ex.getMessage());
156          throw new ProjectManagementPresentationException("Trouble initializing user",ex);
157       }
158    }
159
160    /**
161     * Return the current authorization level (set during login)
162     * @return An int equal to the current authorization level.
163     */

164    protected int getCurrentAuthLevel ()
165          throws ClientPageRedirectException, ProjectManagementPresentationException {
166       int accessLevel=0;
167 //We need to allow ProjectManagement_pres to be functional , so if the requested url is /ProjectManagement_pres/ we allow user to acsses to page
168
try {
169     String JavaDoc uri = myComms.request.getRequestURI();
170         boolean is= uri.startsWith("/ProjectManagement_pres");
171 if(is){
172     accessLevel=2 ;
173 }else{
174           accessLevel=getSessionData().getAuthLevel();
175         }
176       } catch (Exception JavaDoc ex) {
177          throw new ProjectManagementPresentationException("Trouble getting current authorization level",ex);
178       }
179     
180       return accessLevel;
181    }
182
183    /**
184     * Checks the session data to see if user has the authorization to
185     * access the given page. Authorization levels include:
186     * UNAUTH_USER (0) - login not required
187     * ORDINARY_USER (1) - requires normal login
188     * ADMIN_USER (2) - requires to login as administrator
189     * Redirects to the login page if user is not authorized to access the page.
190     */

191    protected void checkAuthLevel()
192          throws ClientPageRedirectException, ProjectManagementPresentationException {
193
194       int currentAuth=getCurrentAuthLevel();
195
196       try {
197          if (currentAuth<getRequiredAuthLevel()) {
198             throw new ClientPageRedirectException(AUTHORIZATION_ERROR_PAGE);
199          }
200       } catch (Exception JavaDoc ex) {
201          throw new ProjectManagementPresentationException("Trouble checking for user login status",ex);
202       }
203    }
204
205    /**
206     * Method to call the proper method for the incoming event
207     *
208     * @param HttpPresentationComms
209     * @exception Exception
210     */

211    public void handleEvent(HttpPresentationComms comms)
212          throws Exception JavaDoc {
213       String JavaDoc event = comms.request.getParameter(EVENT);
214
215       XMLObject returnHTML = null;
216
217       if (event == null || event.length() == 0) {
218          returnHTML = handleDefault();
219       }
220       else {
221          returnHTML = getPageContentForEvent(event);
222       }
223
224       comms.response.writeDOM(returnHTML);
225    }
226
227    /**
228     * Logs user out from the session by setting the usr to null
229     * in the session data.
230     *
231     * @return html document
232     * @exception ProjectManagementPresentationException
233     */

234    public XMLObject handleLogout()
235          throws ProjectManagementPresentationException {
236       try {
237          this.mySessionData=null;
238          SessionManager sessionManager=myComms.session.getSessionManager();
239          sessionManager.deleteSession(myComms.session);
240          throw new ClientPageRedirectException(LOGIN_PAGE);
241       } catch (Exception JavaDoc ex) {
242          throw new ProjectManagementPresentationException("Trouble logging out user",ex);
243       }
244     }
245
246    /**
247     * If an event parameter is defined then this invokes the method that
248     * handles that event.
249     *
250     * @param event, the incoming event name
251     * @exception Exception
252     */

253    public XMLObject getPageContentForEvent(String JavaDoc event)
254          throws Exception JavaDoc {
255       try {
256          Method method = this.getClass().getMethod(toMethodName(event), null);
257          XMLObject thePage = (XMLObject)method.invoke(this, null);
258          return thePage;
259
260       } catch(InvocationTargetException ex) {
261          // Rethrow the originating exception if as it should be propagated as is
262
// It could be a page redirect exception, etc.
263
if (ex.getTargetException() instanceof Exception JavaDoc) {
264             throw (Exception JavaDoc)ex.getTargetException();
265          } else if (ex.getTargetException() instanceof Error JavaDoc) {
266             throw (Error JavaDoc)ex.getTargetException();
267          } else {
268             throw ex;
269          }
270       } catch(NoSuchMethodException JavaDoc ex) {
271          //The method to handle the event does not exist.
272
throw new ProjectManagementPresentationException("NO EVENT HANDLER FOUND FOR EVENT: " +
273             event, ex);
274       } catch(IllegalAccessException JavaDoc ex) {
275          //The method to handle the event does not exist.
276
throw new ProjectManagementPresentationException("ILLEGAL ACCESS TO EVENT HANDLER (is it public?): " +
277             event, ex);
278       }
279    }
280
281    /**
282     * This sets the first letter of the event parameter value in order
283     * to adhere to Java method naming conventions.
284     *
285     * @param String event the incoming name of the event
286     * @return String the properly capitalized name
287     */

288    private String JavaDoc toMethodName(String JavaDoc event) {
289       StringBuffer JavaDoc methodName = new StringBuffer JavaDoc(STANDARD_METHOD_PREFIX);
290       methodName.append(Character.toUpperCase(event.charAt(0)));
291
292       if (event.length() > 1) {
293          methodName.append(event.substring(1));
294       }
295
296       return methodName.toString();
297    }
298
299    /**
300     * Returns the application object associated with the
301     * current request.
302     *
303     * @return the application object.
304     */

305    public ProjectManagement getApplication() {
306       return (ProjectManagement)Enhydra.getApplication();
307    }
308
309    /**
310     * Method to write a debugging message to the debug log
311     * channel when the DEBUG flag is turned on
312     *
313     * @param msg The message to write to the DEBUG log channel
314     */

315    public static void writeDebugMsg(String JavaDoc msg) {
316       Enhydra.getLogChannel().write(Logger.DEBUG,msg);
317    }
318
319    /**
320    * Returns true if the given string is null, empty, or contains
321    * only the white space(s).
322    */

323    protected static boolean isNullField (String JavaDoc field) {
324       if (field==null) {
325          return true;
326       }
327
328       if (field.trim().equals("")) {
329          return true;
330       }
331       return false;
332    }
333
334    public static boolean areDateFieldsValid (String JavaDoc YYYY,String JavaDoc MM,String JavaDoc DD) {
335       if (isNullField(YYYY) || isNullField(MM) || isNullField(DD)) {
336          return false;
337       }
338       Calendar cal=new GregorianCalendar();
339       cal.setLenient(false);
340       try {
341          int yyyy=Integer.valueOf(YYYY).intValue();
342          if (yyyy<1900) {
343             return false;
344          }
345          int mm=Integer.valueOf(MM).intValue();
346          int dd=Integer.valueOf(DD).intValue();
347          cal.set(yyyy,mm-1,dd);
348          // if following method throws exception, date is not ok
349
cal.getTime();
350       } catch (Exception JavaDoc ex) {
351          return false;
352       }
353       return true;
354    }
355 }
356
Popular Tags