001
014
015 package com.liferay.portal.action;
016
017 import com.liferay.portal.kernel.json.JSONFactoryUtil;
018 import com.liferay.portal.kernel.json.JSONObject;
019 import com.liferay.portal.kernel.portlet.AddPortletProvider;
020 import com.liferay.portal.kernel.portlet.PortletJSONUtil;
021 import com.liferay.portal.kernel.servlet.BufferCacheServletResponse;
022 import com.liferay.portal.kernel.servlet.DynamicServletRequest;
023 import com.liferay.portal.kernel.servlet.ServletResponseUtil;
024 import com.liferay.portal.kernel.util.Constants;
025 import com.liferay.portal.kernel.util.ContentTypes;
026 import com.liferay.portal.kernel.util.GetterUtil;
027 import com.liferay.portal.kernel.util.InstancePool;
028 import com.liferay.portal.kernel.util.ParamUtil;
029 import com.liferay.portal.kernel.util.PropertiesParamUtil;
030 import com.liferay.portal.kernel.util.StringBundler;
031 import com.liferay.portal.kernel.util.StringPool;
032 import com.liferay.portal.kernel.util.StringUtil;
033 import com.liferay.portal.kernel.util.UnicodeProperties;
034 import com.liferay.portal.kernel.util.Validator;
035 import com.liferay.portal.kernel.workflow.WorkflowConstants;
036 import com.liferay.portal.model.Layout;
037 import com.liferay.portal.model.LayoutRevision;
038 import com.liferay.portal.model.LayoutTypePortlet;
039 import com.liferay.portal.model.Portlet;
040 import com.liferay.portal.security.permission.ActionKeys;
041 import com.liferay.portal.security.permission.PermissionChecker;
042 import com.liferay.portal.service.LayoutRevisionLocalServiceUtil;
043 import com.liferay.portal.service.LayoutServiceUtil;
044 import com.liferay.portal.service.PortletLocalServiceUtil;
045 import com.liferay.portal.service.ServiceContext;
046 import com.liferay.portal.service.ServiceContextFactory;
047 import com.liferay.portal.service.permission.LayoutPermissionUtil;
048 import com.liferay.portal.servlet.NamespaceServletRequest;
049 import com.liferay.portal.struts.JSONAction;
050 import com.liferay.portal.theme.ThemeDisplay;
051 import com.liferay.portal.util.LayoutClone;
052 import com.liferay.portal.util.LayoutCloneFactory;
053 import com.liferay.portal.util.PortalUtil;
054 import com.liferay.portal.util.WebKeys;
055 import com.liferay.portlet.PortletPreferencesFactoryUtil;
056 import com.liferay.portlet.exportimport.staging.StagingUtil;
057 import com.liferay.registry.collections.ServiceTrackerCollections;
058 import com.liferay.registry.collections.ServiceTrackerMap;
059
060 import javax.portlet.PortletPreferences;
061
062 import javax.servlet.http.HttpServletRequest;
063 import javax.servlet.http.HttpServletResponse;
064
065 import org.apache.struts.action.Action;
066 import org.apache.struts.action.ActionForm;
067 import org.apache.struts.action.ActionMapping;
068
069
072 public class UpdateLayoutAction extends JSONAction {
073
074 @Override
075 public String getJSON(
076 ActionMapping actionMapping, ActionForm actionForm,
077 HttpServletRequest request, HttpServletResponse response)
078 throws Exception {
079
080 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
081 WebKeys.THEME_DISPLAY);
082
083 long userId = themeDisplay.getUserId();
084
085 Layout layout = themeDisplay.getLayout();
086 LayoutTypePortlet layoutTypePortlet =
087 themeDisplay.getLayoutTypePortlet();
088
089 PermissionChecker permissionChecker =
090 themeDisplay.getPermissionChecker();
091
092 String cmd = ParamUtil.getString(request, Constants.CMD);
093
094 String portletId = ParamUtil.getString(request, "p_p_id");
095
096 boolean updateLayout = true;
097
098 if (cmd.equals(Constants.ADD)) {
099 String columnId = ParamUtil.getString(request, "p_p_col_id", null);
100 int columnPos = ParamUtil.getInteger(request, "p_p_col_pos", -1);
101
102 portletId = layoutTypePortlet.addPortletId(
103 userId, portletId, columnId, columnPos);
104
105 storeAddContentPortletPreferences(
106 request, layout, portletId, themeDisplay);
107
108 if (layoutTypePortlet.isCustomizable() &&
109 layoutTypePortlet.isCustomizedView() &&
110 !layoutTypePortlet.isColumnDisabled(columnId)) {
111
112 updateLayout = false;
113 }
114 }
115 else if (cmd.equals(Constants.DELETE)) {
116 if (layoutTypePortlet.hasPortletId(portletId)) {
117 layoutTypePortlet.removePortletId(userId, portletId);
118
119 if (layoutTypePortlet.isCustomizable() &&
120 layoutTypePortlet.isCustomizedView()) {
121
122 updateLayout = false;
123 }
124 }
125 }
126 else if (cmd.equals("drag")) {
127 if (LayoutPermissionUtil.contains(
128 permissionChecker, layout, ActionKeys.UPDATE)) {
129
130 String height = ParamUtil.getString(request, "height");
131 String width = ParamUtil.getString(request, "width");
132 String top = ParamUtil.getString(request, "top");
133 String left = ParamUtil.getString(request, "left");
134
135 PortletPreferences portletPreferences =
136 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
137 layout, portletId);
138
139 StringBundler sb = new StringBundler(12);
140
141 sb.append("height=");
142 sb.append(height);
143 sb.append("\n");
144 sb.append("width=");
145 sb.append(width);
146 sb.append("\n");
147 sb.append("top=");
148 sb.append(top);
149 sb.append("\n");
150 sb.append("left=");
151 sb.append(left);
152 sb.append("\n");
153
154 portletPreferences.setValue(
155 "portlet-freeform-styles", sb.toString());
156
157 portletPreferences.store();
158 }
159 }
160 else if (cmd.equals("minimize")) {
161 boolean restore = ParamUtil.getBoolean(request, "p_p_restore");
162
163 if (restore) {
164 layoutTypePortlet.removeStateMinPortletId(portletId);
165 }
166 else {
167 layoutTypePortlet.addStateMinPortletId(portletId);
168 }
169
170 updateLayout = false;
171 }
172 else if (cmd.equals("move")) {
173 String columnId = ParamUtil.getString(request, "p_p_col_id");
174 int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
175
176 layoutTypePortlet.movePortletId(
177 userId, portletId, columnId, columnPos);
178
179 if (layoutTypePortlet.isCustomizable() &&
180 layoutTypePortlet.isCustomizedView() &&
181 !layoutTypePortlet.isColumnDisabled(columnId)) {
182
183 updateLayout = false;
184 }
185 }
186 else if (cmd.equals("redo_layout_revision")) {
187 long layoutRevisionId = ParamUtil.getLong(
188 request, "layoutRevisionId");
189 long layoutSetBranchId = ParamUtil.getLong(
190 request, "layoutSetBranchId");
191
192 ServiceContext serviceContext = ServiceContextFactory.getInstance(
193 request);
194
195 LayoutRevisionLocalServiceUtil.updateStatus(
196 userId, layoutRevisionId, WorkflowConstants.STATUS_DRAFT,
197 serviceContext);
198
199 StagingUtil.setRecentLayoutRevisionId(
200 request, layoutSetBranchId, layout.getPlid(), layoutRevisionId);
201
202 updateLayout = false;
203 }
204 else if (cmd.equals("select_layout_revision")) {
205 long layoutRevisionId = ParamUtil.getLong(
206 request, "layoutRevisionId");
207 long layoutSetBranchId = ParamUtil.getLong(
208 request, "layoutSetBranchId");
209
210 StagingUtil.setRecentLayoutRevisionId(
211 request, layoutSetBranchId, layout.getPlid(), layoutRevisionId);
212
213 updateLayout = false;
214 }
215 else if (cmd.equals("toggle_customized_view")) {
216 updateLayout = false;
217 }
218 else if (cmd.equals("update_type_settings")) {
219 UnicodeProperties layoutTypeSettingsProperties =
220 layout.getTypeSettingsProperties();
221
222 UnicodeProperties formTypeSettingsProperties =
223 PropertiesParamUtil.getProperties(
224 request, "TypeSettingsProperties--");
225
226 layoutTypeSettingsProperties.putAll(formTypeSettingsProperties);
227 }
228 else if (cmd.equals("undo_layout_revision")) {
229 long layoutRevisionId = ParamUtil.getLong(
230 request, "layoutRevisionId");
231 long layoutSetBranchId = ParamUtil.getLong(
232 request, "layoutSetBranchId");
233
234 ServiceContext serviceContext = ServiceContextFactory.getInstance(
235 request);
236
237 LayoutRevision layoutRevision =
238 LayoutRevisionLocalServiceUtil.updateStatus(
239 userId, layoutRevisionId, WorkflowConstants.STATUS_INACTIVE,
240 serviceContext);
241
242 StagingUtil.setRecentLayoutRevisionId(
243 request, layoutSetBranchId, layout.getPlid(),
244 layoutRevision.getParentLayoutRevisionId());
245
246 updateLayout = false;
247 }
248
249 if (updateLayout) {
250
251
252
253 layoutTypePortlet.resetModes();
254 layoutTypePortlet.resetStates();
255
256 layout = LayoutServiceUtil.updateLayout(
257 layout.getGroupId(), layout.isPrivateLayout(),
258 layout.getLayoutId(), layout.getTypeSettings());
259 }
260 else {
261 LayoutClone layoutClone = LayoutCloneFactory.getInstance();
262
263 if (layoutClone != null) {
264 layoutClone.update(
265 request, layout.getPlid(), layout.getTypeSettings());
266 }
267 }
268
269 if (cmd.equals(Constants.ADD) && (portletId != null)) {
270 addPortlet(actionMapping, actionForm, request, response, portletId);
271 }
272
273 return StringPool.BLANK;
274 }
275
276 protected void addPortlet(
277 ActionMapping actionMapping, ActionForm actionForm,
278 HttpServletRequest request, HttpServletResponse response,
279 String portletId)
280 throws Exception {
281
282
283
284 Action renderPortletAction = (Action)InstancePool.get(
285 RenderPortletAction.class.getName());
286
287
288
289
290 long companyId = PortalUtil.getCompanyId(request);
291
292 Portlet portlet = PortletLocalServiceUtil.getPortletById(
293 companyId, portletId);
294
295 DynamicServletRequest dynamicRequest = null;
296
297 if (portlet.isPrivateRequestAttributes()) {
298 String portletNamespace = PortalUtil.getPortletNamespace(
299 portlet.getPortletId());
300
301 dynamicRequest = new NamespaceServletRequest(
302 request, portletNamespace, portletNamespace);
303 }
304 else {
305 dynamicRequest = new DynamicServletRequest(request);
306 }
307
308 dynamicRequest.setParameter("p_p_id", portletId);
309
310 String dataType = StringUtil.toLowerCase(
311 ParamUtil.getString(request, "dataType"));
312
313 if (dataType.equals("json")) {
314 JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
315
316 BufferCacheServletResponse bufferCacheServletResponse =
317 new BufferCacheServletResponse(response);
318
319 renderPortletAction.execute(
320 actionMapping, actionForm, dynamicRequest,
321 bufferCacheServletResponse);
322
323 String portletHTML = bufferCacheServletResponse.getString();
324
325 portletHTML = portletHTML.trim();
326
327 PortletJSONUtil.populatePortletJSONObject(
328 request, portletHTML, portlet, jsonObject);
329
330 response.setContentType(ContentTypes.APPLICATION_JSON);
331
332 ServletResponseUtil.write(response, jsonObject.toString());
333 }
334 else {
335 renderPortletAction.execute(
336 actionMapping, actionForm, dynamicRequest, response);
337 }
338 }
339
340 protected void storeAddContentPortletPreferences(
341 HttpServletRequest request, Layout layout, String portletId,
342 ThemeDisplay themeDisplay)
343 throws Exception {
344
345
346
347
348 PortletPreferences portletSetup =
349 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
350 layout, portletId);
351
352 String[] portletData = StringUtil.split(
353 ParamUtil.getString(request, "portletData"));
354
355 if (portletData.length == 0) {
356 return;
357 }
358
359 long classPK = GetterUtil.getLong(portletData[0]);
360
361 String className = GetterUtil.getString(portletData[1]);
362
363 if ((classPK <= 0) || Validator.isNull(className)) {
364 return;
365 }
366
367 AddPortletProvider addPortletProvider = _serviceTrackerMap.getService(
368 className);
369
370 if (addPortletProvider == null) {
371 addPortletProvider = _serviceTrackerMap.getService(
372 AddPortletProvider.CLASS_NAME_ANY);
373 }
374
375 if (addPortletProvider != null) {
376 addPortletProvider.updatePortletPreferences(
377 portletSetup, portletId, className, classPK, themeDisplay);
378 }
379
380 portletSetup.store();
381 }
382
383 private static final ServiceTrackerMap<String, AddPortletProvider>
384 _serviceTrackerMap = ServiceTrackerCollections.singleValueMap(
385 AddPortletProvider.class, "model.class.name");
386
387 static {
388 _serviceTrackerMap.open();
389 }
390
391 }