KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > portletregistery > component > UIPortletRole


1 /**
2  * Copyright 2001-2003 The eXo platform SARL All rights reserved.
3  * Please look at license.txt in info directory for more license detail.
4  */

5 package org.exoplatform.portlets.portletregistery.component;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Iterator JavaDoc;
10 import java.util.Map JavaDoc;
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 /**
21  * Created y the eXo platform team
22  * User: Benjamin Mestrallet
23  * Date: 19 juin 2004
24  */

25 public class UIPortletRole extends UIExoCommand {
26   private PortletRegisteryService portletRegisteryService;
27   private OrganizationService orgServvice_;
28   private Portlet portlet_ ;
29
30   public static final String JavaDoc ADD_ROLE = "addRole";
31   public static final String JavaDoc REMOVE_ROLE = "removeRole";
32
33   public static final String JavaDoc AVAILABLE_ROLE_SELECT = "availableRolesSelect";
34   public static final String JavaDoc CURRENT_ROLE_SELECT = "currentRoleSelect";
35
36   private Collection JavaDoc currentRoles;
37   private Collection JavaDoc availableRoles = new ArrayList JavaDoc();
38
39   public UIPortletRole(OrganizationService organizationService,
40                        PortletRegisteryService portletRegisteryService) throws Exception JavaDoc {
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 JavaDoc getFamily() {
53     return "org.exoplatform.portlets.portletregistery.component.UIPortletRole";
54   }
55
56
57   public void setCurrentPortlet(Portlet portlet) throws Exception JavaDoc {
58     portlet_ = portlet ;
59     Collection JavaDoc initialRoleList = orgServvice_.findGroups(null);
60     currentRoles = new ArrayList JavaDoc();
61     Collection JavaDoc temp = portletRegisteryService.getPortletRoles(portlet.getId());
62     for (Iterator JavaDoc i = temp.iterator(); i.hasNext();) {
63       PortletRole portletRole = (PortletRole) i.next();
64       currentRoles.add(portletRole.getPortletRoleName());
65     }
66     availableRoles = new ArrayList JavaDoc();
67     for (Iterator JavaDoc 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 JavaDoc getAvailableRoles() {
76     return availableRoles;
77   }
78
79   public Collection JavaDoc getCurrentRoles() {
80     return currentRoles;
81   }
82
83   private class AddRoleActionListener extends ExoActionListener {
84     public void execute(ExoActionEvent event) throws Exception JavaDoc {
85       Map JavaDoc parameters = FacesContext.getCurrentInstance().getExternalContext().
86           getRequestParameterValuesMap();
87       String JavaDoc[] rolesSelected = (String JavaDoc[]) parameters.get(AVAILABLE_ROLE_SELECT);
88       if(rolesSelected == null)
89         return;
90       for (int i = 0; i < rolesSelected.length; i++) {
91         String JavaDoc 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 JavaDoc {
100       Map JavaDoc parameters = FacesContext.getCurrentInstance().getExternalContext().
101           getRequestParameterValuesMap();
102       String JavaDoc[] rolesSelected = (String JavaDoc[]) parameters.get(CURRENT_ROLE_SELECT);
103       if(rolesSelected == null) return;
104       for (int i = 0; i < rolesSelected.length; i++) {
105         String JavaDoc 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 JavaDoc {
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 JavaDoc {
121         setRenderedSibling(UIPortletCategories.class);
122     }
123   }
124 }
Popular Tags