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