1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.communities.action;
16  
17  import com.liferay.portal.events.EventsProcessorUtil;
18  import com.liferay.portal.kernel.configuration.Filter;
19  import com.liferay.portal.kernel.json.JSONFactoryUtil;
20  import com.liferay.portal.kernel.json.JSONObject;
21  import com.liferay.portal.kernel.util.Constants;
22  import com.liferay.portal.kernel.util.HttpUtil;
23  import com.liferay.portal.kernel.util.ParamUtil;
24  import com.liferay.portal.kernel.util.PropsKeys;
25  import com.liferay.portal.kernel.util.StringPool;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.Layout;
29  import com.liferay.portal.model.LayoutConstants;
30  import com.liferay.portal.model.LayoutPrototype;
31  import com.liferay.portal.security.permission.ActionKeys;
32  import com.liferay.portal.security.permission.PermissionChecker;
33  import com.liferay.portal.service.LayoutLocalServiceUtil;
34  import com.liferay.portal.service.LayoutPrototypeServiceUtil;
35  import com.liferay.portal.service.LayoutServiceUtil;
36  import com.liferay.portal.service.ServiceContext;
37  import com.liferay.portal.service.permission.GroupPermissionUtil;
38  import com.liferay.portal.service.permission.LayoutPermissionUtil;
39  import com.liferay.portal.struts.JSONAction;
40  import com.liferay.portal.theme.ThemeDisplay;
41  import com.liferay.portal.util.PortalUtil;
42  import com.liferay.portal.util.PropsUtil;
43  import com.liferay.portal.util.WebKeys;
44  import com.liferay.portlet.communities.util.CommunitiesUtil;
45  
46  import javax.servlet.http.HttpServletRequest;
47  import javax.servlet.http.HttpServletResponse;
48  
49  import org.apache.struts.action.ActionForm;
50  import org.apache.struts.action.ActionMapping;
51  
52  /**
53   * <a href="UpdatePageAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Ming-Gih Lam
56   */
57  public class UpdatePageAction extends JSONAction {
58  
59      public String getJSON(
60              ActionMapping mapping, ActionForm form, HttpServletRequest request,
61              HttpServletResponse response)
62          throws Exception {
63  
64          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
65              WebKeys.THEME_DISPLAY);
66  
67          PermissionChecker permissionChecker =
68              themeDisplay.getPermissionChecker();
69  
70          long plid = ParamUtil.getLong(request, "plid");
71  
72          long groupId = ParamUtil.getLong(request, "groupId");
73          boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
74          long layoutId = ParamUtil.getLong(request, "layoutId");
75          long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
76  
77          Layout layout = null;
78  
79          if (plid > 0) {
80              layout = LayoutLocalServiceUtil.getLayout(plid);
81          }
82          else if (layoutId > 0) {
83              layout = LayoutLocalServiceUtil.getLayout(
84                  groupId, privateLayout, layoutId);
85          }
86          else if (parentLayoutId > 0) {
87              layout = LayoutLocalServiceUtil.getLayout(
88                  groupId, privateLayout, parentLayoutId);
89          }
90  
91          if (layout != null) {
92              if (!LayoutPermissionUtil.contains(
93                      permissionChecker, layout, ActionKeys.UPDATE)) {
94  
95                  return null;
96              }
97          }
98          else {
99              if (!GroupPermissionUtil.contains(
100                     permissionChecker, groupId, ActionKeys.MANAGE_LAYOUTS)) {
101 
102                 return null;
103             }
104         }
105 
106         String cmd = ParamUtil.getString(request, Constants.CMD);
107 
108         JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
109 
110         if (cmd.equals("add")) {
111             String[] array = addPage(themeDisplay, request, response);
112 
113             jsonObj.put("layoutId", array[0]);
114             jsonObj.put("url", array[1]);
115         }
116         else if (cmd.equals("delete")) {
117             CommunitiesUtil.deleteLayout(request, response);
118         }
119         else if (cmd.equals("display_order")) {
120             updateDisplayOrder(request);
121         }
122         else if (cmd.equals("name")) {
123             updateName(request);
124         }
125         else if (cmd.equals("parent_layout_id")) {
126             updateParentLayoutId(request);
127         }
128         else if (cmd.equals("priority")) {
129             updatePriority(request);
130         }
131 
132         return jsonObj.toString();
133     }
134 
135     protected String[] addPage(
136             ThemeDisplay themeDisplay, HttpServletRequest request,
137             HttpServletResponse response)
138         throws Exception {
139 
140         String doAsUserId = ParamUtil.getString(request, "doAsUserId");
141         String doAsUserLanguageId = ParamUtil.getString(
142             request, "doAsUserLanguageId");
143 
144         long groupId = ParamUtil.getLong(request, "groupId");
145         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
146         long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
147         String name = ParamUtil.getString(request, "name", "New Page");
148         String title = StringPool.BLANK;
149         String description = StringPool.BLANK;
150         String type = LayoutConstants.TYPE_PORTLET;
151         boolean hidden = false;
152         String friendlyURL = StringPool.BLANK;
153         long layoutPrototypeId = ParamUtil.getLong(
154             request, "layoutPrototypeId");
155 
156         ServiceContext serviceContext = new ServiceContext();
157 
158         Layout layout = null;
159 
160         if (layoutPrototypeId > 0) {
161             LayoutPrototype layoutPrototype =
162                 LayoutPrototypeServiceUtil.getLayoutPrototype(
163                     layoutPrototypeId);
164 
165             Layout layoutPrototypeLayout = layoutPrototype.getLayout();
166 
167             layout = LayoutServiceUtil.addLayout(
168                 groupId, privateLayout, parentLayoutId, name, title,
169                 description, LayoutConstants.TYPE_PORTLET, false, friendlyURL,
170                 serviceContext);
171 
172             LayoutServiceUtil.updateLayout(
173                 layout.getGroupId(), layout.isPrivateLayout(),
174                 layout.getLayoutId(), layoutPrototypeLayout.getTypeSettings());
175 
176             ActionUtil.copyPreferences(request, layout, layoutPrototypeLayout);
177         }
178         else {
179             layout = LayoutServiceUtil.addLayout(
180                 groupId, privateLayout, parentLayoutId, name, title,
181                 description, type, hidden, friendlyURL, serviceContext);
182         }
183 
184         String[] eventClasses = StringUtil.split(
185             PropsUtil.get(
186                 PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
187                 new Filter(layout.getType())));
188 
189         EventsProcessorUtil.process(
190             PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE, eventClasses, request,
191             response);
192 
193         String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
194 
195         if (Validator.isNotNull(doAsUserId)) {
196             layoutURL = HttpUtil.addParameter(
197                 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
198         }
199 
200         if (Validator.isNotNull(doAsUserLanguageId)) {
201             layoutURL = HttpUtil.addParameter(
202                 layoutURL, "doAsUserLanguageId",
203                 themeDisplay.getDoAsUserLanguageId());
204         }
205 
206         return new String[] {String.valueOf(layout.getLayoutId()), layoutURL};
207     }
208 
209     protected void updateDisplayOrder(HttpServletRequest request)
210         throws Exception {
211 
212         long groupId = ParamUtil.getLong(request, "groupId");
213         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
214         long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
215         long[] layoutIds = StringUtil.split(
216             ParamUtil.getString(request, "layoutIds"), 0L);
217 
218         LayoutServiceUtil.setLayouts(
219             groupId, privateLayout, parentLayoutId, layoutIds);
220     }
221 
222     protected void updateName(HttpServletRequest request) throws Exception {
223         long plid = ParamUtil.getLong(request, "plid");
224 
225         long groupId = ParamUtil.getLong(request, "groupId");
226         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
227         long layoutId = ParamUtil.getLong(request, "layoutId");
228         String name = ParamUtil.getString(request, "name");
229         String languageId = ParamUtil.getString(request, "languageId");
230 
231         if (plid <= 0) {
232             LayoutServiceUtil.updateName(
233                 groupId, privateLayout, layoutId, name, languageId);
234         }
235         else {
236             LayoutServiceUtil.updateName(plid, name, languageId);
237         }
238     }
239 
240     protected void updateParentLayoutId(HttpServletRequest request)
241         throws Exception {
242 
243         long plid = ParamUtil.getLong(request, "plid");
244 
245         long parentPlid = ParamUtil.getLong(request, "parentPlid");
246 
247         long groupId = ParamUtil.getLong(request, "groupId");
248         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
249         long layoutId = ParamUtil.getLong(request, "layoutId");
250         long parentLayoutId = ParamUtil.getLong(
251             request, "parentLayoutId",
252             LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
253 
254         if (plid <= 0) {
255             LayoutServiceUtil.updateParentLayoutId(
256                 groupId, privateLayout, layoutId, parentLayoutId);
257         }
258         else {
259             LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
260         }
261     }
262 
263     protected void updatePriority(HttpServletRequest request) throws Exception {
264         long plid = ParamUtil.getLong(request, "plid");
265 
266         long groupId = ParamUtil.getLong(request, "groupId");
267         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
268         long layoutId = ParamUtil.getLong(request, "layoutId");
269         int priority = ParamUtil.getInteger(request, "priority");
270 
271         if (plid <= 0) {
272             LayoutServiceUtil.updatePriority(
273                 groupId, privateLayout, layoutId, priority);
274         }
275         else {
276             LayoutServiceUtil.updatePriority(plid, priority);
277         }
278     }
279 
280 }