KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/accounts/A_CmsEditGroupDialog.java,v $
3  * Date : $Date: 2006/03/27 14:52:49 $
4  * Version: $Revision: 1.3 $
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.CmsGroup;
35 import org.opencms.jsp.CmsJspActionElement;
36 import org.opencms.main.CmsException;
37 import org.opencms.main.CmsIllegalArgumentException;
38 import org.opencms.security.I_CmsPrincipal;
39 import org.opencms.util.CmsStringUtil;
40 import org.opencms.util.CmsUUID;
41 import org.opencms.widgets.CmsCheckboxWidget;
42 import org.opencms.widgets.CmsDisplayWidget;
43 import org.opencms.widgets.CmsInputWidget;
44 import org.opencms.widgets.CmsSelectWidget;
45 import org.opencms.widgets.CmsSelectWidgetOption;
46 import org.opencms.widgets.CmsTextareaWidget;
47 import org.opencms.workplace.CmsDialog;
48 import org.opencms.workplace.CmsWidgetDialog;
49 import org.opencms.workplace.CmsWidgetDialogParameter;
50 import org.opencms.workplace.CmsWorkplaceSettings;
51 import org.opencms.workplace.tools.CmsToolManager;
52
53 import java.util.ArrayList JavaDoc;
54 import java.util.HashMap JavaDoc;
55 import java.util.Iterator JavaDoc;
56 import java.util.List JavaDoc;
57 import java.util.Map JavaDoc;
58
59 import javax.servlet.http.HttpServletRequest JavaDoc;
60
61 /**
62  * Skeleton dialog to create a new group or edit an existing group in the administration view.<p>
63  *
64  * @author Michael Moossen
65  *
66  * @version $Revision: 1.3 $
67  *
68  * @since 6.0.0
69  */

