debug change and revert
[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 import javax.servlet.http.*;
16
17
18 /**
19  * Hilfsklasse zum Mergen von Template und Daten
20  */
21 public final class HTMLTemplateProcessor {
22
23   public static String                templateDir;
24   private static FileTemplateCache    templateCache;
25   private static Logfile              theLog;
26   private static String               docRoot;
27   private static String               actionRoot;
28   private static String               productionHost;
29   private static String               audioHost;
30   private static String               videoHost;
31   private static String               imageHost;
32   private static String               openAction;
33   protected static String producerDocRoot = MirConfig.getProp("Producer.DocRoot");
34   protected static String producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
35
36
37   //
38   // init
39
40   static {
41
42     templateDir = MirConfig.getPropWithHome("HTMLTemplateProcessor.Dir");
43     templateCache = new FileTemplateCache(templateDir);
44     templateCache.setLoadingPolicy(templateCache.LOAD_ON_DEMAND);
45     templateCache.startAutoUpdate();
46     theLog = Logfile.getInstance(MirConfig.getPropWithHome("HTMLTemplateProcessor.Logfile"));
47     docRoot = MirConfig.getProp("RootUri");
48     //the quick hack is back in effect as it was more broken than ever before
49     // -mh
50     // sorry: nadir back in town, i have to debug the mirbase.jar in the
51     // nadir evironment. from my point of coding, this needs an urgent
52     // fixxx.
53     //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
54     //actionRoot = docRoot + "/servlet/NadirAktuell";
55     actionRoot = docRoot + "/servlet/Mir";
56     openAction = MirConfig.getProp("Producer.OpenAction");
57     productionHost = MirConfig.getProp("Producer.ProductionHost");
58     videoHost = MirConfig.getProp("Producer.VideoHost");
59     audioHost = MirConfig.getProp("Producer.AudioHost");
60     imageHost = MirConfig.getProp("Producer.Image.Host");
61     producerDocRoot = MirConfig.getProp("Producer.DocRoot");
62     producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
63   }
64
65   /**
66    * empty private constructor, to avoid instantiation
67    */
68   private HTMLTemplateProcessor () { }
69
70
71   // process-methods to merge different datastructures
72   // with freemarker templates
73
74
75   /**
76    * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
77    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
78    * <code>out</code>
79    *
80    * @param templateFilename
81    * @param anEntity
82    * @param out
83    * @exception HTMLParseException
84    */
85
86     public static void process(String templateFilename, Entity anEntity, PrintWriter out)
87       throws HTMLParseException {
88         if (anEntity == null)  throw new HTMLParseException("entity is empty!");
89         else process(templateFilename, anEntity, out);
90     }
91
92
93   /**
94    * Wandelt Liste mit Entities <code>entList</code> in freemarker-Struktur um, mischt die Daten mit
95    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
96    * <code>out</code>
97    *
98    * @param templateFilename
99    * @param entList
100    * @param out
101    * @exception HTMLParseException
102    */
103   public static void process(HttpServletResponse res,String templateFilename, EntityList entList, PrintWriter out)
104       throws HTMLParseException {
105     process(res, templateFilename,  entList,  (String)null, (TemplateModelRoot)null,  out);
106   }
107
108   /**
109    * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
110    * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
111    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
112    * <code>out</code>
113    *
114    * @param templateFilename
115    * @param entList
116    * @param additionalModelName
117    * @param additionalModel
118    * @param out
119    * @exception HTMLParseException
120    */
121     public static void process(HttpServletResponse res,String templateFilename, EntityList entList, String additionalModelName,
122              TemplateModelRoot additionalModel, PrintWriter out)
123       throws HTMLParseException {
124
125       SimpleHash modelRoot = new SimpleHash();
126
127       if (entList == null) {
128          process(null,templateFilename, modelRoot, out);
129       } else {
130         try {
131           modelRoot = makeSimpleHashWithEntitylistInfos(entList);
132
133           // Quickhack um mal ein Popup mit reinzunhemen ..
134           if (additionalModelName != null && additionalModel != null)
135               modelRoot.put(additionalModelName, additionalModel);
136
137           process(res,templateFilename, modelRoot, out);
138         } catch (StorageObjectException e) {
139           throw new HTMLParseException(e.toString());
140         }
141       }
142     }
143
144   /**
145    * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
146    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
147    * <code>out</code>
148    *
149    * @param templateFilename
150    * @param mergeData - a HashMap with mergeData to be converted in SimpleHash
151    * @param out
152    * @exception HTMLParseException
153    */
154     public static void process(HttpServletResponse res,String templateFilename, HashMap mergeData, PrintWriter out)
155       throws HTMLParseException {
156       process(res,templateFilename, makeSimpleHash(mergeData), out);
157     }
158
159   /**
160    * Gibt Template <code>templateFilename</code> an den PrintWriter
161    * <code>out</code>
162    *
163    * @param templateFilename
164    * @param mergeData
165    * @param out
166    * @exception HTMLParseException
167    */
168     public static void process(String templateFilename, PrintWriter out)
169       throws HTMLParseException {
170       process(null,templateFilename, (TemplateModelRoot)null, out);
171     }
172
173
174   /**
175    * Mischt die freemarker-Struktur <code>tmr</code> mit
176    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
177    * <code>out</code>
178    *
179    * @param templateFilename
180    * @param mergeData
181    * @param out
182    * @exception HTMLParseException
183    */
184     public static void process(HttpServletResponse res,String templateFilename, TemplateModelRoot tmr, PrintWriter out)
185       throws HTMLParseException {
186       if (out==null) throw new HTMLParseException("no outputstream");
187       Template tmpl = getTemplateFor(templateFilename);
188       if (tmpl == null) throw new HTMLParseException("no template: " + templateFilename);
189       if (tmr==null) tmr = new SimpleHash();
190
191       /** @todo  what is this for? (rk) */
192       String session="";
193       if (res!=null) {
194         session=res.encodeURL("");
195       }
196
197       // put standard configuration into tempalteRootmodel
198       SimpleHash configHash = new SimpleHash();
199       configHash.put("docroot", new SimpleScalar(producerDocRoot));
200       configHash.put("storageroot", new SimpleScalar(producerStorageRoot));
201       configHash.put("productionhost", new SimpleScalar(productionHost));
202       configHash.put("openaction", new SimpleScalar(openAction));
203       configHash.put("actionRootLogin",new SimpleScalar(actionRoot));
204
205
206       tmr.put("docRoot", new SimpleScalar(docRoot));
207       tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
208       tmr.put("actionRoot", new SimpleScalar(actionRoot+session));
209       tmr.put("openAction", new SimpleScalar(openAction));
210       tmr.put("productionHost", new SimpleScalar(productionHost));
211       tmr.put("videoHost", new SimpleScalar(videoHost));
212       tmr.put("audioHost", new SimpleScalar(audioHost));
213       tmr.put("imageHost", new SimpleScalar(imageHost));
214
215       tmr.put("config", configHash);
216       tmpl.process(tmr, out);
217
218     }
219
220
221   /**
222    *   Converts Entity-List to SimpleList of SimpleHashes.
223    *   @param aList ist eine Liste von Entity
224    *   @return eine freemarker.template.SimpleList von SimpleHashes.
225    */
226   public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
227   {
228     SimpleList  simpleList = new SimpleList();
229     if (aList != null) {
230       for(int i=0;i<aList.size();i++) {
231         simpleList.add(makeSimpleHash(aList.elementAt(i)));
232       }
233     }
234     return simpleList;
235   }
236
237   /**
238    *  Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
239    *  sind die einzelnen Entities ueber ihre id zu erreichen.
240    *  @param aList ist die EntityList
241    *  @return SimpleHash mit den entsprechenden freemarker Daten
242    *
243    */
244   public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
245   {
246     SimpleHash      simpleHash = new SimpleHash();
247     Entity          currentEntity;
248
249     if (aList != null) {
250       for (int i=0;i<aList.size();i++) {
251          currentEntity = (Entity)aList.elementAt(i);
252          simpleHash.put(currentEntity.getId(), makeSimpleHash(currentEntity));
253       }
254     }
255     return simpleHash;
256   }
257
258   /**
259    *  Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
260    *  @param entity ist die Entity
261    *  @return SimpleHash mit den entsprechenden freemarker Daten
262    *
263    */
264   public static SimpleHash makeSimpleHash(Entity entity) {
265     if (entity != null)
266       return makeSimpleHash(entity.getValues());
267     else
268       return null;
269   }
270
271   /**
272    *  Konvertiert ein Hashtable mit den keys und values als String
273    *  in ein freemarker.template.SimpleHash-Modell
274    *  @param mergeData der HashMap mit den String / String Daten
275    *  @return SimpleHash mit den entsprechenden freemarker Daten
276    *
277    */
278   public static SimpleHash makeSimpleHash(HashMap mergeData)
279   {
280     SimpleHash modelRoot = new SimpleHash();
281     String aField;
282     if (mergeData != null) {
283       Set set = mergeData.keySet();
284       Iterator it =  set.iterator();
285       for (int i=0; i<set.size();i++)  {
286         aField = (String)it.next();
287         modelRoot.put(aField, (String)mergeData.get(aField));
288       }
289     }
290     return modelRoot;
291   }
292
293
294   /**
295    * Converts EntityList in SimpleHash and adds additional information
296    * to the returned SimpleHash
297    *
298    * @param entList
299    * @return SimpleHash returns SimpleHash with the converted EntityList plus
300    *        additional Data about the list.
301    * @exception StorageObjectException
302    */
303
304   public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
305     SimpleHash modelRoot = new SimpleHash();
306     if (entList!=null) {
307       modelRoot.put("contentlist", makeSimpleList(entList));
308       modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
309       if (entList.getWhere()!=null) {
310         modelRoot.put("where", new SimpleScalar(entList.getWhere()));
311         modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
312       }
313       if(entList.getOrder()!=null) {
314         modelRoot.put("order", new SimpleScalar(entList.getOrder()));
315         modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
316       }
317       modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
318       modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
319
320       if (entList.hasNextBatch())
321         modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
322       if (entList.hasPrevBatch())
323         modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
324     }
325     return modelRoot;
326   }
327
328   /**
329    * Private methods to get template from a templateFilename
330    * @param templateFilename
331    * @return Template
332    * @exception HTMLParseException
333    */
334   private static Template getTemplateFor(String templateFilename) throws HTMLParseException
335   {
336     if (templateFilename!=null) return templateCache.getTemplate(templateFilename);
337     else {
338       theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
339       throw new HTMLParseException("Templatefile: "+ templateFilename + " not found.");
340     }
341   }
342
343   public static void stopAutoUpdate(){
344     templateCache.stopAutoUpdate();
345     templateCache=null;
346   }
347
348 }