KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > tools > accounts > A_CmsEditUserDialog


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/accounts/A_CmsEditUserDialog.java,v $
3  * Date : $Date: 2006/03/27 14:52:49 $
4  * Version: $Revision: 1.4 $
5  *
6  * This library is part of OpenCms -
7  * the Open Source Content Mananagement System
8  *
9  * Copyright (c) 2005 Alkacon Software GmbH (http://www.alkacon.com)
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Lesser General Public License for more details.
20  *
21  * For further information about Alkacon Software GmbH, please see the
22  * company website: http://www.alkacon.com
23  *
24  * For further information about OpenCms, please see the
25  * project website: http://www.opencms.org
26  *
27  * You should have received a copy of the GNU Lesser General Public
28  * License along with this library; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */

31
32 package org.opencms.workplace.tools.accounts;
33
34 import org.opencms.file.CmsUser;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.CmsException;
37 import org.opencms.security.CmsPasswordInfo;
38 import org.opencms.util.CmsStringUtil;
39 import org.opencms.util.CmsUUID;
40 import org.opencms.widgets.CmsCheckboxWidget;
41 import org.opencms.widgets.CmsDisplayWidget;
42 import org.opencms.widgets.CmsInputWidget;
43 import org.opencms.widgets.CmsPasswordWidget;
44 import org.opencms.widgets.CmsTextareaWidget;
45 import org.opencms.workplace.CmsDialog;
46 import org.opencms.workplace.CmsWidgetDialog;
47 import org.opencms.workplace.CmsWidgetDialogParameter;
48 import org.opencms.workplace.CmsWorkplaceSettings;
49 import org.opencms.workplace.tools.CmsToolManager;
50
51 import java.util.ArrayList JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55
56 import javax.servlet.http.HttpServletRequest JavaDoc;
57
58 /**
59  * Dialog to edit new or existing user in the administration view.<p>
60  *
61  * @author Michael Moossen
62  *
63  * @version $Revision: 1.4 $
64  *
65  * @since 6.0.0
66  */

67 public abstract class A_CmsEditUserDialog extends CmsWidgetDialog {
68
69     /** localized messages Keys prefix. */
70     public static final String JavaDoc KEY_PREFIX = "user";
71
72     /** Defines which pages are valid for this dialog. */
73     public static final String JavaDoc[] PAGES = {"page1"};
74
75     /** Request parameter name for the user id. */
76     public static final String JavaDoc PARAM_USERID = "userid";
77
78     /** Session parameter name for the pwd info object. */
79     private static final Object JavaDoc PWD_OBJECT = "PWD_INFO";
80
81     /** Session parameter name for the user object. */
82     private static final Object JavaDoc USER_OBJECT = "USER";
83
84     /** The user object that is edited on this dialog. */
85     protected CmsUser m_user;
86
87     /** Stores the value of the request parameter for the user id. */
88     private String JavaDoc m_paramUserid;
89
90     /** The password information object. */
91     private CmsPasswordInfo m_pwdInfo;
92
93     /**
94      * Public constructor with JSP action element.<p>
95      *
96      * @param jsp an initialized JSP action element
97      */

98     public A_CmsEditUserDialog(CmsJspActionElement jsp) {
99
100         super(jsp);
101     }
102
103     /**
104      * Commits the edited user to the db.<p>
105      */

106     public void actionCommit() {
107
108         List JavaDoc errors = new ArrayList JavaDoc();
109
110         try {
111             // in the case the confirmation widget has not be enabled
112
if (CmsStringUtil.isNotEmpty(m_pwdInfo.getNewPwd())) {
113                 m_pwdInfo.setConfirmation(m_pwdInfo.getConfirmation());
114             }
115             // if new create it first
116
if (m_user.getId() == null) {
117                 CmsUser newUser = createUser(
118                     m_user.getName(),
119                     m_pwdInfo.getNewPwd(),
120                     m_user.getDescription(),
121                     m_user.getAdditionalInfo());
122                 newUser.setFirstname(m_user.getFirstname());
123                 newUser.setLastname(m_user.getLastname());
124                 newUser.setEmail(m_user.getEmail());
125                 newUser.setAddress(m_user.getAddress());
126                 m_user = newUser;
127             } else if (CmsStringUtil.isNotEmpty(m_pwdInfo.getNewPwd())) {
128                 if (!m_pwdInfo.getNewPwd().equals(m_pwdInfo.getConfirmation())) {
129                     m_pwdInfo.setConfirmation(null);
130                 }
131                 getCms().setPassword(m_user.getName(), m_pwdInfo.getNewPwd());
132             }
133             // write the edited user
134
writeUser(m_user);
135             // refresh the list
136
Map JavaDoc objects = (Map JavaDoc)getSettings().getListObject();
137             if (objects != null) {
138                 objects.remove(getListClass());
139             }
140         } catch (Throwable JavaDoc t) {
141             errors.add(t);
142         }
143
144         if (errors.isEmpty() && isNewUser()) {
145             if (getParamCloseLink() != null && getParamCloseLink().indexOf("path=" + getListRootPath()) > -1) {
146                 // set closelink
147
Map JavaDoc argMap = new HashMap JavaDoc();
148                 argMap.put("userid", m_user.getId());
149                 argMap.put("username", m_user.getName());
150                 setParamCloseLink(CmsToolManager.linkForToolPath(getJsp(), getListRootPath() + "/edit", argMap));
151             }
152         }
153         // set the list of errors to display when saving failed
154
setCommitErrors(errors);
155     }
156
157     /**
158      * Returns the user id parameter value.<p>
159      *
160      * @return the user id parameter value
161      */

162     public String JavaDoc getParamUserid() {
163
164         return m_paramUserid;
165     }
166
167     /**
168      * Sets the user id parameter value.<p>
169      *
170      * @param userId the user id parameter value
171      */

172     public void setParamUserid(String JavaDoc userId) {
173
174         m_paramUserid = userId;
175     }
176
177     /**
178      * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
179      *
180      * This overwrites the method from the super class to create a layout variation for the widgets.<p>
181      *
182      * @param dialog the dialog (page) to get the HTML for
183      * @return the dialog HTML for all defined widgets of the named dialog (page)
184      */

185     protected String JavaDoc createDialogHtml(String JavaDoc dialog) {
186
187         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
188
189         result.append(createWidgetTableStart());
190         // show error header once if there were validation errors
191
result.append(createWidgetErrorHeader());
192
193         if (dialog.equals(PAGES[0])) {
194             // create the widgets for the first dialog page
195
result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_IDENTIFICATION_BLOCK_0)));
196             result.append(createWidgetTableStart());
197             result.append(createDialogRowsHtml(0, 4));
198             result.append(createWidgetTableEnd());
199             result.append(dialogBlockEnd());
200             result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_ADDRESS_BLOCK_0)));
201             result.append(createWidgetTableStart());
202             result.append(createDialogRowsHtml(5, 8));
203             result.append(createWidgetTableEnd());
204             result.append(dialogBlockEnd());
205             result.append(dialogBlockStart(key(Messages.GUI_USER_EDITOR_LABEL_AUTHENTIFICATION_BLOCK_0)));
206             result.append(createWidgetTableStart());
207             if (isPwdChangeAllowed(m_user)) {
208                 result.append(createDialogRowsHtml(9, 11));
209             } else {
210                 result.append(createDialogRowsHtml(9, 9));
211             }
212             result.append(createWidgetTableEnd());
213             result.append(dialogBlockEnd());
214         }
215
216         result.append(createWidgetTableEnd());
217         return result.toString();
218     }
219
220     /**
221      * Creates a new user.<p>
222      *
223      * @param name the name
224      * @param pwd the password
225      * @param desc the description
226      * @param info the additional information map
227      *
228      * @return the new user
229      *
230      * @throws CmsException if something goes wrong
231      */

