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.component.admin.unlock;
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.ecm.webui.selector.ComponentSelector;
25  import org.exoplatform.ecm.webui.selector.UIAnyPermission;
26  import org.exoplatform.ecm.utils.lock.LockUtil;
27  import org.exoplatform.services.cms.lock.LockService;
28  import org.exoplatform.services.organization.Group;
29  import org.exoplatform.services.organization.OrganizationService;
30  import org.exoplatform.services.organization.User;
31  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
32  import org.exoplatform.webui.config.annotation.ComponentConfig;
33  import org.exoplatform.webui.config.annotation.ComponentConfigs;
34  import org.exoplatform.webui.config.annotation.EventConfig;
35  import org.exoplatform.webui.core.UIBreadcumbs;
36  import org.exoplatform.webui.core.UIComponent;
37  import org.exoplatform.webui.core.UITree;
38  import org.exoplatform.webui.event.Event;
39  import org.exoplatform.webui.event.EventListener;
40  import org.exoplatform.webui.organization.UIGroupMembershipSelector;
41  
42  /**
43   * Created by The eXo Platform SARL Author : Dang Van Minh
44   * minh.dang@exoplatform.com Nov 17, 2006
45   */
46  @ComponentConfigs( {
47      @ComponentConfig(
48          template = "classpath:groovy/ecm/webui/UIMemberSelector.gtmpl",
49          events = {
50            @EventConfig(listeners = UIPermissionSelector.ChangeNodeActionListener.class),
51            @EventConfig(listeners = UIPermissionSelector.SelectMembershipActionListener.class),
52            @EventConfig(listeners = UIPermissionSelector.SelectPathActionListener.class)
53          }),
54      @ComponentConfig(
55          type = UITree.class,
56          id = "UITreeGroupSelector",
57          template = "system:/groovy/webui/core/UITree.gtmpl",
58          events = @EventConfig(listeners = UITree.ChangeNodeActionListener.class)),
59      @ComponentConfig(
60          type = UIBreadcumbs.class,
61          id = "BreadcumbGroupSelector",
62          template = "system:/groovy/webui/core/UIBreadcumbs.gtmpl",
63          events = @EventConfig(listeners = UIBreadcumbs.SelectPathActionListener.class))
64      }
65  )
66  
67  public class UIPermissionSelector extends UIGroupMembershipSelector implements ComponentSelector {
68  
69    /** The Constant defaultValue. */
70    final static public String defaultValue    = "/admin";
71  
72    /** The ui component. */
73    private UIComponent        uiComponent;
74  
75    /** The return field name. */
76    private String             returnFieldName = null;
77  
78    /** The is selected membership. */
79    private boolean            isSelectedMembership = true;
80  
81    /** The is selected user. */
82    private boolean            isSelectedUser;
83  
84    /** The is use popup. */
85    private boolean             isUsePopup      = true;
86  
87    /**
88     * Instantiates a new uI permission selector.
89     *
90     * @throws Exception the exception
91     */
92    public UIPermissionSelector() throws Exception {
93      changeGroup(defaultValue);
94      addChild(UIAnyPermission.class, null, null);
95    }
96  
97    /**
98     * Sets the current permission.
99     *
100    * @param per the new current permission
101    *
102    * @throws Exception the exception
103    */
104   public void setCurrentPermission(String per) throws Exception {
105     changeGroup(per);
106   }
107 
108   /**
109    * Gets the return component.
110    *
111    * @return the return component
112    */
113 
114   public UIComponent getSourceComponent() {
115     return uiComponent;
116   }
117 
118   /**
119    * Gets the return field.
120    *
121    * @return the return field
122    */
123   public String getReturnField() {
124     return returnFieldName;
125   }
126 
127   public void setIsUsePopup(boolean isUsePopup) { this.isUsePopup = isUsePopup; }
128 
129   public boolean isUsePopup() { return isUsePopup; }
130 
131   /*
132    * (non-Javadoc)
133    * @seeorg.exoplatform.ecm.webui.selector.ComponentSelector#setComponent(org.
134    * exoplatform.webui.core.UIComponent, java.lang.String[])
135    */
136   public void setSourceComponent(UIComponent uicomponent, String[] initParams) {
137     uiComponent = uicomponent;
138     if (initParams == null || initParams.length == 0)
139       return;
140     for (int i = 0; i < initParams.length; i++) {
141       if (initParams[i].indexOf("returnField") > -1) {
142         String[] array = initParams[i].split("=");
143         returnFieldName = array[1];
144         break;
145       }
146       returnFieldName = initParams[0];
147     }
148   }
149 
150   /**
151    * The listener interface for receiving selectMembershipAction events. The
152    * class that is interested in processing a selectMembershipAction event
153    * implements this interface, and the object created with that class is
154    * registered with a component using the component's
155    * <code>addSelectMembershipActionListener</code> method. When
156    * the selectMembershipAction event occurs, that object's appropriate
157    * method is invoked.
158    */
159   static public class SelectMembershipActionListener extends EventListener<UIPermissionSelector> {
160 
161     /* (non-Javadoc)
162      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
163      */
164     public void execute(Event<UIPermissionSelector> event) throws Exception {
165       UIPermissionSelector uiPermissionSelector = event.getSource();
166       if (uiPermissionSelector.getCurrentGroup() == null)
167         return;
168       String groupId = uiPermissionSelector.getCurrentGroup().getId();
169       String permission = event.getRequestContext().getRequestParameter(OBJECTID);
170       String value = "";
171       if(uiPermissionSelector.isSelectedUser()) {
172         value = permission;
173       } else {
174         value = permission + ":" + groupId;
175       }
176       LockService lockService = uiPermissionSelector.getApplicationComponent(LockService.class);
177       lockService.addGroupsOrUsersForLock(value);
178       LockUtil.updateLockCache(value);
179       UIUnLockManager uiUnLockManager = uiPermissionSelector.getAncestorOfType(UIUnLockManager.class);
180       uiUnLockManager.refresh();
181       uiUnLockManager.getChild(UILockNodeList.class).setRendered(false);
182       uiUnLockManager.getChild(UILockHolderContainer.class).setRendered(true);
183     }
184   }
185 
186   /**
187    * The listener interface for receiving changeNodeAction events. The class
188    * that is interested in processing a changeNodeAction event implements this
189    * interface, and the object created with that class is registered with a
190    * component using the component's
191    * <code>addChangeNodeActionListener</code> method. When
192    * the changeNodeAction event occurs, that object's appropriate
193    * method is invoked.
194    */
195   static public class ChangeNodeActionListener extends EventListener<UITree> {
196 
197     /* (non-Javadoc)
198      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
199      */
200     public void execute(Event<UITree> event) throws Exception {
201       UIPermissionSelector uiPermissionSelector = event.getSource().getParent();
202       String groupId = event.getRequestContext().getRequestParameter(OBJECTID);
203       uiPermissionSelector.changeGroup(groupId);
204       event.getRequestContext().addUIComponentToUpdateByAjax(uiPermissionSelector);
205     }
206   }
207 
208   /**
209    * The listener interface for receiving selectPathAction events. The class
210    * that is interested in processing a selectPathAction event implements this
211    * interface, and the object created with that class is registered with a
212    * component using the component's
213    * <code>addSelectPathActionListener</code> method. When
214    * the selectPathAction event occurs, that object's appropriate
215    * method is invoked.
216    */
217   static public class SelectPathActionListener extends EventListener<UIBreadcumbs> {
218 
219     /* (non-Javadoc)
220      * @see org.exoplatform.webui.event.EventListener#execute(org.exoplatform.webui.event.Event)
221      */
222     public void execute(Event<UIBreadcumbs> event) throws Exception {
223       UIBreadcumbs uiBreadcumbs = event.getSource();
224       UIPermissionSelector uiPermissionSelector = uiBreadcumbs.getParent();
225       String objectId = event.getRequestContext().getRequestParameter(OBJECTID);
226       uiBreadcumbs.setSelectPath(objectId);
227       String selectGroupId = uiBreadcumbs.getSelectLocalPath().getId();
228       uiPermissionSelector.changeGroup(selectGroupId);
229       event.getRequestContext().addUIComponentToUpdateByAjax(uiPermissionSelector);
230     }
231   }
232 
233   /**
234    * Sets the selected user.
235    *
236    * @param bool the new selected user
237    */
238   public void setSelectedUser(boolean bool) {
239     isSelectedUser = bool;
240   }
241 
242   /**
243    * Checks if is selected user.
244    *
245    * @return true, if is selected user
246    */
247   public boolean isSelectedUser() {
248     return isSelectedUser;
249   }
250 
251   /**
252    * Sets the selected membership.
253    *
254    * @param bool the new selected membership
255    */
256   public void setSelectedMembership(boolean bool) {
257     isSelectedMembership = bool;
258   }
259 
260   /**
261    * Checks if is selected membership.
262    *
263    * @return true, if is selected membership
264    */
265   public boolean isSelectedMembership() {
266     return isSelectedMembership;
267   }
268 
269   /**
270    * Gets the users.
271    *
272    * @return the users
273    *
274    * @throws Exception the exception
275    */
276   public List getUsers() throws Exception {
277     List<User> children = new ArrayList<User>();
278     OrganizationService service = WCMCoreUtils.getService(OrganizationService.class);
279     ListAccess<User> userPageList = service.getUserHandler().findUsersByGroupId(this.getCurrentGroup().getId());
280     for (User child : userPageList.load(0, userPageList.getSize())) {
281       children.add(child);
282     }
283     return children;
284   }
285 
286  /**
287   * Check show/hide form to set any permission
288   * @return
289   */
290  public boolean isShowAnyPermission() {
291    return false;
292  }
293  
294  @SuppressWarnings("unchecked")
295  public List<String> getListGroup() throws Exception {
296    OrganizationService service = WCMCoreUtils.getService(OrganizationService.class);
297    List<String> listGroup = new ArrayList<String>();
298    if(getCurrentGroup() == null) return null;
299    Collection<Group> groups = service.getGroupHandler().findGroups(getCurrentGroup());
300    if(groups.size() > 0) {
301      for (Object child : groups) {
302        Group childGroup = (Group)child;
303        listGroup.add(childGroup.getId()) ;
304      }
305    }
306    return listGroup;
307 
308  }
309 }