- Improved user edit support
[mir.git] / source / mircoders / servlet / ServletModuleUsers.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package mircoders.servlet;\r
33 \r
34 import java.util.Map;\r
35 \r
36 import javax.servlet.http.HttpServletRequest;\r
37 import javax.servlet.http.HttpServletResponse;\r
38 \r
39 import freemarker.template.SimpleHash;\r
40 \r
41 import mir.log.LoggerWrapper;\r
42 import mir.module.ModuleException;\r
43 import mir.servlet.ServletModule;\r
44 import mir.servlet.ServletModuleException;\r
45 import mir.servlet.ServletModuleUserException;\r
46 import mir.storage.StorageObjectFailure;\r
47 import mir.util.HTTPRequestParser;\r
48 \r
49 import mircoders.module.ModuleUsers;\r
50 import mircoders.storage.DatabaseUsers;\r
51 \r
52 /*\r
53  *  ServletModuleUsers -\r
54  *  liefert HTML fuer Users\r
55  *\r
56  *\r
57  * @author RK\r
58  */\r
59 \r
60 public class ServletModuleUsers extends ServletModule\r
61 {\r
62   private static ServletModuleUsers instance = new ServletModuleUsers();\r
63   public static ServletModule getInstance() { return instance; }\r
64 \r
65   private ServletModuleUsers() {\r
66     super();\r
67     logger = new LoggerWrapper("ServletModule.Users");\r
68 \r
69     templateListString = configuration.getString("ServletModule.Users.ListTemplate");\r
70     templateObjektString = configuration.getString("ServletModule.Users.ObjektTemplate");\r
71     templateConfirmString = configuration.getString("ServletModule.Users.ConfirmTemplate");\r
72 \r
73     try {\r
74       mainModule = new ModuleUsers(DatabaseUsers.getInstance());\r
75     }\r
76     catch (StorageObjectFailure e) {\r
77       logger.debug("initialization of ServletModuleUsers failed!: " + e.getMessage());\r
78     }\r
79   }\r
80 \r
81   public void edit(HttpServletRequest req, HttpServletResponse res) throws ServletModuleException\r
82   {\r
83     String idParam = req.getParameter("id");\r
84 \r
85     if (idParam == null)\r
86       throw new ServletModuleException("ServletModuleUser.edit: invalid call: (id) not specified");\r
87 \r
88     try {\r
89       deliver(req, res, mainModule.getById(idParam), templateObjektString);\r
90     }\r
91     catch (ModuleException e) {\r
92       throw new ServletModuleException(e.toString());\r
93     }\r
94   }\r
95 \r
96   public void add(HttpServletRequest req, HttpServletResponse res)\r
97       throws ServletModuleException\r
98   {\r
99     try {\r
100       SimpleHash mergeData = new SimpleHash();\r
101       mergeData.put("new", "1");\r
102       deliver(req, res, mergeData, templateObjektString);\r
103     }\r
104     catch (Exception e) { throw new ServletModuleException(e.toString());}\r
105   }\r
106 \r
107   public String checkPassword(HTTPRequestParser aRequestParser) throws ServletModuleException, ServletModuleUserException\r
108   {\r
109     if ( (aRequestParser.getParameter("newpassword") != null &&\r
110           aRequestParser.getParameter("newpassword").length() > 0) ||\r
111         (aRequestParser.getParameter("newpassword2") != null &&\r
112          aRequestParser.getParameter("newpassword2").length() > 0)\r
113         ) {\r
114       String newPassword = aRequestParser.getParameterWithDefault("newpassword", "");\r
115       String newPassword2 = aRequestParser.getParameterWithDefault("newpassword2", "");\r
116 \r
117       if (newPassword.length() == 0 || newPassword2.length() == 0) {\r
118         throw new ServletModuleUserException(\r
119             "The new password must be entered twice!");\r
120       }\r
121 \r
122       if (!newPassword.equals(newPassword2)) {\r
123         throw new ServletModuleUserException(\r
124             "New password differes from confirmation");\r
125       }\r
126 \r
127       return newPassword;\r
128     }\r
129     else\r
130       return null;\r
131   }\r
132 \r
133   public void insert(HttpServletRequest aRequest, HttpServletResponse aResponse)\r
134       throws ServletModuleException\r
135   {\r
136     try {\r
137       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
138       Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject());\r
139 \r
140       String newPassword=checkPassword(requestParser);\r
141       if (newPassword!=null)\r
142         withValues.put("password", newPassword);\r
143       else\r
144         throw new ServletModuleUserException("Password is empty");\r
145 \r
146       String id = mainModule.add(withValues);\r
147       if (requestParser.hasParameter("returnurl"))\r
148         redirect(aResponse, requestParser.getParameter("returnurl"));\r
149       else\r
150         list(aRequest, aResponse);\r
151     }\r
152     catch (Exception e) {\r
153       throw new ServletModuleException(e.toString());\r
154     }\r
155   }\r
156 \r
157   public void update(HttpServletRequest aRequest, HttpServletResponse aResponse) throws ServletModuleException, ServletModuleUserException\r
158   {\r
159     try {\r
160       HTTPRequestParser requestParser = new HTTPRequestParser(aRequest);\r
161 \r
162       Map withValues = getIntersectingValues(aRequest, mainModule.getStorageObject());\r
163 \r
164       String newPassword=checkPassword(requestParser);\r
165       if (newPassword!=null)\r
166         withValues.put("password", newPassword);\r
167 \r
168       mainModule.set(withValues);\r
169 \r
170       if (requestParser.hasParameter("returnurl"))\r
171         redirect(aResponse, requestParser.getParameter("returnurl"));\r
172       else\r
173         list(aRequest, aResponse);\r
174     }\r
175     catch (Throwable t) {\r
176       throw new ServletModuleException("ServletModuleUsers: " + t.getMessage());\r
177     }\r
178 \r
179   }\r
180 \r
181 \r
182 }\r