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.core;
18  
19  import javax.jcr.Node;
20  
21  import org.exoplatform.ecm.permission.info.UIPermissionInputSet;
22  import org.exoplatform.ecm.utils.lock.LockUtil;
23  import org.exoplatform.ecm.webui.utils.PermissionUtil;
24  import org.exoplatform.ecm.webui.utils.Utils;
25  import org.exoplatform.services.log.ExoLogger;
26  import org.exoplatform.services.log.Log;
27  import org.exoplatform.webui.core.UIComponent;
28  import org.exoplatform.webui.core.UIContainer;
29  import org.exoplatform.webui.core.UIGrid;
30  import org.exoplatform.webui.core.UIPopupComponent;
31  import org.exoplatform.webui.core.UIPopupWindow;
32  import org.exoplatform.webui.event.Event;
33  import org.exoplatform.webui.event.EventListener;
34  import org.exoplatform.webui.organization.account.UIUserSelector;
35  
36  public abstract class UIPermissionManagerBase extends UIContainer implements UIPopupComponent{
37    private static final Log LOG  = ExoLogger.getLogger(UIPermissionManagerBase.class.getName());
38  
39    public void initPopupPermission(UIComponent uiSelector) throws Exception {
40      UIPopupWindow uiPopup = getChildById(UIPermissionFormBase.POPUP_SELECT);
41      if(uiPopup == null) {
42        uiPopup = addChild(UIPopupWindow.class, null, UIPermissionFormBase.POPUP_SELECT);
43        uiPopup.setWindowSize(560, 345);
44        uiPopup.setShowMask(true);
45      } else {
46        uiPopup.setShowMask(true);
47        uiPopup.setRendered(true);
48      }
49      uiPopup.setUIComponent(uiSelector);
50      uiPopup.setShow(true);
51      uiPopup.setResizable(true);
52    }
53  
54    public void initUserSelector() throws Exception {
55      UIPopupWindow uiPopup = getChildById("PopupUserSelector");
56      if(uiPopup == null) {
57        uiPopup = addChild(UIPopupWindow.class, null, "PopupUserSelector");
58      }
59      uiPopup.setWindowSize(790, 400);
60      UIUserContainer uiUserContainer = this.createUIComponent(UIUserContainer.class, null, null);
61      uiPopup.setUIComponent(uiUserContainer);
62      uiPopup.setShow(true);
63      uiPopup.setShowMask(true);
64      uiPopup.setResizable(true);
65    }
66  
67    public void activate() {
68      try {
69        getChild(UIPermissionInfoBase.class).updateGrid(1);
70      } catch (Exception e) {
71        if (LOG.isErrorEnabled()) {
72          LOG.error("Unexpected error!", e.getMessage());
73        }
74      }
75    }
76  
77    public void checkPermissonInfo(Node node) throws Exception {
78      if (node.isLocked()) {
79        String lockToken = LockUtil.getLockToken(node);
80        if (lockToken != null)
81          node.getSession().addLockToken(lockToken);
82        if (!Utils.isLockTokenHolder(node)) {
83          getChild(UIPermissionInfoBase.class).getChild(UIGrid.class)
84                                          .configure("usersOrGroups",
85                                                     UIPermissionInfoBase.PERMISSION_BEAN_FIELD,
86                                                     new String[] {});
87          getChild(UIPermissionFormBase.class).setRendered(false);
88        }
89      } else {
90        if (!PermissionUtil.canChangePermission(node)) {
91          getChild(UIPermissionInfoBase.class).getChild(UIGrid.class)
92                                          .configure("usersOrGroups",
93                                                     UIPermissionInfoBase.PERMISSION_BEAN_FIELD,
94                                                     new String[] {});
95          getChild(UIPermissionFormBase.class).setRendered(false);
96        }
97      }
98    }
99    
100   public void deActivate() {
101   }
102 
103   static public class AddUserActionListener extends EventListener<UIUserSelector> {
104     public void execute(Event<UIUserSelector> event) throws Exception {
105       UIUserSelector uiForm = event.getSource();
106       UIPermissionManagerBase uiParent = uiForm.getAncestorOfType(UIPermissionManagerBase.class);
107       UIPermissionFormBase uiPermissionForm = uiParent.getChild(UIPermissionFormBase.class);
108       uiPermissionForm.doSelect(UIPermissionInputSet.FIELD_USERORGROUP, uiForm.getSelectedUsers());
109       UIPopupWindow uiPopup = uiParent.getChild(UIPopupWindow.class);
110       uiPopup.setUIComponent(null);
111       uiPopup.setShow(false);
112       event.getRequestContext().addUIComponentToUpdateByAjax(uiParent);
113     }
114   }
115 }