1 23 24 package org.enhydra.pim.presentation; 25 26 import java.lang.reflect.InvocationTargetException ; 27 import java.lang.reflect.Method ; 28 import java.sql.SQLException ; 29 30 import org.enhydra.pim.Enhydrapim; 31 import org.enhydra.pim.business.TransactionContextI; 32 import org.enhydra.pim.business.api.CommonConstants; 33 import org.enhydra.pim.business.api.OwnerI; 34 import org.enhydra.pim.exception.EnhydraPimException; 35 import org.enhydra.pim.presentation.enhydrapim.ErrorHTML; 36 import org.enhydra.pim.presentation.enhydrapim.IndexHTML; 37 import org.enhydra.pim.spec.TransactionContextFactory; 38 import org.enhydra.xml.xmlc.XMLObject; 39 import org.w3c.dom.Element ; 40 41 import com.lutris.appserver.server.Enhydra; 42 import com.lutris.appserver.server.httpPresentation.ClientPageRedirectException; 43 import com.lutris.appserver.server.httpPresentation.HttpPresentation; 44 import com.lutris.appserver.server.httpPresentation.HttpPresentationComms; 45 import com.lutris.appserver.server.httpPresentation.HttpPresentationException; 46 import com.lutris.logging.Logger; 47 import com.lutris.util.KeywordValueException; 48 49 57 58 public abstract class BasePO implements HttpPresentation { 59 60 public static final String LOGIN_PAGE = "Index.po"; 61 public static final String ERROR_PAGE = "Error.po"; 62 63 private static String EVENT = "event"; 64 65 private static String STANDARD_METHOD_PREFIX = "handle"; 66 67 77 public abstract XMLObject handleDefault() throws HttpPresentationException; 78 79 84 protected abstract boolean loggedInUserRequired(); 85 86 89 protected HttpPresentationComms myComms = null; 90 91 96 protected EnhydraPimSessionData mySessionData = null; 97 98 104 protected OwnerI myPerson = null; 105 106 111 public HttpPresentationComms getComms() { 112 return this.myComms; 113 } 114 115 120 public EnhydraPimSessionData getSessionData() { 121 return this.mySessionData; 122 } 123 124 131 public void setUser(OwnerI thePerson) 132 throws EnhydraPimPresentationException { 133 this.getSessionData().setUser(thePerson); 134 } 135 136 141 public OwnerI getUser() { 142 return this.getSessionData().getUser(); 143 } 144 145 148 public void removeUserFromSession() { 149 this.getSessionData().removeUser(); 150 } 151 152 159 public void run(HttpPresentationComms comms) throws Exception { 160 initSessionData(comms); 162 163 if (this.loggedInUserRequired()) { 165 checkForUserLogin(); 166 } 167 168 TransactionContextI transCtx= TransactionContextFactory.getTransactionContext(); 169 170 try { 171 transCtx.beginContext(CommonConstants.productionDatabaseName); 172 handleEvent(comms); 173 }catch(Exception e){ 174 transCtx.rollbackContext(); 175 throw new EnhydraPimException("Unhandled Application Error"); 176 }finally { 177 try { 178 transCtx.commitContext(); 179 } catch (SQLException e){ 180 throw new EnhydraPimException("Unhandled Application Error"); 181 } 182 } 183 184 } 185 186 194 protected void initSessionData(HttpPresentationComms comms) 195 throws EnhydraPimPresentationException { 196 this.myComms = comms; 197 198 try { 199 Object obj = comms.sessionData.get(EnhydraPimSessionData.SESSION_KEY); 200 if (null != obj) { 202 this.mySessionData = (EnhydraPimSessionData) obj; 203 } else { 204 this.mySessionData = new EnhydraPimSessionData(); 207 comms.sessionData.set(EnhydraPimSessionData.SESSION_KEY, 208 this.mySessionData); 209 } 210 } catch (KeywordValueException ex) { 211 writeDebugMsg("Problem getting session data from session: " 212 + ex.getMessage()); 213 } 214 } 215 216 217 221 protected void checkForUserLogin() throws ClientPageRedirectException, 222 EnhydraPimPresentationException { 223 224 try { 225 OwnerI user = getUser(); 226 227 if (null == user) { 228 232 writeDebugMsg("USER NOT FOUND IN SESSION"); 233 String requestedPO = myComms.request.getRequestURI(); 235 this.writeDebugMsg("PO: " + requestedPO); 236 writeDebugMsg("REDIRECTING TO LOGIN PAGE"); 238 throw new ClientPageRedirectException(LOGIN_PAGE); 239 } else { 241 writeDebugMsg("USER ALREADY LOGGED IN WITH A SESSION"); 242 } 243 244 } catch (Exception ex) { 245 throw new EnhydraPimPresentationException( 246 "Trouble checking for user login status", ex); 247 } 248 } 249 250 251 258 public void handleEvent(HttpPresentationComms comms) throws Exception { 259 String event = comms.request.getParameter(EVENT); 260 261 XMLObject returnHTML = null; 262 263 try { 264 if (event == null || event.length() == 0 || event.equalsIgnoreCase("none") || event.equalsIgnoreCase("Default")) { 266 returnHTML = handleDefault(); 267 } else { 268 returnHTML = getPageContentForEvent(event); 269 } 270 if(getUser()!=null && getUser().getUsername()!=null) { 272 Element htm =returnHTML.getElementById("LogUserName"); 273 htm.getFirstChild().setNodeValue("<"+getUser().getUsername()+">"); 274 } 275 } catch (Exception e) { 276 e.printStackTrace(); 277 returnHTML = showErrorPage("Presentation Error:"+e.getMessage()); 278 } 279 comms.response.writeDOM(returnHTML); 280 } 281 282 290 public XMLObject getPageContentForEvent(String event) throws Exception { 291 292 try { 293 Method method = this.getClass().getMethod(toMethodName(event), null); 294 XMLObject thePage = (XMLObject) method.invoke(this, null); 295 296 return thePage; 297 } catch (InvocationTargetException ex) { 298 if (ex.getTargetException() instanceof Exception ) { 302 throw (Exception ) ex.getTargetException(); 303 } else if (ex.getTargetException() instanceof Error ) { 304 throw (Error ) ex.getTargetException(); 305 } else { 306 throw ex; 307 } 308 } catch (NoSuchMethodException ex) { 309 throw new EnhydraPimPresentationException( 311 "NO EVENT HANDLER FOUND FOR EVENT: " + event, ex); 312 } catch (IllegalAccessException ex) { 313 throw new EnhydraPimPresentationException( 315 "ILLEGAL ACCESS TO EVENT HANDLER (is it public?): " + event, 316 ex); 317 } 318 } 319 320 328 private String toMethodName(String event) { 329 StringBuffer methodName = new StringBuffer (STANDARD_METHOD_PREFIX); 330 methodName.append(Character.toUpperCase(event.charAt(0))); 331 332 if (event.length() > 1) { 333 methodName.append(event.substring(1)); 334 } 335 336 return methodName.toString(); 337 } 338 339 344 public Enhydrapim getApplication() { 345 return (Enhydrapim) Enhydra.getApplication(); 346 } 347 348 355 public static void writeDebugMsg(String msg) { 356 Enhydra.getLogChannel().write(Logger.DEBUG, msg); 357 } 358 359 360 361 368 public void goToErrorPage(String errorText) throws ClientPageRedirectException, HttpPresentationException{ 369 if(errorText!=null) { 370 this.getSessionData().setUserMessage(errorText); 371 } 372 throw new ClientPageRedirectException(ERROR_PAGE); 373 } 374 375 376 383 public XMLObject showErrorPage(String errorText) throws ClientPageRedirectException, HttpPresentationException{ 384 ErrorHTML errorPage = (ErrorHTML) myComms.xmlcFactory.create(ErrorHTML.class); 385 if(errorText!=null) { 386 errorPage.setTextErrorText(errorText); 387 }else { 388 errorPage.setTextErrorText("Unhandled presentation Error"); 389 } 390 return errorPage; 391 } 392 393 394 395 400 public XMLObject showIndexPage() { 401 return (IndexHTML) myComms.xmlcFactory.create(IndexHTML.class); 402 } 403 404 } | Popular Tags |