1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
59
60
61
62
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
91
92
93
94
95
96
97
98
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
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
145 SpaceUtils.checkUserExisting(ctx.getRemoteUser());
146
147
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) {
177 space = spaceService.createSpace(space, creator, invitedIdentities);
178 } else {
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
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 }