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.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 ; 19 20 import projectmanagement.*; 21 import projectmanagement.spec.employee.Employee; 22 23 34 public abstract class BasePO implements HttpPresentation { 35 private static String AUTHORIZATION_ERROR_PAGE = "/AuthorizationError.po"; 36 private static String LOGIN_PAGE = "/Login.po"; 37 private static String EVENT = "event"; 38 private static String STANDARD_METHOD_PREFIX = "handle"; 39 40 50 public abstract XMLObject handleDefault() throws HttpPresentationException; 51 52 57 protected abstract int getRequiredAuthLevel(); 58 59 62 protected HttpPresentationComms myComms = null; 63 protected ProjectManagementSessionData mySessionData = null; 64 65 71 public HttpPresentationComms getComms() { 72 return this.myComms; 73 } 74 75 80 public ProjectManagementSessionData getSessionData() { 81 return this.mySessionData; 82 } 83 84 90 public void setUser(Employee theEmployee) 91 throws ProjectManagementPresentationException { 92 this.getSessionData().setUser(theEmployee); 93 } 94 95 100 public Employee getUser() { 101 return this.getSessionData().getUser(); 102 } 103 104 107 public void removeUserFromSession() { 108 this.getSessionData().removeUser(); 109 } 110 111 117 public void run(HttpPresentationComms comms) 118 throws Exception { 119 initSessionData(comms); 121 122 checkAuthLevel(); 124 125 try { 126 handleEvent(comms); 128 } catch (Exception ex) { 129 throw new Exception ("Exception in run "+ex); 130 } 131 } 132 133 140 protected void initSessionData(HttpPresentationComms comms) 141 throws ProjectManagementPresentationException { 142 this.myComms = comms; 143 try { 144 Object obj = comms.sessionData.get(ProjectManagementSessionData.SESSION_KEY); 145 if(null != obj) { 147 this.mySessionData = (ProjectManagementSessionData)obj; 148 } else { 149 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 164 protected int getCurrentAuthLevel () 165 throws ClientPageRedirectException, ProjectManagementPresentationException { 166 int accessLevel=0; 167 try { 169 String 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 ex) { 177 throw new ProjectManagementPresentationException("Trouble getting current authorization level",ex); 178 } 179 180 return accessLevel; 181 } 182 183 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 ex) { 201 throw new ProjectManagementPresentationException("Trouble checking for user login status",ex); 202 } 203 } 204 205 211 public void handleEvent(HttpPresentationComms comms) 212 throws Exception { 213 String 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 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 ex) { 242 throw new ProjectManagementPresentationException("Trouble logging out user",ex); 243 } 244 } 245 246 253 public XMLObject getPageContentForEvent(String event) 254 throws Exception { 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 if (ex.getTargetException() instanceof Exception ) { 264 throw (Exception )ex.getTargetException(); 265 } else if (ex.getTargetException() instanceof Error ) { 266 throw (Error )ex.getTargetException(); 267 } else { 268 throw ex; 269 } 270 } catch(NoSuchMethodException ex) { 271 throw new ProjectManagementPresentationException("NO EVENT HANDLER FOUND FOR EVENT: " + 273 event, ex); 274 } catch(IllegalAccessException ex) { 275 throw new ProjectManagementPresentationException("ILLEGAL ACCESS TO EVENT HANDLER (is it public?): " + 277 event, ex); 278 } 279 } 280 281 288 private String toMethodName(String event) { 289 StringBuffer methodName = new StringBuffer (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 305 public ProjectManagement getApplication() { 306 return (ProjectManagement)Enhydra.getApplication(); 307 } 308 309 315 public static void writeDebugMsg(String msg) { 316 Enhydra.getLogChannel().write(Logger.DEBUG,msg); 317 } 318 319 323 protected static boolean isNullField (String 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 YYYY,String MM,String 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 cal.getTime(); 350 } catch (Exception ex) { 351 return false; 352 } 353 return true; 354 } 355 } 356 | Popular Tags |