KickJava   Java API By Example, From Geeks To Geeks.

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


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.CmsListItemDetails;
51 import org.opencms.workplace.list.CmsListItemDetailsFormatter;
52 import org.opencms.workplace.list.CmsListMetadata;
53 import org.opencms.workplace.list.CmsListMultiAction;
54 import org.opencms.workplace.list.CmsListPrintIAction;
55 import org.opencms.workplace.list.CmsListSearchAction;
56 import org.opencms.workplace.list.I_CmsListFormatter;
57 import org.opencms.workplace.tools.accounts.Messages;
58
59 import java.util.ArrayList JavaDoc;
60 import java.util.Date JavaDoc;
61 import java.util.HashMap JavaDoc;
62 import java.util.Iterator JavaDoc;
63 import java.util.List JavaDoc;
64 import java.util.Locale 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 CmsListDemo9 extends A_CmsListDialog {
81
82     /** list action id constant. */
83     public static final String JavaDoc LIST_ACTION_DELETE = "ad";
84
85     /** list column id constant. */
86     public static final String JavaDoc LIST_COLUMN_ACTIVATE = "ca";
87
88     /** list column id constant. */
89     public static final String JavaDoc LIST_COLUMN_DELETE = "cd";
90
91     /** list column id constant. */
92     public static final String JavaDoc LIST_COLUMN_EMAIL = "cm";
93
94     /** list column id constant. */
95     public static final String JavaDoc LIST_COLUMN_LASTLOGIN = "cl";
96
97     /** list column id constant. */
98     public static final String JavaDoc LIST_COLUMN_LOGIN = "ci";
99
100     /** list column id constant. */
101     public static final String JavaDoc LIST_COLUMN_NAME = "cn";
102
103     /** list action id constant. */
104     public static final String JavaDoc LIST_DEFACTION_EDIT = "de";
105
106     /** list item detail id constant. */
107     public static final String JavaDoc LIST_DETAIL_GROUPS = "dg";
108
109     /** list independent action id constant. */
110     public static final String JavaDoc LIST_IACTION_REFRESH = "ir";
111
112     /** list id constant. */
113     public static final String JavaDoc LIST_ID = "lsu1";
114
115     /** list action id constant. */
116     public static final String JavaDoc LIST_MACTION_ACTIVATE = "ma";
117
118     /** parameter name for the user id. */
119     private static final String JavaDoc PARAM_USERID = "userid";
120
121     /**
122      * Public constructor.<p>
123      *
124      * @param jsp an initialized JSP action element
125      */

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

138     public CmsListDemo9(PageContext JavaDoc context, HttpServletRequest JavaDoc req, HttpServletResponse JavaDoc res) {
139
140         this(new CmsJspActionElement(context, req, res));
141     }
142
143     /**
144      * @see org.opencms.workplace.list.A_CmsListDialog#executeListIndepActions()
145      */

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

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

184     public void executeListSingleActions() {
185
186         CmsListItem item = getSelectedItem();
187         CmsUUID userId = new CmsUUID(item.getId());
188
189         Map JavaDoc params = new HashMap JavaDoc();
190         params.put(PARAM_USERID, userId);
191         // set action parameter to initial dialog call
192
params.put(CmsDialog.PARAM_ACTION, CmsDialog.DIALOG_INITIAL);
193         if (getParamListAction().equals(LIST_DEFACTION_EDIT)) {
194             // do not really forward to the edit user screen in the demo
195
//getToolManager().jspForwardTool(this, getCurrentToolPath() + "/edit", params);
196
} else if (LIST_ACTION_DELETE.equals(getParamListAction())) {
197             // do not really delete the user in the demo
198
//getCms().deleteUser(userId);
199
// but remove from the list
200
getList().removeItem(userId.toString(), getLocale());
201         } else {
202             throwListUnsupportedActionException();
203         }
204         listSave();
205     }
206
207     /**
208      * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
209      */

210     protected void fillDetails(String JavaDoc detailId) {
211
212         // get content
213
List JavaDoc users = getList().getAllContent();
214         Iterator JavaDoc itUsers = users.iterator();
215         while (itUsers.hasNext()) {
216             CmsListItem item = (CmsListItem)itUsers.next();
217             String JavaDoc userName = item.get(LIST_COLUMN_LOGIN).toString();
218             StringBuffer JavaDoc html = new StringBuffer JavaDoc(512);
219             try {
220                 if (detailId.equals(LIST_DETAIL_GROUPS)) {
221                     // groups
222
Iterator JavaDoc itGroups = getCms().getGroupsOfUser(userName).iterator();
223                     while (itGroups.hasNext()) {
224                         html.append(((CmsGroup)itGroups.next()).getName());
225                         if (itGroups.hasNext()) {
226                             html.append("<br>");
227                         }
228                         html.append("\n");
229                     }
230                 } else {
231                     continue;
232                 }
233             } catch (Exception JavaDoc e) {
234                 // noop
235
}
236             item.set(detailId, html.toString());
237         }
238     }
239
240     /**
241      * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
242      */

243     protected List JavaDoc getListItems() throws CmsException {
244
245         List JavaDoc ret = new ArrayList JavaDoc();
246         // get content
247
List JavaDoc users = getCms().getUsers(CmsUser.USER_TYPE_SYSTEMUSER);
248         Iterator JavaDoc itUsers = users.iterator();
249         while (itUsers.hasNext()) {
250             CmsUser user = (CmsUser)itUsers.next();
251             CmsListItem item = getList().newItem(user.getId().toString());
252             item.set(LIST_COLUMN_ACTIVATE, Boolean.valueOf(user.isEnabled()));
253             item.set(LIST_COLUMN_LOGIN, user.getName());
254             item.set(LIST_COLUMN_NAME, user.getFullName());
255             item.set(LIST_COLUMN_EMAIL, user.getEmail());
256             item.set(LIST_COLUMN_LASTLOGIN, new Date JavaDoc(user.getLastlogin()));
257             ret.add(item);
258         }
259         return ret;
260     }
261
262     /**
263      * @see org.opencms.workplace.CmsWorkplace#initMessages()
264      */

265     protected void initMessages() {
266
267         // add specific dialog resource bundle
268
addMessages(Messages.get().getBundleName());
269         addMessages(org.opencms.workplace.demos.list.Messages.get().getBundleName());
270         addMessages(org.opencms.workplace.demos.Messages.get().getBundleName());
271         // add default resource bundles
272
super.initMessages();
273     }
274
275     /**
276      * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
277      */

278     protected void setColumns(CmsListMetadata metadata) {
279
280         // create column for activation/deactivation
281
CmsListColumnDefinition actCol = new CmsListColumnDefinition(LIST_COLUMN_ACTIVATE);
282         actCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ACTIVATE_0));
283         actCol.setWidth("30");
284         actCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
285         actCol.setSorteable(true);
286         I_CmsListFormatter actFormatter = new I_CmsListFormatter() {
287
288             /**
289              * @see org.opencms.workplace.list.I_CmsListFormatter#format(java.lang.Object, java.util.Locale)
290              */

291             public String JavaDoc format(Object JavaDoc data, Locale JavaDoc locale) {
292
293                 if (data instanceof Boolean JavaDoc) {
294                     Boolean JavaDoc val = (Boolean JavaDoc)data;
295                     if (val.booleanValue()) {
296                         return "<img SRC='" + getResourceUri("list/active.png") + "'>";
297                     }
298                 }
299                 return "<img SRC='" + getResourceUri("list/inactive.png") + "'>";
300             }
301
302         };
303         actCol.setFormatter(actFormatter);
304         metadata.addColumn(actCol);
305
306         // create column for deletion
307
CmsListColumnDefinition deleteCol = new CmsListColumnDefinition(LIST_COLUMN_DELETE);
308         deleteCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_DELETE_0));
309         deleteCol.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_COLS_DELETE_HELP_0));
310         deleteCol.setWidth("20");
311         deleteCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
312         deleteCol.setSorteable(false);
313         // add delete action
314
CmsListDirectAction deleteAction = new CmsListDirectAction(LIST_ACTION_DELETE);
315         deleteAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_NAME_0));
316         deleteAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_HELP_0));
317         deleteAction.setConfirmationMessage(new CmsMessageContainer(null, "Do you want to delete the selected user?"));
318         deleteAction.setIconPath(ICON_DELETE);
319         deleteCol.addDirectAction(deleteAction);
320         metadata.addColumn(deleteCol);
321
322         // create column for login
323
CmsListColumnDefinition loginCol = new CmsListColumnDefinition(LIST_COLUMN_LOGIN);
324         loginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LOGIN_0));
325         loginCol.setWidth("20%");
326         loginCol.setSorteable(true);
327         // create default edit action
328
CmsListDefaultAction defEditAction = new CmsListDefaultAction(LIST_DEFACTION_EDIT);
329         defEditAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_NAME_0));
330         defEditAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_HELP_0));
331         loginCol.addDefaultAction(defEditAction);
332         metadata.addColumn(loginCol);
333
334         // add column for name
335
CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
336         nameCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_USERNAME_0));
337         nameCol.setWidth("30%");
338         nameCol.setSorteable(true);
339         metadata.addColumn(nameCol);
340
341         // add column for email
342
CmsListColumnDefinition emailCol = new CmsListColumnDefinition(LIST_COLUMN_EMAIL);
343         emailCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_EMAIL_0));
344         emailCol.setWidth("30%");
345         emailCol.setSorteable(true);
346         metadata.addColumn(emailCol);
347
348         // add column for last login date
349
CmsListColumnDefinition lastLoginCol = new CmsListColumnDefinition(LIST_COLUMN_LASTLOGIN);
350         lastLoginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LASTLOGIN_0));
351         lastLoginCol.setWidth("20%");
352         lastLoginCol.setSorteable(true);
353         lastLoginCol.setFormatter(CmsListDateMacroFormatter.getDefaultDateFormatter());
354         metadata.addColumn(lastLoginCol);
355     }
356
357     /**
358      * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
359      */

