KickJava   Java API By Example, From Geeks To Geeks.

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


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 CmsListDemo15b 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 = "lsu2";
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 CmsListDemo15b(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 CmsListDemo15b(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#defaultActionHtmlStart()
149      */

150     public String JavaDoc defaultActionHtmlStart() {
151
152         StringBuffer JavaDoc result = new StringBuffer JavaDoc(2048);
153         result.append(dialogContentStart(getParamTitle()));
154         return result.toString();
155     }
156
157     /**
158      * @see org.opencms.workplace.list.A_CmsListDialog#executeListIndepActions()
159      */

160     public void executeListIndepActions() {
161
162         if (getParamListAction().equals(LIST_IACTION_REFRESH)) {
163             refreshList();
164         }
165         super.executeListIndepActions();
166     }
167
168     /**
169      * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
170      */

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

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

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

278     protected List JavaDoc getListItems() throws CmsException {
279
280         List JavaDoc ret = new ArrayList JavaDoc();
281         // get content
282
List JavaDoc users = getCms().getUsers(CmsUser.USER_TYPE_SYSTEMUSER);
283         Iterator JavaDoc itUsers = users.iterator();
284         while (itUsers.hasNext()) {
285             CmsUser user = (CmsUser)itUsers.next();
286             CmsListItem item = getList().newItem(user.getId().toString());
287             item.set(LIST_COLUMN_LOGIN, user.getName());
288             item.set(LIST_COLUMN_NAME, user.getFullName());
289             item.set(LIST_COLUMN_EMAIL, user.getEmail());
290             item.set(LIST_COLUMN_LASTLOGIN, new Date JavaDoc(user.getLastlogin()));
291             ret.add(item);
292         }
293         return ret;
294     }
295
296     /**
297      * @see org.opencms.workplace.CmsWorkplace#initMessages()
298      */

299     protected void initMessages() {
300
301         // add specific dialog resource bundle
302
addMessages(Messages.get().getBundleName());
303         addMessages(org.opencms.workplace.demos.list.Messages.get().getBundleName());
304         addMessages(org.opencms.workplace.demos.Messages.get().getBundleName());
305         // add default resource bundles
306
super.initMessages();
307     }
308
309     /**
310      * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
311      */

312     protected void setColumns(CmsListMetadata metadata) {
313
314         // create column for activation/deactivation
315
CmsListColumnDefinition actCol = new CmsListColumnDefinition(LIST_COLUMN_ACTIVATE);
316         actCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ACTIVATE_0));
317         actCol.setWidth("30");
318         actCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
319         actCol.setListItemComparator(new CmsListItemActionIconComparator());
320         // activate action
321
CmsListDirectAction actAction = new CmsListDirectAction(LIST_ACTION_ACTIVATE) {
322
323             /**
324              * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
325              */

326             public boolean isVisible() {
327
328                 String JavaDoc usrId = getItem().getId();
329                 try {
330                     return !getCms().readUser(new CmsUUID(usrId)).isEnabled();
331                 } catch (CmsException e) {
332                     return super.isVisible();
333                 }
334             }
335         };
336         actAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_ACTIVATE_NAME_0));
337         actAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_ACTIVATE_HELP_0));
338         actAction.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_ACTIVATE_CONF_0));
339         actAction.setIconPath(ICON_INACTIVE);
340         actCol.addDirectAction(actAction);
341
342         // deactivate action
343
CmsListDirectAction deactAction = new CmsListDirectAction(LIST_ACTION_DEACTIVATE) {
344
345             /**
346              * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
347              */

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

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

445     protected void setMultiActions(CmsListMetadata metadata) {
446
447         // add the activate user multi action
448
CmsListMultiAction activateUser = new CmsListMultiAction(LIST_MACTION_ACTIVATE);
449         activateUser.setName(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_NAME_0));
450         activateUser.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_HELP_0));
451         activateUser.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_CONF_0));
452         activateUser.setIconPath(ICON_MULTI_ACTIVATE);
453         metadata.addMultiAction(activateUser);
454
455         // makes the list searchable by several columns
456
CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_LOGIN));
457         searchAction.addColumn(metadata.getColumnDefinition(LIST_COLUMN_EMAIL));
458         metadata.setSearchAction(searchAction);
459     }
460
461 }
Popular Tags