KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/accounts/CmsGroupDependenciesList.java,v $
3  * Date : $Date: 2006/03/28 10:13:29 $
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.CmsGroup;
35 import org.opencms.file.CmsResource;
36 import org.opencms.file.CmsUser;
37 import org.opencms.i18n.CmsMessageContainer;
38 import org.opencms.jsp.CmsJspActionElement;
39 import org.opencms.main.CmsException;
40 import org.opencms.main.CmsRuntimeException;
41 import org.opencms.security.CmsAccessControlEntry;
42 import org.opencms.util.CmsIdentifiableObjectContainer;
43 import org.opencms.util.CmsStringUtil;
44 import org.opencms.util.CmsUUID;
45 import org.opencms.workplace.CmsDialog;
46 import org.opencms.workplace.CmsWorkplaceSettings;
47 import org.opencms.workplace.list.A_CmsListDialog;
48 import org.opencms.workplace.list.CmsHtmlList;
49 import org.opencms.workplace.list.CmsListColumnAlignEnum;
50 import org.opencms.workplace.list.CmsListColumnDefinition;
51 import org.opencms.workplace.list.CmsListDirectAction;
52 import org.opencms.workplace.list.CmsListItem;
53 import org.opencms.workplace.list.CmsListItemActionIconComparator;
54 import org.opencms.workplace.list.CmsListMetadata;
55 import org.opencms.workplace.list.CmsListOrderEnum;
56
57 import java.io.IOException JavaDoc;
58 import java.util.HashMap JavaDoc;
59 import java.util.Iterator JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Map JavaDoc;
62
63 import javax.servlet.ServletException JavaDoc;
64 import javax.servlet.http.HttpServletRequest JavaDoc;
65 import javax.servlet.http.HttpServletResponse JavaDoc;
66 import javax.servlet.jsp.JspException JavaDoc;
67 import javax.servlet.jsp.PageContext JavaDoc;
68
69 /**
70  * Group dependencies list view.<p>
71  *
72  * @author Michael Moossen
73  *
74  * @version $Revision: 1.4 $
75  *
76  * @since 6.0.0
77  */

78 public class CmsGroupDependenciesList extends A_CmsListDialog {
79
80     /** Value for the delete action. */
81     public static final int ACTION_DELETE = 131;
82
83     /** Value for the transfer action. */
84     public static final int ACTION_TRANSFER = 132;
85
86     /** Request parameter value for the delete action. */
87     public static final String JavaDoc DELETE_ACTION = "delete";
88
89     /** list action id constant. */
90     public static final String JavaDoc LIST_ACTION_ICON = "ai";
91
92     /** list column id constant. */
93     public static final String JavaDoc LIST_COLUMN_ICON = "ci";
94
95     /** list column id constant. */
96     public static final String JavaDoc LIST_COLUMN_NAME = "cn";
97
98     /** list column id constant. */
99     public static final String JavaDoc LIST_COLUMN_TYPE = "ct";
100
101     /** list column id constant. */
102     public static final String JavaDoc LIST_COLUMN_PERMISSIONS = "cp";
103
104     /** List id constant. */
105     public static final String JavaDoc LIST_ID = "lgd";
106
107     /** Request parameter name for the group id, could be a list of ids. */
108     public static final String JavaDoc PARAM_GROUPID = "groupid";
109
110     /** Path to the list buttons. */
111     public static final String JavaDoc PATH_BUTTONS = "tools/accounts/buttons/";
112
113     /** Request parameter value for the transfer action. */
114     public static final String JavaDoc TRANSFER_ACTION = "transfer";
115
116     /** Stores the value of the group name, could be a list of names. */
117     private String JavaDoc m_groupName;
118
119     /** Stores the value of the request parameter for the group id, could be a list of ids. */
120     private String JavaDoc m_paramGroupid;
121
122     /**
123      * Public constructor.<p>
124      *
125      * @param jsp an initialized JSP action element
126      */

127     public CmsGroupDependenciesList(CmsJspActionElement jsp) {
128
129         this(LIST_ID, jsp);
130     }
131
132     /**
133      * Public constructor with JSP variables.<p>
134      *
135      * @param context the JSP page context
136      * @param req the JSP request
137      * @param res the JSP response
138      */

139     public CmsGroupDependenciesList(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
140
141         this(new CmsJspActionElement(context, req, res));
142     }
143
144     /**
145      * Protected constructor.<p>
146      *
147      * @param listId the id of the specialized list
148      * @param jsp an initialized JSP action element
149      */

150     protected CmsGroupDependenciesList(String JavaDoc listId, CmsJspActionElement jsp) {
151
152         super(
153             jsp,
154             listId,
155             Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_NAME_0),
156             LIST_COLUMN_NAME,
157             CmsListOrderEnum.ORDER_ASCENDING,
158             LIST_COLUMN_NAME);
159     }
160
161     /**
162      * @see org.opencms.workplace.list.A_CmsListDialog#actionDialog()
163      */