232     protected abstract CmsUser createUser(String JavaDoc name, String JavaDoc pwd, String JavaDoc desc, Map JavaDoc info) throws CmsException;
233
234     /**
235      * Creates the list of widgets for this dialog.<p>
236      */

237     protected void defineWidgets() {
238
239         // initialize the user object to use for the dialog
240
initUserObject();
241
242         setKeyPrefix(KEY_PREFIX);
243
244         // widgets to display
245
if (m_user.getId() == null && isEditable(m_user)) {
246             addWidget(new CmsWidgetDialogParameter(m_user, "name", PAGES[0], new CmsInputWidget()));
247         } else {
248             addWidget(new CmsWidgetDialogParameter(m_user, "name", PAGES[0], new CmsDisplayWidget()));
249         }
250         if (isEditable(m_user)) {
251             addWidget(new CmsWidgetDialogParameter(m_user, "description", "", PAGES[0], new CmsTextareaWidget(), 0, 1));
252             addWidget(new CmsWidgetDialogParameter(m_user, "lastname", PAGES[0], new CmsInputWidget()));
253             addWidget(new CmsWidgetDialogParameter(m_user, "firstname", PAGES[0], new CmsInputWidget()));
254             addWidget(new CmsWidgetDialogParameter(m_user, "email", PAGES[0], new CmsInputWidget()));
255             addWidget(new CmsWidgetDialogParameter(m_user, "address", "", PAGES[0], new CmsInputWidget(), 0, 1));
256             addWidget(new CmsWidgetDialogParameter(m_user, "zipcode", "", PAGES[0], new CmsInputWidget(), 0, 1));
257             addWidget(new CmsWidgetDialogParameter(m_user, "city", "", PAGES[0], new CmsInputWidget(), 0, 1));
258             addWidget(new CmsWidgetDialogParameter(m_user, "country", "", PAGES[0], new CmsInputWidget(), 0, 1));
259         } else {
260             addWidget(new CmsWidgetDialogParameter(m_user, "description", PAGES[0], new CmsDisplayWidget()));
261             addWidget(new CmsWidgetDialogParameter(m_user, "lastname", PAGES[0], new CmsDisplayWidget()));
262             addWidget(new CmsWidgetDialogParameter(m_user, "firstname", PAGES[0], new CmsDisplayWidget()));
263             addWidget(new CmsWidgetDialogParameter(m_user, "email", PAGES[0], new CmsDisplayWidget()));
264             addWidget(new CmsWidgetDialogParameter(m_user, "address", PAGES[0], new CmsDisplayWidget()));
265             addWidget(new CmsWidgetDialogParameter(m_user, "zipcode", PAGES[0], new CmsDisplayWidget()));
266             addWidget(new CmsWidgetDialogParameter(m_user, "city", PAGES[0], new CmsDisplayWidget()));
267             addWidget(new CmsWidgetDialogParameter(m_user, "country", PAGES[0], new CmsDisplayWidget()));
268         }
269         addWidget(new CmsWidgetDialogParameter(m_user, "enabled", PAGES[0], new CmsCheckboxWidget()));
270         if (isPwdChangeAllowed(m_user)) {
271             addWidget(new CmsWidgetDialogParameter(m_pwdInfo, "newPwd", PAGES[0], new CmsPasswordWidget()));
272             addWidget(new CmsWidgetDialogParameter(m_pwdInfo, "confirmation", PAGES[0], new CmsPasswordWidget()));
273         }
274     }
275
276     /**
277      * Returns the dialog class name of the list to refresh.<p>
278      *
279      * @return the list dialog class name
280      */

