ApplicationElement.java
00001 package edu.virtualschool.jwaa.xml;
00002 import java.io.File;
00003 import java.io.IOException;
00004 import java.util.ArrayList;
00005 import java.util.HashMap;
00006 import java.util.Iterator;
00007 import java.util.List;
00008
00009 import org.apache.log4j.Logger;
00010 import org.jdom.Document;
00011 import org.jdom.Element;
00012 import org.jdom.JDOMException;
00013 import org.jdom.input.SAXBuilder;
00014
00015 import edu.virtualschool.jwaa.Config;
00016 import edu.virtualschool.jwaa.Fault;
00017 import edu.virtualschool.jwaa.GenericIdentifiable;
00018 import edu.virtualschool.jwaa.IOFault;
00019 import edu.virtualschool.jwaa.RoleAbstraction;
00020 import edu.virtualschool.jwaa.bean.DemoRole;
00021 import edu.virtualschool.jwaa.dbms.DB;
00022 import edu.virtualschool.jwaa.dbms.DBPool;
00023 import edu.virtualschool.jwaa.field.*;
00024 import edu.virtualschool.jwaa.field.FieldUtil;
00025 import edu.virtualschool.jwaa.field.NameField;
00026 import edu.virtualschool.jwaa.field.TextField;
00027 import edu.virtualschool.jwaa.field.Validatable;
00028
00040 public final class ApplicationElement extends GenericIdentifiable
00041 {
00042 final RootDirectory root;
00043 final FileField fileMonitor;
00044 DBPool dbPool;
00045 VelocityEngine velocityEngine;
00046 NameField name;
00047 TextField description;
00048
00049 final HashMap dictionary = new HashMap();
00050 final HashMap roleMap = new HashMap();
00051 final ArrayList pagesList = new ArrayList();
00052
00053 final static Logger logger = Logger.getLogger(ApplicationElement.class.getName());
00054 final static String innerHomePageID = "INNER";
00055 final static String outerHomePageID = "OUTER";
00056 final static String portalPageID = "PORTAL";
00057 final static String notFoundPageID = "NOTFOUND";
00058 final static String faultPageID = "FAULT";
00059 final static String refusePageID = "DENY";
00060 final static String[] requiredPages = new String[] {
00061 innerHomePageID,
00062 outerHomePageID,
00063 portalPageID,
00064 notFoundPageID,
00065 faultPageID,
00066 refusePageID,
00067 };
00068
00069 ApplicationElement(RootDirectory root, File file) throws Fault
00070 {
00071 super(file.getParentFile().getName());
00072 this.root = root;
00073 this.fileMonitor = new FileField(file);
00074 }
00075 void update() throws Fault
00076 {
00077 if (!fileMonitor.isUpToDate())
00078 {
00079 logger.info("loading application " + fileMonitor);
00080 try
00081 {
00082 SAXBuilder builder = new SAXBuilder();
00083 Document dom = builder.build(fileMonitor.toString());
00084 Element element = dom.getRootElement();
00085 this.name = new NameField(element.getAttributeValue("name"), "");
00086 this.description = new TextField(element.getAttributeValue("description"));
00087 this.velocityEngine = new VelocityEngine(this, element.getChild("macros"));
00088
00089 Element jdbcElement = element.getChild("jdbc", element.getNamespace());
00090 if (jdbcElement == null)
00091 throw new ValidationFault("application element requires a jdbc element");
00092 this.dbPool = new DBPool(
00093 jdbcElement.getAttributeValue("url"),
00094 jdbcElement.getAttributeValue("user"),
00095 jdbcElement.getAttributeValue("password"),
00096 jdbcElement.getAttributeValue("connections"),
00097 new String[] { jdbcElement.getAttributeValue("driver") }
00098 );
00099
00100 dictionary.clear();
00101 Element dictElement = element.getChild("dictionary", element.getNamespace());
00102 if (dictElement != null)
00103 {
00104 List dictWords = dictElement.getChildren("def", dictElement.getNamespace());
00105 for (Iterator iterator = dictWords.iterator(); iterator.hasNext();)
00106 {
00107 Element wordElement = (Element) iterator.next();
00108 String key = wordElement.getAttributeValue("id");
00109 String value = wordElement.getTextNormalize();
00110 addToDictionary(key, value);
00111 }
00112 }
00113 else
00114 logger.info("Application found no dictionary element");
00115
00116 roleMap.clear();
00117 List roles = element.getChildren("role", element.getNamespace());
00118 for (Iterator iterator = roles.iterator(); iterator.hasNext();)
00119 {
00120 Element e = (Element) iterator.next();
00121 String roleID = e.getAttributeValue("id");
00122 if (roleID == null)
00123 throw new ValidationFault("role requires an id attribute");
00124 String personID = e.getAttributeValue("person");
00125 if (personID == null)
00126 throw new ValidationFault("role requires a person attribute");
00127 DemoRole role = DemoRole.findRole(roleID);
00128 if (role == null)
00129 throw new ValidationFault(roleID + " is not a valid role id");
00130 roleMap.put(personID, role);
00131 }
00132 FieldUtil.validateFields(new Validatable[] { fileMonitor, name, description});
00133 pagesList.clear();
00134 List list = element.getChildren("pages", element.getNamespace());
00135 for (Iterator iterator = list.iterator(); iterator.hasNext();)
00136 {
00137 Element e = (Element) iterator.next();
00138 String id = e.getAttributeValue("id");
00139 if (id == null)
00140 throw new ValidationFault("pages requires an id attribute");
00141 if (findPages(id) != null)
00142 throw new ValidationFault(this + " contains a multiply defined pages id: "+id);
00143 PagesElement pages = new PagesElement(this, e);
00144 this.pagesList.add(pages);
00145 }
00146 fileMonitor.update();
00147 }
00148 catch (IOException e)
00149 {
00150 throw new IOFault("Couldn't read file: " + fileMonitor, e);
00151 }
00152 catch (JDOMException e)
00153 {
00154 throw new ValidationFault("XML Validation fault in file" + fileMonitor + "\n\t" + e.getMessage());
00155 }
00156 }
00157 for (Iterator iterator = pagesList.iterator(); iterator.hasNext(); )
00158 {
00159 PagesElement pages = (PagesElement)iterator.next();
00160 pages.update();
00161 }
00162 for (Iterator iterator = pagesList.iterator(); iterator.hasNext(); )
00163 {
00164 PagesElement pages = (PagesElement)iterator.next();
00165 pages.commit();
00166 }
00167 for (int i = 0; i < requiredPages.length; i++)
00168 {
00169 String id = requiredPages[i];
00170 if (findPage(id) == null)
00171 throw new ValidationFault("applications must define a page with id "+id);
00172 }
00173 }
00174 public void addToDictionary(String key, String value)
00175 throws ValidationFault
00176 {
00177 if (key == null)
00178 throw new ValidationFault("dictionary word requires an id attribute");
00179 if (dictionary.containsKey(key))
00180 throw new ValidationFault(key+" is multiply defined in application dictionary");
00181 dictionary.put(key, value);
00182 }
00183 public PageElement findPage(String id)
00184 {
00185 for (Iterator iterator = pagesList.iterator(); iterator.hasNext(); )
00186 {
00187 PagesElement pages = (PagesElement)iterator.next();
00188 PageElement page = pages.findPage(id);
00189 if (page != null)
00190 return page;
00191 }
00192 return null;
00193 }
00194 public PagesElement findPages(String id)
00195 {
00196 for (Iterator iterator = pagesList.iterator(); iterator.hasNext(); )
00197 {
00198 PagesElement pages = (PagesElement)iterator.next();
00199 if (pages.id.equals(id))
00200 return pages;
00201 }
00202 return null;
00203 }
00204 public RoleAbstraction findRole(String personIdentifier)
00205 {
00206 Object o = roleMap.get(personIdentifier);
00207 if (o == null)
00208 return null;
00209 else
00210 return (RoleAbstraction)o;
00211 }
00212 PageElement validateRequiredPage(String id)
00213 throws ValidationFault
00214 {
00215 PageElement page = findPage(id);
00216 if (page == null)
00217 throw new ValidationFault(this+" requires a page with "+id+" id");
00218 return page;
00219 }
00220 public NameField getName()
00221 {
00222 return name;
00223 }
00224 public TextField getDescription()
00225 {
00226 return description;
00227 }
00228 public VelocityEngine getVelocityEngine()
00229 {
00230 return velocityEngine;
00231 }
00232 public String getDirectory()
00233 {
00234 return fileMonitor.file.getParent();
00235 }
00236 public String getFilePath()
00237 {
00238 return fileMonitor.toString();
00239 }
00240 public RootDirectory getRoot()
00241 {
00242 return root;
00243 }
00244 public PageElement getOuterHomePage()
00245 {
00246 return findPage(outerHomePageID);
00247 }
00248 public PageElement getInnerHomePage()
00249 {
00250 return findPage(innerHomePageID);
00251 }
00252 public PageElement getFaultPage()
00253 {
00254 return findPage(faultPageID);
00255 }
00256 public PageElement getNotFoundPage()
00257 {
00258 return findPage(notFoundPageID);
00259 }
00260 public PageElement getRefusePage()
00261 {
00262 return findPage(refusePageID);
00263 }
00264 public HashMap getDictionary()
00265 {
00266 return dictionary;
00267 }
00268 public DB getDBMS() throws DBPool.Fault, Config.Fault, DB.Fault
00269 {
00270 return new DB(dbPool);
00271 }
00272 public Object getLogger()
00273 {
00274 return logger;
00275 }
00276 }
00277