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