164     public void actionDialog() throws JspException JavaDoc, ServletException JavaDoc, IOException JavaDoc {
165
166         switch (getAction()) {
167             case ACTION_DELETE:
168                 Iterator JavaDoc it = CmsStringUtil.splitAsList(getGroupName(), CmsHtmlList.ITEM_SEPARATOR, true).iterator();
169                 while (it.hasNext()) {
170                     String JavaDoc name = (String JavaDoc)it.next();
171                     try {
172                         getCms().deleteGroup(name);
173                     } catch (CmsException e) {
174                         throw new CmsRuntimeException(e.getMessageContainer(), e);
175                     }
176                 }
177                 setAction(ACTION_CANCEL);
178                 actionCloseDialog();
179                 break;
180             case ACTION_TRANSFER:
181                 Map JavaDoc params = new HashMap JavaDoc();
182                 // set action parameter to initial dialog call
183
params.put(CmsDialog.PARAM_ACTION, CmsDialog.DIALOG_INITIAL);
184                 // forward to the select replacement screen
185
params.put(PARAM_GROUPID, getParamGroupid());
186                 getToolManager().jspForwardPage(
187                     this,
188                     getJsp().getRequestContext().getFolderUri() + "group_transfer.jsp",
189                     params);
190                 break;
191
192             default:
193                 super.actionDialog();
194         }
195     }
196
197     /**
198      * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlContent()
199      */

200     public String JavaDoc defaultActionHtmlContent() {
201
202         if (getList().getTotalSize() > 0) {
203             return super.defaultActionHtmlContent();
204         }
205         return "";
206     }
207
208     /**
209      * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
210      */

211     public void executeListMultiActions() {
212
213         throwListUnsupportedActionException();
214     }
215
216     /**
217      * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
218      */

219     public void executeListSingleActions() {
220
221         throwListUnsupportedActionException();
222     }
223
224     /**
225      * Returns the group Name.<p>
226      *
227      * @return the group Name
228      */

229     public String JavaDoc getGroupName() {
230
231         return m_groupName;
232     }
233
234     /**
235      * Returns the group id parameter value.<p>
236      *
237      * @return the group id parameter value
238      */

239     public String JavaDoc getParamGroupid() {
240
241         return m_paramGroupid;
242     }
243
244     /**
245      * Sets the group id parameter value.<p>
246      *
247      * @param groupId the group id parameter value
248      */

249     public void setParamGroupid(String JavaDoc groupId) {
250
251         m_paramGroupid = groupId;
252     }
253
254     /**
255      * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlEnd()
256      */

