View Javadoc
1   /*
2   
3    * Copyright (C) 2003-2009 eXo Platform SAS.
4    *
5    * This program is free software; you can redistribute it and/or
6    * modify it under the terms of the GNU Affero General Public License
7    * as published by the Free Software Foundation; either version 3
8    * of the License, or (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, see<http://www.gnu.org/licenses/>.
17   */
18  package org.exoplatform.social.webui.space;
19  
20  import java.util.List;
21  import java.util.ResourceBundle;
22  
23  import org.apache.commons.lang.StringEscapeUtils;
24  
25  import org.exoplatform.commons.utils.CommonsUtils;
26  import org.exoplatform.container.ExoContainer;
27  import org.exoplatform.container.ExoContainerContext;
28  import org.exoplatform.container.PortalContainer;
29  import org.exoplatform.services.log.ExoLogger;
30  import org.exoplatform.services.log.Log;
31  import org.exoplatform.social.core.identity.model.Identity;
32  import org.exoplatform.social.core.identity.provider.SpaceIdentityProvider;
33  import org.exoplatform.social.core.manager.IdentityManager;
34  import org.exoplatform.social.core.space.SpaceException;
35  import org.exoplatform.social.core.space.SpaceTemplate;
36  import org.exoplatform.social.core.space.SpaceUtils;
37  import org.exoplatform.social.core.space.SpacesAdministrationService;
38  import org.exoplatform.social.core.space.model.Space;
39  import org.exoplatform.social.core.space.spi.SpaceService;
40  import org.exoplatform.social.core.space.spi.SpaceTemplateService;
41  import org.exoplatform.social.webui.Utils;
42  import org.exoplatform.web.application.ApplicationMessage;
43  import org.exoplatform.web.application.JavascriptManager;
44  import org.exoplatform.webui.application.WebuiRequestContext;
45  import org.exoplatform.webui.config.annotation.ComponentConfig;
46  import org.exoplatform.webui.config.annotation.EventConfig;
47  import org.exoplatform.webui.core.UIApplication;
48  import org.exoplatform.webui.core.UIPopupWindow;
49  import org.exoplatform.webui.core.lifecycle.UIFormLifecycle;
50  import org.exoplatform.webui.event.Event;
51  import org.exoplatform.webui.event.Event.Phase;
52  import org.exoplatform.webui.event.EventListener;
53  import org.exoplatform.webui.form.*;
54  
55  import static org.exoplatform.social.webui.space.UISpaceSettings.SPACE_TEMPLATE;
56  
57  /**
58   * UIAddSpaceForm to create new space. By using this UIForm, user can create a
59   * brand new space or a space from an existing group
60   *
61   * @author <a href="mailto:hoatlevan@gmail.com">hoatle</a>
62   * @since Jun 29, 2009
63   */
64  @ComponentConfig(
65    lifecycle = UIFormLifecycle.class,
66    template = "system:/groovy/webui/form/UIFormWithTitle.gtmpl",
67    events = {
68      @EventConfig(listeners = UISpaceAddForm.CreateActionListener.class),
69      @EventConfig(listeners = UISpaceAddForm.ChangeTemplateActionListener.class, phase = Phase.DECODE)
70    }
71  )
72  
73  public class UISpaceAddForm extends UIForm {
74  
75    private static final Log LOG = ExoLogger.getLogger(UISpaceAddForm.class);
76    
77    static private final String MSG_ERROR_SPACE_CREATION = "UISpaceAddForm.msg.error_space_creation";
78    static private final String MSG_ERROR_DATASTORE = "UISpaceAddForm.msg.error_space_not_saved";
79    static private final String MSG_ERROR_UNABLE_TO_INIT_APP = "UISpaceAddForm.msg.error_unable_to_init_app";
80    static private final String MSG_ERROR_UNABLE_TO_ADD_CREATOR = "UISpaceAddForm.msg.error_unable_to_add_creator";
81    static private final String MSG_ERROR_UNABLE_TO_ADD_APPLICATION = "UISpaceAddForm.msg.error_unable_to_add_application";
82    static private final String MSG_ERROR_RETRIEVING_USER = "UISpaceAddForm.msg.error_unable_to_retrieve_user";
83    static private final String MSG_SPACE_CREATION_SUCCESS = "UISpaceAddForm.msg.space_creation_success";
84    static private final String MSG_ERROR_SPACE_ALREADY_EXIST = "UISpaceAddForm.msg.error_space_already_exist";
85    static private final String MSG_ERROR_SPACE_PERMISSION = "UISpaceAddForm.msg.error_space_permission";
86    private final String SPACE_SETTINGS = "UISpaceSettings";
87    private final String SPACE_VISIBILITY = "UISpaceVisibility";
88    
89    /**
90     * Constructor: add 3 UI component to this UIFormTabPane:
91     * <br>
92     * <pre>
93     * {@link UISpaceSettings}
94     * {@link UISpaceVisibility}
95     * {@link UIInvitation}
96     * </pre>
97     *
98     * @throws Exception
99     */
100   public UISpaceAddForm() throws Exception {
101     UISpaceSettings uiSpaceSettings = new UISpaceSettings(SPACE_SETTINGS);
102     addChild(uiSpaceSettings);
103 
104     UIFormInputSet uiSpaceVisibility = new UISpaceVisibility(SPACE_VISIBILITY);
105 
106     addChild(uiSpaceVisibility);
107 
108     addChild(UIInvitation.class, null, null);
109 
110     setActions(new String[]{"Create"});
111   }
112 
113   @Override
114   public String getLabel(ResourceBundle res, String key) {
115     return new StringBuffer(super.getLabel(res, key)).append((key.indexOf("action.") < 0) ? ":" : "").toString();
116   }
117 
118   /**
119    * listener for create space action
120    */
121   static public class CreateActionListener extends EventListener<UISpaceAddForm> {
122     @Override
123     public void execute(Event<UISpaceAddForm> event) throws Exception {
124       UISpaceAddForm uiAddForm = event.getSource();
125       WebuiRequestContext ctx = event.getRequestContext();
126       UIApplication uiApplication = ctx.getUIApplication();
127       SpaceService spaceService = uiAddForm.getApplicationComponent(SpaceService.class);
128       SpacesAdministrationService spacesAdministrationService =  CommonsUtils.getService(SpacesAdministrationService.class);
129       UIInvitation uiInvitation = uiAddForm.getChild(UIInvitation.class);
130       List<Identity> invitedIdentities = uiInvitation.getSelectedIdentities();
131       String creator = ctx.getRemoteUser();          
132       Space space = new Space();
133       uiAddForm.invokeSetBindingBean(space);
134       space.setDisplayName(space.getDisplayName().trim());
135       String spaceDisplayName = uiAddForm.getUIStringInput(UISpaceSettings.SPACE_DISPLAY_NAME).getValue();
136       String spaceDescription = uiAddForm.getUIFormTextAreaInput(UISpaceSettings.SPACE_DESCRIPTION).getValue();
137       String spaceVisibility = uiAddForm.findFirstComponentOfType(UISpaceVisibility.class).getVisibility();
138       space.setDisplayName(spaceDisplayName.trim());
139       space.setDescription(StringEscapeUtils.escapeHtml(spaceDescription));
140       space.setPrettyName(space.getDisplayName());     
141       space.setVisibility(spaceVisibility);
142       String msg = MSG_SPACE_CREATION_SUCCESS;
143       try {
144         // Checks user is still existing or not.
145         SpaceUtils.checkUserExisting(ctx.getRemoteUser());
146         
147         //validate the display name
148         if (spaceService.getSpaceByDisplayName(space.getDisplayName()) != null) {
149           throw new SpaceException(SpaceException.Code.SPACE_ALREADY_EXIST);
150         }
151         
152         //
153         Space got;
154         String prettyName = space.getPrettyName();
155         int count = 1;
156         while ((got = spaceService.getSpaceByPrettyName(prettyName)) != null) {
157           //
158           if (count == 1 && got.getDisplayName().equalsIgnoreCase(space.getDisplayName())) {
159             throw new SpaceException(SpaceException.Code.SPACE_ALREADY_EXIST);
160           }
161           prettyName = space.getPrettyName() + "_" + count;
162           //
163           ++count;
164         }
165         space.setPrettyName(prettyName);
166         
167         ExoContainer container = ExoContainerContext.getCurrentContainer();
168         IdentityManager idm = (IdentityManager) container.getComponentInstanceOfType(IdentityManager.class);
169         Identity identity = idm.getOrCreateIdentity(SpaceIdentityProvider.NAME, space.getPrettyName(), true);
170         if (identity != null) {
171           space.setPrettyName(SpaceUtils.buildPrettyName(space));
172         }
173         if(!spacesAdministrationService.canCreateSpace(ctx.getRemoteUser())) {
174           throw new SpaceException(SpaceException.Code.SPACE_PERMISSION);
175         } else {   
176           if (invitedIdentities != null) {// create space and invite identities to join it
177             space = spaceService.createSpace(space, creator, invitedIdentities);
178           } else { // Create new space
179             space = spaceService.createSpace(space, creator);
180           }
181         }
182       } catch (SpaceException se) {
183         if (se.getCode() == SpaceException.Code.SPACE_ALREADY_EXIST) {
184           msg = MSG_ERROR_SPACE_ALREADY_EXIST;
185           uiApplication.addMessage(new ApplicationMessage(msg, null, ApplicationMessage.WARNING));
186           return;
187         } else if (se.getCode() == SpaceException.Code.SPACE_PERMISSION) {
188           msg = MSG_ERROR_SPACE_PERMISSION;
189           uiApplication.addMessage(new ApplicationMessage(msg, null, ApplicationMessage.WARNING));
190           return;
191         } else if (se.getCode() == SpaceException.Code.UNABLE_TO_ADD_CREATOR) {
192           msg = MSG_ERROR_UNABLE_TO_ADD_CREATOR;
193         } else if (se.getCode() == SpaceException.Code.ERROR_DATASTORE) {
194           msg = MSG_ERROR_DATASTORE;
195         } else if (se.getCode() == SpaceException.Code.UNABLE_TO_INIT_APP) {
196           msg = MSG_ERROR_UNABLE_TO_INIT_APP;
197         } else if (se.getCode() == SpaceException.Code.UNABLE_TO_ADD_APPLICATION) {
198           msg = MSG_ERROR_UNABLE_TO_ADD_APPLICATION;
199         } else if (se.getCode() == SpaceException.Code.ERROR_RETRIEVING_USER) {
200           msg = MSG_ERROR_RETRIEVING_USER;
201         } else {
202           msg = MSG_ERROR_SPACE_CREATION;
203         }
204         LOG.error("Failed to create a new space", se);
205         uiApplication.addMessage(new ApplicationMessage(msg, null, ApplicationMessage.ERROR));
206         return;
207       }
208       SpaceUtils.endRequest();
209       UIPopupWindow uiPopup = uiAddForm.getParent();
210       uiPopup.setUIComponent(null);
211       uiPopup.setRendered(false);
212       uiPopup.setShow(false);
213       Utils.updateWorkingWorkSpace();
214       // TODO Re-check and re-confirm that navigation is ok then re-direct into Home of space.
215       JavascriptManager jsManager = ctx.getJavascriptManager();
216       jsManager.addJavascript("try { window.location.href='" + Utils.getSpaceHomeURL(space) + "' } catch(e) {" +
217               "window.location.href('" + Utils.getSpaceHomeURL(space) + "') }");
218     }
219   }
220 
221   static public class ChangeTemplateActionListener extends EventListener<UISpaceAddForm> {
222     public void execute(Event<UISpaceAddForm> event) throws Exception {
223       UISpaceAddForm uiSpaceAddForm = event.getSource();
224       UISpaceSettings uiSpaceSettings = uiSpaceAddForm.getChild(UISpaceSettings.class);
225       String templateName = uiSpaceSettings.getUIFormSelectBox(SPACE_TEMPLATE).getValue();
226       SpaceTemplateService spaceTemplateService = PortalContainer.getInstance().getComponentInstanceOfType(SpaceTemplateService.class);
227       SpaceTemplate spaceTemplate = spaceTemplateService.getSpaceTemplateByName(templateName);
228       String visibility = spaceTemplate.getVisibility();
229       String registration = spaceTemplate.getRegistration();
230       UISpaceVisibility uiSpaceVisibility = uiSpaceAddForm.findFirstComponentOfType(UISpaceVisibility.class);
231       UISpaceTemplateDescription uiSpaceTemplateDescription = uiSpaceSettings.getChild(UISpaceTemplateDescription.class);
232       UIFormRadioBoxInput uiRegistration = uiSpaceVisibility.findComponentById(UISpaceVisibility.UI_SPACE_REGISTRATION);
233       uiSpaceTemplateDescription.setTemplateName(templateName);
234       uiSpaceVisibility.setVisibility(visibility);
235       uiRegistration.setValue(registration);
236       WebuiRequestContext ctx = event.getRequestContext();
237       ctx.addUIComponentToUpdateByAjax(uiSpaceTemplateDescription);
238       ctx.addUIComponentToUpdateByAjax(uiSpaceVisibility);
239     }
240   }
241 }