70 public abstract class A_CmsEditGroupDialog extends CmsWidgetDialog {
71
72     /** localized messages Keys prefix. */
73     public static final String JavaDoc KEY_PREFIX = "group";
74
75     /** Defines which pages are valid for this dialog. */
76     public static final String JavaDoc[] PAGES = {"page1"};
77
78     /** Request parameter name for the user id. */
79     public static final String JavaDoc PARAM_GROUPID = "groupid";
80
81     /** The user object that is edited on this dialog. */
82     protected CmsGroup m_group;
83
84     /** Stores the value of the request parameter for the group id. */
85     private String JavaDoc m_paramGroupid;
86
87     /** Auxiliary Property for better representation of the bean parentId property. */
88     private String JavaDoc m_parentGroup;
89
90     /**
91      * Public constructor with JSP action element.<p>
92      *
93      * @param jsp an initialized JSP action element
94      */

95     public A_CmsEditGroupDialog(CmsJspActionElement jsp) {
96
97         super(jsp);
98     }
99
100     /**
101      * Returns the dialog class name of the list to refresh.<p>
102      *
103      * @return the list dialog class name
104      */

105     protected abstract String JavaDoc getListClass();
106
107     /**
108      * Returns the root path for the list tool.<p>
109      *
110      * @return the root path
111      */

112     protected abstract String JavaDoc getListRootPath();
113
114     /**
115      * Commits the edited group to the db.<p>
116      */

117     public void actionCommit() {
118
119         List JavaDoc errors = new ArrayList JavaDoc();
120
121         try {
122             // if new create it first
123
if (m_group.getId() == null) {
124                 CmsGroup newGroup = getCms().createGroup(
125                     m_group.getName(),
126                     m_group.getDescription(),
127                     m_group.isEnabled() ? I_CmsPrincipal.FLAG_ENABLED : I_CmsPrincipal.FLAG_DISABLED,
128                     getParentGroup());
129                 newGroup.setProjectManager(m_group.getProjectManager());
130                 newGroup.setProjectCoWorker(m_group.getProjectCoWorker());
131                 newGroup.setRole(m_group.getRole());
132                 m_group = newGroup;
133             } else {
134                 if (getParentGroup() != null) {
135                     m_group.setParentId(getCms().readGroup(getParentGroup()).getId());
136                 } else {
137                     m_group.setParentId(CmsUUID.getNullUUID());
138                 }
139             }
140             // write the edited group
141
getCms().writeGroup(m_group);
142             // refresh the list
143
Map JavaDoc objects = (Map JavaDoc)getSettings().getListObject();
144             if (objects != null) {
145                 objects.remove(getListClass());
146                 objects.remove(A_CmsUsersList.class.getName());
147             }
148         } catch (Throwable JavaDoc t) {
149             errors.add(t);
150         }
151
152         if (errors.isEmpty() && isNewGroup()) {
153             if (getParamCloseLink() != null && getParamCloseLink().indexOf("path=" + getListRootPath()) > -1) {
154                 // set closelink
155
Map JavaDoc argMap = new HashMap JavaDoc();
156                 argMap.put("groupid", m_group.getId());
157                 argMap.put("groupname", m_group.getName());
158                 setParamCloseLink(CmsToolManager.linkForToolPath(getJsp(), getListRootPath() + "/edit", argMap));
159             }
160         }
161         // set the list of errors to display when saving failed
162
setCommitErrors(errors);
163     }
164
165     /**
166      * Returns the user id parameter value.<p>
167      *
168      * @return the user id parameter value
169      */

170     public String JavaDoc getParamGroupid() {
171
172         return m_paramGroupid;
173     }
174
175     /**
176      * Returns the parent Group name.<p>
177      *
178      * @return the parent Group name
179      */

180     public String JavaDoc getParentGroup() {
181
182         return m_parentGroup;
183     }
184
185     /**
186      * Sets the user id parameter value.<p>
187      *
188      * @param userId the user id parameter value
189      */

190     public void setParamGroupid(String JavaDoc userId) {
191
192         m_paramGroupid = userId;
193     }
194
195     /**
196      * Sets the parent Group name.<p>
197      *
198      * @param parentGroup the parent Group name to set
199      */

200     public void setParentGroup(String JavaDoc parentGroup) {
201
202         if (CmsStringUtil.isEmpty(parentGroup) || parentGroup.equals("null") || parentGroup.equals("none")) {
203             parentGroup = null;
204         }
205         if (parentGroup != null) {
206             try {
207                 getCms().readGroup(parentGroup);
208             } catch (CmsException e) {
209                 throw new CmsIllegalArgumentException(e.getMessageContainer());
210             }
211         }
212         m_parentGroup = parentGroup;
213     }
214
215     /**
216      * Creates the dialog HTML for all defined widgets of the named dialog (page).<p>
217      *
218      * This overwrites the method from the super class to create a layout variation for the widgets.<p>
219      *
220      * @param dialog the dialog (page) to get the HTML for
221      * @return the dialog HTML for all defined widgets of the named dialog (page)
222      */

223     protected String JavaDoc createDialogHtml(String JavaDoc dialog) {
224
225         StringBuffer JavaDoc result = new StringBuffer JavaDoc(1024);
226
227         result.append(createWidgetTableStart());
228         // show error header once if there were validation errors
229
result.append(createWidgetErrorHeader());
230
231         if (dialog.equals(PAGES[0])) {
232             // create the widgets for the first dialog page
233
result.append(dialogBlockStart(key(Messages.GUI_GROUP_EDITOR_LABEL_IDENTIFICATION_BLOCK_0)));
234             result.append(createWidgetTableStart());
235             result.append(createDialogRowsHtml(0, 3));
236             result.append(createWidgetTableEnd());
237             result.append(dialogBlockEnd());
238             result.append(dialogBlockStart(key(Messages.GUI_GROUP_EDITOR_LABEL_FLAGS_BLOCK_0)));
239             result.append(createWidgetTableStart());
240             result.append(createDialogRowsHtml(4, 6));
241             result.append(createWidgetTableEnd());
242             result.append(dialogBlockEnd());
243         }
244
245         result.append(createWidgetTableEnd());
246         return result.toString();
247     }
248
249     /**
250      * Creates the list of widgets for this dialog.<p>
251      */

252     protected void defineWidgets() {
253
254         // initialize the user object to use for the dialog
255
initGroupObject();
256
257         setKeyPrefix(KEY_PREFIX);
258
259         // widgets to display
260
if (m_group.getId() == null && isEditable(m_group)) {
261             addWidget(new CmsWidgetDialogParameter(m_group, "name", PAGES[0], new CmsInputWidget()));
262         } else {
263             addWidget(new CmsWidgetDialogParameter(m_group, "name", PAGES[0], new CmsDisplayWidget()));
264         }
265         if (isEditable(m_group)) {
266             addWidget(new CmsWidgetDialogParameter(m_group, "description", PAGES[0], new CmsTextareaWidget()));
267             addWidget(new CmsWidgetDialogParameter(this, "parentGroup", PAGES[0], new CmsSelectWidget(getSelectGroups())));
268             addWidget(new CmsWidgetDialogParameter(m_group, "enabled", PAGES[0], new CmsCheckboxWidget()));
269             addWidget(new CmsWidgetDialogParameter(m_group, "role", PAGES[0], new CmsCheckboxWidget()));
270             addWidget(new CmsWidgetDialogParameter(m_group, "projectManager", PAGES[0], new CmsCheckboxWidget()));
271             addWidget(new CmsWidgetDialogParameter(m_group, "projectCoWorker", PAGES[0], new CmsCheckboxWidget()));
272         } else {
273             addWidget(new CmsWidgetDialogParameter(m_group, "description", PAGES[0], new CmsDisplayWidget()));
274             addWidget(new CmsWidgetDialogParameter(this, "parentGroup", PAGES[0], new CmsDisplayWidget()));
275             addWidget(new CmsWidgetDialogParameter(m_group, "enabled", PAGES[0], new CmsDisplayWidget()));
276             addWidget(new CmsWidgetDialogParameter(m_group, "role", PAGES[0], new CmsDisplayWidget()));
277             addWidget(new CmsWidgetDialogParameter(m_group, "projectManager", PAGES[0], new CmsDisplayWidget()));
278             addWidget(new CmsWidgetDialogParameter(m_group, "projectCoWorker", PAGES[0], new CmsDisplayWidget()));
279         }
280     }
281
282     /**
283      * @see org.opencms.workplace.CmsWidgetDialog#getPageArray()
284      */

285     protected String JavaDoc[] getPageArray() {
286
287         return PAGES;
288     }
289
290     /**
291      * Initializes the group object to work with depending on the dialog state and request parameters.<p>
292      *
293      * Two initializations of the group object on first dialog call are possible:
294      * <ul>
295      * <li>edit an existing group</li>
296      * <li>create a new group</li>
297      * <li>view an existing group overview</li>
298      * <li>view an existing group short info</li>
299      * </ul>
300      */

301     protected void initGroupObject() {
302
303         Object JavaDoc o = null;
304
305         try {
306             if (CmsStringUtil.isEmpty(getParamAction()) || CmsDialog.DIALOG_INITIAL.equals(getParamAction())) {
307                 // edit an existing user, get the user object from db
308
m_group = getCms().readGroup(new CmsUUID(getParamGroupid()));
309             } else {
310                 // this is not the initial call, get the user object from session
311
o = getDialogObject();
312                 m_group = (CmsGroup)o;
313                 // test
314
m_group.getId();
315             }
316             if (m_group.getParentId() != null && !m_group.getParentId().isNullUUID()) {
317                 setParentGroup(getCms().getParent(m_group.getName()).getName());
318             }
319         } catch (Exception JavaDoc e) {
320             // create a new user object
321
m_group = new CmsGroup();
322             setParentGroup(null);
323         }
324     }
325
326     /**
327      * @see org.opencms.workplace.CmsWorkplace#initMessages()
328      */

329     protected void initMessages() {
330
331         // add specific dialog resource bundle
332
addMessages(Messages.get().getBundleName());
333         // add default resource bundles
334
super.initMessages();
335     }
336
337     /**
338      * @see org.opencms.workplace.CmsWorkplace#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
339      */

340     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
341
342         // initialize parameters and dialog actions in super implementation
343
super.initWorkplaceRequestValues(settings, request);
344
345         // save the current state of the group (may be changed because of the widget values)
346
setDialogObject(m_group);
347     }
348
349     /**
350      * @see org.opencms.workplace.CmsWidgetDialog#validateParamaters()
351      */