281     protected abstract String JavaDoc getListClass();
282
283     /**
284      * Returns the root path for the list tool.<p>
285      *
286      * @return the root path
287      */

288     protected abstract String JavaDoc getListRootPath();
289
290     /**
291      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
292      */

293     protected String JavaDoc[] getPageArray() {
294
295         return PAGES;
296     }
297
298     /**
299      * @see org.opencms.workplace.CmsWorkplace#initMessages()
300      */

301     protected void initMessages() {
302
303         // add specific dialog resource bundle
304
addMessages(Messages.get().getBundleName());
305         // add default resource bundles
306
super.initMessages();
307     }
308
309     /**
310      * Initializes the user object to work with depending on the dialog state and request parameters.<p>
311      *
312      * Two initializations of the user object on first dialog call are possible:
313      * <ul>
314      * <li>edit an existing user</li>
315      * <li>create a new user</li>
316      * </ul>
317      */

318     protected void initUserObject() {
319
320         Object JavaDoc o = null;
321
322         try {
323             if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
324                 // edit an existing user, get the user object from db
325
m_user = getCms().readUser(new CmsUUID(getParamUserid()));
326                 m_pwdInfo = new CmsPasswordInfo();
327                 return;
328             } else {
329                 // this is not the initial call, get the user object from session
330
o = getDialogObject();
331                 Map JavaDoc dialogObject = (Map JavaDoc)o;
332                 m_user = (CmsUser)dialogObject.get(USER_OBJECT);
333                 m_pwdInfo = (CmsPasswordInfo)dialogObject.get(PWD_OBJECT);
334                 // test
335
m_user.getId();
336                 return;
337             }
338         } catch (Exception JavaDoc e) {
339             // noop
340
}
341         // create a new user object
342
m_user = new CmsUser();
343         m_pwdInfo = new CmsPasswordInfo();
344     }
345
346     /**
347      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
348      */

349     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
350
351         // initialize parameters and dialog actions in super implementation
352
super.initWorkplaceRequestValues(settings, request);
353
354         // save the current state of the user and pwd (may be changed because of the widget values)
355
Map JavaDoc dialogObject = new HashMap JavaDoc();
356         dialogObject.put(USER_OBJECT, m_user);
357         dialogObject.put(PWD_OBJECT, m_pwdInfo);
358         setDialogObject(dialogObject);
359
360     }
361
362     /**
363      * Tests if the given user is editable or not.<p>
364      *
365      * Not editable means that the user can only be activated and deactivated.<p>
366      *
367      * @param user the user to test
368      *
369      * @return the editable flag
370      */

371     protected abstract boolean isEditable(CmsUser user);
372
373     /**
374      * Indicates if the pwd can be edited or not.<p>
375      *
376      * @param user the edited cms user
377      *
378      * @return <code>true</code> if the pwd can be edited
379      */

380     protected boolean isPwdChangeAllowed(CmsUser user) {
381
382         return user == user; // to avoid warning
383
}
384
385     /**
386      * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
387      */

388     protected void validateParamaters() throws Exception JavaDoc {
389
390         if (!isNewUser()) {
391             // test the needed parameters
392
getCms().readUser(new CmsUUID(getParamUserid())).getName();
393         }
394     }
395
396     /**
397      * Writes a user to the db.<p>
398      *
399      * @param user the user to write
400      *
401      * @throws CmsException if something goes wrong
402      */

403     protected abstract void writeUser(CmsUser user) throws CmsException;
404
405     /**
406      * Checks if the new user dialog has to be displayed.<p>
407      *
408      * @return <code>true</code> if the new user dialog has to be displayed
409      */

410     private boolean isNewUser() {
411
412         return getCurrentToolPath().equals(getListRootPath() + "/new");
413     }
414 }
Popular Tags