changed import from dollar-notation to dot-notation
[mir.git] / source / mir / servlet / ServletModule.java
index f56aca6..ecae535 100755 (executable)
 
 package mir.servlet;
 
+import java.io.PrintWriter;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
+
 import freemarker.template.SimpleHash;
 import freemarker.template.TemplateModelRoot;
-import freemarker.template.TemplateModel;
-
+import mir.config.MirPropertiesConfiguration;
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
 import mir.entity.EntityList;
-import mir.log.*;
-import mir.misc.*;
+import mir.log.LoggerWrapper;
+import mir.misc.HTMLTemplateProcessor;
+import mir.misc.LineFilterWriter;
 import mir.module.AbstractModule;
-import mir.module.ModuleException;
 import mir.storage.StorageObject;
-import mir.storage.StorageObjectException;
+import mir.util.HTTPRequestParser;
+
 
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-import javax.servlet.http.HttpSession;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.Locale;
 
 
 /**
@@ -60,7 +62,7 @@ import java.util.Locale;
  *
  *
  *  Abstrakte Klasse ServletModule stellt die Basisfunktionalitaet der
- *  abgeleiteten ServletModule zur VerfΓΌgung.
+ *  abgeleiteten ServletModule zur Verf?gung.
  *
  * @version 28.6.1999
  * @author RK
@@ -70,12 +72,23 @@ public abstract class ServletModule {
 
   public String defaultAction;
   protected LoggerWrapper logger;
-
+  protected MirPropertiesConfiguration configuration;
   protected AbstractModule mainModule;
   protected String templateListString;
   protected String templateObjektString;
   protected String templateConfirmString;
 
+
+  public ServletModule(){
+    try {
+      configuration = MirPropertiesConfiguration.instance();
+    }
+    catch (PropertiesConfigExc e) {
+      throw new RuntimeException("Can't get configuration: " + e.getMessage());
+    }
+  }
+
+
   /**
    * Singelton - Methode muss in den abgeleiteten Klassen ueberschrieben werden.
    * @return ServletModule
@@ -88,7 +101,7 @@ public abstract class ServletModule {
    * get the module name to be used for generic operations like delete.
    */
   protected String getOperationModuleName() {
-    return getClass().getName().substring((new String("mircoders.servler.ServletModule")).length());
+    return getClass().getName().substring((new String("mircoders.servlet.ServletModule")).length());
   }
 
   /**
@@ -96,9 +109,9 @@ public abstract class ServletModule {
    */
   public String getLanguage(HttpServletRequest req) {
     HttpSession session = req.getSession(false);
-    String language = (String) session.getAttribute("Language");
+    String language = (String) session.getAttribute("language");
     if (language == null) {
-      language = MirConfig.getProp("StandardLanguage");
+      language = configuration.getString("StandardLanguage");
     }
     return language;
   }