352     protected void validateParamaters() throws Exception JavaDoc {
353
354         if (!isNewGroup()) {
355             // test the needed parameters
356
getCms().readGroup(new CmsUUID(getParamGroupid())).getName();
357
358         }
359     }
360
361     /**
362      * Returns the groups names to show in the select box.<p>
363      *
364      * @return the groups names to show in the select box
365      */

366     private List JavaDoc getSelectGroups() {
367
368         List JavaDoc retVal = new ArrayList JavaDoc();
369         retVal.add(new CmsSelectWidgetOption("none", true));
370         try {
371             Iterator JavaDoc itGroups = getCms().getGroups().iterator();
372             while (itGroups.hasNext()) {
373                 CmsGroup group = (CmsGroup)itGroups.next();
374                 if (group.getId().equals(m_group.getId())) {
375                     continue;
376                 }
377                 retVal.add(new CmsSelectWidgetOption(group.getName()));
378             }
379         } catch (Exception JavaDoc e) {
380             // noop
381
}
382         return retVal;
383     }
384
385     /**
386      * Checks if the new Group dialog has to be displayed.<p>
387      *
388      * @return <code>true</code> if the new Group dialog has to be displayed
389      */

390     private boolean isNewGroup() {
391
392         return getCurrentToolPath().equals(getListRootPath() + "/new");
393     }
394
395     /**
396      * Tests if the given group is editable or not.<p>
397      *
398      * Not editable means that no property can be changed.<p>
399      *
400      * @param group the group to test
401      *
402      * @return the editable flag
403      */

404     protected abstract boolean isEditable(CmsGroup group);
405     
406 }
Popular Tags