KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * File : $Source: /usr/local/cvs/opencms/src-modules/org/opencms/workplace/tools/accounts/CmsUserTransferList.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.file.CmsUser;
36 import org.opencms.jsp.CmsJspActionElement;
37 import org.opencms.main.CmsException;
38 import org.opencms.main.CmsRuntimeException;
39 import org.opencms.security.CmsPrincipal;
40 import org.opencms.util.CmsRequestUtil;
41 import org.opencms.util.CmsStringUtil;
42 import org.opencms.util.CmsUUID;
43 import org.opencms.workplace.list.A_CmsListDialog;
44 import org.opencms.workplace.list.CmsHtmlList;
45 import org.opencms.workplace.list.CmsListColumnAlignEnum;
46 import org.opencms.workplace.list.CmsListColumnDefinition;
47 import org.opencms.workplace.list.CmsListDateMacroFormatter;
48 import org.opencms.workplace.list.CmsListDefaultAction;
49 import org.opencms.workplace.list.CmsListDirectAction;
50 import org.opencms.workplace.list.CmsListItem;
51 import org.opencms.workplace.list.CmsListItemDetails;
52 import org.opencms.workplace.list.CmsListItemDetailsFormatter;
53 import org.opencms.workplace.list.CmsListMetadata;
54 import org.opencms.workplace.list.CmsListOrderEnum;
55 import org.opencms.workplace.list.CmsListSearchAction;
56
57 import java.io.IOException JavaDoc;
58 import java.util.ArrayList JavaDoc;
59 import java.util.Date JavaDoc;
60 import java.util.HashSet JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Set JavaDoc;
64
65 import javax.servlet.ServletException JavaDoc;
66 import javax.servlet.http.HttpServletRequest JavaDoc;
67 import javax.servlet.http.HttpServletResponse JavaDoc;
68 import javax.servlet.jsp.PageContext JavaDoc;
69
70 /**
71  * Allows to select an user to transfer the permissions and attributes from list of previous selected users.<p>
72  *
73  * @author Michael Moossen
74  *
75  * @version $Revision: 1.3 $
76  *
77  * @since 6.0.0
78  */