@@ -112,7 +125,7 @@ public abstract class ServletModule {
     HttpSession session = req.getSession(false);
     if (session != null) {
       // session can be null in case of logout
-      loc = (Locale) session.getAttribute("Locale");
+      loc = (Locale) session.getAttribute("locale");
     }
     // if there is nothing in the session get it fron the accept-language
     if (loc == null) {
@@ -121,12 +134,12 @@ public abstract class ServletModule {
     return loc;
   }
 
-  public void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleException {
+  public void redirect(HttpServletResponse aResponse, String aQuery) throws ServletModuleExc, ServletModuleFailure {
     try {
-      aResponse.sendRedirect(MirConfig.getProp("RootUri") + "/Mir?"+aQuery);
+      aResponse.sendRedirect(MirPropertiesConfiguration.instance().getString("RootUri") + "/Mir?"+aQuery);
     }
     catch (Throwable t) {
-      throw new ServletModuleException(t.getMessage());
+      throw new ServletModuleFailure("ServletModule.redirect: " +t.getMessage(), t);
     }
   }
 
@@ -139,7 +152,7 @@ public abstract class ServletModule {
    * @param res Http-Response, die vom Dispatcher durchgereicht wird
    */
   public void list(HttpServletRequest req, HttpServletResponse res)
-      throws ServletModuleException {
+      throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure {
     try {
       EntityList theList;
       String offsetParam = req.getParameter("offset");
@@ -159,16 +172,11 @@ public abstract class ServletModule {
         }
       }
       theList = mainModule.getByWhereClause(null, offset);
-      //theList = mainModule.getByWhereClause((String)null, offset);
-      if (theList == null || theList.getCount() == 0 || theList.getCount() > 1) {
-        HTMLTemplateProcessor.process(res, templateListString, theList, out, getLocale(req));
-      }
-      else {
-        deliver(req, res, theList.elementAt(0), templateObjektString);
-      }
+
+      HTMLTemplateProcessor.process(res, templateListString, theList, out, getLocale(req));
     }
-    catch (Exception e) {
-      throw new ServletModuleException(e.getMessage());
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
     }
   }
 
@@ -180,15 +188,15 @@ public abstract class ServletModule {
    * @param res Http-Response, die vom Dispatcher durchgereicht wird
    */
   public void add(HttpServletRequest req, HttpServletResponse res)
-      throws ServletModuleException {
+      throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure  {
 
     try {
       SimpleHash mergeData = new SimpleHash();
       mergeData.put("new", "1");
       deliver(req, res, mergeData, templateObjektString);
     }
-    catch (Exception e) {
-      throw new ServletModuleException(e.getMessage());
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
     }
   }
 
@@ -202,16 +210,16 @@ public abstract class ServletModule {
    * @param res Http-Response, die vom Dispatcher durchgereicht wird
    */
   public void insert(HttpServletRequest req, HttpServletResponse res)
-      throws ServletModuleException, ServletModuleUserException {
+      throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure  {
     try {
-      HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
+      Map withValues = getIntersectingValues(req, mainModule.getStorageObject());
       logger.debug("--trying to add...");
       String id = mainModule.add(withValues);
       logger.debug("--trying to deliver..." + id);
       list(req, res);
     }
-    catch (Exception e) {
-      throw new ServletModuleException(e.getMessage());
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
     }
   }
 
@@ -220,12 +228,13 @@ public abstract class ServletModule {
    *
    */
 
-  public void delete(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException {
+  public void delete(HttpServletRequest req, HttpServletResponse res)
+      throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure  {
     try {
       String idParam = req.getParameter("id");
 
       if (idParam == null)
-        throw new ServletModuleException("Invalid call to delete: no id supplied");
+        throw new ServletModuleExc("Invalid call to delete: no id supplied");
 
       String confirmParam = req.getParameter("confirm");
       String cancelParam = req.getParameter("cancel");
@@ -238,6 +247,13 @@ public abstract class ServletModule {
         mergeData.put("where", req.getParameter("where"));
         mergeData.put("order", req.getParameter("order"));
         mergeData.put("offset", req.getParameter("offset"));
+        // this stuff is to be compatible with the other more advanced
+        // search method used for media and comments
+        mergeData.put("query_media_folder", req.getParameter("query_media_folder"));
+        mergeData.put("query_is_published", req.getParameter("query_is_published"));
+        mergeData.put("query_text", req.getParameter("query_text"));
+        mergeData.put("query_field", req.getParameter("query_field"));
+
         deliver(req, res, mergeData, templateConfirmString);
       }
       else {
@@ -254,8 +270,8 @@ public abstract class ServletModule {
         }
       }
     }
-    catch (Exception e) {
-      throw new ServletModuleException(e.getMessage());
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
     }
   }
 
@@ -268,13 +284,25 @@ public abstract class ServletModule {
    * @param res Http-Response, die vom Dispatcher durchgereicht wird
    */
   public void edit(HttpServletRequest req, HttpServletResponse res)
-      throws ServletModuleException {
+      throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure  {
+    edit(req, res, req.getParameter("id"));
+  }
+
+  /**
+   *  edit(req,res) - generische Editmethode. Wennn die Funktionalitaet
+   *  nicht reicht, muss sie in der abgeleiteten ServletModule-Klasse
+   *  ueberschreiben werden.
+   *
+   * @param req Http-Request, das vom Dispatcher durchgereicht wird
+   * @param res Http-Response, die vom Dispatcher durchgereicht wird
+   */
+  public void edit(HttpServletRequest aRequest, HttpServletResponse aResponse, String anIdentifier)
+      throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure  {
     try {
-      String idParam = req.getParameter("id");
-      deliver(req, res, mainModule.getById(idParam), templateObjektString);
+      deliver(aRequest, aResponse, mainModule.getById(anIdentifier), templateObjektString);
     }
-    catch (ModuleException e) {
-      throw new ServletModuleException(e.getMessage());
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
     }
   }
 
@@ -288,26 +316,24 @@ public abstract class ServletModule {
    */
 
   public void update(HttpServletRequest req, HttpServletResponse res)
-      throws ServletModuleException {
+      throws ServletModuleExc, ServletModuleUserExc, ServletModuleFailure  {
     try {
       String idParam = req.getParameter("id");
-      HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
+      Map withValues = getIntersectingValues(req, mainModule.getStorageObject());
+
       String id = mainModule.set(withValues);
-      //theLog.printInfo("Showing Entity with id: " + id);
-      //edit(req,res);
       String whereParam = req.getParameter("where");
       String orderParam = req.getParameter("order");
+
       if ((whereParam != null && !whereParam.equals("")) || (orderParam != null && !orderParam.equals(""))) {
-        //theLog.printDebugInfo("update to list");
         list(req, res);
       }
       else {
         edit(req, res);
       }
-      //list(req,res);
     }
-    catch (Exception e) {
-      throw new ServletModuleException(e.getMessage());
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
     }
   }
 
@@ -322,25 +348,23 @@ public abstract class ServletModule {
    * @param tmpl Name des Templates
    * @exception ServletModuleException
    */
-  public void deliver(HttpServletRequest req, HttpServletResponse res,
-                      TemplateModelRoot rtm, TemplateModelRoot popups,
-                      String templateFilename)
-      throws ServletModuleException {
-    if (rtm == null) rtm = new SimpleHash();
+  public void deliver(HttpServletRequest req, HttpServletResponse res, TemplateModelRoot rtm,
+         TemplateModelRoot popups, String templateFilename) throws ServletModuleFailure  {
+    if (rtm == null)
+      rtm = new SimpleHash();
+
     try {
       PrintWriter out = res.getWriter();
-      HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out,
-                                    getLocale(req), "bundles.admin");
+      HTMLTemplateProcessor.process(res, templateFilename, rtm, popups, out, getLocale(req));
+
       // we default to admin bundles here, which is not exactly beautiful...
       // but this whole producer stuff is going to be rewritten soon.
       // ServletModuleOpenIndy overwrites deliver() to use open bundles
       // (br1)
       out.close();
     }
-    catch (HTMLParseException e) {
-      throw new ServletModuleException(e.getMessage());
-    } catch (IOException e) {
-      throw new ServletModuleException(e.getMessage());
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
     }
   }
 
@@ -357,8 +381,7 @@ public abstract class ServletModule {
    * @exception ServletModuleException
    */
   public void deliver(HttpServletRequest req, HttpServletResponse res,
-                      TemplateModelRoot rtm, String templateFilename)
-      throws ServletModuleException {
+        TemplateModelRoot rtm, String templateFilename) throws ServletModuleFailure {
     deliver(req, res, rtm, null, templateFilename);
   }
 
@@ -375,7 +398,7 @@ public abstract class ServletModule {
    */
   public void deliver_compressed(HttpServletRequest req, HttpServletResponse res,
                                  TemplateModelRoot rtm, String templateFilename)
-      throws ServletModuleException {
+      throws ServletModuleFailure {
     if (rtm == null) rtm = new SimpleHash();
     try {
       PrintWriter out = new LineFilterWriter(res.getWriter());
@@ -383,11 +406,8 @@ public abstract class ServletModule {
       HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
       out.close();
     }
-    catch (HTMLParseException e) {
-      throw new ServletModuleException(e.getMessage());
-    }
-    catch (IOException e) {
-      throw new ServletModuleException(e.getMessage());
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
     }
   }
 
@@ -404,8 +424,14 @@ public abstract class ServletModule {
    */
   private void deliver(HttpServletResponse res, HttpServletRequest req, PrintWriter out,
                        TemplateModelRoot rtm, String templateFilename)
-      throws HTMLParseException {
-    HTMLTemplateProcessor.process(res, templateFilename, rtm, out, getLocale(req));
+      throws ServletModuleFailure {
+    try {
+      HTMLTemplateProcessor.process(res, templateFilename, rtm, out,
+                                    getLocale(req));
+    }
+    catch (Throwable e) {
+      throw new ServletModuleFailure(e);
+    }
   }
 
   /**
@@ -420,39 +446,50 @@ public abstract class ServletModule {
   }
 
   /**
-   *  Hier kann vor der Datenaufbereitung schon mal ein response geschickt
-   *  werden (um das subjektive Antwortverhalten bei langsamen Verbindungen
-   *  zu verbessern).
-   */
-  public void predeliver(HttpServletRequest req, HttpServletResponse res) {
-    ;
-  }
-
-  /**
-   * Holt die Felder aus der Metadatenfelderliste des StorageObjects, die
-   * im HttpRequest vorkommen und liefert sie als HashMap zurueck
+   * Gets the fields from a httprequest and matches them with the metadata from
+   * the storage object. Returns the keys that match, with their values.
    *
-   * @return HashMap mit den Werten
+   * @return Map with the values
    */
-  public HashMap getIntersectingValues(HttpServletRequest req, StorageObject theStorage)
-      throws ServletModuleException {
-    ArrayList theFieldList;
+  public Map getIntersectingValues(HttpServletRequest req, StorageObject theStorage)
+      throws ServletModuleExc, ServletModuleFailure {
+
     try {
+      HTTPRequestParser parser;
+      List theFieldList;
+
+      logger.debug("using charset: " + req.getParameter("charset"));
+      logger.debug("using method: " + req.getParameter("do"));
+      if (req.getParameter("charset") != null) {
+        parser = new HTTPRequestParser(req, req.getParameter("charset"));
+        logger.debug("using charset: " + req.getParameter("charset"));
+        logger.debug("original charset: " + req.getCharacterEncoding());
+      }
+      else {
+        parser = new HTTPRequestParser(req);
+      }
+
       theFieldList = theStorage.getFields();
-    }
-    catch (StorageObjectException e) {
-      throw new ServletModuleException("ServletModule.getIntersectingValues: " + e.getMessage());
-    }
 
-    HashMap withValues = new HashMap();
-    String aField, aValue;
+      Map withValues = new HashMap();
+      String aField, aValue;
+
+      for (int i = 0; i < theFieldList.size(); i++) {
+        aField = (String) theFieldList.get(i);
+
+        logger.debug("field " + aField + " = " + parser.getParameter(aField));
+
+        aValue = parser.getParameter(aField);
+        if (aValue != null)
+          withValues.put(aField, aValue);
+      }
+      return withValues;
+    }
+    catch (Throwable e) {
+      e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
 
-    for (int i = 0; i < theFieldList.size(); i++) {
-      aField = (String) theFieldList.get(i);
-      aValue = req.getParameter(aField);
-      if (aValue != null) withValues.put(aField, aValue);
+      throw new ServletModuleFailure( "ServletModule.getIntersectingValues: " + e.getMessage(), e);
     }
-    return withValues;
   }
 
-}
\ No newline at end of file
+}