001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portlet.layoutsadmin.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.staging.LayoutStagingUtil;
021    import com.liferay.portal.kernel.staging.StagingUtil;
022    import com.liferay.portal.kernel.util.ParamUtil;
023    import com.liferay.portal.kernel.util.WebKeys;
024    import com.liferay.portal.model.Layout;
025    import com.liferay.portal.model.LayoutBranch;
026    import com.liferay.portal.model.LayoutRevision;
027    import com.liferay.portal.model.LayoutSetBranch;
028    import com.liferay.portal.model.User;
029    import com.liferay.portal.model.impl.LayoutTypePortletImpl;
030    import com.liferay.portal.service.LayoutLocalServiceUtil;
031    import com.liferay.portal.service.LayoutSetBranchLocalServiceUtil;
032    import com.liferay.portal.struts.JSONAction;
033    import com.liferay.portal.theme.ThemeDisplay;
034    import com.liferay.portal.util.PropsValues;
035    import com.liferay.portlet.sites.util.SitesUtil;
036    
037    import java.util.List;
038    
039    import javax.servlet.http.HttpServletRequest;
040    import javax.servlet.http.HttpServletResponse;
041    
042    import org.apache.struts.action.ActionForm;
043    import org.apache.struts.action.ActionMapping;
044    
045    /**
046     * @author Eduardo Lundgren
047     */
048    public class GetLayoutsAction extends JSONAction {
049    
050            @Override
051            public String getJSON(
052                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
053                            HttpServletResponse response)
054                    throws Exception {
055    
056                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
057                            WebKeys.THEME_DISPLAY);
058    
059                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
060    
061                    List<Layout> layoutAncestors = null;
062    
063                    long selPlid = ParamUtil.getLong(request, "selPlid");
064    
065                    if (selPlid != 0) {
066                            Layout selLayout = LayoutLocalServiceUtil.getLayout(selPlid);
067    
068                            layoutAncestors = selLayout.getAncestors();
069                    }
070    
071                    List<Layout> layouts = getLayouts(request);
072    
073                    for (Layout layout : layouts) {
074                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
075    
076                            jsonObject.put("contentDisplayPage", layout.isContentDisplayPage());
077                            jsonObject.put("hasChildren", layout.hasChildren());
078                            jsonObject.put("layoutId", layout.getLayoutId());
079    
080                            String name = layout.getName(themeDisplay.getLocale());
081    
082                            if (SitesUtil.isLayoutToBeUpdatedFromTemplate(layout)) {
083                                    Layout templateLayout = LayoutTypePortletImpl.getTemplateLayout(
084                                            layout);
085    
086                                    name = templateLayout.getName(themeDisplay.getLocale());
087                            }
088    
089                            jsonObject.put("name", name);
090    
091                            jsonObject.put("parentLayoutId", layout.getParentLayoutId());
092                            jsonObject.put("plid", layout.getPlid());
093                            jsonObject.put("priority", layout.getPriority());
094                            jsonObject.put("privateLayout", layout.isPrivateLayout());
095    
096                            if ((layoutAncestors != null) && layoutAncestors.contains(layout)) {
097                                    jsonObject.put("selLayoutAncestor", true);
098                            }
099    
100                            jsonObject.put("type", layout.getType());
101                            jsonObject.put("uuid", layout.getUuid());
102    
103                            LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
104                                    layout);
105    
106                            if (layoutRevision != null) {
107                                    User user = themeDisplay.getUser();
108    
109                                    long recentLayoutSetBranchId =
110                                            StagingUtil.getRecentLayoutSetBranchId(
111                                                    user, layout.getLayoutSet().getLayoutSetId());
112    
113                                    if (StagingUtil.isIncomplete(layout, recentLayoutSetBranchId)) {
114                                            jsonObject.put("incomplete", true);
115                                    }
116    
117                                    long layoutSetBranchId = layoutRevision.getLayoutSetBranchId();
118    
119                                    LayoutSetBranch layoutSetBranch =
120                                            LayoutSetBranchLocalServiceUtil.getLayoutSetBranch(
121                                                    layoutSetBranchId);
122    
123                                    LayoutBranch layoutBranch = layoutRevision.getLayoutBranch();
124    
125                                    jsonObject.put(
126                                            "layoutBranchId", layoutBranch.getLayoutBranchId());
127                                    jsonObject.put("layoutBranchName", layoutBranch.getName());
128                                    jsonObject.put(
129                                            "layoutRevisionId", layoutRevision.getLayoutRevisionId());
130                                    jsonObject.put("layoutSetBranchId", layoutSetBranchId);
131                                    jsonObject.put(
132                                            "layoutSetBranchName", layoutSetBranch.getName());
133                            }
134    
135                            jsonArray.put(jsonObject);
136                    }
137    
138                    return jsonArray.toString();
139            }
140    
141            protected List<Layout> getLayouts(HttpServletRequest request)
142                    throws Exception {
143    
144                    long groupId = ParamUtil.getLong(request, "groupId");
145                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
146                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
147                    int start = ParamUtil.getInteger(request, "start");
148                    int end = start + PropsValues.LAYOUT_MANAGE_PAGES_INITIAL_CHILDREN;
149    
150                    return LayoutLocalServiceUtil.getLayouts(
151                            groupId, privateLayout, parentLayoutId, true, start, end);
152            }
153    
154    }