79 public class CmsUserTransferList extends A_CmsListDialog {
80
81     /** list action id constant. */
82     public static final String JavaDoc LIST_ACTION_TRANSFER = "at";
83
84     /** list column id constant. */
85     public static final String JavaDoc LIST_COLUMN_EMAIL = "cm";
86
87     /** list column id constant. */
88     public static final String JavaDoc LIST_COLUMN_LASTLOGIN = "cl";
89
90     /** list column id constant. */
91     public static final String JavaDoc LIST_COLUMN_LOGIN = "ci";
92
93     /** list column id constant. */
94     public static final String JavaDoc LIST_COLUMN_NAME = "cn";
95
96     /** list column id constant. */
97     public static final String JavaDoc LIST_COLUMN_TRANSFER = "ct";
98
99     /** list action id constant. */
100     public static final String JavaDoc LIST_DEFACTION_TRANSFER = "dt";
101
102     /** list item detail id constant. */
103     public static final String JavaDoc LIST_DETAIL_ADDRESS = "da";
104
105     /** list item detail id constant. */
106     public static final String JavaDoc LIST_DETAIL_GROUPS = "dg";
107
108     /** List id constant. */
109     public static final String JavaDoc LIST_ID = "lut";
110
111     /** Path to the list buttons. */
112     public static final String JavaDoc PATH_BUTTONS = "tools/accounts/buttons/";
113
114     /** Stores the value of the request parameter for the user id, could be a list of ids. */
115     private String JavaDoc m_paramUserid;
116
117     /** Stores the value of the users name, could be a list of names. */
118     private String JavaDoc m_userName;
119
120     /**
121      * Public constructor.<p>
122      *
123      * @param jsp an initialized JSP action element
124      */

125     public CmsUserTransferList(CmsJspActionElement jsp) {
126
127         this(LIST_ID, jsp);
128     }
129
130     /**
131      * Protected constructor.<p>
132      *
133      * @param listId the id of the specialized list
134      * @param jsp an initialized JSP action element
135      */

136     protected CmsUserTransferList(String JavaDoc listId, CmsJspActionElement jsp) {
137
138         super(
139             jsp,
140             listId,
141             Messages.get().container(Messages.GUI_USERS_TRANSFER_LIST_NAME_0),
142             LIST_COLUMN_NAME,
143             CmsListOrderEnum.ORDER_ASCENDING,
144             null);
145     }
146
147     /**
148      * Public constructor with JSP variables.<p>
149      *
150      * @param context the JSP page context
151      * @param req the JSP request
152      * @param res the JSP response
153      */

154     public CmsUserTransferList(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
155
156         this(new CmsJspActionElement(context, req, res));
157     }
158
159     /**
160      * @see org.opencms.workplace.list.A_CmsListDialog#customHtmlStart()
161      */

162     protected String JavaDoc customHtmlStart() {
163
164         StringBuffer JavaDoc result = new StringBuffer JavaDoc(2048);
165         result.append(dialogBlockStart(Messages.get().container(Messages.GUI_USERS_TRANSFER_NOTICE_0).key(getLocale())));
166         result.append("\n");
167         if (getCurrentToolPath().indexOf("/edit/") < 0) {
168             result.append(key(Messages.GUI_USER_DEPENDENCIES_SELECTED_USERS_0));
169             result.append(":<br>\n");
170             List users = CmsStringUtil.splitAsList(getUserName(), CmsHtmlList.ITEM_SEPARATOR, true);
171             result.append("<ul>\n");
172             Iterator JavaDoc it = users.iterator();
173             while (it.hasNext()) {
174                 String JavaDoc name = (String JavaDoc)it.next();
175                 result.append("<li>");
176                 result.append(name);
177                 result.append("</li>\n");
178             }
179             result.append("</ul>\n");
180         }
181         result.append(key(Messages.GUI_USERS_TRANSFER_NOTICE_TEXT_0));
182         result.append(dialogBlockEnd());
183         return result.toString();
184     }
185
186     /**
187      * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
188      */

189     public void executeListMultiActions() throws CmsRuntimeException {
190
191         throwListUnsupportedActionException();
192     }
193
194     /**
195      * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
196      */

197     public void executeListSingleActions() throws IOException JavaDoc, ServletException JavaDoc {
198
199         if (getParamListAction().equals(LIST_ACTION_TRANSFER) || getParamListAction().equals(LIST_DEFACTION_TRANSFER)) {
200             // execute the delete action
201
try {
202                 Iterator JavaDoc it = CmsStringUtil.splitAsList(getParamUserid(), CmsHtmlList.ITEM_SEPARATOR, true).iterator();
203                 while (it.hasNext()) {
204                     CmsUUID id = new CmsUUID((String JavaDoc)it.next());
205                     getCms().deleteUser(id, new CmsUUID(getSelectedItem().getId()));
206                 }
207                 CmsRequestUtil.forwardRequest(getParamCloseLink(), getJsp().getRequest(), getJsp().getResponse());
208                 setForwarded(true);
209             } catch (CmsException e) {
210                 throw new CmsRuntimeException(Messages.get().container(
211                     Messages.ERR_TRANSFER_USER_1,
212                     getSelectedItem().get(LIST_COLUMN_NAME)), e);
213             }
214         } else {
215             throwListUnsupportedActionException();
216         }
217         listSave();
218     }
219
220     /**
221      * Returns the user id parameter value.<p>
222      *
223      * @return the user id parameter value
224      */

225     public String JavaDoc getParamUserid() {
226
227         return m_paramUserid;
228     }
229
230     /**
231      * Returns the users Name.<p>
232      *
233      * @return the users Name
234      */

235     public String JavaDoc getUserName() {
236
237         return m_userName;
238     }
239
240     /**
241      * Sets the user id parameter value.<p>
242      *
243      * @param userId the user id parameter value
244      */

245     public void setParamUserid(String JavaDoc userId) {
246
247         m_paramUserid = userId;
248     }
249
250     /**
251      * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
252      */

253     protected void fillDetails(String JavaDoc detailId) {
254
255         // get content
256
List users = getList().getAllContent();
257         Iterator JavaDoc itUsers = users.iterator();
258         while (itUsers.hasNext()) {
259             CmsListItem item = (CmsListItem)itUsers.next();
260             String JavaDoc userName = item.get(LIST_COLUMN_LOGIN).toString();
261             StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
262             try {
263                 if (detailId.equals(LIST_DETAIL_ADDRESS)) {
264                     CmsUser user = getCms().readUser(userName);
265                     // address
266
html.append(user.getAddress());
267                     if (user.getCity() != null) {
268                         html.append("<br>");
269                         if (user.getZipcode() != null) {
270                             html.append(user.getZipcode());
271                             html.append(" ");
272                         }
273                         html.append(user.getCity());
274                     }
275                     if (user.getCountry() != null) {
276                         html.append("<br>");
277                         html.append(user.getCountry());
278                     }
279                 } else if (detailId.equals(LIST_DETAIL_GROUPS)) {
280                     // groups
281
Iterator JavaDoc itGroups = getCms().getGroupsOfUser(userName).iterator();
282                     while (itGroups.hasNext()) {
283                         html.append(((CmsGroup)itGroups.next()).getName());
284                         if (itGroups.hasNext()) {
285                             html.append("<br>");
286                         }
287                         html.append("\n");
288                     }
289                 } else {
290                     continue;
291                 }
292             } catch (Exception JavaDoc e) {
293                 // noop
294
}
295             item.set(detailId, html.toString());
296         }
297     }
298
299     /**
300      * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
301      */

302     protected List getListItems() throws CmsException {
303
304         List ret = new ArrayList JavaDoc();
305         // get content
306
List users = getUsers();
307         Set JavaDoc selUsers = new HashSet JavaDoc(CmsStringUtil.splitAsList(getParamUserid(), CmsHtmlList.ITEM_SEPARATOR, true));
308         Iterator JavaDoc itUsers = users.iterator();
309         while (itUsers.hasNext()) {
310             CmsUser user = (CmsUser)itUsers.next();
311             if (selUsers.contains(user.getId().toString())) {
312                 continue;
313             }
314             CmsListItem item = getList().newItem(user.getId().toString());
315             item.set(LIST_COLUMN_LOGIN, user.getName());
316             item.set(LIST_COLUMN_NAME, user.getFullName());
317             item.set(LIST_COLUMN_EMAIL, user.getEmail());
318             item.set(LIST_COLUMN_LASTLOGIN, new Date JavaDoc(user.getLastlogin()));
319             ret.add(item);
320         }
321         return ret;
322     }
323
324     /**
325      * Returns the list of users to display.<p>
326      *
327      * @return the list of users to display
328      *
329      * @throws CmsException if something goes wrong
330      */

331     protected List getUsers() throws CmsException {
332
333         return CmsPrincipal.filterCore(getCms().getUsers());
334     }
335
336     /**
337      * @see org.opencms.workplace.CmsWorkplace#initMessages()
338      */

339     protected void initMessages() {
340
341         // add specific dialog resource bundle
342
addMessages(Messages.get().getBundleName());
343         // add default resource bundles
344
super.initMessages();
345     }
346
347     /**
348      * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
349      */

350     protected void setColumns(CmsListMetadata metadata) {
351
352         // create column for transfer
353
CmsListColumnDefinition transferCol = new CmsListColumnDefinition(LIST_COLUMN_TRANSFER);
354         transferCol.setName(Messages.get().container(Messages.GUI_USERS_TRANSFER_LIST_COLS_TRANSFER_0));
355         transferCol.setHelpText(Messages.get().container(Messages.GUI_USERS_TRANSFER_LIST_COLS_TRANSFER_HELP_0));
356         transferCol.setWidth("20");
357         transferCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
358         transferCol.setSorteable(false);
359
360         // add transfer action
361
setTransferAction(transferCol);
362
363         // add it to the list definition
364
metadata.addColumn(transferCol);
365
366         // create column for login
367
CmsListColumnDefinition loginCol = new CmsListColumnDefinition(LIST_COLUMN_LOGIN);
368         loginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LOGIN_0));
369         loginCol.setWidth("20%");
370
371         // create default transfer action
372
CmsListDefaultAction defTransferAction = new CmsListDefaultAction(LIST_DEFACTION_TRANSFER);
373         defTransferAction.setName(Messages.get().container(Messages.GUI_USERS_TRANSFER_LIST_DEFACTION_TRANSFER_NAME_0));
374         defTransferAction.setHelpText(Messages.get().container(
375             Messages.GUI_USERS_TRANSFER_LIST_DEFACTION_TRANSFER_HELP_0));
376         loginCol.addDefaultAction(defTransferAction);
377
378         // add it to the list definition
379
metadata.addColumn(loginCol);
380
381         // add column for name
382
CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
383         nameCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_USERNAME_0));
384         nameCol.setWidth("30%");
385         metadata.addColumn(nameCol);
386
387         // add column for email
388
CmsListColumnDefinition emailCol = new CmsListColumnDefinition(LIST_COLUMN_EMAIL);
389         emailCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_EMAIL_0));
390         emailCol.setWidth("30%");
391         metadata.addColumn(emailCol);
392
393         // add column for last login date
394
CmsListColumnDefinition lastLoginCol = new CmsListColumnDefinition(LIST_COLUMN_LASTLOGIN);
395         lastLoginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LASTLOGIN_0));
396         lastLoginCol.setWidth("20%");
397         lastLoginCol.setFormatter(CmsListDateMacroFormatter.getDefaultDateFormatter());
398         metadata.addColumn(lastLoginCol);
399     }
400
401     /**
402      * Sets the icon actions for the transfer list.<p>
403      *
404      * @param transferCol the column to set the action
405      */

