KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > opencms > workplace > demos > list > CmsListDemo10


1 /*
2  * File : $Source$
3  * Date : $Date$
4  * Version: $Revision$
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.demos.list;
33
34 import org.opencms.file.CmsGroup;
35 import org.opencms.file.CmsUser;
36 import org.opencms.i18n.CmsMessageContainer;
37 import org.opencms.jsp.CmsJspActionElement;
38 import org.opencms.main.CmsException;
39 import org.opencms.main.CmsRuntimeException;
40 import org.opencms.util.CmsUUID;
41 import org.opencms.workplace.CmsDialog;
42 import org.opencms.workplace.list.A_CmsListDialog;
43 import org.opencms.workplace.list.CmsListColumnAlignEnum;
44 import org.opencms.workplace.list.CmsListColumnDefinition;
45 import org.opencms.workplace.list.CmsListDateMacroFormatter;
46 import org.opencms.workplace.list.CmsListDefaultAction;
47 import org.opencms.workplace.list.CmsListDirectAction;
48 import org.opencms.workplace.list.CmsListIndependentAction;
49 import org.opencms.workplace.list.CmsListItem;
50 import org.opencms.workplace.list.CmsListItemActionIconComparator;
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.CmsListMultiAction;
55 import org.opencms.workplace.list.CmsListSearchAction;
56 import org.opencms.workplace.tools.accounts.Messages;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.Date JavaDoc;
60 import java.util.HashMap JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import javax.servlet.http.HttpServletRequest JavaDoc;
66 import javax.servlet.http.HttpServletResponse JavaDoc;
67 import javax.servlet.jsp.PageContext JavaDoc;
68
69 /**
70  * User accounts list demo.<p>
71  *
72  * @author Michael Moossen
73  *
74  * @version $Revision$
75  *
76  * @since 6.0.0
77  */

78 public class CmsListDemo10 extends A_CmsListDialog {
79
80     /** list action id constant. */
81     public static final String JavaDoc LIST_ACTION_ACTIVATE = "aa";
82
83     /** list action id constant. */
84     public static final String JavaDoc LIST_ACTION_DEACTIVATE = "ac";
85
86     /** list action id constant. */
87     public static final String JavaDoc LIST_ACTION_DELETE = "ad";
88
89     /** list column id constant. */
90     public static final String JavaDoc LIST_COLUMN_ACTIVATE = "ca";
91
92     /** list column id constant. */
93     public static final String JavaDoc LIST_COLUMN_DELETE = "cd";
94
95     /** list column id constant. */
96     public static final String JavaDoc LIST_COLUMN_EMAIL = "cm";
97
98     /** list column id constant. */
99     public static final String JavaDoc LIST_COLUMN_LASTLOGIN = "cl";
100
101     /** list column id constant. */
102     public static final String JavaDoc LIST_COLUMN_LOGIN = "ci";
103
104     /** list column id constant. */
105     public static final String JavaDoc LIST_COLUMN_NAME = "cn";
106
107     /** list action id constant. */
108     public static final String JavaDoc LIST_DEFACTION_EDIT = "de";
109
110     /** list item detail id constant. */
111     public static final String JavaDoc LIST_DETAIL_GROUPS = "dg";
112
113     /** list independent action id constant. */
114     public static final String JavaDoc LIST_IACTION_REFRESH = "ir";
115
116     /** list id constant. */
117     public static final String JavaDoc LIST_ID = "lsu1";
118
119     /** list action id constant. */
120     public static final String JavaDoc LIST_MACTION_ACTIVATE = "ma";
121
122     /** parameter name for the user id. */
123     private static final String JavaDoc PARAM_USERID = "userid";
124
125     /**
126      * Public constructor.<p>
127      *
128      * @param jsp an initialized JSP action element
129      */

130     public CmsListDemo10(CmsJspActionElement jsp) {
131
132         super(jsp, LIST_ID, Messages.get().container(Messages.GUI_USERS_LIST_NAME_0), null, null, null);
133     }
134
135     /**
136      * Public constructor with JSP variables.<p>
137      *
138      * @param context the JSP page context
139      * @param req the JSP request
140      * @param res the JSP response
141      */

142     public CmsListDemo10(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
143
144         this(new CmsJspActionElement(context, req, res));
145     }
146
147     /**
148      * @see org.opencms.workplace.list.A_CmsListDialog#executeListIndepActions()
149      */

150     public void executeListIndepActions() {
151
152         if (getParamListAction().equals(LIST_IACTION_REFRESH)) {
153             refreshList();
154         }
155         super.executeListIndepActions();
156     }
157
158     /**
159      * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
160      */

161     public void executeListMultiActions() throws CmsRuntimeException {
162
163         if (getParamListAction().equals(LIST_MACTION_ACTIVATE)) {
164             // execute the activate multiaction
165
try {
166                 Iterator JavaDoc itItems = getSelectedItems().iterator();
167                 while (itItems.hasNext()) {
168                     CmsListItem listItem = (CmsListItem)itItems.next();
169                     String JavaDoc usrName = listItem.get(LIST_COLUMN_LOGIN).toString();
170                     CmsUser user = getCms().readUser(usrName);
171                     if (!user.isEnabled()) {
172                         user.setEnabled(true);
173                         //getCms().writeUser(user);
174
}
175                 }
176             } catch (CmsException e) {
177                 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ACTIVATE_SELECTED_USERS_0), e);
178             }
179             // refreshing no needed becaus the activate action does not add/remove rows to the list
180
} else {
181             throwListUnsupportedActionException();
182         }
183     }
184
185     /**
186      * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
187      */

