Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

MetaPage.java

00001 package edu.virtualschool.jwaa;
00002 
00003 import java.io.IOException;
00004 import java.io.UnsupportedEncodingException;
00005 import java.net.URLEncoder;
00006 import java.util.Iterator;
00007 
00008 import javax.servlet.RequestDispatcher;
00009 import javax.servlet.ServletException;
00010 import javax.servlet.http.HttpServletResponse;
00011 
00012 import org.apache.log4j.Logger;
00013 
00018 public class MetaPage extends TreeNode 
00019 {
00020   final Class pageClass;
00021   final String defaultAnchor;
00022   final String defaultTitle;
00023   final GenericURI link;
00024   boolean isSelected = false;
00025   final RoleAbstraction requiredRole;
00026 
00027   private final static Logger logger = Logger.getLogger(MetaPage.class.getName());
00038   public MetaPage( Class pageClass, String url, String anchor,
00039     String title, MetaPage[] children)
00040   {
00041     this(pageClass, url, anchor, title, null, children);
00042   }
00043   public MetaPage(Class pageClass, String url, String anchor, String title)
00044   {
00045     this(pageClass, url, anchor, title, null, null);
00046   }
00047   public MetaPage( Class pageClass, String url, String anchor,
00048     String title, RoleAbstraction requiredRole)
00049   {
00050     this(pageClass, url, anchor, title, requiredRole, null);
00051   }
00052   public MetaPage(Class pageClass, String url, String anchor,
00053     String title, RoleAbstraction requiredRole, MetaPage[] children)
00054   {
00055     if (url == null)
00056       url = "";
00057     if (anchor == null)
00058       anchor = url;
00059     if (title == null)
00060       title = anchor;
00061     this.defaultAnchor = anchor;
00062     this.defaultTitle = title;
00063     this.link = new GenericURI.DynamicPageRef(url);
00064     this.pageClass = pageClass;
00065     this.requiredRole = requiredRole;
00066     if (pageClass == null)
00067       throw new RuntimeFault(this +" has null pageClass");
00068     if (children != null)
00069     {
00070       for (int i = 0; i < children.length; i++)
00071         addChildNode(children[i]);
00072     }
00073   }
00074   public boolean isSelected()
00075   {
00076     return isSelected;
00077   }
00078   public void setSelected(boolean yesno)
00079   {
00080     isSelected = yesno;
00081   }
00082   public GenericURI getLink()
00083   {
00084     return link;
00085   }
00089   public final String getAnchor()
00090   {
00091     return defaultAnchor;
00092   }
00097   public final String getTitle()
00098   {
00099     return defaultTitle;
00100   }
00101   
00106   public final RoleAbstraction getRequiredRole() { return requiredRole; }
00112   public final MetaPage nextPage()
00113   {
00114     Iterator iterator = childrenIterator();
00115     if (iterator.hasNext()) 
00116       return (MetaPage)iterator.next();
00117 
00118     MetaPage sibling = (MetaPage)nextSiblingNode();
00119     if (sibling != null) 
00120       return sibling;
00121 
00122     MetaPage parent = (MetaPage)getParentNode();
00123     if (parent != null) 
00124       return (MetaPage)parent.nextSiblingNode();
00125     else 
00126       return null;
00127   }
00133   public final MetaPage prevPage()
00134   {
00135     MetaPage sibling = (MetaPage)prevSiblingNode();
00136     if (sibling != null) return sibling;
00137     return (MetaPage)getParentNode();
00138   }
00148   public boolean allowsAccessBy(AccountAbstraction person)
00149   {
00150     boolean allowed = false;
00151     if (requiredRole == null)
00152       allowed = true;
00153     else if (person == null)
00154       allowed = false;
00155     else if (person.isNull())
00156       allowed = false;
00157     else 
00158       allowed = person.hasRole(requiredRole);
00159     if (!allowed)
00160       logger.warn(this + " denied access by person " + person);
00161     return allowed;
00162   }
00167 //  public final String toString()
00168 //  {
00169 //    return "PageElement:"+link.getText();
00170 //  }
00171   String encodeRedirectURL(GenericPage ctlr, String uri)
00172   {
00173     HttpServletResponse rsp = ctlr.httpResponse;
00174     String encodedURL = rsp.encodeRedirectURL(uri);
00175     return encodedURL;
00176   }
00177   String encodeURL(GenericPage ctlr, String uri)
00178   {
00179     HttpServletResponse rsp = ctlr.httpResponse;
00180     String encodedURL = rsp.encodeURL(uri);
00181     return encodedURL;
00182   }
00183   String composeURIAndArgs(String uri, Object[] args)
00184   {
00185     StringBuffer buf = new StringBuffer(uri);
00186     if (args != null)
00187     {
00188       buf.append("?");
00189       for (int i = 0; i < args.length; i += 2)
00190       {
00191         try
00192         {
00193           buf.append(args[i]);
00194           buf.append("=");
00195           if (args[i+1] != null)
00196             buf.append(URLEncoder.encode(args[i + 1].toString(), "UTF-8"));
00197           if (i+2 < args.length-1)
00198             buf.append("&amp;");
00199         }
00200         catch (UnsupportedEncodingException e)
00201         {
00202           throw new RuntimeFault("Unsupported encoding: UTF-8", e);
00203         }
00204       }
00205     }
00206     return buf.toString();
00207   }
00208   public String emitLink(GenericPage ctlr, Object a, Object t, Object[] args)
00209   {
00210     String title = (t == null) ? this.defaultTitle : t.toString();
00211     String anchor = (a == null) ? this.defaultAnchor : a.toString();
00212     String uri = link.getText();
00213     String encodedURI = encodeURL(ctlr, uri);
00214     String href = composeURIAndArgs(encodedURI, args);
00215     return "<a href=\"" + href  + "\" title=\"" + title + "\">" + anchor  + "</a>";
00216   }
00217   public String emitForm(GenericPage ctlr, Object[] args)
00218   {
00219     String uri = link.text;
00220     String encodedURI = encodeURL(ctlr, uri);
00221     String href = composeURIAndArgs(encodedURI, args);
00222     return "<form action=\""  + href + "\""
00223     + " method=\"post\" enctype=\"application/x-www-form-urlencoded\">\n";
00224   }
00225   public final void forward(GenericPage ctlr)
00226     throws IOFault, IgnorableFault, ServletFault
00227   {
00228     RequestDispatcher dispatcher =
00229       ctlr.servlet.getRequestDispatcher(link.getText());
00230     // logger.debug(" dispatcher: " + dispatcher + "");
00231     try
00232     {
00233       dispatcher.forward(ctlr.httpRequest, ctlr.httpResponse);
00234       throw new IgnorableFault(); // don't return to caller
00235     }
00236     catch (ServletException e)
00237     {
00238       throw new ServletFault(e, e);
00239     }
00240     catch (IOException e)
00241     {
00242       throw new IOFault(e, e);
00243     }
00244   }
00245 }