406     protected void setTransferAction(CmsListColumnDefinition transferCol) {
407
408         CmsListDirectAction transferAction = new CmsListDirectAction(LIST_ACTION_TRANSFER);
409         transferAction.setName(Messages.get().container(Messages.GUI_USERS_TRANSFER_LIST_ACTION_TRANSFER_NAME_0));
410         transferAction.setHelpText(Messages.get().container(Messages.GUI_USERS_TRANSFER_LIST_ACTION_TRANSFER_HELP_0));
411         transferAction.setIconPath(PATH_BUTTONS + "user.png");
412         transferCol.addDirectAction(transferAction);
413     }
414
415     /**
416      * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
417      */

418     protected void setIndependentActions(CmsListMetadata metadata) {
419
420         // add user address details
421
CmsListItemDetails userAddressDetails = new CmsListItemDetails(LIST_DETAIL_ADDRESS);
422         userAddressDetails.setAtColumn(LIST_COLUMN_LOGIN);
423         userAddressDetails.setVisible(false);
424         userAddressDetails.setShowActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_ADDRESS_NAME_0));
425         userAddressDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_ADDRESS_HELP_0));
426         userAddressDetails.setHideActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_ADDRESS_NAME_0));
427         userAddressDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_ADDRESS_HELP_0));
428         userAddressDetails.setName(Messages.get().container(Messages.GUI_USERS_DETAIL_ADDRESS_NAME_0));
429         userAddressDetails.setFormatter(new CmsListItemDetailsFormatter(Messages.get().container(
430             Messages.GUI_USERS_DETAIL_ADDRESS_NAME_0)));
431         metadata.addItemDetails(userAddressDetails);
432
433         // add user groups details
434
CmsListItemDetails userGroupsDetails = new CmsListItemDetails(LIST_DETAIL_GROUPS);
435         userGroupsDetails.setAtColumn(LIST_COLUMN_LOGIN);
436         userGroupsDetails.setVisible(false);
437         userGroupsDetails.setShowActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_GROUPS_NAME_0));
438         userGroupsDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_GROUPS_HELP_0));
439         userGroupsDetails.setHideActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_GROUPS_NAME_0));
440         userGroupsDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_GROUPS_HELP_0));
441         userGroupsDetails.setName(Messages.get().container(Messages.GUI_USERS_DETAIL_GROUPS_NAME_0));
442         userGroupsDetails.setFormatter(new CmsListItemDetailsFormatter(Messages.get().container(
443             Messages.GUI_USERS_DETAIL_GROUPS_NAME_0)));
444         metadata.addItemDetails(userGroupsDetails);
445
446         // makes the list searchable
447
CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_LOGIN));
448         searchAction.addColumn(metadata.getColumnDefinition(LIST_COLUMN_NAME));
449         metadata.setSearchAction(searchAction);
450     }
451
452     /**
453      * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
454      */

455     protected void setMultiActions(CmsListMetadata metadata) {
456
457         // no-op
458
}
459
460     /**
461      * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
462      */

463     protected void validateParamaters() throws Exception JavaDoc {
464
465         m_userName = "";
466         Iterator JavaDoc itUsers = CmsStringUtil.splitAsList(getParamUserid(), CmsHtmlList.ITEM_SEPARATOR, true).iterator();
467         while (itUsers.hasNext()) {
468             CmsUUID id = new CmsUUID(itUsers.next().toString());
469             m_userName += getCms().readUser(id).getName();
470             if (itUsers.hasNext()) {
471                 m_userName += CmsHtmlList.ITEM_SEPARATOR;
472             }
473         }
474     }
475 }
Popular Tags