Initial revision
[mir.git] / source / mir / misc / HTMLTemplateProcessor.java
diff --git a/source/mir/misc/HTMLTemplateProcessor.java b/source/mir/misc/HTMLTemplateProcessor.java
new file mode 100755 (executable)
index 0000000..70ec150
--- /dev/null
@@ -0,0 +1,343 @@
+/*
+ * put your module comment here
+ */
+
+
+package mir.misc;
+
+import  java.lang.*;
+import  java.util.*;
+import  java.io.*;
+import  java.net.*;
+import  freemarker.template.*;
+import  mir.entity.*;
+import  mir.storage.*;
+
+
+/**
+ * Hilfsklasse zum Mergen von Template und Daten
+ */
+public final class HTMLTemplateProcessor {
+
+    public static String                templateDir;
+               private static FileTemplateCache    templateCache;
+               private static Logfile                theLog;
+               private static String                 docRoot;
+               private static String                 actionRoot;
+               private static String                 productionHost;
+               private static String               audioHost;
+               private static String               videoHost;
+               private static String               imageHost;
+               private static String                             openAction;
+    protected static String producerDocRoot = Configuration.getProperty("Producer.DocRoot");
+  protected static String producerStorageRoot = Configuration.getProperty("Producer.StorageRoot");
+
+
+               //
+               // Initialisierung
+
+               static {
+      templateDir = Configuration.getProperty("Home") + Configuration.getProperty("HTMLTemplateProcessor.Dir");
+                       templateCache = new FileTemplateCache(templateDir);
+                       templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND);
+                       //templateCache.startAutoUpdate();
+                       theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("HTMLTemplateProcessor.Logfile"));
+                       docRoot = Configuration.getProperty("HTMLTemplateProcessor.DocRoot");
+                       actionRoot = Configuration.getProperty("HTMLTemplateProcessor.ActionRoot");
+                       openAction = Configuration.getProperty("Producer.OpenAction");
+                       productionHost = Configuration.getProperty("Producer.ProductionHost");
+                       videoHost = Configuration.getProperty("Producer.VideoHost");
+                       audioHost = Configuration.getProperty("Producer.AudioHost");
+                       imageHost = Configuration.getProperty("Producer.Image.Host");
+      producerDocRoot = Configuration.getProperty("Producer.DocRoot");
+      producerStorageRoot = Configuration.getProperty("Producer.StorageRoot");
+
+
+               }
+
+  /**
+        * Privater Konstruktor, um versehentliche Instantiierung zu verhindern
+        */
+       private HTMLTemplateProcessor () {
+       }
+
+
+               //
+               //  process-Methoden zum Mischen verschiedener Datenstrukturen mit HTML-Templates
+
+
+       /**
+        * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
+        * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
+        * <code>out</code>
+        *
+        * @param templateFilename
+        * @param anEntity
+        * @param out
+        * @exception HTMLParseException
+        */
+
+               public static void process(String templateFilename, Entity anEntity, PrintWriter out)
+                       throws HTMLParseException {
+                               if (anEntity == null)  throw new HTMLParseException("Entity leer!");
+                               else process(templateFilename, anEntity, out);
+               }
+
+
+       /**
+        * Wandelt Liste mit Entities <code>entList</code> in freemarker-Struktur um, mischt die Daten mit
+        * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
+        * <code>out</code>
+        *
+        * @param templateFilename
+        * @param entList
+        * @param out
+        * @exception HTMLParseException
+        */
+               public static void process(String templateFilename, EntityList entList, PrintWriter out)
+                       throws HTMLParseException {
+                       process( templateFilename,  entList,  (String)null, (TemplateModelRoot)null,  out);
+               }
+
+       /**
+        * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
+        * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
+        * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
+        * <code>out</code>
+        *
+        * @param templateFilename
+        * @param entList
+        * @param additionalModelName
+        * @param additionalModel
+        * @param out
+        * @exception HTMLParseException
+        */
+               public static void process(String templateFilename, EntityList entList, String additionalModelName,
+                                                TemplateModelRoot additionalModel, PrintWriter out)
+                       throws HTMLParseException {
+
+                       SimpleHash modelRoot = new SimpleHash();
+
+                       if (entList == null) {
+                                //theLog.printInfo("Keine Daten! Suche erfolglos.");
+                                process(templateFilename, modelRoot, out);
+                       } else {
+                               try {
+                                       modelRoot = makeSimpleHashWithEntitylistInfos(entList);
+                                       //
+                                       // Hilfskruecke um mal ein Popup mit reinzunhemen ..
+                                       if (additionalModelName != null && additionalModel != null)
+                                                       modelRoot.put(additionalModelName, additionalModel);
+
+                                       process(templateFilename, modelRoot, out);
+                               } catch (StorageObjectException e) {
+                                       throw new HTMLParseException(e.toString());
+                               }
+                       }
+               }
+
+       /**
+        * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
+        * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
+        * <code>out</code>
+        *
+        * @param templateFilename
+        * @param mergeData
+        * @param out
+        * @exception HTMLParseException
+        */
+               public static void process(String templateFilename, HashMap mergeData, PrintWriter out)
+                       throws HTMLParseException {
+                       process(templateFilename, makeSimpleHash(mergeData), out);
+               }
+
+       /**
+        * Gibt Template <code>templateFilename</code> an den PrintWriter
+        * <code>out</code>
+        *
+        * @param templateFilename
+        * @param mergeData
+        * @param out
+        * @exception HTMLParseException
+        */
+               public static void process(String templateFilename, PrintWriter out)
+                       throws HTMLParseException {
+                       process(templateFilename, (TemplateModelRoot)null, out);
+               }
+
+
+       /**
+        * Mischt die freemarker-Struktur <code>tmr</code> mit
+        * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
+        * <code>out</code>
+        *
+        * @param templateFilename
+        * @param mergeData
+        * @param out
+        * @exception HTMLParseException
+        */
+               public static void process(String templateFilename, TemplateModelRoot tmr, PrintWriter out)
+                       throws HTMLParseException {
+                       if (out==null) throw new HTMLParseException("KEIN OUTPUTSTREAM");
+                       Template tmpl = getTemplateFor(templateFilename);
+                       if (tmpl == null) throw new HTMLParseException("KEIN TEMPLATE: " + templateFilename);
+                       if (tmr==null) tmr = new SimpleHash();
+
+      //some utilities
+      SimpleHash utilityHash = new SimpleHash();
+      utilityHash.put("htmlEscape",new freemarker.template.utility.HtmlEscape());
+
+      // put standard configuration into tempalteRootmodel
+                       SimpleHash configHash = new SimpleHash();
+                       configHash.put("docroot", new SimpleScalar(producerDocRoot));
+                       configHash.put("storageroot", new SimpleScalar(producerStorageRoot));
+                       configHash.put("productionhost", new SimpleScalar(productionHost));
+                       configHash.put("openaction", new SimpleScalar(openAction));
+
+
+      tmr.put("docRoot", new SimpleScalar(docRoot));
+                       tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
+                       tmr.put("actionRoot", new SimpleScalar(actionRoot));
+                       tmr.put("openAction", new SimpleScalar(openAction));
+                       tmr.put("productionHost", new SimpleScalar(productionHost));
+                       tmr.put("videoHost", new SimpleScalar(videoHost));
+                       tmr.put("audioHost", new SimpleScalar(audioHost));
+                       tmr.put("imageHost", new SimpleScalar(imageHost));
+
+      tmr.put("config", configHash);
+      tmr.put("utility",utilityHash);
+                       tmpl.process(tmr, out);
+
+               }
+
+
+  /**
+        *   Wandelt eine Entity-Liste in eine SimpleList von SimpleHashes um.
+        *   @param aList ist eine Liste von Entity
+        *   @return eine freemarker.template.SimpleList von SimpleHashes.
+        */
+       public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
+       {
+               SimpleList      simpleList = new SimpleList();
+               if (aList != null) {
+                       for(int i=0;i<aList.size();i++){
+                               simpleList.add(makeSimpleHash(aList.elementAt(i)));
+                       }
+               }
+               return simpleList;
+       }
+
+       /**
+        *  Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
+        *  sind die einzelnen Entities ueber ihre id zu erreichen.
+        *  @param aList ist die EntityList
+        *  @return SimpleHash mit den entsprechenden freemarker Daten
+        *
+        */
+       public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
+       {
+               SimpleHash      simpleHash = new SimpleHash();
+               Entity          currentEntity;
+
+               if (aList != null) {
+                       for (int i=0;i<aList.size();i++) {
+                                currentEntity = (Entity)aList.elementAt(i);
+                                simpleHash.put(currentEntity.getId(), makeSimpleHash(currentEntity));
+                       }
+               }
+               return simpleHash;
+       }
+
+       /**
+        *  Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
+        *  @param entity ist die Entity
+        *  @return SimpleHash mit den entsprechenden freemarker Daten
+        *
+        */
+               public static SimpleHash makeSimpleHash(Entity entity) {
+                       if (entity != null) {
+                               return makeSimpleHash(entity.getValues());
+                       }
+                       else {
+                               //theLog.printWarning("Entity ist <null>");
+                               return null;
+                       }
+               }
+
+       /**
+        *  Konvertiert ein Hashtable mit den keys und values als String
+        *  in ein freemarker.template.SimpleHash-Modell
+        *  @param mergeData der HashMap mit den String / String Daten
+        *  @return SimpleHash mit den entsprechenden freemarker Daten
+        *
+        */
+       public static SimpleHash makeSimpleHash(HashMap mergeData)
+       {
+               SimpleHash modelRoot = new SimpleHash();
+               String aField;
+               if (mergeData != null) {
+                       Set set = mergeData.keySet();
+                       Iterator it =  set.iterator();
+                       for (int i=0; i<set.size();i++)  {
+                               aField = (String)it.next();
+                               modelRoot.put(aField, (String)mergeData.get(aField));
+                       }
+               }
+               return modelRoot;
+       }
+
+
+       /**
+        * Wandelt EntityListe in SimpleHash um, und versieht die Liste mit StandardInfos,
+        * die aus EntityList kommen.
+        *
+        * @param entList
+        * @return SimpleHash mit Entity-Daten und ZusatzInfos.
+        * @exception StorageObjectException
+        */
+               public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
+                       SimpleHash modelRoot = new SimpleHash();
+                       if (entList!=null) {
+                                                               modelRoot.put("contentlist", makeSimpleList(entList));
+                                                               modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
+                                       if (entList.getWhere()!=null) {
+                                               modelRoot.put("where", new SimpleScalar(entList.getWhere()));
+                                               modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
+                                       }
+
+                                       if(entList.getOrder()!=null) {
+                                               modelRoot.put("order", new SimpleScalar(entList.getOrder()));
+                                               modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
+                                       }
+                                       modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
+                                       modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
+
+                                       if (entList.hasNextBatch())
+                                               modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
+                                       if (entList.hasPrevBatch())
+                                               modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
+                       }
+                       return modelRoot;
+               }
+
+       /**
+        * Private Methode, um für templateFilename das Template zu bekommen.
+        * @param templateFilename
+        * @return Template
+        * @exception HTMLParseException
+        */
+       private static Template getTemplateFor(String templateFilename) throws HTMLParseException
+               {
+                       if (templateFilename!=null) return templateCache.getTemplate(templateFilename);
+                       else {
+                               theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
+                               throw new HTMLParseException("Templatedatei: "+ templateFilename + " nicht gefunden!");
+                       }
+               }
+
+
+    public static void stopAutoUpdate(){
+      templateCache.stopAutoUpdate();
+      templateCache=null;
+    }
+}