no message
[mir.git] / source / mir / misc / HTMLTemplateProcessor.java
1 /*
2  * put your module comment here
3  */
4
5
6 package mir.misc;
7
8 import  java.lang.*;
9 import  java.util.*;
10 import  java.io.*;
11 import  java.net.*;
12 import  freemarker.template.*;
13 import  mir.entity.*;
14 import  mir.storage.*;
15
16
17 /**
18  * Hilfsklasse zum Mergen von Template und Daten
19  */
20 public final class HTMLTemplateProcessor {
21
22     public static String                templateDir;
23                 private static FileTemplateCache    templateCache;
24                 private static Logfile                theLog;
25                 private static String                 docRoot;
26                 private static String                 actionRoot;
27                 private static String                 productionHost;
28                 private static String               audioHost;
29                 private static String               videoHost;
30                 private static String               imageHost;
31                 private static String                             openAction;
32     protected static String producerDocRoot = Configuration.getProperty("Producer.DocRoot");
33   protected static String producerStorageRoot = Configuration.getProperty("Producer.StorageRoot");
34
35
36                 //
37                 // Initialisierung
38
39                 static {
40       templateDir = Configuration.getProperty("Home") + Configuration.getProperty("HTMLTemplateProcessor.Dir");
41                         templateCache = new FileTemplateCache(templateDir);
42                         templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND);
43                         //templateCache.startAutoUpdate();
44                         theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("HTMLTemplateProcessor.Logfile"));
45                         docRoot = Configuration.getProperty("HTMLTemplateProcessor.DocRoot");
46                         actionRoot = Configuration.getProperty("HTMLTemplateProcessor.ActionRoot");
47                         openAction = Configuration.getProperty("Producer.OpenAction");
48                         productionHost = Configuration.getProperty("Producer.ProductionHost");
49                         videoHost = Configuration.getProperty("Producer.VideoHost");
50                         audioHost = Configuration.getProperty("Producer.AudioHost");
51                         imageHost = Configuration.getProperty("Producer.Image.Host");
52       producerDocRoot = Configuration.getProperty("Producer.DocRoot");
53       producerStorageRoot = Configuration.getProperty("Producer.StorageRoot");
54
55
56                 }
57
58   /**
59          * Privater Konstruktor, um versehentliche Instantiierung zu verhindern
60          */
61         private HTMLTemplateProcessor () {
62         }
63
64
65                 //
66                 //  process-Methoden zum Mischen verschiedener Datenstrukturen mit HTML-Templates
67
68
69         /**
70          * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
71          * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
72          * <code>out</code>
73          *
74          * @param templateFilename
75          * @param anEntity
76          * @param out
77          * @exception HTMLParseException
78          */
79
80                 public static void process(String templateFilename, Entity anEntity, PrintWriter out)
81                         throws HTMLParseException {
82                                 if (anEntity == null)  throw new HTMLParseException("Entity leer!");
83                                 else process(templateFilename, anEntity, out);
84                 }
85
86
87         /**
88          * Wandelt Liste mit Entities <code>entList</code> in freemarker-Struktur um, mischt die Daten mit
89          * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
90          * <code>out</code>
91          *
92          * @param templateFilename
93          * @param entList
94          * @param out
95          * @exception HTMLParseException
96          */
97                 public static void process(String templateFilename, EntityList entList, PrintWriter out)
98                         throws HTMLParseException {
99                         process( templateFilename,  entList,  (String)null, (TemplateModelRoot)null,  out);
100                 }
101
102         /**
103          * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
104          * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
105          * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
106          * <code>out</code>
107          *
108          * @param templateFilename
109          * @param entList
110          * @param additionalModelName
111          * @param additionalModel
112          * @param out
113          * @exception HTMLParseException
114          */
115                 public static void process(String templateFilename, EntityList entList, String additionalModelName,
116                                                  TemplateModelRoot additionalModel, PrintWriter out)
117                         throws HTMLParseException {
118
119                         SimpleHash modelRoot = new SimpleHash();
120
121                         if (entList == null) {
122                                  //theLog.printInfo("Keine Daten! Suche erfolglos.");
123                                  process(templateFilename, modelRoot, out);
124                         } else {
125                                 try {
126                                         modelRoot = makeSimpleHashWithEntitylistInfos(entList);
127                                         //
128                                         // Hilfskruecke um mal ein Popup mit reinzunhemen ..
129                                         if (additionalModelName != null && additionalModel != null)
130                                                         modelRoot.put(additionalModelName, additionalModel);
131
132                                         process(templateFilename, modelRoot, out);
133                                 } catch (StorageObjectException e) {
134                                         throw new HTMLParseException(e.toString());
135                                 }
136                         }
137                 }
138
139         /**
140          * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
141          * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
142          * <code>out</code>
143          *
144          * @param templateFilename
145          * @param mergeData
146          * @param out
147          * @exception HTMLParseException
148          */
149                 public static void process(String templateFilename, HashMap mergeData, PrintWriter out)
150                         throws HTMLParseException {
151                         process(templateFilename, makeSimpleHash(mergeData), out);
152                 }
153
154         /**
155          * Gibt Template <code>templateFilename</code> an den PrintWriter
156          * <code>out</code>
157          *
158          * @param templateFilename
159          * @param mergeData
160          * @param out
161          * @exception HTMLParseException
162          */
163                 public static void process(String templateFilename, PrintWriter out)
164                         throws HTMLParseException {
165                         process(templateFilename, (TemplateModelRoot)null, out);
166                 }
167
168
169         /**
170          * Mischt die freemarker-Struktur <code>tmr</code> mit
171          * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
172          * <code>out</code>
173          *
174          * @param templateFilename
175          * @param mergeData
176          * @param out
177          * @exception HTMLParseException
178          */
179                 public static void process(String templateFilename, TemplateModelRoot tmr, PrintWriter out)
180                         throws HTMLParseException {
181                         if (out==null) throw new HTMLParseException("KEIN OUTPUTSTREAM");
182                         Template tmpl = getTemplateFor(templateFilename);
183                         if (tmpl == null) throw new HTMLParseException("KEIN TEMPLATE: " + templateFilename);
184                         if (tmr==null) tmr = new SimpleHash();
185
186       //some utilities
187       SimpleHash utilityHash = new SimpleHash();
188       utilityHash.put("htmlEscape",new freemarker.template.utility.HtmlEscape());
189
190       // put standard configuration into tempalteRootmodel
191                         SimpleHash configHash = new SimpleHash();
192                         configHash.put("docroot", new SimpleScalar(producerDocRoot));
193                         configHash.put("storageroot", new SimpleScalar(producerStorageRoot));
194                         configHash.put("productionhost", new SimpleScalar(productionHost));
195                         configHash.put("openaction", new SimpleScalar(openAction));
196
197
198       tmr.put("docRoot", new SimpleScalar(docRoot));
199                         tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
200                         tmr.put("actionRoot", new SimpleScalar(actionRoot));
201                         tmr.put("openAction", new SimpleScalar(openAction));
202                         tmr.put("productionHost", new SimpleScalar(productionHost));
203                         tmr.put("videoHost", new SimpleScalar(videoHost));
204                         tmr.put("audioHost", new SimpleScalar(audioHost));
205                         tmr.put("imageHost", new SimpleScalar(imageHost));
206
207       tmr.put("config", configHash);
208       tmr.put("utility",utilityHash);
209                         tmpl.process(tmr, out);
210
211                 }
212
213
214   /**
215          *   Wandelt eine Entity-Liste in eine SimpleList von SimpleHashes um.
216          *   @param aList ist eine Liste von Entity
217          *   @return eine freemarker.template.SimpleList von SimpleHashes.
218          */
219         public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
220         {
221                 SimpleList      simpleList = new SimpleList();
222                 if (aList != null) {
223                         for(int i=0;i<aList.size();i++){
224                                 simpleList.add(makeSimpleHash(aList.elementAt(i)));
225                         }
226                 }
227                 return simpleList;
228         }
229
230         /**
231          *  Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
232          *  sind die einzelnen Entities ueber ihre id zu erreichen.
233          *  @param aList ist die EntityList
234          *  @return SimpleHash mit den entsprechenden freemarker Daten
235          *
236          */
237         public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
238         {
239                 SimpleHash      simpleHash = new SimpleHash();
240                 Entity          currentEntity;
241
242                 if (aList != null) {
243                         for (int i=0;i<aList.size();i++) {
244                                  currentEntity = (Entity)aList.elementAt(i);
245                                  simpleHash.put(currentEntity.getId(), makeSimpleHash(currentEntity));
246                         }
247                 }
248                 return simpleHash;
249         }
250
251         /**
252          *  Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
253          *  @param entity ist die Entity
254          *  @return SimpleHash mit den entsprechenden freemarker Daten
255          *
256          */
257                 public static SimpleHash makeSimpleHash(Entity entity) {
258                         if (entity != null) {
259                                 return makeSimpleHash(entity.getValues());
260                         }
261                         else {
262                                 //theLog.printWarning("Entity ist <null>");
263                                 return null;
264                         }
265                 }
266
267         /**
268          *  Konvertiert ein Hashtable mit den keys und values als String
269          *  in ein freemarker.template.SimpleHash-Modell
270          *  @param mergeData der HashMap mit den String / String Daten
271          *  @return SimpleHash mit den entsprechenden freemarker Daten
272          *
273          */
274         public static SimpleHash makeSimpleHash(HashMap mergeData)
275         {
276                 SimpleHash modelRoot = new SimpleHash();
277                 String aField;
278                 if (mergeData != null) {
279                         Set set = mergeData.keySet();
280                         Iterator it =  set.iterator();
281                         for (int i=0; i<set.size();i++)  {
282                                 aField = (String)it.next();
283                                 modelRoot.put(aField, (String)mergeData.get(aField));
284                         }
285                 }
286                 return modelRoot;
287         }
288
289
290         /**
291          * Wandelt EntityListe in SimpleHash um, und versieht die Liste mit StandardInfos,
292          * die aus EntityList kommen.
293          *
294          * @param entList
295          * @return SimpleHash mit Entity-Daten und ZusatzInfos.
296          * @exception StorageObjectException
297          */
298                 public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
299                         SimpleHash modelRoot = new SimpleHash();
300                         if (entList!=null) {
301                                                                 modelRoot.put("contentlist", makeSimpleList(entList));
302                                                                 modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
303                                         if (entList.getWhere()!=null) {
304                                                 modelRoot.put("where", new SimpleScalar(entList.getWhere()));
305                                                 modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
306                                         }
307
308                                         if(entList.getOrder()!=null) {
309                                                 modelRoot.put("order", new SimpleScalar(entList.getOrder()));
310                                                 modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
311                                         }
312                                         modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
313                                         modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
314
315                                         if (entList.hasNextBatch())
316                                                 modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
317                                         if (entList.hasPrevBatch())
318                                                 modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
319                         }
320                         return modelRoot;
321                 }
322
323         /**
324          * Private Methode, um für templateFilename das Template zu bekommen.
325          * @param templateFilename
326          * @return Template
327          * @exception HTMLParseException
328          */
329         private static Template getTemplateFor(String templateFilename) throws HTMLParseException
330                 {
331                         if (templateFilename!=null) return templateCache.getTemplate(templateFilename);
332                         else {
333                                 theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
334                                 throw new HTMLParseException("Templatedatei: "+ templateFilename + " nicht gefunden!");
335                         }
336                 }
337
338
339     public static void stopAutoUpdate(){
340       templateCache.stopAutoUpdate();
341       templateCache=null;
342     }
343 }