257     protected String JavaDoc customHtmlEnd() {
258
259         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
260         result.append(super.customHtmlEnd());
261         result.append("<form name='actions' method='post' action='");
262         result.append(getDialogRealUri());
263         result.append("' class='nomargin' onsubmit=\"return submitAction('ok', null, 'actions');\">\n");
264         result.append(allParamsAsHidden());
265         result.append(dialogButtonRow(HTML_START));
266         result.append("<input name='");
267         result.append(DELETE_ACTION);
268         result.append("' type='button' value='");
269         result.append(Messages.get().container(Messages.GUI_DEPENDENCIES_BUTTON_DELETE_0).key(getLocale()));
270         result.append("' onclick=\"submitAction('");
271         result.append(DELETE_ACTION);
272         result.append("', form);\" class='dialogbutton'>\n");
273         if (getList().getTotalSize() > 0) {
274             result.append("<input name='");
275             result.append(TRANSFER_ACTION);
276             result.append("' type='button' value='");
277             result.append(Messages.get().container(Messages.GUI_DEPENDENCIES_BUTTON_TRANSFER_0).key(getLocale()));
278             result.append("' onclick=\"submitAction('");
279             result.append(TRANSFER_ACTION);
280             result.append("', form);\" class='dialogbutton'>\n");
281         }
282         dialogButtonsHtml(result, BUTTON_CANCEL, "");
283         result.append(dialogButtonRow(HTML_END));
284         result.append("</form>\n");
285         return result.toString();
286     }
287
288     /**
289      * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlStart()
290      */

291     protected String JavaDoc customHtmlStart() {
292
293         StringBuffer JavaDoc result = new StringBuffer JavaDoc(512);
294         result.append(dialogBlockStart(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_NOTICE_0).key(
295             getLocale())));
296         if (getCurrentToolPath().indexOf("/edit/") < 0) {
297             result.append(key(Messages.GUI_GROUP_DEPENDENCIES_SELECTED_GROUPS_0));
298             result.append(":<br>\n");
299             List users = CmsStringUtil.splitAsList(getGroupName(), CmsHtmlList.ITEM_SEPARATOR, true);
300             result.append("<ul>\n");
301             Iterator JavaDoc it = users.iterator();
302             while (it.hasNext()) {
303                 String JavaDoc name = (String JavaDoc)it.next();
304                 result.append("<li>");
305                 result.append(name);
306                 result.append("</li>\n");
307             }
308             result.append("</ul>\n");
309         }
310         if (getList().getTotalSize() > 0) {
311             result.append(key(Messages.GUI_GROUP_DEPENDENCIES_NOTICE_TEXT_0));
312         } else {
313             result.append(key(Messages.GUI_GROUP_DEPENDENCIES_DELETE_0));
314         }
315         result.append(dialogBlockEnd());
316         return result.toString();
317     }
318
319     /**
320      * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
321      */

322     protected void fillDetails(String JavaDoc detailId) {
323
324         // no-op
325
}
326
327     /**
328      * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
329      */

