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.portlet.communities.action;
022:
023: import com.germinus.easyconf.Filter;
024:
025: import com.liferay.portal.events.EventsProcessor;
026: import com.liferay.portal.kernel.security.permission.ActionKeys;
027: import com.liferay.portal.kernel.security.permission.PermissionChecker;
028: import com.liferay.portal.kernel.util.Constants;
029: import com.liferay.portal.kernel.util.ParamUtil;
030: import com.liferay.portal.kernel.util.StringPool;
031: import com.liferay.portal.kernel.util.StringUtil;
032: import com.liferay.portal.kernel.util.Validator;
033: import com.liferay.portal.model.Layout;
034: import com.liferay.portal.model.LayoutTypePortlet;
035: import com.liferay.portal.model.impl.LayoutImpl;
036: import com.liferay.portal.service.LayoutServiceUtil;
037: import com.liferay.portal.service.permission.PortletPermissionUtil;
038: import com.liferay.portal.struts.JSONAction;
039: import com.liferay.portal.theme.ThemeDisplay;
040: import com.liferay.portal.util.PortalUtil;
041: import com.liferay.portal.util.PropsUtil;
042: import com.liferay.portal.util.PropsValues;
043: import com.liferay.portal.util.WebKeys;
044: import com.liferay.portlet.communities.util.CommunitiesUtil;
045: import com.liferay.util.Http;
046:
047: import javax.servlet.http.HttpServletRequest;
048: import javax.servlet.http.HttpServletResponse;
049:
050: import org.apache.struts.action.ActionForm;
051: import org.apache.struts.action.ActionMapping;
052:
053: import org.json.JSONObject;
054:
055: /**
056: * <a href="UpdatePageAction.java.html"><b><i>View Source</i></b></a>
057: *
058: * @author Ming-Gih Lam
059: *
060: */
061: public class UpdatePageAction extends JSONAction {
062:
063: public String getJSON(ActionMapping mapping, ActionForm form,
064: HttpServletRequest req, HttpServletResponse res)
065: throws Exception {
066:
067: ThemeDisplay themeDisplay = (ThemeDisplay) req
068: .getAttribute(WebKeys.THEME_DISPLAY);
069:
070: PermissionChecker permissionChecker = themeDisplay
071: .getPermissionChecker();
072:
073: String portletId = ParamUtil.getString(req, "portletId");
074:
075: if (!PortletPermissionUtil.contains(permissionChecker,
076: themeDisplay.getPlid(), portletId,
077: ActionKeys.CONFIGURATION)) {
078:
079: return null;
080: }
081:
082: String cmd = ParamUtil.getString(req, Constants.CMD);
083:
084: JSONObject jsonObj = new JSONObject();
085:
086: if (cmd.equals("add")) {
087: String[] array = addPage(themeDisplay, req, res);
088:
089: jsonObj.put("layoutId", array[0]);
090: jsonObj.put("url", array[1]);
091: } else if (cmd.equals("delete")) {
092: CommunitiesUtil.deleteLayout(req, res);
093: } else if (cmd.equals("display_order")) {
094: updateDisplayOrder(req);
095: } else if (cmd.equals("name")) {
096: updateName(req);
097: } else if (cmd.equals("parent_layout_id")) {
098: updateParentLayoutId(req);
099: } else if (cmd.equals("priority")) {
100: updatePriority(req);
101: }
102:
103: return jsonObj.toString();
104: }
105:
106: protected String[] addPage(ThemeDisplay themeDisplay,
107: HttpServletRequest req, HttpServletResponse res)
108: throws Exception {
109:
110: String doAsUserId = ParamUtil.getString(req, "doAsUserId");
111:
112: long groupId = ParamUtil.getLong(req, "groupId");
113: boolean privateLayout = ParamUtil.getBoolean(req,
114: "privateLayout");
115: long parentLayoutId = ParamUtil.getLong(req, "parentLayoutId");
116: String name = ParamUtil.getString(req, "name", "New Page");
117: String title = StringPool.BLANK;
118: String description = StringPool.BLANK;
119: String type = LayoutImpl.TYPE_PORTLET;
120: boolean hidden = false;
121: String friendlyURL = StringPool.BLANK;
122:
123: Layout layout = LayoutServiceUtil.addLayout(groupId,
124: privateLayout, parentLayoutId, name, title,
125: description, type, hidden, friendlyURL);
126:
127: LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout
128: .getLayoutType();
129:
130: layoutTypePortlet.setLayoutTemplateId(0,
131: PropsValues.LAYOUT_DEFAULT_TEMPLATE_ID, false);
132:
133: LayoutServiceUtil.updateLayout(layout.getGroupId(), layout
134: .isPrivateLayout(), layout.getLayoutId(), layout
135: .getTypeSettings());
136:
137: String[] eventClasses = StringUtil.split(PropsUtil
138: .getComponentProperties().getString(
139: PropsUtil.LAYOUT_CONFIGURATION_ACTION_UPDATE,
140: Filter.by(layout.getType())));
141:
142: EventsProcessor.process(eventClasses, req, res);
143:
144: String layoutURL = PortalUtil
145: .getLayoutURL(layout, themeDisplay);
146:
147: if (Validator.isNotNull(doAsUserId)) {
148: layoutURL = Http.addParameter(layoutURL, "doAsUserId",
149: themeDisplay.getDoAsUserId());
150: }
151:
152: return new String[] { String.valueOf(layout.getLayoutId()),
153: layoutURL };
154: }
155:
156: protected void updateDisplayOrder(HttpServletRequest req)
157: throws Exception {
158: long groupId = ParamUtil.getLong(req, "groupId");
159: boolean privateLayout = ParamUtil.getBoolean(req,
160: "privateLayout");
161: long parentLayoutId = ParamUtil.getLong(req, "parentLayoutId");
162: long[] layoutIds = StringUtil.split(ParamUtil.getString(req,
163: "layoutIds"), 0L);
164:
165: LayoutServiceUtil.setLayouts(groupId, privateLayout,
166: parentLayoutId, layoutIds);
167: }
168:
169: protected void updateName(HttpServletRequest req) throws Exception {
170: long plid = ParamUtil.getLong(req, "plid");
171:
172: long groupId = ParamUtil.getLong(req, "groupId");
173: boolean privateLayout = ParamUtil.getBoolean(req,
174: "privateLayout");
175: long layoutId = ParamUtil.getLong(req, "layoutId");
176: String name = ParamUtil.getString(req, "name");
177: String languageId = ParamUtil.getString(req, "languageId");
178:
179: if (plid <= 0) {
180: LayoutServiceUtil.updateName(groupId, privateLayout,
181: layoutId, name, languageId);
182: } else {
183: LayoutServiceUtil.updateName(plid, name, languageId);
184: }
185: }
186:
187: protected void updateParentLayoutId(HttpServletRequest req)
188: throws Exception {
189:
190: long plid = ParamUtil.getLong(req, "plid");
191:
192: long groupId = ParamUtil.getLong(req, "groupId");
193: boolean privateLayout = ParamUtil.getBoolean(req,
194: "privateLayout");
195: long layoutId = ParamUtil.getLong(req, "layoutId");
196: long parentPlid = ParamUtil.getLong(req, "parentPlid");
197: long parentLayoutId = ParamUtil.getLong(req, "parentLayoutId",
198: LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
199:
200: if (plid <= 0) {
201: LayoutServiceUtil.updateParentLayoutId(groupId,
202: privateLayout, layoutId, parentLayoutId);
203: } else {
204: LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
205: }
206: }
207:
208: protected void updatePriority(HttpServletRequest req)
209: throws Exception {
210: long plid = ParamUtil.getLong(req, "plid");
211:
212: long groupId = ParamUtil.getLong(req, "groupId");
213: boolean privateLayout = ParamUtil.getBoolean(req,
214: "privateLayout");
215: long layoutId = ParamUtil.getLong(req, "layoutId");
216: int priority = ParamUtil.getInteger(req, "priority");
217:
218: if (plid <= 0) {
219: LayoutServiceUtil.updatePriority(groupId, privateLayout,
220: layoutId, priority);
221: } else {
222: LayoutServiceUtil.updatePriority(plid, priority);
223: }
224: }
225:
226: }
|