001: package projectmanagement.presentation;
002:
003: import com.lutris.appserver.server.httpPresentation.*;
004: import com.lutris.appserver.server.session.*;
005: import com.lutris.appserver.server.Enhydra; //import com.lutris.xml.xmlc.*;
006: //import com.lutris.xml.xmlc.html.*;
007: import com.lutris.logging.*;
008: import com.lutris.util.KeywordValueException;
009: import com.lutris.appserver.server.user.User;
010:
011: import org.enhydra.xml.xmlc.XMLObject;
012: import org.w3c.dom.*;
013: import org.w3c.dom.html.HTMLElement;
014:
015: import java.lang.reflect.*;
016: import java.util.*;
017: import java.lang.Throwable;
018:
019: import projectmanagement.*;
020: import projectmanagement.spec.employee.Employee;
021:
022: /**
023: * This is the parent Presentation object. All presentation objects
024: * should extend this class.
025: *
026: * The run method looks for an event parameter and then calls
027: * handle<EventName>. If the "event" Parameter is not defined then
028: * the handleDefault() method is called in your child class.
029: *
030: * @author Sasa Bojanic
031: * @version 1.0
032: */
033: public abstract class BasePO implements HttpPresentation {
034: private static String AUTHORIZATION_ERROR_PAGE = "/AuthorizationError.po";
035: private static String LOGIN_PAGE = "/Login.po";
036: private static String EVENT = "event";
037: private static String STANDARD_METHOD_PREFIX = "handle";
038:
039: /**
040: * This is the procedure that is called if there is no "event"
041: * HTTP parameter found. It must be overriden by the subclass to
042: * do default processing or error checking/handling.
043: *
044: * @return String The String representation of the HTML or (other format)
045: * of the document to be displayed. This method would need to be changed
046: * if you wanted to return binary data as well. It returns a String
047: * for simplicity right now.
048: */
049: public abstract XMLObject handleDefault()
050: throws HttpPresentationException;
051:
052: /**
053: * This method should be implemented in the subclass so that it returns
054: * true if this particular request requires the user to be logged
055: * in, otherwise false.
056: */
057: protected abstract int getRequiredAuthLevel();
058:
059: /**
060: * Saved input and output context, and session data
061: */
062: protected HttpPresentationComms myComms = null;
063: protected ProjectManagementSessionData mySessionData = null;
064:
065: /**
066: * Gets HttpPresentation object
067: *
068: * @return The saved comms objects
069: * to whichever subclass needs it
070: */
071: public HttpPresentationComms getComms() {
072: return this .myComms;
073: }
074:
075: /**
076: * Gets the session data
077: *
078: * @return session data
079: */
080: public ProjectManagementSessionData getSessionData() {
081: return this .mySessionData;
082: }
083:
084: /**
085: * Sets the user into the session
086: *
087: * @param theEmployee, the employee to be set in the session
088: * @exception ProjectManagementPresentationException
089: */
090: public void setUser(Employee theEmployee)
091: throws ProjectManagementPresentationException {
092: this .getSessionData().setUser(theEmployee);
093: }
094:
095: /**
096: * Gets the user from the session
097: *
098: * @return the employee object in the session
099: */
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) throws Exception {
118: // Initialize new or get the existing session data
119: initSessionData(comms);
120:
121: // Check if the user can access the given page
122: checkAuthLevel();
123:
124: try {
125: // Handle the incoming event request
126: handleEvent(comms);
127: } catch (Exception ex) {
128: throw new Exception("Exception in run " + ex);
129: }
130: }
131:
132: /**
133: * Method to get or create the AgSessionData object from the user session
134: * This object is saved in the EbrokerPresentation object
135: *
136: * @param HttpPresentationComms
137: * @exception Exception
138: */
139: protected void initSessionData(HttpPresentationComms comms)
140: throws ProjectManagementPresentationException {
141: this .myComms = comms;
142: try {
143: Object obj = comms.sessionData
144: .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(
152: ProjectManagementSessionData.SESSION_KEY,
153: this .mySessionData);
154: }
155: } catch (KeywordValueException ex) {
156: writeDebugMsg("Problem getting session data from session: "
157: + ex.getMessage());
158: throw new ProjectManagementPresentationException(
159: "Trouble initializing user", ex);
160: }
161: }
162:
163: /**
164: * Return the current authorization level (set during login)
165: * @return An int equal to the current authorization level.
166: */
167: protected int getCurrentAuthLevel()
168: throws ClientPageRedirectException,
169: ProjectManagementPresentationException {
170: int accessLevel = 0;
171: //We need to allow ProjectManagement_pres to be functional , so if the requested url is /ProjectManagement_pres/ we allow user to acsses to page
172: try {
173: String uri = myComms.request.getRequestURI();
174: boolean is = uri.startsWith("/ProjectManagement_pres");
175: if (is) {
176: accessLevel = 2;
177: } else {
178: accessLevel = getSessionData().getAuthLevel();
179: }
180: } catch (Exception ex) {
181: throw new ProjectManagementPresentationException(
182: "Trouble getting current authorization level", ex);
183: }
184:
185: return accessLevel;
186: }
187:
188: /**
189: * Checks the session data to see if user has the authorization to
190: * access the given page. Authorization levels include:
191: * UNAUTH_USER (0) - login not required
192: * ORDINARY_USER (1) - requires normal login
193: * ADMIN_USER (2) - requires to login as administrator
194: * Redirects to the login page if user is not authorized to access the page.
195: */
196: protected void checkAuthLevel() throws ClientPageRedirectException,
197: ProjectManagementPresentationException {
198:
199: int currentAuth = getCurrentAuthLevel();
200:
201: try {
202: if (currentAuth < getRequiredAuthLevel()) {
203: throw new ClientPageRedirectException(
204: AUTHORIZATION_ERROR_PAGE);
205: }
206: } catch (Exception ex) {
207: throw new ProjectManagementPresentationException(
208: "Trouble checking for user login status", ex);
209: }
210: }
211:
212: /**
213: * Method to call the proper method for the incoming event
214: *
215: * @param HttpPresentationComms
216: * @exception Exception
217: */
218: public void handleEvent(HttpPresentationComms comms)
219: throws Exception {
220: String event = comms.request.getParameter(EVENT);
221:
222: XMLObject returnHTML = null;
223:
224: if (event == null || event.length() == 0) {
225: returnHTML = handleDefault();
226: } else {
227: returnHTML = getPageContentForEvent(event);
228: }
229:
230: comms.response.writeDOM(returnHTML);
231: }
232:
233: /**
234: * Logs user out from the session by setting the usr to null
235: * in the session data.
236: *
237: * @return html document
238: * @exception ProjectManagementPresentationException
239: */
240: public XMLObject handleLogout()
241: throws ProjectManagementPresentationException {
242: try {
243: this .mySessionData = null;
244: SessionManager sessionManager = myComms.session
245: .getSessionManager();
246: sessionManager.deleteSession(myComms.session);
247: throw new ClientPageRedirectException(LOGIN_PAGE);
248: } catch (Exception ex) {
249: throw new ProjectManagementPresentationException(
250: "Trouble logging out user", ex);
251: }
252: }
253:
254: /**
255: * If an event parameter is defined then this invokes the method that
256: * handles that event.
257: *
258: * @param event, the incoming event name
259: * @exception Exception
260: */
261: public XMLObject getPageContentForEvent(String event)
262: throws Exception {
263: try {
264: Method method = this .getClass().getMethod(
265: toMethodName(event), null);
266: XMLObject thePage = (XMLObject) method.invoke(this , null);
267: return thePage;
268:
269: } catch (InvocationTargetException ex) {
270: // Rethrow the originating exception if as it should be propagated as is
271: // It could be a page redirect exception, etc.
272: if (ex.getTargetException() instanceof Exception) {
273: throw (Exception) ex.getTargetException();
274: } else if (ex.getTargetException() instanceof Error) {
275: throw (Error) ex.getTargetException();
276: } else {
277: throw ex;
278: }
279: } catch (NoSuchMethodException ex) {
280: //The method to handle the event does not exist.
281: throw new ProjectManagementPresentationException(
282: "NO EVENT HANDLER FOUND FOR EVENT: " + event, ex);
283: } catch (IllegalAccessException ex) {
284: //The method to handle the event does not exist.
285: throw new ProjectManagementPresentationException(
286: "ILLEGAL ACCESS TO EVENT HANDLER (is it public?): "
287: + event, ex);
288: }
289: }
290:
291: /**
292: * This sets the first letter of the event parameter value in order
293: * to adhere to Java method naming conventions.
294: *
295: * @param String event the incoming name of the event
296: * @return String the properly capitalized name
297: */
298: private String toMethodName(String event) {
299: StringBuffer methodName = new StringBuffer(
300: STANDARD_METHOD_PREFIX);
301: methodName.append(Character.toUpperCase(event.charAt(0)));
302:
303: if (event.length() > 1) {
304: methodName.append(event.substring(1));
305: }
306:
307: return methodName.toString();
308: }
309:
310: /**
311: * Returns the application object associated with the
312: * current request.
313: *
314: * @return the application object.
315: */
316: public ProjectManagement getApplication() {
317: return (ProjectManagement) Enhydra.getApplication();
318: }
319:
320: /**
321: * Method to write a debugging message to the debug log
322: * channel when the DEBUG flag is turned on
323: *
324: * @param msg The message to write to the DEBUG log channel
325: */
326: public static void writeDebugMsg(String msg) {
327: Enhydra.getLogChannel().write(Logger.DEBUG, msg);
328: }
329:
330: /**
331: * Returns true if the given string is null, empty, or contains
332: * only the white space(s).
333: */
334: protected static boolean isNullField(String field) {
335: if (field == null) {
336: return true;
337: }
338:
339: if (field.trim().equals("")) {
340: return true;
341: }
342: return false;
343: }
344:
345: public static boolean areDateFieldsValid(String YYYY, String MM,
346: String DD) {
347: if (isNullField(YYYY) || isNullField(MM) || isNullField(DD)) {
348: return false;
349: }
350: Calendar cal = new GregorianCalendar();
351: cal.setLenient(false);
352: try {
353: int yyyy = Integer.valueOf(YYYY).intValue();
354: if (yyyy < 1900) {
355: return false;
356: }
357: int mm = Integer.valueOf(MM).intValue();
358: int dd = Integer.valueOf(DD).intValue();
359: cal.set(yyyy, mm - 1, dd);
360: // if following method throws exception, date is not ok
361: cal.getTime();
362: } catch (Exception ex) {
363: return false;
364: }
365: return true;
366: }
367: }
|