1 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.CmsListItemSelectionAction; 56 import org.opencms.workplace.list.CmsListMetadata; 57 import org.opencms.workplace.list.CmsListMultiAction; 58 import org.opencms.workplace.list.CmsListRadioMultiAction; 59 import org.opencms.workplace.list.CmsListSearchAction; 60 import org.opencms.workplace.tools.accounts.Messages; 61 62 import java.util.ArrayList ; 63 import java.util.Arrays ; 64 import java.util.Date ; 65 import java.util.HashMap ; 66 import java.util.Iterator ; 67 import java.util.List ; 68 import java.util.Map ; 69 70 import javax.servlet.http.HttpServletRequest ; 71 import javax.servlet.http.HttpServletResponse ; 72 import javax.servlet.jsp.PageContext ; 73 74 83 public class CmsListDemo12 extends A_CmsListDialog { 84 85 86 public static final String LIST_ACTION_ACTIVATE = "aa"; 87 88 89 public static final String LIST_ACTION_DEACTIVATE = "ac"; 90 91 92 public static final String LIST_ACTION_DELETE = "ad"; 93 94 95 public static final String LIST_COLUMN_ACTIVATE = "ca"; 96 97 98 public static final String LIST_COLUMN_DELETE = "cd"; 99 100 101 public static final String LIST_COLUMN_EMAIL = "cm"; 102 103 104 public static final String LIST_COLUMN_LASTLOGIN = "cl"; 105 106 107 public static final String LIST_COLUMN_LOGIN = "ci"; 108 109 110 public static final String LIST_COLUMN_NAME = "cn"; 111 112 113 public static final String LIST_COLUMN_SEL1 = "cs1"; 114 115 116 public static final String LIST_COLUMN_SEL2 = "cs2"; 117 118 119 public static final String LIST_DEFACTION_EDIT = "de"; 120 121 122 public static final String LIST_DETAIL_GROUPS = "dg"; 123 124 125 public static final String LIST_IACTION_REFRESH = "ir"; 126 127 128 public static final String LIST_ID = "lsu1"; 129 130 131 public static final String LIST_MACTION_ACTIVATE = "ma"; 132 133 134 public static final String LIST_RACTION_SEL1 = "rs1"; 135 136 137 public static final String LIST_RACTION_SEL2 = "rs2"; 138 139 140 private static final String LIST_MACTION_COMPARE = "mc"; 141 142 143 private static final String PARAM_USERID = "userid"; 144 145 150 public CmsListDemo12(CmsJspActionElement jsp) { 151 152 super(jsp, LIST_ID, Messages.get().container(Messages.GUI_USERS_LIST_NAME_0), null, null, null); 153 } 154 155 162 public CmsListDemo12(PageContext context, HttpServletRequest req, HttpServletResponse res) { 163 164 this(new CmsJspActionElement(context, req, res)); 165 } 166 167 170 public void executeListIndepActions() { 171 172 if (getParamListAction().equals(LIST_IACTION_REFRESH)) { 173 refreshList(); 174 } 175 super.executeListIndepActions(); 176 } 177 178 181 public void executeListMultiActions() throws CmsRuntimeException { 182 183 if (getParamListAction().equals(LIST_MACTION_COMPARE)) { 184 List selItems = getSelectedItems(); 185 selItems.toString(); 186 } else if (getParamListAction().equals(LIST_MACTION_ACTIVATE)) { 188 try { 190 Iterator itItems = getSelectedItems().iterator(); 191 while (itItems.hasNext()) { 192 CmsListItem listItem = (CmsListItem)itItems.next(); 193 String usrName = listItem.get(LIST_COLUMN_LOGIN).toString(); 194 CmsUser user = getCms().readUser(usrName); 195 if (!user.isEnabled()) { 196 user.setEnabled(true); 197 } 199 } 200 } catch (CmsException e) { 201 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ACTIVATE_SELECTED_USERS_0), e); 202 } 203 } else { 205 throwListUnsupportedActionException(); 206 } 207 } 208 209 212 public void executeListSingleActions() { 213 214 CmsListItem item = getSelectedItem(); 215 CmsUUID userId = new CmsUUID(item.getId()); 216 String userName = item.get(LIST_COLUMN_LOGIN).toString(); 217 218 Map params = new HashMap (); 219 params.put(PARAM_USERID, userId); 220 params.put(CmsDialog.PARAM_ACTION, CmsDialog.DIALOG_INITIAL); 222 if (getParamListAction().equals(LIST_DEFACTION_EDIT)) { 223 } else if (LIST_ACTION_DELETE.equals(getParamListAction())) { 226 getList().removeItem(userId.toString(), getLocale()); 230 } else if (getParamListAction().equals(LIST_ACTION_ACTIVATE)) { 231 try { 233 CmsUser user = getCms().readUser(userId); 234 user.setEnabled(true); 235 } catch (CmsException e) { 238 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ACTIVATE_USER_1, userName), e); 239 } 240 } else if (getParamListAction().equals(LIST_ACTION_DEACTIVATE)) { 241 try { 243 CmsUser user = getCms().readUser(userId); 244 user.setEnabled(false); 245 } catch (CmsException e) { 248 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_DEACTIVATE_USER_1, userName), e); 249 } 250 } else { 251 throwListUnsupportedActionException(); 252 } 253 listSave(); 254 } 255 256 259 protected void fillDetails(String detailId) { 260 261 List users = getList().getAllContent(); 263 Iterator itUsers = users.iterator(); 264 while (itUsers.hasNext()) { 265 CmsListItem item = (CmsListItem)itUsers.next(); 266 String userName = item.get(LIST_COLUMN_LOGIN).toString(); 267 StringBuffer html = new StringBuffer (512); 268 try { 269 if (detailId.equals(LIST_DETAIL_GROUPS)) { 270 Iterator itGroups = getCms().getGroupsOfUser(userName).iterator(); 272 while (itGroups.hasNext()) { 273 html.append(((CmsGroup)itGroups.next()).getName()); 274 if (itGroups.hasNext()) { 275 html.append("<br>"); 276 } 277 html.append("\n"); 278 } 279 } else { 280 continue; 281 } 282 } catch (Exception e) { 283 } 285 item.set(detailId, html.toString()); 286 } 287 } 288 289 292 protected List getListItems() throws CmsException { 293 294 List ret = new ArrayList (); 295 List users = getCms().getUsers(CmsUser.USER_TYPE_SYSTEMUSER); 297 Iterator itUsers = users.iterator(); 298 while (itUsers.hasNext()) { 299 CmsUser user = (CmsUser)itUsers.next(); 300 CmsListItem item = getList().newItem(user.getId().toString()); 301 item.set(LIST_COLUMN_LOGIN, user.getName()); 302 item.set(LIST_COLUMN_NAME, user.getFullName()); 303 item.set(LIST_COLUMN_EMAIL, user.getEmail()); 304 item.set(LIST_COLUMN_LASTLOGIN, new Date (user.getLastlogin())); 305 ret.add(item); 306 } 307 return ret; 308 } 309 310 313 protected void initMessages() { 314 315 addMessages(Messages.get().getBundleName()); 317 addMessages(org.opencms.workplace.demos.list.Messages.get().getBundleName()); 318 addMessages(org.opencms.workplace.demos.Messages.get().getBundleName()); 319 super.initMessages(); 321 } 322 323 326 protected void setColumns(CmsListMetadata metadata) { 327 328 CmsListColumnDefinition actCol = new CmsListColumnDefinition(LIST_COLUMN_ACTIVATE); 330 actCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ACTIVATE_0)); 331 actCol.setWidth("30"); 332 actCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER); 333 actCol.setListItemComparator(new CmsListItemActionIconComparator()); 334 CmsListDirectAction actAction = new CmsListDirectAction(LIST_ACTION_ACTIVATE) { 336 337 340 public boolean isVisible() { 341 342 String 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 actAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_ACTIVATE_NAME_0)); 351 actAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_ACTIVATE_HELP_0)); 352 actAction.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_ACTIVATE_CONF_0)); 353 actAction.setIconPath(ICON_INACTIVE); 354 actCol.addDirectAction(actAction); 355 356 CmsListDirectAction deactAction = new CmsListDirectAction(LIST_ACTION_DEACTIVATE) { 358 359 362 public boolean isVisible() { 363 364 String usrId = getItem().getId(); 365 try { 366 return getCms().readUser(new CmsUUID(usrId)).isEnabled(); 367 } catch (CmsException e) { 368 return super.isVisible(); 369 } 370 } 371 }; 372 deactAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DEACTIVATE_NAME_0)); 373 deactAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DEACTIVATE_HELP_0)); 374 deactAction.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DEACTIVATE_CONF_0)); 375 deactAction.setIconPath(ICON_ACTIVE); 376 actCol.addDirectAction(deactAction); 377 metadata.addColumn(actCol); 378 379 CmsListColumnDefinition deleteCol = new CmsListColumnDefinition(LIST_COLUMN_DELETE); 381 deleteCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_DELETE_0)); 382 deleteCol.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_COLS_DELETE_HELP_0)); 383 deleteCol.setWidth("20"); 384 deleteCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER); 385 deleteCol.setSorteable(false); 386 CmsListDirectAction deleteAction = new A_CmsListDirectJsAction(LIST_ACTION_DELETE) { 388 389 392 public String jsCode() { 393 394 String email = (String )getItem().get(LIST_COLUMN_EMAIL); 395 return "alert('the email of the select user is:" + CmsStringUtil.escapeJavaScript(email) + "');"; 396 } 397 }; 398 deleteAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_NAME_0)); 399 deleteAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_ACTION_DELETE_HELP_0)); 400 deleteAction.setConfirmationMessage(new CmsMessageContainer(null, "Do you want to delete the selected user?")); 401 deleteAction.setIconPath(ICON_DELETE); 402 deleteCol.addDirectAction(deleteAction); 403 metadata.addColumn(deleteCol); 404 405 CmsListColumnDefinition loginCol = new CmsListColumnDefinition(LIST_COLUMN_LOGIN); 407 loginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LOGIN_0)); 408 loginCol.setWidth("20%"); 409 loginCol.setSorteable(true); 410 CmsListDefaultAction defEditAction = new CmsListDefaultAction(LIST_DEFACTION_EDIT); 412 defEditAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_NAME_0)); 413 defEditAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_HELP_0)); 414 loginCol.addDefaultAction(defEditAction); 415 metadata.addColumn(loginCol); 416 417 CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME); 419 nameCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_USERNAME_0)); 420 nameCol.setWidth("30%"); 421 nameCol.setSorteable(true); 422 metadata.addColumn(nameCol); 423 424 CmsListColumnDefinition emailCol = new CmsListColumnDefinition(LIST_COLUMN_EMAIL); 426 emailCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_EMAIL_0)); 427 emailCol.setWidth("30%"); 428 emailCol.setSorteable(true); 429 metadata.addColumn(emailCol); 430 431 CmsListColumnDefinition lastLoginCol = new CmsListColumnDefinition(LIST_COLUMN_LASTLOGIN); 433 lastLoginCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LASTLOGIN_0)); 434 lastLoginCol.setWidth("20%"); 435 lastLoginCol.setSorteable(true); 436 lastLoginCol.setFormatter(CmsListDateMacroFormatter.getDefaultDateFormatter()); 437 metadata.addColumn(lastLoginCol); 438 439 CmsListColumnDefinition radioSel1Col = new CmsListColumnDefinition(LIST_COLUMN_SEL1); 441 radioSel1Col.setName(new CmsMessageContainer(null, "V1")); 442 radioSel1Col.setWidth("30"); 443 radioSel1Col.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER); 444 radioSel1Col.setSorteable(false); 445 CmsListItemSelectionAction sel1Action = new CmsListItemSelectionAction(LIST_RACTION_SEL1, LIST_MACTION_COMPARE); 447 sel1Action.setName(new CmsMessageContainer(null, "1st Version")); 448 sel1Action.setHelpText(new CmsMessageContainer(null, "Select this file as the 1st version")); 449 sel1Action.setEnabled(true); 450 radioSel1Col.addDirectAction(sel1Action); 451 metadata.addColumn(radioSel1Col); 452 453 CmsListColumnDefinition radioSel2Col = new CmsListColumnDefinition(LIST_COLUMN_SEL2); 455 radioSel2Col.setName(new CmsMessageContainer(null, "V2")); 456 radioSel2Col.setWidth("30"); 457 radioSel2Col.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER); 458 radioSel2Col.setSorteable(false); 459 CmsListItemSelectionAction sel2Action = new CmsListItemSelectionAction(LIST_RACTION_SEL2, LIST_MACTION_COMPARE); 461 sel2Action.setName(new CmsMessageContainer(null, "2nd Version")); 462 sel2Action.setHelpText(new CmsMessageContainer(null, "Select this file as the 2nd version")); 463 sel2Action.setEnabled(true); 464 radioSel2Col.addDirectAction(sel2Action); 465 metadata.addColumn(radioSel2Col); 466 } 467 468 471 protected void setIndependentActions(CmsListMetadata metadata) { 472 473 CmsListItemDetails userGroupsDetails = new CmsListItemDetails(LIST_DETAIL_GROUPS); 475 userGroupsDetails.setAtColumn(LIST_COLUMN_LOGIN); 476 userGroupsDetails.setVisible(false); 477 userGroupsDetails.setShowActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_GROUPS_NAME_0)); 478 userGroupsDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_GROUPS_HELP_0)); 479 userGroupsDetails.setHideActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_GROUPS_NAME_0)); 480 userGroupsDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_GROUPS_HELP_0)); 481 userGroupsDetails.setName(Messages.get().container(Messages.GUI_USERS_DETAIL_GROUPS_NAME_0)); 482 userGroupsDetails.setFormatter(new CmsListItemDetailsFormatter(Messages.get().container( 483 Messages.GUI_USERS_DETAIL_GROUPS_NAME_0))); 484 metadata.addItemDetails(userGroupsDetails); 485 486 CmsListIndependentAction refreshAction = new CmsListIndependentAction(LIST_IACTION_REFRESH); 488 refreshAction.setName(new CmsMessageContainer(null, "Refresh")); 489 refreshAction.setHelpText(new CmsMessageContainer(null, "Click here to refresh the list")); 490 refreshAction.setIconPath(ICON_MULTI_ADD); 491 metadata.addIndependentAction(refreshAction); 492 } 493 494 497 protected void setMultiActions(CmsListMetadata metadata) { 498 499 CmsListRadioMultiAction compareAction = new CmsListRadioMultiAction( 501 LIST_MACTION_COMPARE, 502 Arrays.asList(new String [] {LIST_RACTION_SEL1, LIST_RACTION_SEL2})); 503 compareAction.setName(new CmsMessageContainer(null, "Compare")); 504 compareAction.setHelpText(new CmsMessageContainer(null, "Click here to compare the two selected versions")); 505 compareAction.setIconPath(ICON_ADD); 506 metadata.addMultiAction(compareAction); 507 508 CmsListMultiAction activateUser = new CmsListMultiAction(LIST_MACTION_ACTIVATE); 510 activateUser.setName(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_NAME_0)); 511 activateUser.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_HELP_0)); 512 activateUser.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ACTIVATE_CONF_0)); 513 activateUser.setIconPath(ICON_MULTI_ACTIVATE); 514 metadata.addMultiAction(activateUser); 515 516 CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_LOGIN)); 518 searchAction.addColumn(metadata.getColumnDefinition(LIST_COLUMN_EMAIL)); 519 metadata.setSearchAction(searchAction); 520 } 521 522 } | Popular Tags |