1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.action;
24  
25  import com.liferay.portal.kernel.json.JSONFactoryUtil;
26  import com.liferay.portal.kernel.json.JSONObject;
27  import com.liferay.portal.kernel.servlet.StringServletResponse;
28  import com.liferay.portal.kernel.util.Constants;
29  import com.liferay.portal.kernel.util.InstancePool;
30  import com.liferay.portal.kernel.util.ParamUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.model.LayoutTypePortlet;
34  import com.liferay.portal.model.Portlet;
35  import com.liferay.portal.model.PortletConstants;
36  import com.liferay.portal.model.ResourceConstants;
37  import com.liferay.portal.security.permission.ActionKeys;
38  import com.liferay.portal.security.permission.PermissionChecker;
39  import com.liferay.portal.service.LayoutServiceUtil;
40  import com.liferay.portal.service.PortletLocalServiceUtil;
41  import com.liferay.portal.service.ResourceLocalServiceUtil;
42  import com.liferay.portal.service.permission.LayoutPermissionUtil;
43  import com.liferay.portal.service.permission.PortletPermissionUtil;
44  import com.liferay.portal.servlet.NamespaceServletRequest;
45  import com.liferay.portal.struts.ActionConstants;
46  import com.liferay.portal.theme.ThemeDisplay;
47  import com.liferay.portal.util.LayoutClone;
48  import com.liferay.portal.util.LayoutCloneFactory;
49  import com.liferay.portal.util.PortalUtil;
50  import com.liferay.portal.util.WebKeys;
51  import com.liferay.portlet.PortletPreferencesFactoryUtil;
52  import com.liferay.util.servlet.DynamicServletRequest;
53  import com.liferay.util.servlet.ServletResponseUtil;
54  
55  import javax.portlet.PortletPreferences;
56  
57  import javax.servlet.http.HttpServletRequest;
58  import javax.servlet.http.HttpServletResponse;
59  
60  import org.apache.struts.action.Action;
61  import org.apache.struts.action.ActionForm;
62  import org.apache.struts.action.ActionForward;
63  import org.apache.struts.action.ActionMapping;
64  
65  /**
66   * <a href="UpdateLayoutAction.java.html"><b><i>View Source</i></b></a>
67   *
68   * @author Brian Wing Shun Chan
69   */
70  public class UpdateLayoutAction extends Action {
71  
72      public ActionForward execute(
73              ActionMapping mapping, ActionForm form, HttpServletRequest request,
74              HttpServletResponse response)
75          throws Exception {
76  
77          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
78              WebKeys.THEME_DISPLAY);
79  
80          long userId = themeDisplay.getUserId();
81  
82          Layout layout = themeDisplay.getLayout();
83          LayoutTypePortlet layoutTypePortlet =
84              themeDisplay.getLayoutTypePortlet();
85  
86          PermissionChecker permissionChecker =
87              themeDisplay.getPermissionChecker();
88  
89          String cmd = ParamUtil.getString(request, Constants.CMD);
90  
91          String portletId = ParamUtil.getString(request, "p_p_id");
92  
93          boolean updateLayout = true;
94          boolean deletePortlet = false;
95  
96          if (cmd.equals(Constants.ADD)) {
97              portletId = layoutTypePortlet.addPortletId(userId, portletId);
98  
99              String columnId = ParamUtil.getString(request, "p_p_col_id");
100             int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
101 
102             if (Validator.isNotNull(columnId) &&
103                 Validator.isNotNull(portletId)) {
104 
105                 layoutTypePortlet.movePortletId(
106                     userId, portletId, columnId, columnPos);
107             }
108         }
109         else if (cmd.equals(Constants.DELETE)) {
110             if (layoutTypePortlet.hasPortletId(portletId)) {
111                 deletePortlet = true;
112 
113                 layoutTypePortlet.removePortletId(userId, portletId);
114             }
115         }
116         else if (cmd.equals("drag")) {
117             if (LayoutPermissionUtil.contains(
118                     permissionChecker, layout.getGroupId(),
119                     layout.isPrivateLayout(), layout.getLayoutId(),
120                     ActionKeys.UPDATE)) {
121 
122                 String height = ParamUtil.getString(request, "height");
123                 String width = ParamUtil.getString(request, "width");
124                 String top = ParamUtil.getString(request, "top");
125                 String left = ParamUtil.getString(request, "left");
126 
127                 PortletPreferences prefs =
128                     PortletPreferencesFactoryUtil.getLayoutPortletSetup(
129                         layout, portletId);
130 
131                 StringBuilder sb = new StringBuilder();
132 
133                 sb.append("height=" + height + "\n");
134                 sb.append("width=" + width + "\n");
135                 sb.append("top=" + top + "\n");
136                 sb.append("left=" + left + "\n");
137 
138                 prefs.setValue("portlet-freeform-styles", sb.toString());
139 
140                 prefs.store();
141             }
142         }
143         else if (cmd.equals("minimize")) {
144             boolean restore = ParamUtil.getBoolean(request, "p_p_restore");
145 
146             if (restore) {
147                 layoutTypePortlet.removeStateMinPortletId(portletId);
148             }
149             else {
150                 layoutTypePortlet.addStateMinPortletId(portletId);
151             }
152 
153             updateLayout = false;
154         }
155         else if (cmd.equals("move")) {
156             String columnId = ParamUtil.getString(request, "p_p_col_id");
157             int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
158 
159             layoutTypePortlet.movePortletId(
160                 userId, portletId, columnId, columnPos);
161         }
162         else if (cmd.equals("template")) {
163             String layoutTemplateId = ParamUtil.getString(
164                 request, "layoutTemplateId");
165 
166             layoutTypePortlet.setLayoutTemplateId(userId, layoutTemplateId);
167         }
168 
169         if (updateLayout) {
170 
171             // LEP-3648
172 
173             layoutTypePortlet.resetStates();
174 
175             LayoutServiceUtil.updateLayout(
176                 layout.getGroupId(), layout.isPrivateLayout(),
177                 layout.getLayoutId(), layout.getTypeSettings());
178 
179             // See LEP-1411. Delay the delete of extraneous portlet resources
180             // only after the user has proven that he has the valid permissions.
181 
182             if (deletePortlet) {
183                 String rootPortletId = PortletConstants.getRootPortletId(
184                     portletId);
185 
186                 ResourceLocalServiceUtil.deleteResource(
187                     layout.getCompanyId(), rootPortletId,
188                     ResourceConstants.SCOPE_INDIVIDUAL,
189                     PortletPermissionUtil.getPrimaryKey(
190                         layout.getPlid(), portletId));
191             }
192         }
193         else {
194             LayoutClone layoutClone = LayoutCloneFactory.getInstance();
195 
196             if (layoutClone != null) {
197                 layoutClone.update(
198                     request, layout.getPlid(), layout.getTypeSettings());
199             }
200         }
201 
202         // The check for the refresh variable can be removed in the future. See
203         // LEP-6910.
204 
205         if (ParamUtil.getBoolean(request, "refresh")) {
206             return mapping.findForward(ActionConstants.COMMON_REFERER);
207         }
208         else {
209             if (cmd.equals(Constants.ADD) && (portletId != null)) {
210                 addPortlet(mapping, form, request, response, portletId);
211             }
212 
213             return null;
214         }
215     }
216 
217     protected void addPortlet(
218             ActionMapping mapping, ActionForm form, HttpServletRequest request,
219             HttpServletResponse response, String portletId)
220         throws Exception {
221 
222         // Run the render portlet action to add a portlet without refreshing.
223 
224         Action renderPortletAction = (Action)InstancePool.get(
225             RenderPortletAction.class.getName());
226 
227         // Pass in the portlet id because the portlet id may be the instance id.
228         // Namespace the request if necessary. See LEP-4644.
229 
230         long companyId = PortalUtil.getCompanyId(request);
231 
232         Portlet portlet = PortletLocalServiceUtil.getPortletById(
233             companyId, portletId);
234 
235         DynamicServletRequest dynamicRequest = null;
236 
237         if (portlet.isPrivateRequestAttributes()) {
238             String portletNamespace =
239                 PortalUtil.getPortletNamespace(portlet.getPortletId());
240 
241             dynamicRequest = new NamespaceServletRequest(
242                 request, portletNamespace, portletNamespace);
243         }
244         else {
245             dynamicRequest = new DynamicServletRequest(request);
246         }
247 
248         dynamicRequest.setParameter("p_p_id", portletId);
249 
250         String dataType = ParamUtil.getString(request, "dataType");
251 
252         if (dataType.equals("json")) {
253             JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
254 
255             if (portlet.isAjaxable()) {
256                 StringServletResponse stringResponse =
257                     new StringServletResponse(response);
258 
259                 renderPortletAction.execute(
260                     mapping, form, dynamicRequest, stringResponse);
261 
262                 jsonObj.put("refresh", false);
263                 jsonObj.put("portletHTML", stringResponse.getString().trim());
264             }
265             else {
266                 jsonObj.put("refresh", true);
267             }
268 
269             ServletResponseUtil.write(response, jsonObj.toString());
270         }
271         else {
272             renderPortletAction.execute(
273                 mapping, form, dynamicRequest, response);
274         }
275     }
276 
277 }