188     public void executeListSingleActions() {
189
190         CmsListItem item = getSelectedItem();
191         CmsUUID userId = new CmsUUID(item.getId());
192         String JavaDoc userName = item.get(LIST_COLUMN_LOGIN).toString();
193
194         Map JavaDoc params = new HashMap JavaDoc();
195         params.put(PARAM_USERID, userId);
196         // set action parameter to initial dialog call
197
params.put(CmsDialog.PARAM_ACTION, CmsDialog.DIALOG_INITIAL);
198         if (getParamListAction().equals(LIST_DEFACTION_EDIT)) {
199             // do not really forward to the edit user screen in the demo
200
//getToolManager().jspForwardTool(this, getCurrentToolPath() + "/edit", params);
201
} else if (LIST_ACTION_DELETE.equals(getParamListAction())) {
202             // do not really delete the user in the demo
203
//getCms().deleteUser(userId);
204
// but remove from the list
205
getList().removeItem(userId.toString(), getLocale());
206         } else if (getParamListAction().equals(LIST_ACTION_ACTIVATE)) {
207             // execute the activate action
208
try {
209                 CmsUser user = getCms().readUser(userId);
210                 user.setEnabled(true);
211                 // do not really activate the user in the demo
212
//getCms().writeUser(user);
213
} catch (CmsException e) {
214                 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ACTIVATE_USER_1, userName), e);
215             }
216         } else if (getParamListAction().equals(LIST_ACTION_DEACTIVATE)) {
217             // execute the activate action
218
try {
219                 CmsUser user = getCms().readUser(userId);
220                 user.setEnabled(false);
221                 // do not really deactivate the user in the demo
222
//getCms().writeUser(user);
223
} catch (CmsException e) {
224                 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_DEACTIVATE_USER_1, userName), e);
225             }
226         } else {
227             throwListUnsupportedActionException();
228         }
229         listSave();
230     }
231
232     /**
233      * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
234      */

235     protected void fillDetails(String JavaDoc detailId) {
236
237         // get content
238
List JavaDoc users = getList().getAllContent();
239         Iterator JavaDoc itUsers = users.iterator();
240         while (itUsers.hasNext()) {
241             CmsListItem item = (CmsListItem)itUsers.next();
242             String JavaDoc userName = item.get(LIST_COLUMN_LOGIN).toString();
243             StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
244             try {
245                 if (detailId.equals(LIST_DETAIL_GROUPS)) {
246                     // groups
247
Iterator JavaDoc itGroups = getCms().getGroupsOfUser(userName).iterator();
248                     while (itGroups.hasNext()) {
249                         html.append(((CmsGroup)itGroups.next()).getName());
250                         if (itGroups.hasNext()) {
251                             html.append("<br>");
252                         }
253                         html.append("\n");
254                     }
255                 } else {
256                     continue;
257                 }
258             } catch (Exception JavaDoc e) {
259                 // noop
260
}
261             item.set(detailId, html.toString());
262         }
263     }
264
265     /**
266      * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
267      */

