1 5 package org.exoplatform.portlets.portletregistery.component; 6 7 import java.util.ArrayList ; 8 import java.util.Collection ; 9 import java.util.Iterator ; 10 import java.util.Map ; 11 import javax.faces.context.FacesContext; 12 import org.exoplatform.faces.core.component.UIExoCommand; 13 import org.exoplatform.faces.core.event.ExoActionEvent; 14 import org.exoplatform.faces.core.event.ExoActionListener; 15 import org.exoplatform.services.organization.OrganizationService; 16 import org.exoplatform.services.organization.Group; 17 import org.exoplatform.services.portletregistery.Portlet; 18 import org.exoplatform.services.portletregistery.PortletRegisteryService; 19 import org.exoplatform.services.portletregistery.PortletRole; 20 25 public class UIPortletRole extends UIExoCommand { 26 private PortletRegisteryService portletRegisteryService; 27 private OrganizationService orgServvice_; 28 private Portlet portlet_ ; 29 30 public static final String ADD_ROLE = "addRole"; 31 public static final String REMOVE_ROLE = "removeRole"; 32 33 public static final String AVAILABLE_ROLE_SELECT = "availableRolesSelect"; 34 public static final String CURRENT_ROLE_SELECT = "currentRoleSelect"; 35 36 private Collection currentRoles; 37 private Collection availableRoles = new ArrayList (); 38 39 public UIPortletRole(OrganizationService organizationService, 40 PortletRegisteryService portletRegisteryService) throws Exception { 41 setRendererType("PortletRoleRenderer"); 42 setId("UIPortletRole"); 43 this.orgServvice_ = organizationService; 44 this.portletRegisteryService = portletRegisteryService; 45 46 addFacesListener(new UpdateActionListener().setActionToListen(SAVE_ACTION)); 47 addFacesListener(new CancelActionListener().setActionToListen(CANCEL_ACTION)); 48 addFacesListener(new AddRoleActionListener().setActionToListen(ADD_ROLE)); 49 addFacesListener(new RemoveRoleActionListener().setActionToListen(REMOVE_ROLE)); 50 } 51 52 public String getFamily() { 53 return "org.exoplatform.portlets.portletregistery.component.UIPortletRole"; 54 } 55 56 57 public void setCurrentPortlet(Portlet portlet) throws Exception { 58 portlet_ = portlet ; 59 Collection initialRoleList = orgServvice_.findGroups(null); 60 currentRoles = new ArrayList (); 61 Collection temp = portletRegisteryService.getPortletRoles(portlet.getId()); 62 for (Iterator i = temp.iterator(); i.hasNext();) { 63 PortletRole portletRole = (PortletRole) i.next(); 64 currentRoles.add(portletRole.getPortletRoleName()); 65 } 66 availableRoles = new ArrayList (); 67 for (Iterator i = initialRoleList.iterator(); i.hasNext();) { 68 Group availableRole = (Group) i.next(); 69 if(!currentRoles.contains(availableRole.getGroupName())){ 70 availableRoles.add(availableRole.getGroupName()); 71 } 72 } 73 } 74 75 public Collection getAvailableRoles() { 76 return availableRoles; 77 } 78 79 public Collection getCurrentRoles() { 80 return currentRoles; 81 } 82 83 private class AddRoleActionListener extends ExoActionListener { 84 public void execute(ExoActionEvent event) throws Exception { 85 Map parameters = FacesContext.getCurrentInstance().getExternalContext(). 86 getRequestParameterValuesMap(); 87 String [] rolesSelected = (String []) parameters.get(AVAILABLE_ROLE_SELECT); 88 if(rolesSelected == null) 89 return; 90 for (int i = 0; i < rolesSelected.length; i++) { 91 String roleToAdd = rolesSelected[i]; 92 currentRoles.add(roleToAdd); 93 availableRoles.remove(roleToAdd); 94 } 95 } 96 } 97 98 private class RemoveRoleActionListener extends ExoActionListener { 99 public void execute(ExoActionEvent event) throws Exception { 100 Map parameters = FacesContext.getCurrentInstance().getExternalContext(). 101 getRequestParameterValuesMap(); 102 String [] rolesSelected = (String []) parameters.get(CURRENT_ROLE_SELECT); 103 if(rolesSelected == null) return; 104 for (int i = 0; i < rolesSelected.length; i++) { 105 String roleToRemove = rolesSelected[i]; 106 currentRoles.remove(roleToRemove); 107 availableRoles.add(roleToRemove); 108 } 109 } 110 } 111 112 private class UpdateActionListener extends ExoActionListener { 113 public void execute(ExoActionEvent event) throws Exception { 114 portletRegisteryService.updatePortletRoles(portlet_.getId(), currentRoles); 115 setRenderedSibling(UIPortletCategories.class); 116 } 117 } 118 119 private class CancelActionListener extends ExoActionListener { 120 public void execute(ExoActionEvent event) throws Exception { 121 setRenderedSibling(UIPortletCategories.class); 122 } 123 } 124 } | Popular Tags |