330     protected List getListItems() throws CmsException {
331
332         CmsIdentifiableObjectContainer ret = new CmsIdentifiableObjectContainer(true, false);
333         Iterator JavaDoc itGroups = CmsStringUtil.splitAsList(getParamGroupid(), CmsHtmlList.ITEM_SEPARATOR, true).iterator();
334         getCms().getRequestContext().saveSiteRoot();
335         getCms().getRequestContext().setSiteRoot("/");
336         while (itGroups.hasNext()) {
337             CmsGroup group = getCms().readGroup(new CmsUUID(itGroups.next().toString()));
338             // get content
339
List resources = getCms().getResourcesForPrincipal(group.getId(), null, true);
340             Iterator JavaDoc itRes = resources.iterator();
341             while (itRes.hasNext()) {
342                 CmsResource resource = (CmsResource)itRes.next();
343                 CmsListItem item = (CmsListItem)ret.getObject(resource.getResourceId().toString());
344                 if (item == null) {
345                     item = getList().newItem(resource.getResourceId().toString());
346                     item.set(LIST_COLUMN_NAME, resource.getRootPath());
347                     item.set(LIST_COLUMN_TYPE, new Integer JavaDoc(resource.getTypeId()));
348                     Iterator JavaDoc itAces = getCms().getAccessControlEntries(resource.getRootPath(), false).iterator();
349                     while (itAces.hasNext()) {
350                         CmsAccessControlEntry ace = (CmsAccessControlEntry)itAces.next();
351                         if (ace.getPrincipal().equals(group.getId())) {
352                             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(ace.getPermissions().getPermissionString())) {
353                                 item.set(LIST_COLUMN_PERMISSIONS, group.getName()
354                                     + ": "
355                                     + ace.getPermissions().getPermissionString());
356                             }
357                             break;
358                         }
359                     }
360                     ret.addIdentifiableObject(item.getId(), item);
361                 } else {
362                     String JavaDoc oldData = (String JavaDoc)item.get(LIST_COLUMN_PERMISSIONS);
363                     Iterator JavaDoc itAces = getCms().getAccessControlEntries(resource.getRootPath(), false).iterator();
364                     while (itAces.hasNext()) {
365                         CmsAccessControlEntry ace = (CmsAccessControlEntry)itAces.next();
366                         if (ace.getPrincipal().equals(group.getId())) {
367                             if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(ace.getPermissions().getPermissionString())) {
368                                 String JavaDoc data = group.getName() + ": " + ace.getPermissions().getPermissionString();
369                                 if (oldData != null) {
370                                     data = oldData + ", " + data;
371                                 }
372                                 item.set(LIST_COLUMN_PERMISSIONS, data);
373                             }
374                             break;
375                         }
376                     }
377                 }
378             }
379             // add users
380
Iterator JavaDoc itUsers = getCms().getUsersOfGroup(group.getName()).iterator();
381             while (itUsers.hasNext()) {
382                 CmsUser user = (CmsUser)itUsers.next();
383                 CmsListItem item = (CmsListItem)ret.getObject(user.getId().toString());
384                 if (item == null) {
385                     item = getList().newItem(user.getId().toString());
386                     item.set(LIST_COLUMN_NAME, user.getName());
387                     item.set(LIST_COLUMN_PERMISSIONS, "--");
388                     ret.addIdentifiableObject(item.getId(), item);
389                 }
390             }
391             // add child groups
392
Iterator JavaDoc itChilds = getCms().getChild(group.getName()).iterator();
393             while (itChilds.hasNext()) {
394                 CmsGroup child = (CmsGroup)itChilds.next();
395                 CmsListItem item = (CmsListItem)ret.getObject(child.getId().toString());
396                 if (item == null) {
397                     item = getList().newItem(child.getId().toString());
398                     item.set(LIST_COLUMN_NAME, child.getName());
399                     item.set(LIST_COLUMN_PERMISSIONS, "--");
400                     ret.addIdentifiableObject(item.getId(), item);
401                 }
402             }
403         }
404         getCms().getRequestContext().restoreSiteRoot();
405         return ret.elementList();
406     }
407
408     /**
409      * @see org.opencms.workplace.CmsWorkplace#initMessages()
410      */

411     protected void initMessages() {
412
413         // add specific dialog resource bundle
414
addMessages(Messages.get().getBundleName());
415         // add cms dialog resource bundle
416
addMessages(org.opencms.workplace.Messages.get().getBundleName());
417         // add default resource bundles
418
super.initMessages();
419     }
420
421     /**
422      * @see org.opencms.workplace.list.A_CmsListDialog#initWorkplaceRequestValues(org.opencms.workplace.CmsWorkplaceSettings, javax.servlet.http.HttpServletRequest)
423      */

424     protected void initWorkplaceRequestValues(CmsWorkplaceSettings settings, HttpServletRequest JavaDoc request) {
425
426         super.initWorkplaceRequestValues(settings, request);
427         if (DELETE_ACTION.equals(getParamAction())) {
428             setAction(ACTION_DELETE);
429         } else if (TRANSFER_ACTION.equals(getParamAction())) {
430             setAction(ACTION_TRANSFER);
431         }
432     }
433
434     /**
435      * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
436      */