360     protected void setIndependentActions(CmsListMetadata metadata) {
361
362         // add user groups details
363
CmsListItemDetails userGroupsDetails = new CmsListItemDetails(LIST_DETAIL_GROUPS);
364         userGroupsDetails.setAtColumn(LIST_COLUMN_LOGIN);
365         userGroupsDetails.setVisible(false);
366         userGroupsDetails.setShowActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_GROUPS_NAME_0));
367         userGroupsDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_GROUPS_HELP_0));
368         userGroupsDetails.setHideActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_GROUPS_NAME_0));
369         userGroupsDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_GROUPS_HELP_0));
370         userGroupsDetails.setName(Messages.get().container(Messages.GUI_USERS_DETAIL_GROUPS_NAME_0));
371         userGroupsDetails.setFormatter(new CmsListItemDetailsFormatter(Messages.get().container(
372             Messages.GUI_USERS_DETAIL_GROUPS_NAME_0)));
373         metadata.addItemDetails(userGroupsDetails);
374
375         // add refresh action
376
CmsListIndependentAction refreshAction = new CmsListIndependentAction(LIST_IACTION_REFRESH);
377         refreshAction.setName(new CmsMessageContainer(null, "Refresh"));
378         refreshAction.setHelpText(new CmsMessageContainer(null, "Click here to refresh the list"));
379         refreshAction.setIconPath(ICON_MULTI_ADD);
380         metadata.addIndependentAction(refreshAction);
381     }
382
383     /**
384      * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
385      */

386     protected void setMultiActions(CmsListMetadata metadata) {
387
388         // add the activate user multi action
389
CmsListMultiAction activateUser = new CmsListMultiAction(LIST_MACTION_ACTIVATE);
390         activateUser.setName(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_NAME_0));
391         activateUser.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_HELP_0));
392         activateUser.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_CONF_0));
393         activateUser.setIconPath(ICON_MULTI_ACTIVATE);
394         metadata.addMultiAction(activateUser);
395
396         // makes the list searchable by several columns
397
CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_LOGIN));
398         searchAction.addColumn(metadata.getColumnDefinition(LIST_COLUMN_EMAIL));
399         metadata.setSearchAction(searchAction);
400
401         // remove print action
402
metadata.getIndependentAction(CmsListPrintIAction.LIST_ACTION_ID).setVisible(false);
403     }
404
405 }
Popular Tags