1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions 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.util.Constants;
26  import com.liferay.portal.kernel.util.InstancePool;
27  import com.liferay.portal.kernel.util.ParamUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.Layout;
30  import com.liferay.portal.model.LayoutTypePortlet;
31  import com.liferay.portal.model.Portlet;
32  import com.liferay.portal.model.PortletConstants;
33  import com.liferay.portal.model.ResourceConstants;
34  import com.liferay.portal.security.permission.ActionKeys;
35  import com.liferay.portal.security.permission.PermissionChecker;
36  import com.liferay.portal.service.LayoutServiceUtil;
37  import com.liferay.portal.service.PortletLocalServiceUtil;
38  import com.liferay.portal.service.ResourceLocalServiceUtil;
39  import com.liferay.portal.service.permission.LayoutPermissionUtil;
40  import com.liferay.portal.service.permission.PortletPermissionUtil;
41  import com.liferay.portal.servlet.NamespaceServletRequest;
42  import com.liferay.portal.struts.ActionConstants;
43  import com.liferay.portal.theme.ThemeDisplay;
44  import com.liferay.portal.util.LayoutClone;
45  import com.liferay.portal.util.LayoutCloneFactory;
46  import com.liferay.portal.util.PortalUtil;
47  import com.liferay.portal.util.WebKeys;
48  import com.liferay.portlet.PortletPreferencesFactoryUtil;
49  import com.liferay.util.servlet.DynamicServletRequest;
50  
51  import javax.portlet.PortletPreferences;
52  
53  import javax.servlet.http.HttpServletRequest;
54  import javax.servlet.http.HttpServletResponse;
55  
56  import org.apache.struts.action.Action;
57  import org.apache.struts.action.ActionForm;
58  import org.apache.struts.action.ActionForward;
59  import org.apache.struts.action.ActionMapping;
60  
61  /**
62   * <a href="UpdateLayoutAction.java.html"><b><i>View Source</i></b></a>
63   *
64   * @author Brian Wing Shun Chan
65   *
66   */
67  public class UpdateLayoutAction extends Action {
68  
69      public ActionForward execute(
70              ActionMapping mapping, ActionForm form, HttpServletRequest request,
71              HttpServletResponse response)
72          throws Exception {
73  
74          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
75              WebKeys.THEME_DISPLAY);
76  
77          long userId = themeDisplay.getUserId();
78  
79          Layout layout = themeDisplay.getLayout();
80          LayoutTypePortlet layoutTypePortlet =
81              themeDisplay.getLayoutTypePortlet();
82  
83          PermissionChecker permissionChecker =
84              themeDisplay.getPermissionChecker();
85  
86          String cmd = ParamUtil.getString(request, Constants.CMD);
87  
88          String portletId = ParamUtil.getString(request, "p_p_id");
89  
90          boolean updateLayout = true;
91          boolean deletePortlet = false;
92  
93          if (cmd.equals(Constants.ADD)) {
94              portletId = layoutTypePortlet.addPortletId(userId, portletId);
95  
96              String columnId = ParamUtil.getString(request, "p_p_col_id");
97              int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
98  
99              if (Validator.isNotNull(columnId)) {
100                 layoutTypePortlet.movePortletId(
101                     userId, portletId, columnId, columnPos);
102             }
103         }
104         else if (cmd.equals(Constants.DELETE)) {
105             if (layoutTypePortlet.hasPortletId(portletId)) {
106                 deletePortlet = true;
107 
108                 layoutTypePortlet.removePortletId(portletId);
109             }
110         }
111         else if (cmd.equals("drag")) {
112             if (LayoutPermissionUtil.contains(
113                     permissionChecker, layout.getGroupId(),
114                     layout.isPrivateLayout(), layout.getLayoutId(),
115                     ActionKeys.UPDATE)) {
116 
117                 String height = ParamUtil.getString(request, "height");
118                 String width = ParamUtil.getString(request, "width");
119                 String top = ParamUtil.getString(request, "top");
120                 String left = ParamUtil.getString(request, "left");
121 
122                 PortletPreferences prefs =
123                     PortletPreferencesFactoryUtil.getLayoutPortletSetup(
124                         layout, portletId);
125 
126                 StringBuilder sb = new StringBuilder();
127 
128                 sb.append("height=" + height + "\n");
129                 sb.append("width=" + width + "\n");
130                 sb.append("top=" + top + "\n");
131                 sb.append("left=" + left + "\n");
132 
133                 prefs.setValue("portlet-freeform-styles", sb.toString());
134 
135                 prefs.store();
136             }
137         }
138         else if (cmd.equals("minimize")) {
139             boolean restore = ParamUtil.getBoolean(request, "p_p_restore");
140 
141             if (restore) {
142                 layoutTypePortlet.removeStateMinPortletId(portletId);
143             }
144             else {
145                 layoutTypePortlet.addStateMinPortletId(portletId);
146             }
147 
148             updateLayout = false;
149         }
150         else if (cmd.equals("move")) {
151             String columnId = ParamUtil.getString(request, "p_p_col_id");
152             int columnPos = ParamUtil.getInteger(request, "p_p_col_pos");
153 
154             layoutTypePortlet.movePortletId(
155                 userId, portletId, columnId, columnPos);
156         }
157         else if (cmd.equals("template")) {
158             String layoutTemplateId = ParamUtil.getString(
159                 request, "layoutTemplateId");
160 
161             layoutTypePortlet.setLayoutTemplateId(userId, layoutTemplateId);
162         }
163 
164         if (updateLayout) {
165 
166             // LEP-3648
167 
168             layoutTypePortlet.resetStates();
169 
170             LayoutServiceUtil.updateLayout(
171                 layout.getGroupId(), layout.isPrivateLayout(),
172                 layout.getLayoutId(), layout.getTypeSettings());
173 
174             // See LEP-1411. Delay the delete of extraneous portlet resources
175             // only after the user has proven that he has the valid permissions.
176 
177             if (deletePortlet) {
178                 String rootPortletId = PortletConstants.getRootPortletId(
179                     portletId);
180 
181                 ResourceLocalServiceUtil.deleteResource(
182                     layout.getCompanyId(), rootPortletId,
183                     ResourceConstants.SCOPE_INDIVIDUAL,
184                     PortletPermissionUtil.getPrimaryKey(
185                         layout.getPlid(), portletId));
186             }
187         }
188         else {
189             LayoutClone layoutClone = LayoutCloneFactory.getInstance();
190 
191             if (layoutClone != null) {
192                 layoutClone.update(
193                     request, layout.getPlid(), layout.getTypeSettings());
194             }
195         }
196 
197         if (ParamUtil.getBoolean(request, "refresh")) {
198             return mapping.findForward(ActionConstants.COMMON_REFERER);
199         }
200         else {
201             if (cmd.equals(Constants.ADD) && (portletId != null)) {
202 
203                 // Run the render portlet action to add a portlet without
204                 // refreshing.
205 
206                 Action renderPortletAction = (Action)InstancePool.get(
207                     RenderPortletAction.class.getName());
208 
209                 // Pass in the portlet id because the portlet id may be the
210                 // instance id. Namespace the request if necessary. See
211                 // LEP-4644.
212 
213                 long companyId = PortalUtil.getCompanyId(request);
214 
215                 Portlet portlet = PortletLocalServiceUtil.getPortletById(
216                     companyId, portletId);
217 
218                 DynamicServletRequest dynamicRequest = null;
219 
220                 if (portlet.isPrivateRequestAttributes()) {
221                     String portletNamespace =
222                         PortalUtil.getPortletNamespace(portlet.getPortletId());
223 
224                     dynamicRequest = new NamespaceServletRequest(
225                         request, portletNamespace, portletNamespace);
226                 }
227                 else {
228                     dynamicRequest = new DynamicServletRequest(request);
229                 }
230 
231                 dynamicRequest.setParameter("p_p_id", portletId);
232 
233                 renderPortletAction.execute(
234                     mapping, form, dynamicRequest, response);
235             }
236 
237             return null;
238         }
239     }
240 
241 }