1   /**
2    * Copyright (c) 2000-2007 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.PropsUtil;
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(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(HttpServletRequest req) throws Exception {
110         String mainPath = ParamUtil.getString(req, "mainPath");
111         String doAsUserId = ParamUtil.getString(req, "doAsUserId");
112 
113         long groupId = ParamUtil.getLong(req, "groupId");
114         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
115         long parentLayoutId = ParamUtil.getLong(req, "parentLayoutId");
116         String name = ParamUtil.getString(req, "name", "New Page");
117         String title = StringPool.BLANK;
118         String description = StringPool.BLANK;
119         String type = LayoutImpl.TYPE_PORTLET;
120         boolean hidden = false;
121         String friendlyURL = StringPool.BLANK;
122 
123         Layout layout = LayoutServiceUtil.addLayout(
124             groupId, privateLayout, parentLayoutId, name, title, description,
125             type, hidden, friendlyURL);
126 
127         LayoutTypePortlet layoutTypePortlet =
128             (LayoutTypePortlet)layout.getLayoutType();
129 
130         layoutTypePortlet.setLayoutTemplateId(
131             0, PropsUtil.get(PropsUtil.LAYOUT_DEFAULT_TEMPLATE_ID), false);
132 
133         LayoutServiceUtil.updateLayout(
134             layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(),
135             layout.getTypeSettings());
136 
137         String layoutURL = PortalUtil.getLayoutActualURL(layout, mainPath);
138 
139         if (Validator.isNotNull(doAsUserId)) {
140             layoutURL = Http.addParameter(layoutURL, "doAsUserId", doAsUserId);
141         }
142 
143         return new String[] {String.valueOf(layout.getLayoutId()), layoutURL};
144     }
145 
146     protected void deletePage(HttpServletRequest req) throws Exception {
147         long plid = ParamUtil.getLong(req, "plid");
148 
149         long groupId = ParamUtil.getLong(req, "groupId");
150         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
151         long layoutId = ParamUtil.getLong(req, "layoutId");
152 
153         if (plid <= 0) {
154             LayoutServiceUtil.deleteLayout(groupId, privateLayout, layoutId);
155         }
156         else {
157             LayoutServiceUtil.deleteLayout(plid);
158         }
159     }
160 
161     protected void updateDisplayOrder(HttpServletRequest req) throws Exception {
162         long groupId = ParamUtil.getLong(req, "groupId");
163         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
164         long parentLayoutId = ParamUtil.getLong(req, "parentLayoutId");
165         long[] layoutIds = StringUtil.split(
166             ParamUtil.getString(req, "layoutIds"), 0L);
167 
168         LayoutServiceUtil.setLayouts(
169             groupId, privateLayout, parentLayoutId, layoutIds);
170     }
171 
172     protected void updateName(HttpServletRequest req) throws Exception {
173         long plid = ParamUtil.getLong(req, "plid");
174 
175         long groupId = ParamUtil.getLong(req, "groupId");
176         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
177         long layoutId = ParamUtil.getLong(req, "layoutId");
178         String name = ParamUtil.getString(req, "name");
179         String languageId = ParamUtil.getString(req, "languageId");
180 
181         if (plid <= 0) {
182             LayoutServiceUtil.updateName(
183                 groupId, privateLayout, layoutId, name, languageId);
184         }
185         else {
186             LayoutServiceUtil.updateName(plid, name, languageId);
187         }
188     }
189 
190     protected void updateParentLayoutId(HttpServletRequest req)
191         throws Exception {
192 
193         long plid = ParamUtil.getLong(req, "plid");
194 
195         long groupId = ParamUtil.getLong(req, "groupId");
196         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
197         long layoutId = ParamUtil.getLong(req, "layoutId");
198         long parentPlid = ParamUtil.getLong(req, "parentPlid");
199         long parentLayoutId = ParamUtil.getLong(
200             req, "parentLayoutId", LayoutImpl.DEFAULT_PARENT_LAYOUT_ID);
201 
202         if (plid <= 0) {
203             LayoutServiceUtil.updateParentLayoutId(
204                 groupId, privateLayout, layoutId, parentLayoutId);
205         }
206         else {
207             LayoutServiceUtil.updateParentLayoutId(plid, parentPlid);
208         }
209     }
210 
211     protected void updatePriority(HttpServletRequest req) throws Exception {
212         long plid = ParamUtil.getLong(req, "plid");
213 
214         long groupId = ParamUtil.getLong(req, "groupId");
215         boolean privateLayout = ParamUtil.getBoolean(req, "privateLayout");
216         long layoutId = ParamUtil.getLong(req, "layoutId");
217         int priority = ParamUtil.getInteger(req, "priority");
218 
219         if (plid <= 0) {
220             LayoutServiceUtil.updatePriority(
221                 groupId, privateLayout, layoutId, priority);
222         }
223         else {
224             LayoutServiceUtil.updatePriority(plid, priority);
225         }
226     }
227 
228 }