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 org.exoplatform.ecm.webui.form.UIFormInputSetWithAction;
20  import org.exoplatform.ecm.webui.selector.UISelectable;
21  import org.exoplatform.services.cms.lock.LockService;
22  import org.exoplatform.web.application.ApplicationMessage;
23  import org.exoplatform.webui.config.annotation.ComponentConfig;
24  import org.exoplatform.webui.config.annotation.EventConfig;
25  import org.exoplatform.webui.core.UIApplication;
26  import org.exoplatform.webui.core.UIPopupWindow;
27  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
28  import org.exoplatform.webui.event.Event;
29  import org.exoplatform.webui.event.Event.Phase;
30  import org.exoplatform.webui.event.EventListener;
31  import org.exoplatform.webui.form.UIForm;
32  import org.exoplatform.webui.form.UIFormStringInput;
33  import org.exoplatform.webui.form.validator.MandatoryValidator;
34  
35  /**
36   * Created by The eXo Platform SARL
37   * Author : Dang Van Minh
38   *          minh.dang@exoplatform.com
39   * Dec 29, 2006
40   * 11:30:29 AM
41   */
42  @ComponentConfig(
43      lifecycle = UIFormLifecycle.class,
44      template = "system:/groovy/webui/form/UIForm.gtmpl",
45      events = {
46        @EventConfig(listeners = UIUnLockForm.SaveActionListener.class),
47        @EventConfig(phase = Phase.DECODE, listeners = UIUnLockForm.CancelActionListener.class),
48        @EventConfig(phase = Phase.DECODE, listeners = UIUnLockForm.AddPermissionActionListener.class)
49      }
50  )
51  public class UIUnLockForm extends UIForm implements UISelectable {
52  
53    final static public String GROUPS_OR_USERS = "groupsOrUsers";
54    final static public String[] ACTIONS = {"Save", "Cancel"};
55  
56    public UIUnLockForm() throws Exception {
57      UIFormInputSetWithAction uiInputAct = new UIFormInputSetWithAction("PermissionButton");
58      uiInputAct.addUIFormInput( new UIFormStringInput(GROUPS_OR_USERS, GROUPS_OR_USERS, null).
59                                 setReadOnly(true).addValidator(MandatoryValidator.class));
60      uiInputAct.setActionInfo(GROUPS_OR_USERS, new String[] {"AddPermission"});
61      addUIComponentInput(uiInputAct);
62    }
63  
64    public String[] getActions() { return ACTIONS ; }
65  
66    public void doSelect(String selectField, Object value) {
67      getUIStringInput(selectField).setValue(value.toString());
68      UIUnLockManager uiManager = getAncestorOfType(UIUnLockManager.class);
69      UIPopupWindow uiPopup = uiManager.getChildById("PermissionPopup");
70      uiPopup.setRendered(false);
71      uiPopup.setShow(false);
72      uiManager.getChild(UILockNodeList.class).setRendered(false);
73      uiManager.getChild(UIUnLockForm.class).setRendered(true);
74    }
75  
76    public void update()throws Exception {
77      getUIStringInput(GROUPS_OR_USERS).setValue("");
78    }
79  
80    static public class CancelActionListener extends EventListener<UIUnLockForm> {
81      public void execute(Event<UIUnLockForm> event) throws Exception {
82        UIUnLockForm uiForm = event.getSource();
83        UIUnLockManager uiManager = uiForm.getAncestorOfType(UIUnLockManager.class);
84        uiManager.removeChildById("PermissionPopup");
85        event.getRequestContext().addUIComponentToUpdateByAjax(uiManager);
86      }
87    }
88  
89    static public class SaveActionListener extends EventListener<UIUnLockForm> {
90      public void execute(Event<UIUnLockForm> event) throws Exception {
91        UIUnLockForm uiUnLockForm = event.getSource();
92        UIApplication uiApp = uiUnLockForm.getAncestorOfType(UIApplication.class);
93        UIFormInputSetWithAction permField = uiUnLockForm.getChildById("PermissionButton");
94        String groupsOrUsers = permField.getUIStringInput(GROUPS_OR_USERS).getValue();
95        if((groupsOrUsers == null)||(groupsOrUsers.trim().length() == 0)) {
96          uiApp.addMessage(new ApplicationMessage("UIUnLockForm.msg.permission-require", null,
97                                                  ApplicationMessage.WARNING));
98  
99          return;
100       }
101       UIUnLockManager uiManager = uiUnLockForm.getAncestorOfType(UIUnLockManager.class);
102       LockService lockService = uiUnLockForm.getApplicationComponent(LockService.class);
103       lockService.addGroupsOrUsersForLock(groupsOrUsers);
104       UILockNodeList uiLockList = uiManager.getChild(UILockNodeList.class);
105       uiUnLockForm.update();
106       uiLockList.refresh(1);
107       uiLockList.setRendered(true);
108       uiManager.removeChildById("PermissionPopup");
109       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager);
110     }
111   }
112 
113   static public class AddPermissionActionListener extends EventListener<UIUnLockForm> {
114     public void execute(Event<UIUnLockForm> event) throws Exception {
115       UIUnLockManager uiManager = event.getSource().getAncestorOfType(UIUnLockManager.class);
116       String membership = event.getSource().getUIStringInput(GROUPS_OR_USERS).getValue();
117       uiManager.initPermissionPopup(membership);
118       event.getRequestContext().addUIComponentToUpdateByAjax(uiManager);
119     }
120   }
121 }