another media handling merge.
[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     //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
51     actionRoot = docRoot + "/servlet/Mir";
52     openAction = MirConfig.getProp("Producer.OpenAction");
53     productionHost = MirConfig.getProp("Producer.ProductionHost");
54     videoHost = MirConfig.getProp("Producer.VideoHost");
55     audioHost = MirConfig.getProp("Producer.AudioHost");
56     imageHost = MirConfig.getProp("Producer.Image.Host");
57     producerDocRoot = MirConfig.getProp("Producer.DocRoot");
58     producerStorageRoot = MirConfig.getProp("Producer.StorageRoot");
59   }
60
61   /**
62    * empty private constructor, to avoid instantiation
63    */
64   private HTMLTemplateProcessor () { }
65
66
67   // process-methods to merge different datastructures
68   // with freemarker templates
69
70
71   /**
72    * Wandelt <code>anEntity</code> in freemarker-Struktur um, mischt die Daten mit
73    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
74    * <code>out</code>
75    *
76    * @param templateFilename
77    * @param anEntity
78    * @param out
79    * @exception HTMLParseException
80    */
81
82     public static void process(String templateFilename, Entity anEntity, PrintWriter out)
83       throws HTMLParseException {
84         if (anEntity == null)  throw new HTMLParseException("entity is empty!");
85         else process(templateFilename, anEntity, out);
86     }
87
88
89   /**
90    * Wandelt Liste mit Entities <code>entList</code> in freemarker-Struktur um, mischt die Daten mit
91    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
92    * <code>out</code>
93    *
94    * @param templateFilename
95    * @param entList
96    * @param out
97    * @exception HTMLParseException
98    */
99   public static void process(HttpServletResponse res,String templateFilename, EntityList entList, PrintWriter out)
100       throws HTMLParseException {
101     process(res, templateFilename,  entList,  (String)null, (TemplateModelRoot)null,  out);
102   }
103
104   /**
105    * Wandelt Entitylist in freemarker-Struktur um, fügt <code>additionalModel</code>
106    * unter dem Namen <code>additionalModelName</code> ein und mischt die Daten mit
107    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
108    * <code>out</code>
109    *
110    * @param templateFilename
111    * @param entList
112    * @param additionalModelName
113    * @param additionalModel
114    * @param out
115    * @exception HTMLParseException
116    */
117     public static void process(HttpServletResponse res,String templateFilename, EntityList entList, String additionalModelName,
118              TemplateModelRoot additionalModel, PrintWriter out)
119       throws HTMLParseException {
120
121       SimpleHash modelRoot = new SimpleHash();
122
123       if (entList == null) {
124          process(null,templateFilename, modelRoot, out);
125       } else {
126         try {
127           modelRoot = makeSimpleHashWithEntitylistInfos(entList);
128
129           // Quickhack um mal ein Popup mit reinzunhemen ..
130           if (additionalModelName != null && additionalModel != null)
131               modelRoot.put(additionalModelName, additionalModel);
132
133           process(res,templateFilename, modelRoot, out);
134         } catch (StorageObjectException e) {
135           throw new HTMLParseException(e.toString());
136         }
137       }
138     }
139
140   /**
141    * Wandelt HashMap <code>mergeData</code> in freemarker-Struktur und mischt diese mit
142    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
143    * <code>out</code>
144    *
145    * @param templateFilename
146    * @param mergeData - a HashMap with mergeData to be converted in SimpleHash
147    * @param out
148    * @exception HTMLParseException
149    */
150     public static void process(HttpServletResponse res,String templateFilename, HashMap mergeData, PrintWriter out)
151       throws HTMLParseException {
152       process(res,templateFilename, makeSimpleHash(mergeData), out);
153     }
154
155   /**
156    * Gibt Template <code>templateFilename</code> an den PrintWriter
157    * <code>out</code>
158    *
159    * @param templateFilename
160    * @param mergeData
161    * @param out
162    * @exception HTMLParseException
163    */
164     public static void process(String templateFilename, PrintWriter out)
165       throws HTMLParseException {
166       process(null,templateFilename, (TemplateModelRoot)null, out);
167     }
168
169
170   /**
171    * Mischt die freemarker-Struktur <code>tmr</code> mit
172    * Template <code>templateFilename</code> und gibt das Ergebnis an den PrintWriter
173    * <code>out</code>
174    *
175    * @param templateFilename
176    * @param mergeData
177    * @param out
178    * @exception HTMLParseException
179    */
180     public static void process(HttpServletResponse res,String templateFilename, TemplateModelRoot tmr, PrintWriter out)
181       throws HTMLParseException {
182       if (out==null) throw new HTMLParseException("no outputstream");
183       Template tmpl = getTemplateFor(templateFilename);
184       if (tmpl == null) throw new HTMLParseException("no template: " + templateFilename);
185       if (tmr==null) tmr = new SimpleHash();
186
187       /** @todo  what is this for? (rk) */
188       String session="";
189       if (res!=null) {
190         session=res.encodeURL("");
191       }
192
193       // put standard configuration into tempalteRootmodel
194       SimpleHash configHash = new SimpleHash();
195       configHash.put("docroot", new SimpleScalar(producerDocRoot));
196       configHash.put("storageroot", new SimpleScalar(producerStorageRoot));
197       configHash.put("productionhost", new SimpleScalar(productionHost));
198       configHash.put("openaction", new SimpleScalar(openAction));
199       configHash.put("actionRootLogin",new SimpleScalar(actionRoot));
200
201
202       tmr.put("docRoot", new SimpleScalar(docRoot));
203       tmr.put("now", new SimpleScalar(StringUtil.date2readableDateTime(new GregorianCalendar())));
204       tmr.put("actionRoot", new SimpleScalar(actionRoot+session));
205       tmr.put("openAction", new SimpleScalar(openAction));
206       tmr.put("productionHost", new SimpleScalar(productionHost));
207       tmr.put("videoHost", new SimpleScalar(videoHost));
208       tmr.put("audioHost", new SimpleScalar(audioHost));
209       tmr.put("imageHost", new SimpleScalar(imageHost));
210
211       tmr.put("config", configHash);
212       tmpl.process(tmr, out);
213
214     }
215
216
217   /**
218    *   Converts Entity-List to SimpleList of SimpleHashes.
219    *   @param aList ist eine Liste von Entity
220    *   @return eine freemarker.template.SimpleList von SimpleHashes.
221    */
222   public static SimpleList makeSimpleList(EntityList aList) throws StorageObjectException
223   {
224     SimpleList  simpleList = new SimpleList();
225     if (aList != null) {
226       for(int i=0;i<aList.size();i++) {
227         simpleList.add(makeSimpleHash(aList.elementAt(i)));
228       }
229     }
230     return simpleList;
231   }
232
233   /**
234    *  Konvertiert ein EntityList in ein freemarker.template.SimpleHash-Modell. Im Hash
235    *  sind die einzelnen Entities ueber ihre id zu erreichen.
236    *  @param aList ist die EntityList
237    *  @return SimpleHash mit den entsprechenden freemarker Daten
238    *
239    */
240   public static SimpleHash makeSimpleHash(EntityList aList) throws StorageObjectException
241   {
242     SimpleHash      simpleHash = new SimpleHash();
243     Entity          currentEntity;
244
245     if (aList != null) {
246       for (int i=0;i<aList.size();i++) {
247          currentEntity = (Entity)aList.elementAt(i);
248          simpleHash.put(currentEntity.getId(), makeSimpleHash(currentEntity));
249       }
250     }
251     return simpleHash;
252   }
253
254   /**
255    *  Konvertiert ein Entity in ein freemarker.template.SimpleHash-Modell
256    *  @param entity ist die Entity
257    *  @return SimpleHash mit den entsprechenden freemarker Daten
258    *
259    */
260   public static SimpleHash makeSimpleHash(Entity entity) {
261     if (entity != null)
262       return makeSimpleHash(entity.getValues());
263     else
264       return null;
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    * Converts EntityList in SimpleHash and adds additional information
292    * to the returned SimpleHash
293    *
294    * @param entList
295    * @return SimpleHash returns SimpleHash with the converted EntityList plus
296    *        additional Data about the list.
297    * @exception StorageObjectException
298    */
299
300   public static SimpleHash makeSimpleHashWithEntitylistInfos(EntityList entList) throws StorageObjectException {
301     SimpleHash modelRoot = new SimpleHash();
302     if (entList!=null) {
303       modelRoot.put("contentlist", makeSimpleList(entList));
304       modelRoot.put("count", new SimpleScalar((new Integer(entList.getCount())).toString()));
305       if (entList.getWhere()!=null) {
306         modelRoot.put("where", new SimpleScalar(entList.getWhere()));
307         modelRoot.put("where_encoded", new SimpleScalar(URLEncoder.encode(entList.getWhere())));
308       }
309       if(entList.getOrder()!=null) {
310         modelRoot.put("order", new SimpleScalar(entList.getOrder()));
311         modelRoot.put("order_encoded", new SimpleScalar(URLEncoder.encode(entList.getOrder())));
312       }
313       modelRoot.put("from", new SimpleScalar((new Integer(entList.getFrom())).toString()));
314       modelRoot.put("to", new SimpleScalar((new Integer(entList.getTo())).toString()));
315
316       if (entList.hasNextBatch())
317         modelRoot.put("next", new SimpleScalar((new Integer(entList.getNextBatch())).toString()));
318       if (entList.hasPrevBatch())
319         modelRoot.put("prev", new SimpleScalar((new Integer(entList.getPrevBatch())).toString()));
320     }
321     return modelRoot;
322   }
323
324   /**
325    * Private methods to get template from a templateFilename
326    * @param templateFilename
327    * @return Template
328    * @exception HTMLParseException
329    */
330   private static Template getTemplateFor(String templateFilename) throws HTMLParseException
331   {
332     if (templateFilename!=null) return templateCache.getTemplate(templateFilename);
333     else {
334       theLog.printError("CACHE (ERR): Unknown template: " + templateFilename);
335       throw new HTMLParseException("Templatefile: "+ templateFilename + " not found.");
336     }
337   }
338
339   public static void stopAutoUpdate(){
340     templateCache.stopAutoUpdate();
341     templateCache=null;
342   }
343
344 }