testing TemplateHashModel
authorrk <rk>
Thu, 24 Jan 2002 00:09:49 +0000 (00:09 +0000)
committerrk <rk>
Thu, 24 Jan 2002 00:09:49 +0000 (00:09 +0000)
source/mir/misc/HTMLTemplateProcessor.java
source/mir/module/AbstractModule.java
source/mir/servlet/ServletModule.java
source/mircoders/entity/EntityBreaking.java
source/mircoders/servlet/ServletModuleBreaking.java
source/mircoders/servlet/ServletModuleContent.java

index ef15a58..56747aa 100755 (executable)
@@ -51,10 +51,11 @@ public final class HTMLTemplateProcessor {
     // nadir evironment. from my point of coding, this needs an urgent
     // fixxx.
     // yeah, from my point too - tob.
-       //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
+       //actionRoot = docRoot + "/servlet/" + MirConfig.getProp("ServletName");
     //actionRoot = docRoot + "/servlet/NadirAktuell";
-       
+
     actionRoot = docRoot + "/servlet/Mir";
+
     openAction = MirConfig.getProp("Producer.OpenAction");
     productionHost = MirConfig.getProp("Producer.ProductionHost");
     videoHost = MirConfig.getProp("Producer.VideoHost");
index b834130..68212d6 100755 (executable)
@@ -14,10 +14,17 @@ import  mir.entity.*;
 
 
 /**
+ * This class provides the base functionality for the derived Module-Classes.
+ * These classes should provide methods to make more or less complex actions
+ * on Database and Entity classes. The modules are used by ServletModules.
+ * Future possibility could be access via Applications.
+ *
  * Abstrakte Klasse, von denen die Modules die Basisfunktionalität erben.
  * Die Moduleschicht dient dazu, Funktionalitaeten zur Verfügung zu stellen,
  * die von mehreren ServletModulen verwendet werden.
+ *
  */
+
 public class AbstractModule {
        protected StorageObject theStorage;
 
@@ -41,10 +48,10 @@ public class AbstractModule {
        public Entity getById (String id) throws ModuleException {
                try {
                        if (theStorage == null)
-                               throw  new ModuleException("Kein StorageObject gesetzt");
+                               throw  new ModuleException("No StorageObject set!");
                        Entity entity = (Entity)theStorage.selectById(id);
                        if (entity == null)
-                                throw new ModuleException("Objekt nicht vorhanden: ID=" + id);
+                                throw new ModuleException("No object for id = " + id);
                        else return entity;
                        }
                catch (StorageObjectException e){
index 6bd2d87..2938c65 100755 (executable)
@@ -14,6 +14,11 @@ import  mir.misc.*;
 
 
 /**
+ * Abstract class ServletModule provides the base functionality for servlets.
+ * Deriving a class from ServletModule enables class to insert/edit/update/delete
+ * and list Entity from a Database via mainModule.
+ *
+ *
  *  Abstrakte Klasse ServletModule stellt die Basisfunktionalitaet der
  *  abgeleiteten ServletModule zur Verfügung.
  *
@@ -276,6 +281,31 @@ public abstract class ServletModule {
    * TemplateModelRoot rtm gemischt wurde
    *
    * @param res Http-Response, die vom Dispatcher durchgereicht wird
+   * @param rtm beinahalten das freemarker.template.TempalteModelRoot mit den
+   *   Daten, die ins Template gemerged werden sollen.
+   * @param tmpl Name des Templates
+   * @exception ServletModuleException
+   */
+  public void deliver_compressed(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot rtm, String templateFilename)
+    throws ServletModuleException {
+    if (rtm == null) rtm = new SimpleHash();
+    try {
+      PrintWriter out =  new LineFilterWriter(res.getWriter());
+      //PrintWriter out =  res.getWriter();
+      HTMLTemplateProcessor.process(res,getLanguage(req)+"/"+templateFilename, rtm , out);
+      out.close();
+    } catch (HTMLParseException e) {
+      throw new ServletModuleException(e.toString());
+    } catch (IOException e) {
+      throw new ServletModuleException(e.toString());
+    }
+  }
+  /**
+   * deliver liefert das Template mit dem Filenamen templateFilename
+   * an den HttpServletResponse res aus, nachdem es mit den Daten aus
+   * TemplateModelRoot rtm gemischt wurde
+   *
+   * @param res Http-Response, die vom Dispatcher durchgereicht wird
    * @param entity Entity, aus der die Daten, die ins Template gemerged werden sollen.
    * @param tmpl Name des Templates
    * @exception ServletModuleException
index 1227601..e19ee76 100755 (executable)
@@ -14,13 +14,15 @@ import java.io.*;
 import java.util.*;
 import java.sql.*;
 
+import freemarker.template.*;
+
 import mir.entity.*;
 import mir.misc.*;
 import mir.storage.*;
 
-public class EntityBreaking extends AbstractEntity implements Entity
+public class EntityBreaking extends AbstractEntity implements Entity, TemplateHashModel
 {
-       private static int instances;
+  private static int instances;
 
        public EntityBreaking() {       super(); instances++;   }
        public EntityBreaking(StorageObject theStorage) {       this(); setStorage(theStorage); }
@@ -38,4 +40,25 @@ public class EntityBreaking extends AbstractEntity implements Entity
                        returnHash.put("webdb_create_formatted", StringUtil.dateToReadableDate(date));
                return returnHash;
        }
+
+  // Now implements freemarkers TemplateHashModel
+  // two methods have to be overridden:
+  // 1. public boolean isEmpty() throws TemplateModelException
+  // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
+
+  public boolean isEmpty() throws TemplateModelException
+  {
+    if (theValuesHash==null || theValuesHash.isEmpty())
+      return true;
+    return false;
+  }
+
+  public TemplateModel get(java.lang.String key) throws TemplateModelException
+  {
+    return new SimpleScalar(getValue("key"));
+  }
+
+
+  //////////////////////////////////////////////////////////////////////////////////
+
 }
index 745ad65..35bc5f0 100755 (executable)
@@ -74,4 +74,19 @@ public class ServletModuleBreaking extends ServletModule
                catch (IOException e) {throw new ServletModuleException(e.toString());}
                catch (Exception e) {throw new ServletModuleException(e.toString());}
        }
+
+
+
+  // test: deriving entity from freemarker.template.TemplateHashModel
+  //////////////////////////////////////////////////////////////////////////////
+
+  public void edit(HttpServletRequest req, HttpServletResponse res)
+    throws ServletModuleException {
+    try {
+      String idParam = req.getParameter("id");
+      deliver(req, res, (TemplateModelRoot)mainModule.getById(idParam), templateObjektString);
+    } catch(ModuleException e) {
+      throw new ServletModuleException(e.toString());
+    }
+  }
 }
index e26ef65..9f81ea7 100755 (executable)
@@ -220,6 +220,10 @@ public class ServletModuleContent extends ServletModule
       if (confirmParam!= null && !confirmParam.equals("")) {
         try {
           mainModule.deleteById(idParam);
+
+          /** @todo the following two should be imlied in
+           *  DatabaseContent */
+
           //delete rows in the content_x_topic-table
           DatabaseContentToTopics.getInstance().deleteByContentId(idParam);
           //delete rows in the comment-table
@@ -302,10 +306,9 @@ public class ServletModuleContent extends ServletModule
       EntityUsers   user = _getUser(req);
       if (user==null) theLog.printDebugInfo("user null!");
       String idParam = req.getParameter("id");
-      if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
+      if (idParam == null) throw new ServletModuleException("Wrong call: (id) is missing");
 
       HashMap withValues = getIntersectingValues(req, DatabaseContent.getInstance());
-      //String topic_id = req.getParameter("to_topic");
       String[] topic_id = req.getParameterValues("to_topic");
       String content_id = req.getParameter("id");
       // withValues.put("publish_path", StringUtil.webdbDate2path((String)withValues.get("date")));