268     protected List JavaDoc getListItems() throws CmsException {
269
270         List JavaDoc ret = new ArrayList JavaDoc();
271         // get content
272
List JavaDoc users = getCms().getUsers(CmsUser.USER_TYPE_SYSTEMUSER);
273         Iterator JavaDoc itUsers = users.iterator();
274         while (itUsers.hasNext()) {
275             CmsUser user = (CmsUser)itUsers.next();
276             CmsListItem item = getList().newItem(user.getId().toString());
277             item.set(LIST_COLUMN_LOGIN, user.getName());
278             item.set(LIST_COLUMN_NAME, user.getFullName());
279             item.set(LIST_COLUMN_EMAIL, user.getEmail());
280             item.set(LIST_COLUMN_LASTLOGIN, new Date JavaDoc(user.getLastlogin()));
281             ret.add(item);
282         }
283         return ret;
284     }
285
286     /**
287      * @see org.opencms.workplace.CmsWorkplace#initMessages()
288      */

289     protected void initMessages() {
290
291         // add specific dialog resource bundle
292
addMessages(Messages.get().getBundleName());
293         addMessages(org.opencms.workplace.demos.list.Messages.get().getBundleName());
294         addMessages(org.opencms.workplace.demos.Messages.get().getBundleName());
295         // add default resource bundles
296
super.initMessages();
297     }
298
299     /**
300      * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
301      */

302     protected void setColumns(CmsListMetadata metadata) {
303
304         // create column for activation/deactivation
305
CmsListColumnDefinition actCol = new CmsListColumnDefinition(LIST_COLUMN_ACTIVATE);
306         actCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ACTIVATE_0));
307         actCol.setWidth("30");
308         actCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
309         actCol.setListItemComparator(new CmsListItemActionIconComparator());
310         // activate action
311
CmsListDirectAction actAction = new CmsListDirectAction(LIST_ACTION_ACTIVATE) {
312
313             /**
314              * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
315              */

316             public boolean isVisible() {
317
318                 String JavaDoc usrId = getItem().getId();
319                 try {
320                     return !getCms().readUser(new CmsUUID(usrId)).isEnabled();
321                 } catch (CmsException e) {
322                     return super.isVisible();
323                 }
324             }
325         };
326         actAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_ACTIVATE_NAME_0));
327         actAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_ACTIVATE_HELP_0));
328         actAction.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_ACTIVATE_CONF_0));
329         actAction.setIconPath(ICON_INACTIVE);
330         actCol.addDirectAction(actAction);
331
332         // deactivate action
333
CmsListDirectAction deactAction = new CmsListDirectAction(LIST_ACTION_DEACTIVATE) {
334
335             /**
336              * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
337              */

338             public boolean isVisible() {
339
340                 String JavaDoc usrId = getItem().getId();
341                 try {
342                     return getCms().readUser(new CmsUUID(usrId)).isEnabled();
343                 } catch (CmsException e) {
344                     return super.isVisible();
345                 }
346             }
347         };
348         deactAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DEACTIVATE_NAME_0));
349         deactAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DEACTIVATE_HELP_0));
350         deactAction.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DEACTIVATE_CONF_0));
351         deactAction.setIconPath(ICON_ACTIVE);
352         actCol.addDirectAction(deactAction);
353         metadata.addColumn(actCol);
354
355         // create column for deletion
356
CmsListColumnDefinition deleteCol = new CmsListColumnDefinition(LIST_COLUMN_DELETE);
357         deleteCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_DELETE_0));
358         deleteCol.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_COLS_DELETE_HELP_0));
359         deleteCol.setWidth("20");
360         deleteCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
361         deleteCol.setSorteable(false);
362         // add delete action
363
CmsListDirectAction deleteAction = new CmsListDirectAction(LIST_ACTION_DELETE);
364         deleteAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_NAME_0));
365         deleteAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_HELP_0));
366         deleteAction.setConfirmationMessage(new CmsMessageContainer(null, "Do you want to delete the selected user?"));
367         deleteAction.setIconPath(ICON_DELETE);
368         deleteCol.addDirectAction(deleteAction);
369         metadata.addColumn(deleteCol);
370
371         // create column for login
372
CmsListColumnDefinition loginCol = new CmsListColumnDefinition(LIST_COLUMN_LOGIN);
373         loginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LOGIN_0));
374         loginCol.setWidth("20%");
375         loginCol.setSorteable(true);
376         // create default edit action
377
CmsListDefaultAction defEditAction = new CmsListDefaultAction(LIST_DEFACTION_EDIT);
378         defEditAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_NAME_0));
379         defEditAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_HELP_0));
380         loginCol.addDefaultAction(defEditAction);
381         metadata.addColumn(loginCol);
382
383         // add column for name
384
CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
385         nameCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_USERNAME_0));
386         nameCol.setWidth("30%");
387         nameCol.setSorteable(true);
388         metadata.addColumn(nameCol);
389
390         // add column for email
391
CmsListColumnDefinition emailCol = new CmsListColumnDefinition(LIST_COLUMN_EMAIL);
392         emailCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_EMAIL_0));
393         emailCol.setWidth("30%");
394         emailCol.setSorteable(true);
395         metadata.addColumn(emailCol);
396
397         // add column for last login date
398
CmsListColumnDefinition lastLoginCol = new CmsListColumnDefinition(LIST_COLUMN_LASTLOGIN);
399         lastLoginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LASTLOGIN_0));
400         lastLoginCol.setWidth("20%");
401         lastLoginCol.setSorteable(true);
402         lastLoginCol.setFormatter(CmsListDateMacroFormatter.getDefaultDateFormatter());
403         metadata.addColumn(lastLoginCol);
404     }
405
406     /**
407      * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
408      */

