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