1 17 package org.alfresco.web.bean; 18 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.HashMap ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import javax.faces.application.FacesMessage; 28 import javax.faces.component.UIComponent; 29 import javax.faces.context.FacesContext; 30 import javax.faces.event.ActionEvent; 31 import javax.faces.model.DataModel; 32 import javax.faces.model.ListDataModel; 33 import javax.faces.model.SelectItem; 34 import javax.faces.validator.ValidatorException; 35 import javax.transaction.UserTransaction ; 36 37 import org.alfresco.model.ContentModel; 38 import org.alfresco.service.ServiceRegistry; 39 import org.alfresco.service.cmr.repository.NodeRef; 40 import org.alfresco.service.cmr.repository.NodeService; 41 import org.alfresco.service.cmr.security.AuthorityService; 42 import org.alfresco.service.cmr.security.AuthorityType; 43 import org.alfresco.service.cmr.security.PersonService; 44 import org.alfresco.service.namespace.NamespaceService; 45 import org.alfresco.web.app.Application; 46 import org.alfresco.web.app.context.IContextListener; 47 import org.alfresco.web.app.context.UIContextService; 48 import org.alfresco.web.bean.repository.Repository; 49 import org.alfresco.web.ui.common.SortableSelectItem; 50 import org.alfresco.web.ui.common.Utils; 51 import org.alfresco.web.ui.common.component.IBreadcrumbHandler; 52 import org.alfresco.web.ui.common.component.UIActionLink; 53 import org.alfresco.web.ui.common.component.UIBreadcrumb; 54 import org.alfresco.web.ui.common.component.UIGenericPicker; 55 import org.alfresco.web.ui.common.component.UIModeList; 56 import org.alfresco.web.ui.common.component.data.UIRichList; 57 import org.apache.log4j.Logger; 58 59 64 public class GroupsBean implements IContextListener 65 { 66 private static final String FILTER_CHILDREN = "children"; 67 private static final String FILTER_ALL = "all"; 68 69 private static final String DEFAULT_OUTCOME = "finish"; 70 71 private static final String MSG_GROUPS = "root_groups"; 72 73 private static Logger logger = Logger.getLogger(GroupsBean.class); 74 75 76 private NodeService nodeService; 77 78 79 private AuthorityService authService; 80 81 82 private PersonService personService; 83 84 85 private UIRichList groupsRichList; 86 private UIRichList usersRichList; 87 88 89 private DataModel usersDataModel = null; 90 91 92 private String group = null; 93 private String groupName = null; 94 95 96 private String actionGroup = null; 97 private String actionGroupName = null; 98 private int actionGroupItems = 0; 99 100 101 private List <UserAuthorityDetails> usersForGroup = null; 102 103 104 private String name = null; 105 106 107 private String viewMode = "icons"; 108 109 110 private String filterMode = FILTER_CHILDREN; 111 112 113 private List <IBreadcrumbHandler> location = null; 114 115 116 119 122 public GroupsBean() 123 { 124 UIContextService.getInstance(FacesContext.getCurrentInstance()).registerBean(this); 125 } 126 127 128 131 134 public void setNodeService(NodeService nodeService) 135 { 136 this.nodeService = nodeService; 137 } 138 139 142 public void setAuthorityService(AuthorityService authService) 143 { 144 this.authService = authService; 145 } 146 147 150 public void setPersonService(PersonService personService) 151 { 152 this.personService = personService; 153 } 154 155 158 public UIRichList getGroupsRichList() 159 { 160 return this.groupsRichList; 161 } 162 163 166 public void setGroupsRichList(UIRichList list) 167 { 168 this.groupsRichList = list; 169 } 170 171 174 public UIRichList getUsersRichList() 175 { 176 return this.usersRichList; 177 } 178 179 182 public void setUsersRichList(UIRichList usersRichList) 183 { 184 this.usersRichList = usersRichList; 185 } 186 187 190 public DataModel getUsersDataModel() 191 { 192 if (this.usersDataModel == null) 193 { 194 this.usersDataModel = new ListDataModel(); 195 } 196 197 this.usersDataModel.setWrappedData(this.usersForGroup); 198 199 return this.usersDataModel; 200 } 201 202 205 public void setUsersDataModel(DataModel usersDataModel) 206 { 207 this.usersDataModel = usersDataModel; 208 } 209 210 213 public String getName() 214 { 215 return this.name; 216 } 217 218 221 public void setName(String name) 222 { 223 this.name = name; 224 } 225 226 229 public String getViewMode() 230 { 231 return this.viewMode; 232 } 233 234 237 public void setViewMode(String viewMode) 238 { 239 this.viewMode = viewMode; 240 } 241 242 245 public String getFilterMode() 246 { 247 return this.filterMode; 248 } 249 250 253 public void setFilterMode(String filterMode) 254 { 255 this.filterMode = filterMode; 256 257 contextUpdated(); 259 } 260 261 264 public String getActionGroup() 265 { 266 return this.actionGroup; 267 } 268 269 272 public void setActionGroup(String group) 273 { 274 this.actionGroup = group; 275 276 if (group != null) 277 { 278 setActionGroupName(this.authService.getShortName(group)); 280 int count = this.authService.getContainedAuthorities(AuthorityType.GROUP, group, false).size(); 281 count += this.authService.getContainedAuthorities(AuthorityType.USER, group, false).size(); 282 setActionGroupItems(count); 283 } 284 else 285 { 286 setActionGroupName(null); 287 setActionGroupItems(0); 288 } 289 290 this.name = null; 292 293 this.usersForGroup = new ArrayList <UserAuthorityDetails>(); 295 } 296 297 300 public String getActionGroupName() 301 { 302 return this.actionGroupName; 303 } 304 305 308 public void setActionGroupName(String actionGroupName) 309 { 310 this.actionGroupName = actionGroupName; 311 } 312 313 316 public int getActionGroupItems() 317 { 318 return this.actionGroupItems; 319 } 320 321 324 public void setActionGroupItems(int actionGroupItems) 325 { 326 this.actionGroupItems = actionGroupItems; 327 } 328 329 332 public String getCurrentGroup() 333 { 334 return this.group; 335 } 336 337 344 public void setCurrentGroup(String group, String groupName) 345 { 346 if (logger.isDebugEnabled()) 347 logger.debug("Setting current group: " + group); 348 349 this.group = group; 351 this.groupName = groupName; 352 353 contextUpdated(); 355 } 356 357 360 public String getGroupName() 361 { 362 return this.groupName; 363 } 364 365 368 public void setGroupName(String groupName) 369 { 370 this.groupName = groupName; 371 } 372 373 376 public List <IBreadcrumbHandler> getLocation() 377 { 378 if (this.location == null) 379 { 380 List <IBreadcrumbHandler> loc = new ArrayList <IBreadcrumbHandler>(8); 381 loc.add(new GroupBreadcrumbHandler(null, 382 Application.getMessage(FacesContext.getCurrentInstance(), MSG_GROUPS))); 383 384 this.location = loc; 385 } 386 return this.location; 387 } 388 389 392 public void setLocation(List <IBreadcrumbHandler> location) 393 { 394 this.location = location; 395 } 396 397 401 public List <Map > getGroups() 402 { 403 List <Map > groups; 404 405 UserTransaction tx = null; 406 try 407 { 408 FacesContext context = FacesContext.getCurrentInstance(); 409 tx = Repository.getUserTransaction(context); 410 tx.begin(); 411 412 Set <String > authorities; 413 boolean immediate = (this.filterMode.equals(FILTER_CHILDREN)); 414 if (this.group == null) 415 { 416 if (immediate == true) 418 { 419 authorities = this.authService.getAllRootAuthorities(AuthorityType.GROUP); 420 } 421 else 422 { 423 authorities = this.authService.getAllAuthorities(AuthorityType.GROUP); 424 } 425 } 426 else 427 { 428 authorities = this.authService.getContainedAuthorities(AuthorityType.GROUP, group, immediate); 430 } 431 groups = new ArrayList <Map >(authorities.size()); 432 for (String authority : authorities) 433 { 434 Map authMap = new HashMap (3, 1.0f); 435 436 String name = this.authService.getShortName(authority); 437 authMap.put("name", name); 438 authMap.put("id", authority); 439 440 groups.add(authMap); 441 } 442 443 tx.commit(); 445 } 446 catch (Throwable err) 447 { 448 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 449 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 450 groups = Collections.<Map >emptyList(); 451 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 452 } 453 454 return groups; 455 } 456 457 460 public List <Map > getUsers() 461 { 462 List <Map > users; 463 464 UserTransaction tx = null; 465 try 466 { 467 FacesContext context = FacesContext.getCurrentInstance(); 468 tx = Repository.getUserTransaction(context); 469 tx.begin(); 470 471 Set <String > authorities; 472 if (this.group == null) 473 { 474 authorities = Collections.<String >emptySet(); 475 } 476 else 477 { 478 boolean immediate = (this.filterMode.equals(FILTER_CHILDREN)); 480 authorities = this.authService.getContainedAuthorities(AuthorityType.USER, group, immediate); 481 } 482 users = new ArrayList <Map >(authorities.size()); 483 for (String authority : authorities) 484 { 485 Map authMap = new HashMap (3, 1.0f); 486 487 String userName = this.authService.getShortName(authority); 488 authMap.put("userName", userName); 489 authMap.put("id", authority); 490 491 NodeRef ref = this.personService.getPerson(authority); 493 String firstName = (String )this.nodeService.getProperty(ref, ContentModel.PROP_FIRSTNAME); 494 String lastName = (String )this.nodeService.getProperty(ref, ContentModel.PROP_LASTNAME); 495 496 StringBuilder label = new StringBuilder (48); 498 label.append(firstName) 499 .append(' ') 500 .append(lastName); 501 authMap.put("name", label.toString()); 502 503 users.add(authMap); 504 } 505 506 tx.commit(); 508 } 509 catch (Throwable err) 510 { 511 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 512 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 513 users = Collections.<Map >emptyList(); 514 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 515 } 516 517 return users; 518 } 519 520 523 public void validateGroupName(FacesContext context, UIComponent component, Object value) 524 throws ValidatorException 525 { 526 String name = (String )value; 527 if (name.indexOf('\'') != -1 || name.indexOf('"') != -1 || name.indexOf('\\') != -1) 528 { 529 String err = MessageFormat.format(Application.getMessage(context, "groups_err_group_name"), 530 new Object []{"', \", \\"}); 531 throw new ValidatorException(new FacesMessage(err)); 532 } 533 } 534 535 545 public SelectItem[] pickerCallback(int filterIndex, String contains) 546 { 547 FacesContext context = FacesContext.getCurrentInstance(); 548 549 SelectItem[] items; 550 551 UserTransaction tx = null; 552 try 553 { 554 tx = Repository.getUserTransaction(context); 555 tx.begin(); 556 557 ServiceRegistry services = Repository.getServiceRegistry(context); 559 NodeRef peopleRef = personService.getPeopleContainer(); 560 String xpath = "*[like(@" + NamespaceService.CONTENT_MODEL_PREFIX + ":" + "firstName, '%" + contains + "%', false)" + 561 " or " + "like(@" + NamespaceService.CONTENT_MODEL_PREFIX + ":" + "lastName, '%" + contains + "%', false)]"; 562 563 List <NodeRef> nodes = services.getSearchService().selectNodes( 564 peopleRef, 565 xpath, 566 null, 567 services.getNamespaceService(), 568 false); 569 570 items = new SelectItem[nodes.size()]; 571 for (int index=0; index<nodes.size(); index++) 572 { 573 NodeRef personRef = nodes.get(index); 574 String firstName = (String )this.nodeService.getProperty(personRef, ContentModel.PROP_FIRSTNAME); 575 String lastName = (String )this.nodeService.getProperty(personRef, ContentModel.PROP_LASTNAME); 576 String username = (String )this.nodeService.getProperty(personRef, ContentModel.PROP_USERNAME); 577 SelectItem item = new SortableSelectItem(username, firstName + " " + lastName, lastName); 578 items[index] = item; 579 } 580 581 tx.commit(); 583 } 584 catch (Exception err) 585 { 586 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 587 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err ); 588 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 589 590 items = new SelectItem[0]; 591 } 592 593 return items; 594 } 595 596 599 public void setupGroupAction(ActionEvent event) 600 { 601 UIActionLink link = (UIActionLink)event.getComponent(); 602 Map <String , String > params = link.getParameterMap(); 603 String group = params.get("id"); 604 if (group != null && group.length() != 0) 605 { 606 if (logger.isDebugEnabled()) 607 logger.debug("Setup for action, setting current Group to: " + group); 608 609 setActionGroup(group); 611 612 contextUpdated(); 614 } 615 } 616 617 620 public void clearGroupAction(ActionEvent event) 621 { 622 setActionGroup(null); 623 624 contextUpdated(); 626 } 627 628 632 public void clickGroup(ActionEvent event) 633 { 634 UIActionLink link = (UIActionLink)event.getComponent(); 635 Map <String , String > params = link.getParameterMap(); 636 String group = params.get("id"); 637 if (group != null && group.length() != 0) 638 { 639 updateUILocation(group); 641 } 642 } 643 644 647 public String finishCreate() 648 { 649 String outcome = DEFAULT_OUTCOME; 650 651 UserTransaction tx = null; 652 try 653 { 654 FacesContext context = FacesContext.getCurrentInstance(); 655 tx = Repository.getUserTransaction(context); 656 tx.begin(); 657 658 this.authService.createAuthority(AuthorityType.GROUP, getActionGroup(), this.name); 660 661 tx.commit(); 663 } 664 catch (Throwable err) 665 { 666 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 668 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 669 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 670 outcome = null; 671 } 672 673 return outcome; 674 } 675 676 679 public String finishDelete() 680 { 681 String outcome = DEFAULT_OUTCOME; 682 683 UserTransaction tx = null; 684 try 685 { 686 FacesContext context = FacesContext.getCurrentInstance(); 687 tx = Repository.getUserTransaction(context); 688 tx.begin(); 689 690 this.authService.deleteAuthority(getActionGroup()); 692 693 tx.commit(); 695 696 List <IBreadcrumbHandler> location = getLocation(); 698 GroupBreadcrumbHandler handler = (GroupBreadcrumbHandler)location.get(location.size() - 1); 699 700 if ( getActionGroup().equals(handler.Group) ) 702 { 703 location.remove(location.size() - 1); 704 705 if (location.size() != 0) 707 { 708 handler = (GroupBreadcrumbHandler)location.get(location.size() - 1); 709 this.setCurrentGroup(handler.Group, handler.Label); 710 } 711 } 712 713 setActionGroup(null); 715 } 716 catch (Throwable err) 717 { 718 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 720 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 721 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 722 outcome = null; 723 } 724 725 return outcome; 726 } 727 728 731 public void removeUser(ActionEvent event) 732 { 733 UIActionLink link = (UIActionLink)event.getComponent(); 734 Map <String , String > params = link.getParameterMap(); 735 String authority = params.get("id"); 736 if (authority != null && authority.length() != 0) 737 { 738 try 739 { 740 this.authService.removeAuthority(this.group, authority); 741 742 contextUpdated(); 744 } 745 catch (Throwable err) 746 { 747 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 748 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 749 } 750 } 751 } 752 753 756 public String finishAddUser() 757 { 758 String outcome = DEFAULT_OUTCOME; 759 760 UserTransaction tx = null; 761 try 762 { 763 FacesContext context = FacesContext.getCurrentInstance(); 764 tx = Repository.getUserTransaction(context); 765 tx.begin(); 766 767 for (UserAuthorityDetails wrapper : this.usersForGroup) 769 { 770 this.authService.addAuthority(getActionGroup(), wrapper.authority); 771 } 772 773 tx.commit(); 775 } 776 catch (Throwable err) 777 { 778 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 780 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 781 FacesContext.getCurrentInstance(), Repository.ERROR_GENERIC), err.getMessage()), err); 782 outcome = null; 783 } 784 785 return outcome; 786 } 787 788 791 public void addSelectedUsers(ActionEvent event) 792 { 793 UIGenericPicker picker = (UIGenericPicker)event.getComponent().findComponent("picker"); 794 String [] results = picker.getSelectedResults(); 795 if (results != null) 796 { 797 for (int i=0; i<results.length; i++) 798 { 799 String authority = results[i]; 800 801 boolean foundExisting = false; 803 for (int n=0; n<this.usersForGroup.size(); n++) 804 { 805 UserAuthorityDetails wrapper = this.usersForGroup.get(n); 806 if (authority.equals(wrapper.getAuthority())) 807 { 808 foundExisting = true; 809 break; 810 } 811 } 812 813 if (foundExisting == false) 814 { 815 StringBuilder label = new StringBuilder (48); 816 817 if (this.personService.personExists(authority) == true) 819 { 820 NodeRef ref = this.personService.getPerson(authority); 822 String firstName = (String )this.nodeService.getProperty(ref, ContentModel.PROP_FIRSTNAME); 823 String lastName = (String )this.nodeService.getProperty(ref, ContentModel.PROP_LASTNAME); 824 825 label.append(firstName) 827 .append(' ') 828 .append(lastName); 829 830 UserAuthorityDetails userDetails = new UserAuthorityDetails(label.toString(), authority); 832 this.usersForGroup.add(userDetails); 833 } 834 } 835 } 836 } 837 } 838 839 842 public void removeUserSelection(ActionEvent event) 843 { 844 UserAuthorityDetails wrapper = (UserAuthorityDetails)this.usersDataModel.getRowData(); 845 if (wrapper != null) 846 { 847 this.usersForGroup.remove(wrapper); 848 } 849 } 850 851 854 public void viewModeChanged(ActionEvent event) 855 { 856 UIModeList viewList = (UIModeList)event.getComponent(); 857 858 setViewMode(viewList.getValue().toString()); 860 } 861 862 865 public void filterModeChanged(ActionEvent event) 866 { 867 UIModeList viewList = (UIModeList)event.getComponent(); 868 869 setFilterMode(viewList.getValue().toString()); 871 } 872 873 876 private void updateUILocation(String group) 877 { 878 String groupName = this.authService.getShortName(group); 879 this.location.add(new GroupBreadcrumbHandler(group, groupName)); 880 this.setCurrentGroup(group, groupName); 881 } 882 883 884 887 890 public void contextUpdated() 891 { 892 if (logger.isDebugEnabled()) 893 logger.debug("Invalidating Group Management Components..."); 894 895 this.groupsRichList.setValue(null); 897 this.usersRichList.setValue(null); 898 } 899 900 901 904 907 private class GroupBreadcrumbHandler implements IBreadcrumbHandler 908 { 909 private static final long serialVersionUID = 1871876653151036630L; 910 911 917 public GroupBreadcrumbHandler(String group, String label) 918 { 919 this.Group = group; 920 this.Label = label; 921 } 922 923 926 public String toString() 927 { 928 return this.Label; 929 } 930 931 934 public String navigationOutcome(UIBreadcrumb breadcrumb) 935 { 936 setCurrentGroup(this.Group, this.Label); 939 setLocation( (List )breadcrumb.getValue() ); 940 941 return null; 942 } 943 944 public String Group; 945 public String Label; 946 } 947 948 951 public static class UserAuthorityDetails 952 { 953 public UserAuthorityDetails(String name, String authority) 954 { 955 this.name = name; 956 this.authority = authority; 957 } 958 959 public String getName() 960 { 961 return this.name; 962 } 963 964 public String getAuthority() 965 { 966 return this.authority; 967 } 968 969 private String name; 970 private String authority; 971 } 972 } 973 | Popular Tags |