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