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.kernel.security.permission.ActionKeys;
26  import com.liferay.portal.kernel.security.permission.PermissionChecker;
27  import com.liferay.portal.kernel.util.Constants;
28  import com.liferay.portal.kernel.util.ParamUtil;
29  import com.liferay.portal.kernel.util.StringPool;
30  import com.liferay.portal.kernel.util.StringUtil;
31  import com.liferay.portal.kernel.util.Validator;
32  import com.liferay.portal.model.Layout;
33  import com.liferay.portal.model.LayoutTypePortlet;
34  import com.liferay.portal.model.impl.LayoutImpl;
35  import com.liferay.portal.service.LayoutServiceUtil;
36  import com.liferay.portal.service.permission.PortletPermissionUtil;
37  import com.liferay.portal.struts.JSONAction;
38  import com.liferay.portal.theme.ThemeDisplay;
39  import com.liferay.portal.util.PortalUtil;
40  import com.liferay.portal.util.PropsValues;
41  import com.liferay.portal.util.WebKeys;
42  import com.liferay.util.Http;
43  
44  import javax.servlet.http.HttpServletRequest;
45  import javax.servlet.http.HttpServletResponse;
46  
47  import org.apache.struts.action.ActionForm;
48  import org.apache.struts.action.ActionMapping;
49  
50  import org.json.JSONObject;
51  
52  /**
53   * <a href="UpdatePageAction.java.html"><b><i>View Source</i></b></a>
54   *
55   * @author Ming-Gih Lam
56   *
57   */
58  public class UpdatePageAction extends JSONAction {
59  
60      public String getJSON(
61              ActionMapping mapping, ActionForm form, HttpServletRequest req,
62              HttpServletResponse res)
63          throws Exception {
64  
65          ThemeDisplay themeDisplay =
66              (ThemeDisplay)req.getAttribute(WebKeys.THEME_DISPLAY);
67  
68          PermissionChecker permissionChecker =
69              themeDisplay.getPermissionChecker();
70  
71          String portletId = ParamUtil.getString(req, "portletId");
72  
73          if (!PortletPermissionUtil.contains(
74                  permissionChecker, themeDisplay.getPlid(), portletId,
75                  ActionKeys.CONFIGURATION)) {
76  
77              return null;
78          }
79  
80          String cmd = ParamUtil.getString(req, Constants.CMD);
81  
82          JSONObject jsonObj = new JSONObject();
83  
84          if (cmd.equals("add")) {
85              String[] array = addPage(themeDisplay, req);
86  
87              jsonObj.put("layoutId", array[0]);
88              jsonObj.put("url", array[1]);
89          }
90          else if (cmd.equals("delete")) {
91              deletePage(req);
92          }
93          else if (cmd.equals("display_order")) {
94              updateDisplayOrder(req);
95          }
96          else if (cmd.equals("name")) {
97              updateName(req);
98          }
99          else if (cmd.equals("parent_layout_id")) {
100             updateParentLayoutId(req);
101         }
102         else if (cmd.equals("priority")) {
103             updatePriority(req);
104         }
105 
106         return jsonObj.toString();
107     }
108 
109     protected String[] addPage(
110             ThemeDisplay themeDisplay, HttpServletRequest req)
111         throws Exception {
112 
113         String doAsUserId = ParamUtil.getString(req, "doAsUserId");
114 
115         long groupId = ParamUtil.getLong(req, "groupId");
116         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
117         long parentLayoutId = ParamUtil.getLong(req, "parentLayoutId");
118         String name = ParamUtil.getString(req, "name", "New Page");
119         String title = StringPool.BLANK;
120         String description = StringPool.BLANK;
121         String type = LayoutImpl.TYPE_PORTLET;
122         boolean hidden = false;
123         String friendlyURL = StringPool.BLANK;
124 
125         Layout layout = LayoutServiceUtil.addLayout(
126             groupId, privateLayout, parentLayoutId, name, title, description,
127             type, hidden, friendlyURL);
128 
129         LayoutTypePortlet layoutTypePortlet =
130             (LayoutTypePortlet)layout.getLayoutType();
131 
132         layoutTypePortlet.setLayoutTemplateId(
133             0, PropsValues.LAYOUT_DEFAULT_TEMPLATE_ID, false);
134 
135         LayoutServiceUtil.updateLayout(
136             layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
137             layout.getTypeSettings());
138 
139         String layoutURL = PortalUtil.getLayoutURL(layout, themeDisplay);
140 
141         if (Validator.isNotNull(doAsUserId)) {
142             layoutURL = Http.addParameter(
143                 layoutURL, "doAsUserId", themeDisplay.getDoAsUserId());
144         }
145 
146         return new String[] {String.valueOf(layout.getLayoutId()), layoutURL};
147     }
148 
149     protected void deletePage(HttpServletRequest req) throws Exception {
150         long plid = ParamUtil.getLong(req, "plid");
151 
152         long groupId = ParamUtil.getLong(req, "groupId");
153         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
154         long layoutId = ParamUtil.getLong(req, "layoutId");
155 
156         if (plid <= 0) {
157             LayoutServiceUtil.deleteLayout(groupId, privateLayout, layoutId);
158         }
159         else {
160             LayoutServiceUtil.deleteLayout(plid);
161         }
162     }
163 
164     protected void updateDisplayOrder(HttpServletRequest req) throws Exception {
165         long groupId = ParamUtil.getLong(req, "groupId");
166         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
167         long parentLayoutId = ParamUtil.getLong(req, "parentLayoutId");
168         long[] layoutIds = StringUtil.split(
169             ParamUtil.getString(req, "layoutIds"), 0L);
170 
171         LayoutServiceUtil.setLayouts(
172             groupId, privateLayout, parentLayoutId, layoutIds);
173     }
174 
175     protected void updateName(HttpServletRequest req) throws Exception {
176         long plid = ParamUtil.getLong(req, "plid");
177 
178         long groupId = ParamUtil.getLong(req, "groupId");
179         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
180         long layoutId = ParamUtil.getLong(req, "layoutId");
181         String name = ParamUtil.getString(req, "name");
182         String languageId = ParamUtil.getString(req, "languageId");
183 
184         if (plid <= 0) {
185             LayoutServiceUtil.updateName(
186                 groupId, privateLayout, layoutId, name, languageId);
187         }
188         else {
189             LayoutServiceUtil.updateName(plid, name, languageId);
190         }
191     }
192 
193     protected void updateParentLayoutId(HttpServletRequest req)
194         throws Exception {
195 
196         long plid = ParamUtil.getLong(req, "plid");
197 
198         long groupId = ParamUtil.getLong(req, "groupId");
199         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
200         long layoutId = ParamUtil.getLong(req, "layoutId");
201         long parentPlid = ParamUtil.getLong(req, "parentPlid");
202         long parentLayoutId = ParamUtil.getLong(
203             req, "parentLayoutId", LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
204 
205         if (plid <= 0) {
206             LayoutServiceUtil.updateParentLayoutId(
207                 groupId, privateLayout, layoutId, parentLayoutId);
208         }
209         else {
210             LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
211         }
212     }
213 
214     protected void updatePriority(HttpServletRequest req) throws Exception {
215         long plid = ParamUtil.getLong(req, "plid");
216 
217         long groupId = ParamUtil.getLong(req, "groupId");
218         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
219         long layoutId = ParamUtil.getLong(req, "layoutId");
220         int priority = ParamUtil.getInteger(req, "priority");
221 
222         if (plid <= 0) {
223             LayoutServiceUtil.updatePriority(
224                 groupId, privateLayout, layoutId, priority);
225         }
226         else {
227             LayoutServiceUtil.updatePriority(plid, priority);
228         }
229     }
230 
231 }