1 23 24 package transactionsDiscRack.presentation; 25 26 import com.lutris.appserver.server.httpPresentation.*; 27 import com.lutris.appserver.server.Enhydra; 28 import com.lutris.logging.*; 31 import com.lutris.util.KeywordValueException; 32 import com.lutris.appserver.server.user.User; 33 34 import org.enhydra.xml.xmlc.XMLObject; 35 import org.w3c.dom.*; 36 import org.w3c.dom.html.HTMLElement; 37 38 import java.lang.reflect.*; 39 import java.util.*; 40 import java.lang.Throwable ; 41 42 import transactionsDiscRack.spec.*; 43 import transactionsDiscRack.*; 44 45 55 public abstract class BasePO implements HttpPresentation { 56 protected static final String USER_KEY = "DiscRackPerson"; 57 protected static String LOGIN_PAGE = "personMgmt/Login.po"; 58 protected static String DISC_CATALOG_PAGE = "discMgmt/DiscCatalog.po"; 59 private static String EVENT = "event"; 60 private static String STANDARD_METHOD_PREFIX = "handle"; 61 62 72 public abstract XMLObject handleDefault() throws HttpPresentationException; 73 74 79 protected abstract boolean loggedInUserRequired(); 80 81 84 protected HttpPresentationComms myComms = null; 85 protected TransactionsDiscRackSessionData mySessionData = null; 86 87 90 protected Person myPerson = null; 91 92 98 public HttpPresentationComms getComms() { 99 return this.myComms; 100 } 101 102 107 public TransactionsDiscRackSessionData getSessionData() { 108 return this.mySessionData; 109 } 110 111 117 public void setUser(Person thePerson) 118 throws TransactionsDiscRackPresentationException { 119 this.getSessionData().setUser(thePerson); 120 } 121 122 127 public Person getUser() { 128 return this.getSessionData().getUser(); 129 } 130 131 134 public void removeUserFromSession() { 135 this.getSessionData().removeUser(); 136 } 137 138 144 public void run(HttpPresentationComms comms) 145 throws Exception { 146 initSessionData(comms); 148 if(this.loggedInUserRequired()) { 150 checkForUserLogin(); 151 } 152 handleEvent(comms); 154 } 155 156 163 protected void initSessionData(HttpPresentationComms comms) 164 throws TransactionsDiscRackPresentationException { 165 this.myComms = comms; 166 167 try { 168 Object obj = comms.sessionData.get(TransactionsDiscRackSessionData.SESSION_KEY); 169 if(null != obj) { 171 this.mySessionData = (TransactionsDiscRackSessionData)obj; 172 } else { 173 this.mySessionData = new TransactionsDiscRackSessionData(); 175 comms.sessionData.set(TransactionsDiscRackSessionData.SESSION_KEY, this.mySessionData); 176 } 177 } catch(KeywordValueException ex) { 178 writeDebugMsg("Problem getting session data from session: " + 179 ex.getMessage()); 180 } 181 } 182 183 186 protected void checkForUserLogin() 187 throws ClientPageRedirectException, TransactionsDiscRackPresentationException { 188 189 try { 190 Person user = getUser(); 191 192 if (null == user) { 193 String uri = myComms.request.getRequestURI(); 195 boolean is= uri.startsWith("/TransactionsDiscRack_pres"); 196 197 if(!is) 198 { 199 writeDebugMsg("USER NOT FOUND IN SESSION"); 200 String requestedPO = myComms.request.getRequestURI(); 202 this.writeDebugMsg("PO: "+ requestedPO); 203 writeDebugMsg("REDIRECTING TO LOGIN PAGE"); 205 throw new ClientPageRedirectException(getComms().request.getApplicationPath()+LOGIN_PAGE); 206 } 207 } else { 208 writeDebugMsg("USER ALREADY LOGGED IN WITH A SESSION"); 209 } 210 } catch (Exception ex) { 211 throw new TransactionsDiscRackPresentationException("Trouble checking for user login status", ex); 212 } 213 } 214 215 221 public void handleEvent(HttpPresentationComms comms) 222 throws Exception { 223 String event = comms.request.getParameter(EVENT); 224 225 XMLObject returnHTML = null; 226 227 if (event == null || event.length() == 0) { 228 returnHTML = handleDefault(); 229 } 230 else { 231 returnHTML = getPageContentForEvent(event); 232 } 233 comms.response.writeDOM(returnHTML); 234 } 235 236 243 public XMLObject getPageContentForEvent(String event) 244 throws Exception { 245 try { 246 Method method = this.getClass().getMethod(toMethodName(event), null); 247 XMLObject thePage = (XMLObject)method.invoke(this, null); 248 return thePage; 249 250 } catch(InvocationTargetException ex) { 251 if (ex.getTargetException() instanceof Exception ) { 254 throw (Exception )ex.getTargetException(); 255 } else if (ex.getTargetException() instanceof Error ) { 256 throw (Error )ex.getTargetException(); 257 } else { 258 throw ex; 259 } 260 } catch(NoSuchMethodException ex) { 261 throw new TransactionsDiscRackPresentationException("NO EVENT HANDLER FOUND FOR EVENT: " + 263 event, ex); 264 } catch(IllegalAccessException ex) { 265 throw new TransactionsDiscRackPresentationException("ILLEGAL ACCESS TO EVENT HANDLER (is it public?): " + 267 event, ex); 268 } 269 } 270 271 278 private String toMethodName(String event) { 279 StringBuffer methodName = new StringBuffer (STANDARD_METHOD_PREFIX); 280 methodName.append(Character.toUpperCase(event.charAt(0))); 281 282 if (event.length() > 1) { 283 methodName.append(event.substring(1)); 284 } 285 286 return methodName.toString(); 287 } 288 289 295 public TransactionsDiscRack getApplication() { 296 return (TransactionsDiscRack)Enhydra.getApplication(); 297 } 298 299 305 public static void writeDebugMsg(String msg) { 306 Enhydra.getLogChannel().write(Logger.DEBUG,msg); 307 } 308 } 309 | Popular Tags |