View Javadoc
1   /*
2    * Copyright (C) 2003-2009 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.social.webui.space;
18  
19  import java.util.Iterator;
20  import java.util.ResourceBundle;
21  
22  import org.exoplatform.services.organization.Group;
23  import org.exoplatform.services.security.ConversationState;
24  import org.exoplatform.services.security.MembershipEntry;
25  import org.exoplatform.social.core.space.SpaceUtils;
26  import org.exoplatform.social.webui.UISocialGroupSelector;
27  import org.exoplatform.webui.application.WebuiRequestContext;
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.UIContainer;
32  import org.exoplatform.webui.core.UIPopupWindow;
33  import org.exoplatform.webui.event.Event;
34  import org.exoplatform.webui.event.Event.Phase;
35  import org.exoplatform.webui.event.EventListener;
36  import org.exoplatform.webui.form.UIFormInputInfo;
37  import org.exoplatform.webui.form.input.UICheckBoxInput;
38  
39  @ComponentConfigs({
40    @ComponentConfig(
41      template = "war:/groovy/social/webui/space/UISpaceGroupBound.gtmpl",
42      events = {@EventConfig(listeners = UISpaceGroupBound.SelectGroupActionListener.class, phase=Phase.DECODE) }
43    ),
44    @ComponentConfig(
45      type = UIPopupWindow.class,
46      id = "SelectGroup",
47      template =  "system:/groovy/webui/core/UIPopupWindow.gtmpl",
48      events = @EventConfig(listeners = UISpaceGroupBound.ClosePopupActionListener.class, name = "ClosePopup")
49   )
50  })
51  
52  public class UISpaceGroupBound extends UIContainer {
53    private final String USE_EXISTING_GROUP = "UseExistingGroupCheckBox";
54    private final String POPUP_GROUP_BOUND = "UIPopupGroupBound";
55    private final String SELECTED_GROUP = "groupId";
56    private final String ANY_MEMBERSHIP_TYPE = "*";
57  
58    /** Html attribute title. */
59    private static final String HTML_ATTRIBUTE_TITLE   = "title";
60    
61    /**
62     * constructor
63     * @throws Exception
64     */
65    public UISpaceGroupBound() throws Exception {
66      WebuiRequestContext requestContext = WebuiRequestContext.getCurrentInstance();
67      ResourceBundle resourceBundle = requestContext.getApplicationResourceBundle();
68      UICheckBoxInput uiUseExisting = new UICheckBoxInput(USE_EXISTING_GROUP, null, false);
69      uiUseExisting.setHTMLAttribute(HTML_ATTRIBUTE_TITLE, resourceBundle.getString("UISpaceGroupBound.label.useExistingGroup"));
70      uiUseExisting.setId(USE_EXISTING_GROUP);
71      uiUseExisting.setOnChange("ToggleUseGroup");
72      addChild(uiUseExisting);
73      
74      UIFormInputInfo uiFormInputInfo = new UIFormInputInfo(SELECTED_GROUP, null, null);
75      uiFormInputInfo.setId(SELECTED_GROUP);
76      addChild(uiFormInputInfo);
77      UIPopupWindow uiPopup = createUIComponent(UIPopupWindow.class, "SelectGroup", POPUP_GROUP_BOUND);
78      uiPopup.setWindowSize(550, 0);
79      addChild(uiPopup);
80    }
81  
82    /**
83     * gets selected group from group bound
84     * @return selected group
85     */
86    @SuppressWarnings("unchecked")
87    public String getSelectedGroup() {
88      UICheckBoxInput uiCheckBox = getChild(UICheckBoxInput.class);
89      if(uiCheckBox.isChecked()) {
90        UIFormInputInfo uiInfo = getChild(UIFormInputInfo.class);
91        return uiInfo.getValue();
92      }
93      return null;
94    }
95  
96    /**
97     * Check current user is manager of group or not.
98     * 
99     * @return True if current user has one group that he is manager of that group.
100    * @throws Exception
101    */
102   protected boolean hasGroupWithManagerRole() throws Exception {
103     String adminMSType = SpaceUtils.getUserACL().getAdminMSType();
104 
105     Iterator<MembershipEntry> iter = ConversationState.getCurrent().getIdentity().getMemberships().iterator();
106     while (iter.hasNext()) {
107       String msType = iter.next().getMembershipType();
108       // has any or super membership type
109       if (ANY_MEMBERSHIP_TYPE.equals(msType) || adminMSType.equals(msType)) {
110         return true;
111       }
112     }
113 
114     return false;
115   }
116 
117   /**
118    * triggers this action when user clicks on select group on UIGroupSelector
119    */
120   static public class SelectGroupActionListener extends EventListener<UISocialGroupSelector> {
121     public void execute(Event<UISocialGroupSelector> event) throws Exception {
122       WebuiRequestContext context = event.getRequestContext();
123       String groupId = context.getRequestParameter(OBJECTID);
124       UISocialGroupSelector uiGroupSelector = event.getSource();
125       UISpaceGroupBound uiGroupBound = uiGroupSelector.getAncestorOfType(UISpaceGroupBound.class);
126       UIFormInputInfo uiFormInputInfo = uiGroupBound.getChild(UIFormInputInfo.class);
127       uiFormInputInfo.setValue(groupId);
128       event.getRequestContext().addUIComponentToUpdateByAjax(uiGroupBound);
129     }
130   }
131 
132   /**
133    * Check if user selected a group or  not when closing the popup,
134    * if not un-check the checked check box
135    */
136   static public class ClosePopupActionListener extends EventListener<UIPopupWindow> {
137     @SuppressWarnings("unchecked")
138     @Override
139     public void execute(Event<UIPopupWindow> event) throws Exception {
140       UIPopupWindow uiPopup = event.getSource();
141       UISpaceGroupBound uiGroupBound = uiPopup.getAncestorOfType(UISpaceGroupBound.class);
142       UISocialGroupSelector uiGroupSelector = (UISocialGroupSelector)uiPopup.getUIComponent();
143       Group group = uiGroupSelector.getCurrentGroup();
144       if (group == null) {
145         UICheckBoxInput uiUseExisting = uiGroupBound.getChild(UICheckBoxInput.class);
146         uiUseExisting.setChecked(false);
147       } else {
148         UIFormInputInfo uiSelected = uiGroupBound.getChild(UIFormInputInfo.class);
149         uiSelected.setValue(group.getId());
150       }
151       uiPopup.setShow(false);
152     }
153   }
154 }