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