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