001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.action;
022:
023: import com.liferay.portal.kernel.security.permission.ActionKeys;
024: import com.liferay.portal.kernel.security.permission.PermissionChecker;
025: import com.liferay.portal.kernel.util.Constants;
026: import com.liferay.portal.kernel.util.InstancePool;
027: import com.liferay.portal.kernel.util.ParamUtil;
028: import com.liferay.portal.kernel.util.StringMaker;
029: import com.liferay.portal.kernel.util.Validator;
030: import com.liferay.portal.model.Layout;
031: import com.liferay.portal.model.LayoutTypePortlet;
032: import com.liferay.portal.model.Portlet;
033: import com.liferay.portal.model.impl.PortletImpl;
034: import com.liferay.portal.model.impl.ResourceImpl;
035: import com.liferay.portal.service.LayoutServiceUtil;
036: import com.liferay.portal.service.PortletLocalServiceUtil;
037: import com.liferay.portal.service.ResourceLocalServiceUtil;
038: import com.liferay.portal.service.permission.LayoutPermissionUtil;
039: import com.liferay.portal.service.permission.PortletPermissionUtil;
040: import com.liferay.portal.servlet.NamespaceServletRequest;
041: import com.liferay.portal.struts.ActionConstants;
042: import com.liferay.portal.theme.ThemeDisplay;
043: import com.liferay.portal.util.PortalUtil;
044: import com.liferay.portal.util.WebKeys;
045: import com.liferay.portlet.PortletPreferencesFactoryUtil;
046: import com.liferay.util.servlet.DynamicServletRequest;
047:
048: import javax.portlet.PortletPreferences;
049:
050: import javax.servlet.http.HttpServletRequest;
051: import javax.servlet.http.HttpServletResponse;
052:
053: import org.apache.struts.action.Action;
054: import org.apache.struts.action.ActionForm;
055: import org.apache.struts.action.ActionForward;
056: import org.apache.struts.action.ActionMapping;
057:
058: /**
059: * <a href="UpdateLayoutAction.java.html"><b><i>View Source</i></b></a>
060: *
061: * @author Brian Wing Shun Chan
062: *
063: */
064: public class UpdateLayoutAction extends Action {
065:
066: public ActionForward execute(ActionMapping mapping,
067: ActionForm form, HttpServletRequest req,
068: HttpServletResponse res) throws Exception {
069:
070: ThemeDisplay themeDisplay = (ThemeDisplay) req
071: .getAttribute(WebKeys.THEME_DISPLAY);
072:
073: long userId = themeDisplay.getUserId();
074:
075: Layout layout = themeDisplay.getLayout();
076: LayoutTypePortlet layoutTypePortlet = themeDisplay
077: .getLayoutTypePortlet();
078:
079: PermissionChecker permissionChecker = themeDisplay
080: .getPermissionChecker();
081:
082: String cmd = ParamUtil.getString(req, Constants.CMD);
083:
084: String portletId = ParamUtil.getString(req, "p_p_id");
085:
086: boolean updateLayout = true;
087: boolean deletePortlet = false;
088:
089: if (cmd.equals(Constants.ADD)) {
090: portletId = layoutTypePortlet.addPortletId(userId,
091: portletId);
092:
093: String columnId = ParamUtil.getString(req, "p_p_col_id");
094: int columnPos = ParamUtil.getInteger(req, "p_p_col_pos");
095:
096: if (Validator.isNotNull(columnId)) {
097: layoutTypePortlet.movePortletId(userId, portletId,
098: columnId, columnPos);
099: }
100: } else if (cmd.equals(Constants.DELETE)) {
101: if (layoutTypePortlet.hasPortletId(portletId)) {
102: deletePortlet = true;
103:
104: layoutTypePortlet.removePortletId(portletId);
105: }
106: } else if (cmd.equals("drag")) {
107: if (LayoutPermissionUtil.contains(permissionChecker, layout
108: .getGroupId(), layout.isPrivateLayout(), layout
109: .getLayoutId(), ActionKeys.UPDATE)) {
110:
111: String height = ParamUtil.getString(req, "height");
112: String width = ParamUtil.getString(req, "width");
113: String top = ParamUtil.getString(req, "top");
114: String left = ParamUtil.getString(req, "left");
115:
116: PortletPreferences prefs = PortletPreferencesFactoryUtil
117: .getPortletSetup(req, portletId, true, true);
118:
119: StringMaker sm = new StringMaker();
120:
121: sm.append("height=" + height + "\n");
122: sm.append("width=" + width + "\n");
123: sm.append("top=" + top + "\n");
124: sm.append("left=" + left + "\n");
125:
126: prefs
127: .setValue("portlet-freeform-styles", sm
128: .toString());
129:
130: prefs.store();
131: }
132: } else if (cmd.equals("minimize")) {
133: boolean restore = ParamUtil.getBoolean(req, "p_p_restore");
134:
135: if (restore) {
136: layoutTypePortlet.removeStateMinPortletId(portletId);
137: } else {
138: layoutTypePortlet.addStateMinPortletId(portletId);
139:
140: updateLayout = false;
141: }
142: } else if (cmd.equals("move")) {
143: String columnId = ParamUtil.getString(req, "p_p_col_id");
144: int columnPos = ParamUtil.getInteger(req, "p_p_col_pos");
145:
146: layoutTypePortlet.movePortletId(userId, portletId,
147: columnId, columnPos);
148: } else if (cmd.equals("template")) {
149: String layoutTemplateId = ParamUtil.getString(req,
150: "layoutTemplateId");
151:
152: layoutTypePortlet.setLayoutTemplateId(userId,
153: layoutTemplateId);
154: }
155:
156: if (updateLayout) {
157:
158: // LEP-3648
159:
160: layoutTypePortlet.resetStates();
161:
162: LayoutServiceUtil.updateLayout(layout.getGroupId(), layout
163: .isPrivateLayout(), layout.getLayoutId(), layout
164: .getTypeSettings());
165:
166: // See LEP-1411. Delay the delete of extraneous portlet resources
167: // only after the user has proven that he has the valid permissions.
168:
169: if (deletePortlet) {
170: String rootPortletId = PortletImpl
171: .getRootPortletId(portletId);
172:
173: ResourceLocalServiceUtil.deleteResource(layout
174: .getCompanyId(), rootPortletId,
175: ResourceImpl.SCOPE_INDIVIDUAL,
176: PortletPermissionUtil.getPrimaryKey(layout
177: .getPlid(), portletId));
178: }
179: }
180:
181: if (ParamUtil.getBoolean(req, "refresh")) {
182: return mapping.findForward(ActionConstants.COMMON_REFERER);
183: } else {
184: if (cmd.equals(Constants.ADD) && (portletId != null)) {
185:
186: // Run the render portlet action to add a portlet without
187: // refreshing.
188:
189: Action renderPortletAction = (Action) InstancePool
190: .get(RenderPortletAction.class.getName());
191:
192: // Pass in the portlet id because the portlet id may be the
193: // instance id. Namespace the request if necessary. See
194: // LEP-4644.
195:
196: long companyId = PortalUtil.getCompanyId(req);
197:
198: Portlet portlet = PortletLocalServiceUtil
199: .getPortletById(companyId, portletId);
200:
201: DynamicServletRequest dynamicReq = null;
202:
203: if (portlet.isPrivateRequestAttributes()) {
204: String portletNamespace = PortalUtil
205: .getPortletNamespace(portlet.getPortletId());
206:
207: dynamicReq = new NamespaceServletRequest(req,
208: portletNamespace, portletNamespace);
209: } else {
210: dynamicReq = new DynamicServletRequest(req);
211: }
212:
213: dynamicReq.setParameter("p_p_id", portletId);
214:
215: renderPortletAction.execute(mapping, form, dynamicReq,
216: res);
217: }
218:
219: return null;
220: }
221: }
222:
223: }
|