437     protected void setColumns(CmsListMetadata metadata) {
438
439         // create column for edit
440
CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
441         iconCol.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_COLS_ICON_0));
442         iconCol.setHelpText(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_COLS_ICON_HELP_0));
443         iconCol.setWidth("20");
444         iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
445         iconCol.setListItemComparator(new CmsListItemActionIconComparator());
446
447         // add icon actions
448
setIconActions(iconCol);
449
450         // add it to the list definition
451
metadata.addColumn(iconCol);
452
453         // add column for name
454
CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
455         nameCol.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_COLS_NAME_0));
456         nameCol.setWidth("80%");
457         metadata.addColumn(nameCol);
458
459         // add column for permissions
460
CmsListColumnDefinition permissionsCol = new CmsListColumnDefinition(LIST_COLUMN_PERMISSIONS);
461         permissionsCol.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_COLS_PERMISSIONS_0));
462         permissionsCol.setWidth("20%");
463         metadata.addColumn(permissionsCol);
464
465         // add column for type
466
CmsListColumnDefinition typeCol = new CmsListColumnDefinition(LIST_COLUMN_TYPE);
467         typeCol.setName(new CmsMessageContainer(null, "type"));
468         typeCol.setVisible(false);
469         metadata.addColumn(typeCol);
470     }
471
472     /**
473      * Sets the right icon actions for the dialog.<p>
474      *
475      * @param iconCol the column to set the actions
476      */

477     protected void setIconActions(CmsListColumnDefinition iconCol) {
478
479         // add resource icon action
480
CmsListDirectAction resourceIconAction = new CmsDependencyIconAction(
481             LIST_ACTION_ICON,
482             CmsDependencyIconActionType.RESOURCE,
483             getCms());
484         resourceIconAction.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_RES_NAME_0));
485         resourceIconAction.setHelpText(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_RES_HELP_0));
486         resourceIconAction.setEnabled(false);
487         iconCol.addDirectAction(resourceIconAction);
488
489         // add group icon action
490
CmsListDirectAction groupIconAction = new CmsDependencyIconAction(
491             LIST_ACTION_ICON,
492             CmsDependencyIconActionType.GROUP,
493             getCms());
494         groupIconAction.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_GRP_NAME_0));
495         groupIconAction.setHelpText(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_GRP_HELP_0));
496         groupIconAction.setEnabled(false);
497         iconCol.addDirectAction(groupIconAction);
498
499         // add group icon action
500
CmsListDirectAction userIconAction = new CmsDependencyIconAction(
501             LIST_ACTION_ICON,
502             CmsDependencyIconActionType.USER,
503             getCms());
504         userIconAction.setName(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_USR_NAME_0));
505         userIconAction.setHelpText(Messages.get().container(Messages.GUI_GROUP_DEPENDENCIES_LIST_ACTION_USR_HELP_0));
506         userIconAction.setEnabled(false);
507         iconCol.addDirectAction(userIconAction);
508     }
509
510     /**
511      * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
512      */

513     protected void setIndependentActions(CmsListMetadata metadata) {
514
515         // no-op
516
}
517
518     /**
519      * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
520      */

521     protected void setMultiActions(CmsListMetadata metadata) {
522
523         // no-op
524
}
525
526     /**
527      * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
528      */

529     protected void validateParamaters() throws Exception JavaDoc {
530
531         // test the needed parameters
532
m_groupName = "";
533         Iterator JavaDoc itGroups = CmsStringUtil.splitAsList(getParamGroupid(), CmsHtmlList.ITEM_SEPARATOR, true).iterator();
534         while (itGroups.hasNext()) {
535             CmsUUID id = new CmsUUID(itGroups.next().toString());
536             m_groupName += getCms().readGroup(id).getName();
537             if (itGroups.hasNext()) {
538                 m_groupName += CmsHtmlList.ITEM_SEPARATOR;
539             }
540         }
541     }
542 }
Popular Tags