- Improved user edit support
[mir.git] / source / mircoders / servlet / ServletModuleUsers.java
index 163f674..b7506df 100755 (executable)
-package mircoders.servlet;
-
-import java.io.*;
-import java.sql.*;
-import java.util.*;
-import javax.servlet.*;
-import javax.servlet.http.*;
-
-import freemarker.template.*;
-
-import webdb.servlet.*;
-import webdb.module.*;
-import webdb.misc.*;
-import webdb.entity.*;
-import webdb.storage.*;
-
-import mir.entity.*;
-import mir.storage.*;
-import mir.module.*;
-
-/*
- *  ServletModuleUsers -
- *  liefert HTML fuer Users
- *
- *
- * @author RK
- */
-
-public class ServletModuleUsers extends webdb.servlet.ServletModule
-{
-       // Singelton / Kontruktor
-       private static ServletModuleUsers instance = new ServletModuleUsers();
-       public static ServletModule getInstance() { return instance; }
-
-       private ServletModuleUsers() {
-       theLog = Logfile.getInstance(Configuration.getProperty("Home") + Configuration.getProperty("ServletModule.Users.Logfile"));
-       templateListString = Configuration.getProperty("ServletModule.Users.ListTemplate");
-       templateObjektString = Configuration.getProperty("ServletModule.Users.ObjektTemplate");
-       templateConfirmString = Configuration.getProperty("ServletModule.Users.ConfirmTemplate");
-                               try {
-                                       mainModule = new ModuleUsers(DatabaseUsers.getInstance());
-                               }
-                               catch (StorageObjectException e) {
-                                       theLog.printDebugInfo("servletmoduleusers konnte nicht initialisiert werden");
-                               }
-       }
-
-       public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException
-       {
-               String        idParam = req.getParameter("id");
-               if (idParam == null) throw new ServletModuleException("Falscher Aufruf: (id) nicht angegeben");
-                try {
-                       //theLog.printInfo("Showing User with id: " + idParam);
-                       SimpleHash mergeData =  HTMLTemplateProcessor.makeSimpleHash(mainModule.getById(idParam));
-                       deliver(req, res, mergeData, templateObjektString);
-               }
-               catch (ModuleException e) { throw new ServletModuleException(e.toString());}
-       }
-
-       public void add(HttpServletRequest req, HttpServletResponse res)
-               throws ServletModuleException
-       {
-               try {
-                       SimpleHash mergeData = new SimpleHash();
-                       mergeData.put("new", "1");
-                       deliver(req, res, mergeData, templateObjektString);
-               }
-               catch (Exception e) { throw new ServletModuleException(e.toString());}
-       }
-
- public void insert(HttpServletRequest req, HttpServletResponse res)
-       throws ServletModuleException
-       {
-               try {
-                       HashMap withValues = getIntersectingValues(req, mainModule.getStorageObject());
-                       String id = mainModule.add(withValues);
-                       SimpleHash mergeData = HTMLTemplateProcessor.makeSimpleHash(mainModule.getById(id));
-                       deliver(req, res, mergeData, templateObjektString);
-               }
-               catch (Exception e) { throw new ServletModuleException(e.toString());}
-       }
-}
+/*\r
+ * Copyright (C) 2001, 2002  The Mir-coders group\r
+ *\r
+ * This file is part of Mir.\r
+ *\r
+ * Mir is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Mir is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with Mir; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * In addition, as a special exception, The Mir-coders gives permission to link\r
+ * the code of this program with the com.oreilly.servlet library, any library\r
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
+ * the above that use the same license as the above), and distribute linked\r
+ * combinations including the two.  You must obey the GNU General Public\r
+ * License in all respects for all of the code used other than the above\r
+ * mentioned libraries.  If you modify this file, you may extend this exception\r
+ * to your version of the file, but you are not obligated to do so.  If you do\r
+ * not wish to do so, delete this exception statement from your version.\r
+ */\r
+\r
+package mircoders.servlet;\r
+\r
+import java.util.Map;\r
+\r
+import javax.servlet.http.HttpServletRequest;\r
+import javax.servlet.http.HttpServletResponse;\r
+\r
+import freemarker.template.SimpleHash;\r
+\r
+import mir.log.LoggerWrapper;\r
+import mir.module.ModuleException;\r
+import mir.servlet.ServletModule;\r
+import mir.servlet.ServletModuleException;\r
+import mir.servlet.ServletModuleUserException;\r
+import mir.storage.StorageObjectFailure;\r
+import mir.util.HTTPRequestParser;\r
+\r
+import mircoders.module.ModuleUsers;\r
+import mircoders.storage.DatabaseUsers;\r
+\r
+/*\r
+ *  ServletModuleUsers -\r
+ *  liefert HTML fuer Users\r
+ *\r
+ *\r
+ * @author RK\r
+ */\r
+\r
+public class ServletModuleUsers extends ServletModule\r
+{\r
+  private static ServletModuleUsers instance = new ServletModuleUsers();\r
+  public static ServletModule getInstance() { return instance; }\r
+\r
+  private ServletModuleUsers() {\r
+    super();\r
+    logger = new LoggerWrapper("ServletModule.Users");\r
+\r
+    templateListString = configuration.getString("ServletModule.Users.ListTemplate");\r
+    templateObjektString = configuration.getString("ServletModule.Users.ObjektTemplate");\r
+    templateConfirmString = configuration.getString("ServletModule.Users.ConfirmTemplate");\r
+\r
+    try {\r
+      mainModule = new ModuleUsers(DatabaseUsers.getInstance());\r
+    }\r
+    catch (StorageObjectFailure e) {\r
+      logger.debug("initialization of ServletModuleUsers failed!: " + e.getMessage());\r
+    }\r
+  }\r
+\r
+  public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException\r
+  {\r
+    String idParam = req.getParameter("id");\r
+\r
+    if (idParam == null)\r
+      throw new ServletModuleException("ServletModuleUser.edit: invalid call: (id) not specified");\r
+\r
+    try {\r
+      deliver(req, res, mainModule.getById(idParam), templateObjektString);\r
+    }\r
+    catch (ModuleException e) {\r
+      throw new ServletModuleException(e.toString());\r
+    }\r
+  }\r
+\r
+  public void add(HttpServletRequest req, HttpServletResponse res)\r
+      throws ServletModuleException\r
+  {\r
+    try {\r
+      SimpleHash mergeData = new SimpleHash();\r
+      mergeData.put("new", "1");\r
+      deliver(req, res, mergeData, templateObjektString);\r
+    }\r
+    catch (Exception e) { throw new ServletModuleException(e.toString());}\r
+  }\r
+\r
+  public String checkPassword(HTTPRequestParser aRequestParser) throws ServletModuleException, ServletModuleUserException\r
+  {\r
+    if ( (aRequestParser.getParameter("newpassword") != null &&\r
+          aRequestParser.getParameter("newpassword").length() > 0) ||\r
+        (aRequestParser.getParameter("newpassword2") != null &&\r
+         aRequestParser.getParameter("newpassword2").length() > 0)\r
+        ) {\r
+      String newPassword = aRequestParser.getParameterWithDefault("newpassword", "");\r
+      String newPassword2 = aRequestParser.getParameterWithDefault("newpassword2", "");\r
+\r
+      if (newPassword.length() == 0 || newPassword2.length() == 0) {\r
+        throw new ServletModuleUserException(\r
+            "The new password must be entered twice!");\r
+      }\r
+\r
+      if (!newPassword.equals(newPassword2)) {\r
+        throw new ServletModuleUserException(\r
+            "New password differes from confirmation");\r
+      }\r
+\r
+      return newPassword;\r
+    }\r
+    else\r
+      return null;\r
+  }\r
+\r
+  public void insert(HttpServletRequest aRequest, HttpServletResponse aResponse)\r
+      throws ServletModuleException\r
+  {\r
+    try {\r
+      HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
+      Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject());\r
+\r
+      String newPassword=checkPassword(requestParser);\r
+      if (newPassword!=null)\r
+        withValues.put("password", newPassword);\r
+      else\r
+        throw new ServletModuleUserException("Password is empty");\r
+\r
+      String id = mainModule.add(withValues);\r
+      if (requestParser.hasParameter("returnurl"))\r
+        redirect(aResponse, requestParser.getParameter("returnurl"));\r
+      else\r
+        list(aRequest, aResponse);\r
+    }\r
+    catch (Exception e) {\r
+      throw new ServletModuleException(e.toString());\r
+    }\r
+  }\r
+\r
+  public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException, ServletModuleUserException\r
+  {\r
+    try {\r
+      HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
+\r
+      Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject());\r
+\r
+      String newPassword=checkPassword(requestParser);\r
+      if (newPassword!=null)\r
+        withValues.put("password", newPassword);\r
+\r
+      mainModule.set(withValues);\r
+\r
+      if (requestParser.hasParameter("returnurl"))\r
+        redirect(aResponse, requestParser.getParameter("returnurl"));\r
+      else\r
+        list(aRequest, aResponse);\r
+    }\r
+    catch (Throwable t) {\r
+      throw new ServletModuleException("ServletModuleUsers: " + t.getMessage());\r
+    }\r
+\r
+  }\r
+\r
+\r
+}\r