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