409     protected void setIndependentActions(CmsListMetadata metadata) {
410
411         // add user groups details
412
CmsListItemDetails userGroupsDetails = new CmsListItemDetails(LIST_DETAIL_GROUPS);
413         userGroupsDetails.setAtColumn(LIST_COLUMN_LOGIN);
414         userGroupsDetails.setVisible(false);
415         userGroupsDetails.setShowActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_GROUPS_NAME_0));
416         userGroupsDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_GROUPS_HELP_0));
417         userGroupsDetails.setHideActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_GROUPS_NAME_0));
418         userGroupsDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_GROUPS_HELP_0));
419         userGroupsDetails.setName(Messages.get().container(Messages.GUI_USERS_DETAIL_GROUPS_NAME_0));
420         userGroupsDetails.setFormatter(new CmsListItemDetailsFormatter(Messages.get().container(
421             Messages.GUI_USERS_DETAIL_GROUPS_NAME_0)));
422         metadata.addItemDetails(userGroupsDetails);
423
424         // add refresh action
425
CmsListIndependentAction refreshAction = new CmsListIndependentAction(LIST_IACTION_REFRESH);
426         refreshAction.setName(new CmsMessageContainer(null, "Refresh"));
427         refreshAction.setHelpText(new CmsMessageContainer(null, "Click here to refresh the list"));
428         refreshAction.setIconPath(ICON_MULTI_ADD);
429         metadata.addIndependentAction(refreshAction);
430     }
431
432     /**
433      * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
434      */

435     protected void setMultiActions(CmsListMetadata metadata) {
436
437         // add the activate user multi action
438
CmsListMultiAction activateUser = new CmsListMultiAction(LIST_MACTION_ACTIVATE);
439         activateUser.setName(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_NAME_0));
440         activateUser.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_HELP_0));
441         activateUser.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_CONF_0));
442         activateUser.setIconPath(ICON_MULTI_ACTIVATE);
443         metadata.addMultiAction(activateUser);
444
445         // makes the list searchable by several columns
446
CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_LOGIN));
447         searchAction.addColumn(metadata.getColumnDefinition(LIST_COLUMN_EMAIL));
448         metadata.setSearchAction(searchAction);
449     }
450
451 }
Popular Tags