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