1   /**
2    * Copyright (c) 2000-2009 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         String doAsUserLanguageId = ParamUtil.getString(
148             request, "doAsUserLanguageId");
149 
150         long groupId = ParamUtil.getLong(request, "groupId");
151         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
152         long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
153         String name = ParamUtil.getString(request, "name", "New Page");
154         String title = StringPool.BLANK;
155         String description = StringPool.BLANK;
156         String type = LayoutConstants.TYPE_PORTLET;
157         boolean hidden = false;
158         String friendlyURL = StringPool.BLANK;
159 
160         Layout layout = LayoutServiceUtil.addLayout(
161             groupId, privateLayout, parentLayoutId, name, title, description,
162             type, hidden, friendlyURL);
163 
164         String[] eventClasses = StringUtil.split(
165             PropsUtil.get(
166                 PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE,
167                 new Filter(layout.getType())));
168 
169         EventsProcessor.process(
170             PropsKeys.LAYOUT_CONFIGURATION_ACTION_UPDATE, eventClasses, request,
171             response);
172 
173         String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
174 
175         if (Validator.isNotNull(doAsUserId)) {
176             layoutURL = HttpUtil.addParameter(
177                 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
178         }
179 
180         if (Validator.isNotNull(doAsUserLanguageId)) {
181             layoutURL = HttpUtil.addParameter(
182                 layoutURL, "doAsUserLanguageId",
183                 themeDisplay.getDoAsUserLanguageId());
184         }
185 
186         return new String[] {String.valueOf(layout.getLayoutId()), layoutURL};
187     }
188 
189     protected void updateDisplayOrder(HttpServletRequest request)
190         throws Exception {
191 
192         long groupId = ParamUtil.getLong(request, "groupId");
193         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
194         long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
195         long[] layoutIds = StringUtil.split(
196             ParamUtil.getString(request, "layoutIds"), 0L);
197 
198         LayoutServiceUtil.setLayouts(
199             groupId, privateLayout, parentLayoutId, layoutIds);
200     }
201 
202     protected void updateName(HttpServletRequest request) throws Exception {
203         long plid = ParamUtil.getLong(request, "plid");
204 
205         long groupId = ParamUtil.getLong(request, "groupId");
206         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
207         long layoutId = ParamUtil.getLong(request, "layoutId");
208         String name = ParamUtil.getString(request, "name");
209         String languageId = ParamUtil.getString(request, "languageId");
210 
211         if (plid <= 0) {
212             LayoutServiceUtil.updateName(
213                 groupId, privateLayout, layoutId, name, languageId);
214         }
215         else {
216             LayoutServiceUtil.updateName(plid, name, languageId);
217         }
218     }
219 
220     protected void updateParentLayoutId(HttpServletRequest request)
221         throws Exception {
222 
223         long plid = ParamUtil.getLong(request, "plid");
224 
225         long parentPlid = ParamUtil.getLong(request, "parentPlid");
226 
227         long groupId = ParamUtil.getLong(request, "groupId");
228         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
229         long layoutId = ParamUtil.getLong(request, "layoutId");
230         long parentLayoutId = ParamUtil.getLong(
231             request, "parentLayoutId",
232             LayoutConstants.DEFAULT_PARENT_LAYOUT_ID);
233 
234         if (plid <= 0) {
235             LayoutServiceUtil.updateParentLayoutId(
236                 groupId, privateLayout, layoutId, parentLayoutId);
237         }
238         else {
239             LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
240         }
241     }
242 
243     protected void updatePriority(HttpServletRequest request) throws Exception {
244         long plid = ParamUtil.getLong(request, "plid");
245 
246         long groupId = ParamUtil.getLong(request, "groupId");
247         boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
248         long layoutId = ParamUtil.getLong(request, "layoutId");
249         int priority = ParamUtil.getInteger(request, "priority");
250 
251         if (plid <= 0) {
252             LayoutServiceUtil.updatePriority(
253                 groupId, privateLayout, layoutId, priority);
254         }
255         else {
256             LayoutServiceUtil.updatePriority(plid, priority);
257         }
258     }
259 
260 }