KickJava   Java API By Example, From Geeks To Geeks.

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


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.CmsStringUtil;
41 import org.opencms.util.CmsUUID;
42 import org.opencms.workplace.CmsDialog;
43 import org.opencms.workplace.list.A_CmsListDialog;
44 import org.opencms.workplace.list.A_CmsListDirectJsAction;
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.CmsListIndependentAction;
51 import org.opencms.workplace.list.CmsListItem;
52 import org.opencms.workplace.list.CmsListItemActionIconComparator;
53 import org.opencms.workplace.list.CmsListItemDetails;
54 import org.opencms.workplace.list.CmsListItemDetailsFormatter;
55 import org.opencms.workplace.list.CmsListMetadata;
56 import org.opencms.workplace.list.CmsListMultiAction;
57 import org.opencms.workplace.list.CmsListSearchAction;
58 import org.opencms.workplace.tools.accounts.Messages;
59
60 import java.util.ArrayList JavaDoc;
61 import java.util.Date JavaDoc;
62 import java.util.HashMap JavaDoc;
63 import java.util.Iterator JavaDoc;
64 import java.util.List JavaDoc;
65 import java.util.Map JavaDoc;
66
67 import javax.servlet.http.HttpServletRequest JavaDoc;
68 import javax.servlet.http.HttpServletResponse JavaDoc;
69 import javax.servlet.jsp.PageContext JavaDoc;
70
71 /**
72  * User accounts list demo.<p>
73  *
74  * @author Michael Moossen
75  *
76  * @version $Revision$
77  *
78  * @since 6.0.0
79  */

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

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

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

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

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

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

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

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

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

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

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

340             public boolean isVisible() {
341
342                 String JavaDoc usrId = getItem().getId();
343                 try {
344                     return getCms().readUser(new CmsUUID(usrId)).isEnabled();
345                 } catch (CmsException e) {
346                     return super.isVisible();
347                 }
348             }
349         };
350         deactAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DEACTIVATE_NAME_0));
351         deactAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DEACTIVATE_HELP_0));
352         deactAction.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DEACTIVATE_CONF_0));
353         deactAction.setIconPath(ICON_ACTIVE);
354         actCol.addDirectAction(deactAction);
355         metadata.addColumn(actCol);
356
357         // create column for deletion
358
CmsListColumnDefinition deleteCol = new CmsListColumnDefinition(LIST_COLUMN_DELETE);
359         deleteCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_DELETE_0));
360         deleteCol.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_COLS_DELETE_HELP_0));
361         deleteCol.setWidth("20");
362         deleteCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
363         deleteCol.setSorteable(false);
364         // add delete action
365
CmsListDirectAction deleteAction = new A_CmsListDirectJsAction(LIST_ACTION_DELETE) {
366
367             /**
368              * @see org.opencms.workplace.list.A_CmsListDirectJsAction#jsCode()
369              */

370             public String JavaDoc jsCode() {
371
372                 String JavaDoc email = (String JavaDoc)getItem().get(LIST_COLUMN_EMAIL);
373                 return "alert('the email of the select user is:" + CmsStringUtil.escapeJavaScript(email) + "');";
374             }
375         };
376         deleteAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_NAME_0));
377         deleteAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_HELP_0));
378         deleteAction.setConfirmationMessage(new CmsMessageContainer(null, "Do you want to delete the selected user?"));
379         deleteAction.setIconPath(ICON_DELETE);
380         deleteCol.addDirectAction(deleteAction);
381         metadata.addColumn(deleteCol);
382
383         // create column for login
384
CmsListColumnDefinition loginCol = new CmsListColumnDefinition(LIST_COLUMN_LOGIN);
385         loginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LOGIN_0));
386         loginCol.setWidth("20%");
387         loginCol.setSorteable(true);
388         // create default edit action
389
CmsListDefaultAction defEditAction = new CmsListDefaultAction(LIST_DEFACTION_EDIT);
390         defEditAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_NAME_0));
391         defEditAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_HELP_0));
392         loginCol.addDefaultAction(defEditAction);
393         metadata.addColumn(loginCol);
394
395         // add column for name
396
CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
397         nameCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_USERNAME_0));
398         nameCol.setWidth("30%");
399         nameCol.setSorteable(true);
400         metadata.addColumn(nameCol);
401
402         // add column for email
403
CmsListColumnDefinition emailCol = new CmsListColumnDefinition(LIST_COLUMN_EMAIL);
404         emailCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_EMAIL_0));
405         emailCol.setWidth("30%");
406         emailCol.setSorteable(true);
407         metadata.addColumn(emailCol);
408
409         // add column for last login date
410
CmsListColumnDefinition lastLoginCol = new CmsListColumnDefinition(LIST_COLUMN_LASTLOGIN);
411         lastLoginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LASTLOGIN_0));
412         lastLoginCol.setWidth("20%");
413         lastLoginCol.setSorteable(true);
414         lastLoginCol.setFormatter(CmsListDateMacroFormatter.getDefaultDateFormatter());
415         metadata.addColumn(lastLoginCol);
416     }
417
418     /**
419      * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
420      */

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

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