1
14
15 package com.liferay.portal.action;
16
17 import com.liferay.portal.kernel.json.JSONFactoryUtil;
18 import com.liferay.portal.kernel.json.JSONObject;
19 import com.liferay.portal.kernel.servlet.StringServletResponse;
20 import com.liferay.portal.kernel.util.Constants;
21 import com.liferay.portal.kernel.util.InstancePool;
22 import com.liferay.portal.kernel.util.ParamUtil;
23 import com.liferay.portal.kernel.util.StringBundler;
24 import com.liferay.portal.kernel.util.Validator;
25 import com.liferay.portal.model.Layout;
26 import com.liferay.portal.model.LayoutTypePortlet;
27 import com.liferay.portal.model.Portlet;
28 import com.liferay.portal.model.PortletConstants;
29 import com.liferay.portal.model.ResourceConstants;
30 import com.liferay.portal.security.permission.ActionKeys;
31 import com.liferay.portal.security.permission.PermissionChecker;
32 import com.liferay.portal.service.LayoutServiceUtil;
33 import com.liferay.portal.service.PortletLocalServiceUtil;
34 import com.liferay.portal.service.ResourceLocalServiceUtil;
35 import com.liferay.portal.service.permission.LayoutPermissionUtil;
36 import com.liferay.portal.service.permission.PortletPermissionUtil;
37 import com.liferay.portal.servlet.NamespaceServletRequest;
38 import com.liferay.portal.struts.ActionConstants;
39 import com.liferay.portal.theme.ThemeDisplay;
40 import com.liferay.portal.util.LayoutClone;
41 import com.liferay.portal.util.LayoutCloneFactory;
42 import com.liferay.portal.util.PortalUtil;
43 import com.liferay.portal.util.WebKeys;
44 import com.liferay.portlet.PortletPreferencesFactoryUtil;
45 import com.liferay.util.servlet.DynamicServletRequest;
46 import com.liferay.util.servlet.ServletResponseUtil;
47
48 import javax.portlet.PortletPreferences;
49
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpServletResponse;
52
53 import org.apache.struts.action.Action;
54 import org.apache.struts.action.ActionForm;
55 import org.apache.struts.action.ActionForward;
56 import org.apache.struts.action.ActionMapping;
57
58
63 public class UpdateLayoutAction extends Action {
64
65 public ActionForward execute(
66 ActionMapping mapping, ActionForm form, HttpServletRequest request,
67 HttpServletResponse response)
68 throws Exception {
69
70 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
71 WebKeys.THEME_DISPLAY);
72
73 long userId = themeDisplay.getUserId();
74
75 Layout layout = themeDisplay.getLayout();
76 LayoutTypePortlet layoutTypePortlet =
77 themeDisplay.getLayoutTypePortlet();
78
79 PermissionChecker permissionChecker =
80 themeDisplay.getPermissionChecker();
81
82 String cmd = ParamUtil.getString(request, Constants.CMD);
83
84 String portletId = ParamUtil.getString(request, "p_p_id");
85
86 boolean updateLayout = true;
87 boolean deletePortlet = false;
88
89 if (cmd.equals(Constants.ADD)) {
90 portletId = layoutTypePortlet.addPortletId(userId, portletId);
91
92 String columnId = ParamUtil.getString(request, "p_p_col_id");
93 int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
94
95 if (Validator.isNotNull(columnId) &&
96 Validator.isNotNull(portletId)) {
97
98 layoutTypePortlet.movePortletId(
99 userId, portletId, columnId, columnPos);
100 }
101 }
102 else if (cmd.equals(Constants.DELETE)) {
103 if (layoutTypePortlet.hasPortletId(portletId)) {
104 deletePortlet = true;
105
106 layoutTypePortlet.removePortletId(userId, portletId);
107 }
108 }
109 else if (cmd.equals("drag")) {
110 if (LayoutPermissionUtil.contains(
111 permissionChecker, layout.getGroupId(),
112 layout.isPrivateLayout(), layout.getLayoutId(),
113 ActionKeys.UPDATE)) {
114
115 String height = ParamUtil.getString(request, "height");
116 String width = ParamUtil.getString(request, "width");
117 String top = ParamUtil.getString(request, "top");
118 String left = ParamUtil.getString(request, "left");
119
120 PortletPreferences preferences =
121 PortletPreferencesFactoryUtil.getLayoutPortletSetup(
122 layout, portletId);
123
124 StringBundler sb = new StringBundler(12);
125
126 sb.append("height=");
127 sb.append(height);
128 sb.append("\n");
129 sb.append("width=");
130 sb.append(width);
131 sb.append("\n");
132 sb.append("top=");
133 sb.append(top);
134 sb.append("\n");
135 sb.append("left=");
136 sb.append(left);
137 sb.append("\n");
138
139 preferences.setValue("portlet-freeform-styles", sb.toString());
140
141 preferences.store();
142 }
143 }
144 else if (cmd.equals("minimize")) {
145 boolean restore = ParamUtil.getBoolean(request, "p_p_restore");
146
147 if (restore) {
148 layoutTypePortlet.removeStateMinPortletId(portletId);
149 }
150 else {
151 layoutTypePortlet.addStateMinPortletId(portletId);
152 }
153
154 updateLayout = false;
155 }
156 else if (cmd.equals("move")) {
157 String columnId = ParamUtil.getString(request, "p_p_col_id");
158 int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
159
160 layoutTypePortlet.movePortletId(
161 userId, portletId, columnId, columnPos);
162 }
163 else if (cmd.equals("template")) {
164 String layoutTemplateId = ParamUtil.getString(
165 request, "layoutTemplateId");
166
167 layoutTypePortlet.setLayoutTemplateId(userId, layoutTemplateId);
168 }
169
170 if (updateLayout) {
171
172
174 layoutTypePortlet.resetModes();
175 layoutTypePortlet.resetStates();
176
177 LayoutServiceUtil.updateLayout(
178 layout.getGroupId(), layout.isPrivateLayout(),
179 layout.getLayoutId(), layout.getTypeSettings());
180
181
184 if (deletePortlet) {
185 String rootPortletId = PortletConstants.getRootPortletId(
186 portletId);
187
188 ResourceLocalServiceUtil.deleteResource(
189 layout.getCompanyId(), rootPortletId,
190 ResourceConstants.SCOPE_INDIVIDUAL,
191 PortletPermissionUtil.getPrimaryKey(
192 layout.getPlid(), portletId));
193 }
194 }
195 else {
196 LayoutClone layoutClone = LayoutCloneFactory.getInstance();
197
198 if (layoutClone != null) {
199 layoutClone.update(
200 request, layout.getPlid(), layout.getTypeSettings());
201 }
202 }
203
204
207 if (ParamUtil.getBoolean(request, "refresh")) {
208 return mapping.findForward(ActionConstants.COMMON_REFERER);
209 }
210 else {
211 if (cmd.equals(Constants.ADD) && (portletId != null)) {
212 addPortlet(mapping, form, request, response, portletId);
213 }
214
215 return null;
216 }
217 }
218
219 protected void addPortlet(
220 ActionMapping mapping, ActionForm form, HttpServletRequest request,
221 HttpServletResponse response, String portletId)
222 throws Exception {
223
224
226 Action renderPortletAction = (Action)InstancePool.get(
227 RenderPortletAction.class.getName());
228
229
232 long companyId = PortalUtil.getCompanyId(request);
233
234 Portlet portlet = PortletLocalServiceUtil.getPortletById(
235 companyId, portletId);
236
237 DynamicServletRequest dynamicRequest = null;
238
239 if (portlet.isPrivateRequestAttributes()) {
240 String portletNamespace =
241 PortalUtil.getPortletNamespace(portlet.getPortletId());
242
243 dynamicRequest = new NamespaceServletRequest(
244 request, portletNamespace, portletNamespace);
245 }
246 else {
247 dynamicRequest = new DynamicServletRequest(request);
248 }
249
250 dynamicRequest.setParameter("p_p_id", portletId);
251
252 String dataType = ParamUtil.getString(request, "dataType");
253
254 if (dataType.equals("json")) {
255 JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
256
257 if (portlet.isAjaxable()) {
258 StringServletResponse stringResponse =
259 new StringServletResponse(response);
260
261 renderPortletAction.execute(
262 mapping, form, dynamicRequest, stringResponse);
263
264 jsonObj.put("refresh", false);
265 jsonObj.put("portletHTML", stringResponse.getString().trim());
266 }
267 else {
268 jsonObj.put("refresh", true);
269 }
270
271 ServletResponseUtil.write(response, jsonObj.toString());
272 }
273 else {
274 renderPortletAction.execute(
275 mapping, form, dynamicRequest, response);
276 }
277 }
278
279 }