Controller.java
00001 package edu.virtualschool.jwaa.xml;
00002
00003 import java.util.ArrayList;
00004 import java.util.HashMap;
00005 import java.util.Iterator;
00006
00007 import org.apache.log4j.Logger;
00008 import org.apache.velocity.VelocityContext;
00009
00010 import edu.virtualschool.VirtualSchoolNavigationBar;
00011 import edu.virtualschool.jwaa.AccountAbstraction;
00012 import edu.virtualschool.jwaa.Fault;
00013 import edu.virtualschool.jwaa.GenericPage;
00014 import edu.virtualschool.jwaa.IgnorableFault;
00015 import edu.virtualschool.jwaa.bean.DemoAccountBean;
00016 import edu.virtualschool.jwaa.dbms.DB;
00017
00067 public class Controller extends GenericPage
00068 {
00069 DB dbms;
00070 PageElement page;
00071 HashMap dictionary;
00072 final HashMap param = new HashMap();
00073 final VelocityContext context = new VelocityContext();
00074
00075 final static Logger logger = Logger.getLogger(Controller.class.getName());
00076 public final static edu.virtualschool.jwaa.MetaPage meta = new edu.virtualschool.jwaa.MetaPage(
00077 Controller.class,
00078 "/jwaa/xml",
00079 "Controller",
00080 "Jwaa XML Page Controller"
00081 );
00082
00083 public Controller()
00084 {
00085 }
00086 public final void run() throws Fault
00087 {
00088 String path = httpRequest.getPathInfo();
00089
00090 JwaaServlet servlet = (JwaaServlet)this.servlet;
00091 RootDirectory root = servlet.getRoot();
00092 this.page = root.findPath(path);
00093 if (page == null)
00094 throw new Fault(path+" is not a valid application");
00095
00096 ApplicationElement app = page.getApplication();
00097 AccountAbstraction account = getAccount();
00098 if (!page.allowsAccessBy(account))
00099 page = app.getRefusePage();
00100
00101 try
00102 {
00103 this.dbms = app.getDBMS();
00104 this.dictionary = app.getDictionary();
00105
00106 GenericForm form = page.getFormInstance(this);
00107 this.context.put("self", this);
00108 this.context.put("account", account);
00109 this.context.put("dbms", dbms);
00110 this.context.put("form", form);
00111
00112 VelocityEngine engine = app.getVelocityEngine();
00113 String pageText = page.getText();
00114 String evalText = engine.evaluate(pageText, this.context);
00115 send(evalText);
00116 dbms.commit();
00117 }
00118 catch (Fault e)
00119 {
00120 logger.error(e, e);
00121 if (dbms != null)
00122 dbms.rollback();
00123 throw e;
00124 }
00125 finally
00126 {
00127 if (dbms != null)
00128 dbms.close();
00129 }
00130 }
00131 public final void fault(String text, Throwable e)
00132 {
00133 logger.error(e, e);
00134 try
00135 {
00136 if (page != null)
00137 {
00138 PageElement faultPage = page.getApplication().getFaultPage();
00139 context.put("message", text);
00140 context.put("fault", e);
00141 VelocityEngine engine = page.getApplication().getVelocityEngine();
00142 String pageText = "#header()\n"+faultPage.text+"\n#footer()";
00143 String evalText = engine.evaluate(pageText, context);
00144 send(evalText);
00145 }
00146 else
00147 send("<h1 align=\"center\">Whups!</h1><p>"+e.getMessage()+"</p>\n");
00148 }
00149 catch (Throwable e1)
00150 {
00151 logger.error(e1, e1);
00152 }
00153 }
00154 public final void gotoPage(String pageID)
00155 {
00156 PageElement page = findPage(pageID);
00157 if (page == null)
00158 {
00159 logger.error("page "+pageID+" not found");
00160 return;
00161 }
00162 try
00163 {
00164 if (page == null)
00165 logger.error("gotoPage(null) ignored");
00166 else
00167 redirect(page);
00168 }
00169 catch (IgnorableFault e) {}
00170 catch (Throwable e)
00171 {
00172 logger.error(e, e);
00173 }
00174 }
00180 public final PageElement findPage(String id)
00181 {
00182 return getApplication().findPage(id);
00183 }
00188 public final ApplicationElement getApplication()
00189 {
00190 return page.getApplication();
00191 }
00196 public final PageElement getPage()
00197 {
00198 return page;
00199 }
00204 public final DB getDB()
00205 {
00206 return dbms;
00207 }
00212 static final VirtualSchoolNavigationBar topRow = new VirtualSchoolNavigationBar("JWAA");
00213 public String getNavigationBar()
00214 {
00215 int row=1;
00216 AccountAbstraction account = getAccount();
00217 StringBuffer buf = new StringBuffer();
00218 ArrayList pathToRoot = page.getRootPath();
00219 pathToRoot.add(topRow);
00220 for (int i = pathToRoot.size()-1; i >= 0; i--)
00221 {
00222 Object object = pathToRoot.get(i);
00223 edu.virtualschool.jwaa.MetaPage parent = (edu.virtualschool.jwaa.MetaPage)object;
00224 StringBuffer rowBuf = new StringBuffer();
00225 for (Iterator it = parent.childrenIterator(); it.hasNext(); )
00226 {
00227 edu.virtualschool.jwaa.MetaPage child = (edu.virtualschool.jwaa.MetaPage)it.next();
00228 if (child == null)
00229 continue;
00230 String anchor = child.getAnchor();
00231 if (anchor == null || anchor.equals(""))
00232 continue;
00233 else if (child.allowsAccessBy(account))
00234 {
00235 rowBuf.append((child.isSelected() || pathToRoot.contains(child)) ? " <td class=\"highlight\">" : " <td>");
00236 rowBuf.append(emitLink(child));
00237 rowBuf.append("</td>\n");
00238 }
00239 }
00240 if (!rowBuf.toString().equals(""))
00241 buf.append(
00242 "<table class=\"row"+row+"\" width=\"100%\">\n"+
00243 " <tr>\n"+rowBuf+"\n </tr>\n"+
00244 "</table>\n");
00245 row++;
00246 }
00247 return buf.toString();
00248 }
00249 public final PageElement getNextPage()
00250 {
00251 ArrayList siblings = page.pages.getPages();
00252 int index = siblings.indexOf(page);
00253 if (index < siblings.size() - 1)
00254 return (PageElement) siblings.get(index + 1);
00255 else
00256 return page.getApplication().getInnerHomePage();
00257 }
00258 public final PageElement getPrevPage()
00259 {
00260 ArrayList siblings = page.pages.getPages();
00261 int index = siblings.indexOf(page);
00262 if (index >= 1)
00263 return (PageElement) siblings.get(index - 1);
00264 else
00265 return page.getApplication().getInnerHomePage();
00266 }
00271 public String link(String key)
00272 {
00273 String uri = (String)dictionary.get(key);
00274 if (uri != null)
00275 return (String)httpResponse.encodeURL(uri);
00276 logger.error(key + " not found in application dictionary");
00277 return "<b>("+key+" not found in dictionary)</b>";
00278 }
00283 public final void logout()
00284 {
00285 super.setAccount(DemoAccountBean.Null);
00286 }
00287 }
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339