1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.communities.action;
16  
17  import com.liferay.portal.kernel.json.JSONArray;
18  import com.liferay.portal.kernel.json.JSONFactoryUtil;
19  import com.liferay.portal.kernel.json.JSONObject;
20  import com.liferay.portal.kernel.util.ParamUtil;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.service.LayoutLocalServiceUtil;
23  import com.liferay.portal.struts.JSONAction;
24  
25  import java.util.List;
26  
27  import javax.servlet.http.HttpServletRequest;
28  import javax.servlet.http.HttpServletResponse;
29  
30  import org.apache.struts.action.ActionForm;
31  import org.apache.struts.action.ActionMapping;
32  
33  /**
34   * <a href="GetLayoutsAction.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Eduardo Lundgren
37   */
38  public class GetLayoutsAction extends JSONAction {
39  
40      public String getJSON(
41              ActionMapping mapping, ActionForm form, HttpServletRequest request,
42              HttpServletResponse response)
43          throws Exception {
44  
45          long groupId = ParamUtil.getLong(request, "groupId");
46          boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
47          long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
48  
49          JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
50  
51          List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
52              groupId, privateLayout, parentLayoutId);
53  
54          for (Layout layout : layouts) {
55              JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
56  
57              jsonObject.put("hasChildren", layout.hasChildren());
58              jsonObject.put("layoutId", layout.getLayoutId());
59              jsonObject.put("name", layout.getName());
60              jsonObject.put("parentLayoutId", layout.getParentLayoutId());
61              jsonObject.put("plid", layout.getPlid());
62              jsonObject.put("priority", layout.getPriority());
63              jsonObject.put("privateLayout", layout.getPrivateLayout());
64              jsonObject.put("type", layout.getType());
65  
66              jsonArray.put(jsonObject);
67          }
68  
69          return jsonArray.toString();
70      }
71  
72  }