001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.communities.action;
016    
017    import com.liferay.portal.kernel.json.JSONArray;
018    import com.liferay.portal.kernel.json.JSONFactoryUtil;
019    import com.liferay.portal.kernel.json.JSONObject;
020    import com.liferay.portal.kernel.util.ParamUtil;
021    import com.liferay.portal.model.Layout;
022    import com.liferay.portal.service.LayoutLocalServiceUtil;
023    import com.liferay.portal.struts.JSONAction;
024    
025    import java.util.List;
026    
027    import javax.servlet.http.HttpServletRequest;
028    import javax.servlet.http.HttpServletResponse;
029    
030    import org.apache.struts.action.ActionForm;
031    import org.apache.struts.action.ActionMapping;
032    
033    /**
034     * @author Eduardo Lundgren
035     */
036    public class GetLayoutsAction extends JSONAction {
037    
038            public String getJSON(
039                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
040                            HttpServletResponse response)
041                    throws Exception {
042    
043                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
044    
045                    List<Layout> layouts = getLayouts(request);
046    
047                    for (Layout layout : layouts) {
048                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
049    
050                            jsonObject.put("hasChildren", layout.hasChildren());
051                            jsonObject.put("layoutId", layout.getLayoutId());
052                            jsonObject.put("name", layout.getName());
053                            jsonObject.put("parentLayoutId", layout.getParentLayoutId());
054                            jsonObject.put("plid", layout.getPlid());
055                            jsonObject.put("priority", layout.getPriority());
056                            jsonObject.put("privateLayout", layout.getPrivateLayout());
057                            jsonObject.put("type", layout.getType());
058    
059                            jsonArray.put(jsonObject);
060                    }
061    
062                    return jsonArray.toString();
063            }
064    
065            protected List<Layout> getLayouts(HttpServletRequest request)
066                    throws Exception {
067    
068                    long groupId = ParamUtil.getLong(request, "groupId");
069                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
070                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
071    
072                    return LayoutLocalServiceUtil.getLayouts(
073                            groupId, privateLayout, parentLayoutId);
074            }
075    
076    }