1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.communities.action;
24  
25  import com.liferay.portal.events.EventsProcessor;
26  import com.liferay.portal.kernel.configuration.Filter;
27  import com.liferay.portal.kernel.json.JSONFactoryUtil;
28  import com.liferay.portal.kernel.json.JSONObject;
29  import com.liferay.portal.kernel.util.Constants;
30  import com.liferay.portal.kernel.util.HttpUtil;
31  import com.liferay.portal.kernel.util.ParamUtil;
32  import com.liferay.portal.kernel.util.StringPool;
33  import com.liferay.portal.kernel.util.StringUtil;
34  import com.liferay.portal.kernel.util.Validator;
35  import com.liferay.portal.model.Layout;
36  import com.liferay.portal.model.LayoutConstants;
37  import com.liferay.portal.security.permission.ActionKeys;
38  import com.liferay.portal.security.permission.PermissionChecker;
39  import com.liferay.portal.service.LayoutLocalServiceUtil;
40  import com.liferay.portal.service.LayoutServiceUtil;
41  import com.liferay.portal.service.permission.GroupPermissionUtil;
42  import com.liferay.portal.service.permission.LayoutPermissionUtil;
43  import com.liferay.portal.struts.JSONAction;
44  import com.liferay.portal.theme.ThemeDisplay;
45  import com.liferay.portal.util.PortalUtil;
46  import com.liferay.portal.util.PropsKeys;
47  import com.liferay.portal.util.PropsUtil;
48  import com.liferay.portal.util.WebKeys;
49  import com.liferay.portlet.communities.util.CommunitiesUtil;
50  
51  import javax.servlet.http.HttpServletRequest;
52  import javax.servlet.http.HttpServletResponse;
53  
54  import org.apache.struts.action.ActionForm;
55  import org.apache.struts.action.ActionMapping;
56  
57  /**
58   * <a href="UpdatePageAction.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Ming-Gih Lam
61   *
62   */
63  public class UpdatePageAction extends JSONAction {
64  
65      public String getJSON(
66              ActionMapping mapping, ActionForm form, HttpServletRequest request,
67              HttpServletResponse response)
68          throws Exception {
69  
70          ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
71              WebKeys.THEME_DISPLAY);
72  
73          PermissionChecker permissionChecker =
74              themeDisplay.getPermissionChecker();
75  
76          long plid = ParamUtil.getLong(request, "plid");
77  
78          long groupId = ParamUtil.getLong(request, "groupId");
79          boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
80          long layoutId = ParamUtil.getLong(request, "layoutId");
81          long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
82  
83          Layout layout = null;
84  
85          if (plid > 0) {
86              layout = LayoutLocalServiceUtil.getLayout(plid);
87          }
88          else if (layoutId > 0) {
89              layout = LayoutLocalServiceUtil.getLayout(
90                  groupId, privateLayout, layoutId);
91          }
92          else if (parentLayoutId > 0) {
93              layout = LayoutLocalServiceUtil.getLayout(
94                  groupId, privateLayout, parentLayoutId);
95          }
96  
97          if (layout != null) {
98              if (!LayoutPermissionUtil.contains(
99                      permissionChecker, layout, ActionKeys.UPDATE)) {
100 
101                 return null;
102             }
103         }
104         else {
105             if (!GroupPermissionUtil.contains(
106                     permissionChecker, groupId, ActionKeys.MANAGE_LAYOUTS)) {
107 
108                 return null;
109             }
110         }
111 
112         String cmd = ParamUtil.getString(request, Constants.CMD);
113 
114         JSONObject jsonObj = JSONFactoryUtil.createJSONObject();
115 
116         if (cmd.equals("add")) {
117             String[] array = addPage(themeDisplay, request, response);
118 
119             jsonObj.put("layoutId", array[0]);
120             jsonObj.put("url", array[1]);
121         }
122         else if (cmd.equals("delete")) {
123             CommunitiesUtil.deleteLayout(request, response);
124         }
125         else if (cmd.equals("display_order")) {
126             updateDisplayOrder(request);
127         }
128         else if (cmd.equals("name")) {
129             updateName(request);
130         }
131         else if (cmd.equals("parent_layout_id")) {
132             updateParentLayoutId(request);
133         }
134         else if (cmd.equals("priority")) {
135             updatePriority(request);
136         }
137 
138         return jsonObj.toString();
139     }
140 
141     protected String[] addPage(
142             ThemeDisplay themeDisplay, HttpServletRequest request,
143             HttpServletResponse response)
144         throws Exception {
145 
146         String doAsUserId = ParamUtil.getString(request, "doAsUserId");
147 
148         long groupId = ParamUtil.getLong(request, "groupId");
149         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
150         long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
151         String name = ParamUtil.getString(request, "name", "New Page");
152         String title = StringPool.BLANK;
153         String description = StringPool.BLANK;
154         String type = LayoutConstants.TYPE_PORTLET;
155         boolean hidden = false;
156         String friendlyURL = StringPool.BLANK;
157 
158         Layout layout = LayoutServiceUtil.addLayout(
159             groupId, privateLayout, parentLayoutId, name, title, description,
160             type, hidden, friendlyURL);
161 
162         String[] eventClasses = StringUtil.split(
163             PropsUtil.get(
164                 PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
165                 new Filter(layout.getType())));
166 
167         EventsProcessor.process(
168             PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE, eventClasses, request,
169             response);
170 
171         String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
172 
173         if (Validator.isNotNull(doAsUserId)) {
174             layoutURL = HttpUtil.addParameter(
175                 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
176         }
177 
178         return new String[] {String.valueOf(layout.getLayoutId()), layoutURL};
179     }
180 
181     protected void updateDisplayOrder(HttpServletRequest request)
182         throws Exception {
183 
184         long groupId = ParamUtil.getLong(request, "groupId");
185         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
186         long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
187         long[] layoutIds = StringUtil.split(
188             ParamUtil.getString(request, "layoutIds"), 0L);
189 
190         LayoutServiceUtil.setLayouts(
191             groupId, privateLayout, parentLayoutId, layoutIds);
192     }
193 
194     protected void updateName(HttpServletRequest request) throws Exception {
195         long plid = ParamUtil.getLong(request, "plid");
196 
197         long groupId = ParamUtil.getLong(request, "groupId");
198         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
199         long layoutId = ParamUtil.getLong(request, "layoutId");
200         String name = ParamUtil.getString(request, "name");
201         String languageId = ParamUtil.getString(request, "languageId");
202 
203         if (plid <= 0) {
204             LayoutServiceUtil.updateName(
205                 groupId, privateLayout, layoutId, name, languageId);
206         }
207         else {
208             LayoutServiceUtil.updateName(plid, name, languageId);
209         }
210     }
211 
212     protected void updateParentLayoutId(HttpServletRequest request)
213         throws Exception {
214 
215         long plid = ParamUtil.getLong(request, "plid");
216 
217         long parentPlid = ParamUtil.getLong(request, "parentPlid");
218 
219         long groupId = ParamUtil.getLong(request, "groupId");
220         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
221         long layoutId = ParamUtil.getLong(request, "layoutId");
222         long parentLayoutId = ParamUtil.getLong(
223             request, "parentLayoutId",
224             LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
225 
226         if (plid <= 0) {
227             LayoutServiceUtil.updateParentLayoutId(
228                 groupId, privateLayout, layoutId, parentLayoutId);
229         }
230         else {
231             LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
232         }
233     }
234 
235     protected void updatePriority(HttpServletRequest request) throws Exception {
236         long plid = ParamUtil.getLong(request, "plid");
237 
238         long groupId = ParamUtil.getLong(request, "groupId");
239         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
240         long layoutId = ParamUtil.getLong(request, "layoutId");
241         int priority = ParamUtil.getInteger(request, "priority");
242 
243         if (plid <= 0) {
244             LayoutServiceUtil.updatePriority(
245                 groupId, privateLayout, layoutId, priority);
246         }
247         else {
248             LayoutServiceUtil.updatePriority(plid, priority);
249         }
250     }
251 
252 }