View Javadoc
1   /*
2    * Copyright (C) 2003-2007 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.selector;
18  
19  import java.util.ArrayList;
20  import java.util.Collection;
21  import java.util.List;
22  
23  import org.exoplatform.commons.utils.ListAccess;
24  import org.exoplatform.services.organization.Group;
25  import org.exoplatform.services.organization.OrganizationService;
26  import org.exoplatform.services.organization.User;
27  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
28  import org.exoplatform.webui.config.annotation.ComponentConfig;
29  import org.exoplatform.webui.config.annotation.ComponentConfigs;
30  import org.exoplatform.webui.config.annotation.EventConfig;
31  import org.exoplatform.webui.core.UIBreadcumbs;
32  import org.exoplatform.webui.core.UIComponent;
33  import org.exoplatform.webui.core.UIPopupWindow;
34  import org.exoplatform.webui.core.UITree;
35  import org.exoplatform.webui.event.Event;
36  import org.exoplatform.webui.event.EventListener;
37  import org.exoplatform.webui.organization.UIGroupMembershipSelector;
38  
39  /**
40   * Created by The eXo Platform SARL Author : Dang Van Minh
41   * minh.dang@exoplatform.com Nov 17, 2006
42   */
43  @ComponentConfigs( {
44      @ComponentConfig(
45          template = "classpath:groovy/ecm/webui/UIMemberSelector.gtmpl",
46          events = {
47            @EventConfig(listeners = UIPermissionSelector.ChangeNodeActionListener.class),
48            @EventConfig(listeners = UIPermissionSelector.SelectMembershipActionListener.class),
49            @EventConfig(listeners = UIPermissionSelector.SelectPathActionListener.class),
50            @EventConfig(listeners = UIPermissionSelector.AddAnyPermissionActionListener.class)
51          }),
52      @ComponentConfig(
53          type = UITree.class,
54          id = "UITreeGroupSelector",
55          template = "system:/groovy/webui/core/UITree.gtmpl",
56          events = @EventConfig(listeners = UITree.ChangeNodeActionListener.class)),
57      @ComponentConfig(
58          type = UIBreadcumbs.class,
59          id = "BreadcumbGroupSelector",
60          template = "system:/groovy/webui/core/UIBreadcumbs.gtmpl",
61          events = @EventConfig(listeners = UIBreadcumbs.SelectPathActionListener.class))
62      }
63  )
64  
65  public class UIPermissionSelector extends UIGroupMembershipSelector implements ComponentSelector {
66  
67    /** The Constant defaultValue. */
68    final static public String defaultValue    = "/admin";
69  
70    /** The ui component. */
71    private UIComponent        uiComponent;
72  
73    /** The return field name. */
74    private String             returnFieldName = null;
75  
76    /** The is selected membership. */
77    private boolean            isSelectedMembership = true;
78  
79    /** The is selected user. */
80    private boolean            isSelectedUser;
81  
82    /** The is use popup. */
83    private boolean             isUsePopup      = true;
84  
85    /** Show/hide Add AnyPermission form   */
86    private boolean             isShowAnyPermission = true;
87  
88    /**
89     * Instantiates a new uI permission selector.
90     *
91     * @throws Exception the exception
92     */
93    public UIPermissionSelector() throws Exception {
94      changeGroup(defaultValue);
95      addChild(UIAnyPermission.class, null, null);
96    }
97  
98    /**
99     * Sets the current permission.
100    *
101    * @param per the new current permission
102    *
103    * @throws Exception the exception
104    */
105   public void setCurrentPermission(String per) throws Exception {
106     changeGroup(per);
107   }
108 
109   /**
110    * Gets the return component.
111    *
112    * @return the return component
113    */
114 
115   public UIComponent getSourceComponent() {
116     return uiComponent;
117   }
118 
119   /**
120    * Gets the return field.
121    *
122    * @return the return field
123    */
124   public String getReturnField() {
125     return returnFieldName;
126   }
127 
128   public void setIsUsePopup(boolean isUsePopup) { this.isUsePopup = isUsePopup; }
129 
130   public boolean isUsePopup() { return isUsePopup; }
131 
132   /*
133    * (non-Javadoc)
134    * @seeorg.exoplatform.ecm.webui.selector.ComponentSelector#setComponent(org.
135    * exoplatform.webui.core.UIComponent, java.lang.String[])
136    */
137   public void setSourceComponent(UIComponent uicomponent, String[] initParams) {
138     uiComponent = uicomponent;
139     if (initParams == null || initParams.length == 0)
140       return;
141     for (int i = 0; i < initParams.length; i++) {
142       if (initParams[i].indexOf("returnField") > -1) {
143         String[] array = initParams[i].split("=");
144         returnFieldName = array[1];
145         break;
146       }
147       returnFieldName = initParams[0];
148     }
149   }
150 
151   /**
152    * The listener interface for receiving selectMembershipAction events. The
153    * class that is interested in processing a selectMembershipAction event
154    * implements this interface, and the object created with that class is
155    * registered with a component using the component's
156    * <code>addSelectMembershipActionListener</code> method. When
157    * the selectMembershipAction event occurs, that object's appropriate
158    * method is invoked.
159    */
160   static public class SelectMembershipActionListener extends EventListener<UIPermissionSelector> {
161 
162     /* (non-Javadoc)
163      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
164      */
165     public void execute(Event<UIPermissionSelector> event) throws Exception {
166       UIPermissionSelector uiPermissionSelector = event.getSource();
167       if (uiPermissionSelector.getCurrentGroup() == null)
168         return;
169       String groupId = uiPermissionSelector.getCurrentGroup().getId();
170       String permission = event.getRequestContext().getRequestParameter(OBJECTID);
171       String value = "";
172       if(uiPermissionSelector.isSelectedUser()) {
173         value = permission;
174       } else {
175         value = permission + ":" + groupId;
176       }
177       String returnField = uiPermissionSelector.getReturnField();
178       ((UISelectable) uiPermissionSelector.getSourceComponent()).doSelect(returnField, value);
179       if (uiPermissionSelector.isUsePopup) {
180         UIPopupWindow uiPopup = uiPermissionSelector.getParent();
181         uiPopup.setShow(false);
182         uiPopup.setRendered(false);
183         UIComponent uicomp = uiPermissionSelector.getSourceComponent().getParent();
184         event.getRequestContext().addUIComponentToUpdateByAjax(uicomp);
185         if (!uiPopup.getId().equals("PopupComponent"))
186           event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup);
187       } else {
188         event.getRequestContext().addUIComponentToUpdateByAjax(
189             uiPermissionSelector.getSourceComponent());
190       }
191     }
192   }
193 
194   /**
195    * The listener interface for receiving changeNodeAction events. The class
196    * that is interested in processing a changeNodeAction event implements this
197    * interface, and the object created with that class is registered with a
198    * component using the component's
199    * <code>addChangeNodeActionListener</code> method. When
200    * the changeNodeAction event occurs, that object's appropriate
201    * method is invoked.
202    */
203   static public class ChangeNodeActionListener extends EventListener<UITree> {
204 
205     /* (non-Javadoc)
206      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
207      */
208     public void execute(Event<UITree> event) throws Exception {
209       UIPermissionSelector uiPermissionSelector = event.getSource().getParent();
210       String groupId = event.getRequestContext().getRequestParameter(OBJECTID);
211       uiPermissionSelector.changeGroup(groupId);
212       event.getRequestContext().addUIComponentToUpdateByAjax(uiPermissionSelector);
213     }
214   }
215 
216   /**
217    * The listener interface for receiving selectPathAction events. The class
218    * that is interested in processing a selectPathAction event implements this
219    * interface, and the object created with that class is registered with a
220    * component using the component's
221    * <code>addSelectPathActionListener</code> method. When
222    * the selectPathAction event occurs, that object's appropriate
223    * method is invoked.
224    */
225   static public class SelectPathActionListener extends EventListener<UIBreadcumbs> {
226 
227     /* (non-Javadoc)
228      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
229      */
230     public void execute(Event<UIBreadcumbs> event) throws Exception {
231       UIBreadcumbs uiBreadcumbs = event.getSource();
232       UIPermissionSelector uiPermissionSelector = uiBreadcumbs.getParent();
233       String objectId = event.getRequestContext().getRequestParameter(OBJECTID);
234       uiBreadcumbs.setSelectPath(objectId);
235       String selectGroupId = uiBreadcumbs.getSelectLocalPath().getId();
236       uiPermissionSelector.changeGroup(selectGroupId);
237       if (uiPermissionSelector.isUsePopup) {
238         UIPopupWindow uiPopup = uiBreadcumbs.getAncestorOfType(UIPopupWindow.class);
239         uiPopup.setShow(true);
240         uiPopup.setShowMask(true);
241       }
242       event.getRequestContext().addUIComponentToUpdateByAjax(uiPermissionSelector);
243     }
244   }
245 
246   static public class AddAnyPermissionActionListener extends EventListener<UIAnyPermission> {
247     public void execute(Event<UIAnyPermission> event) throws Exception {
248       UIAnyPermission uiAnyPermission = event.getSource();
249       UIPermissionSelector uiPermissionSelector = uiAnyPermission.getParent();
250       String returnField = uiPermissionSelector.getReturnField();
251       String value = "*";
252       ((UISelectable)uiPermissionSelector.getSourceComponent()).doSelect(returnField, value);
253       if (uiPermissionSelector.isUsePopup()) {
254         UIPopupWindow uiPopup = uiPermissionSelector.getParent();
255         uiPopup.setShow(false);
256         UIComponent uicomp = uiPermissionSelector.getSourceComponent().getParent();
257         event.getRequestContext().addUIComponentToUpdateByAjax(uicomp);
258         if (!uiPopup.getId().equals("PopupComponent"))
259           event.getRequestContext().addUIComponentToUpdateByAjax(uiPopup);
260       } else {
261         event.getRequestContext().addUIComponentToUpdateByAjax(
262             uiPermissionSelector.getSourceComponent());
263       }
264     }
265   }
266 
267   /**
268    * Sets the selected user.
269    *
270    * @param bool the new selected user
271    */
272   public void setSelectedUser(boolean bool) {
273     isSelectedUser = bool;
274   }
275 
276   /**
277    * Checks if is selected user.
278    *
279    * @return true, if is selected user
280    */
281   public boolean isSelectedUser() {
282     return isSelectedUser;
283   }
284 
285   /**
286    * Sets the selected membership.
287    *
288    * @param bool the new selected membership
289    */
290   public void setSelectedMembership(boolean bool) {
291     isSelectedMembership = bool;
292   }
293 
294   /**
295    * Checks if is selected membership.
296    *
297    * @return true, if is selected membership
298    */
299   public boolean isSelectedMembership() {
300     return isSelectedMembership;
301   }
302 
303   /**
304    * Gets the users.
305    *
306    * @return the users
307    *
308    * @throws Exception the exception
309    */
310   public List getUsers() throws Exception {
311     List<User> children = new ArrayList<User>();
312     OrganizationService service = WCMCoreUtils.getService(OrganizationService.class);
313     ListAccess<User> userPageList = service.getUserHandler().findUsersByGroupId(this.getCurrentGroup().getId());
314     for (User child : userPageList.load(0, userPageList.getSize())) {
315       children.add(child);
316     }
317     return children;
318   }
319 
320  /**
321   * Check show/hide form to set any permission
322   * @return
323   */
324  public boolean isShowAnyPermission() {
325    return isShowAnyPermission;
326  }
327 
328  /**
329   * Set show/hide any permission form
330   * @param isShowAnyPermission
331   * isShowAnyPermission =  true: Show form  <br>
332   * isShowAnyPermission =  false: Hide form
333   */
334  public void setShowAnyPermission(boolean isShowAnyPermission) {
335    this.isShowAnyPermission = isShowAnyPermission;
336  }
337  
338  @SuppressWarnings("unchecked")
339  public List<String> getListGroup() throws Exception {
340    OrganizationService service = WCMCoreUtils.getService(OrganizationService.class);
341    List<String> listGroup = new ArrayList<String>();
342    if(getCurrentGroup() == null) return null;
343    Collection<Group> groups = service.getGroupHandler().findGroups(getCurrentGroup());
344    if(groups.size() > 0) {
345      for (Object child : groups) {
346        Group childGroup = (Group)child;
347        listGroup.add(childGroup.getId()) ;
348      }
349    }
350    return listGroup;
351 
352  }
353 }