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