001    /**
002     * Copyright (c) 2000-2012 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.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.service.LayoutLocalServiceUtil;
030    import com.liferay.portal.service.LayoutSetBranchLocalServiceUtil;
031    import com.liferay.portal.struts.JSONAction;
032    import com.liferay.portal.theme.ThemeDisplay;
033    import com.liferay.portal.util.PropsValues;
034    import com.liferay.portlet.sites.util.SitesUtil;
035    
036    import java.util.List;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    import org.apache.struts.action.ActionForm;
042    import org.apache.struts.action.ActionMapping;
043    
044    /**
045     * @author Eduardo Lundgren
046     */
047    public class GetLayoutsAction extends JSONAction {
048    
049            @Override
050            public String getJSON(
051                            ActionMapping mapping, ActionForm form, HttpServletRequest request,
052                            HttpServletResponse response)
053                    throws Exception {
054    
055                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
056                            WebKeys.THEME_DISPLAY);
057    
058                    JSONArray jsonArray = JSONFactoryUtil.createJSONArray();
059    
060                    List<Layout> layoutAncestors = null;
061    
062                    long selPlid = ParamUtil.getLong(request, "selPlid");
063    
064                    if (selPlid != 0) {
065                            Layout selLayout = LayoutLocalServiceUtil.getLayout(selPlid);
066    
067                            layoutAncestors = selLayout.getAncestors();
068                    }
069    
070                    List<Layout> layouts = getLayouts(request);
071    
072                    for (Layout layout : layouts) {
073                            JSONObject jsonObject = JSONFactoryUtil.createJSONObject();
074    
075                            jsonObject.put("contentDisplayPage", layout.isContentDisplayPage());
076                            jsonObject.put("hasChildren", layout.hasChildren());
077                            jsonObject.put("layoutId", layout.getLayoutId());
078                            jsonObject.put("name", layout.getName(themeDisplay.getLocale()));
079                            jsonObject.put("parentLayoutId", layout.getParentLayoutId());
080                            jsonObject.put("plid", layout.getPlid());
081                            jsonObject.put("priority", layout.getPriority());
082                            jsonObject.put("privateLayout", layout.isPrivateLayout());
083    
084                            if ((layoutAncestors != null) && layoutAncestors.contains(layout)) {
085                                    jsonObject.put("selLayoutAncestor", true);
086                            }
087    
088                            jsonObject.put("type", layout.getType());
089                            jsonObject.put("updateable", SitesUtil.isLayoutUpdateable(layout));
090                            jsonObject.put("uuid", layout.getUuid());
091    
092                            LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
093                                    layout);
094    
095                            if (layoutRevision != null) {
096                                    User user = themeDisplay.getUser();
097    
098                                    long recentLayoutSetBranchId =
099                                            StagingUtil.getRecentLayoutSetBranchId(
100                                                    user, layout.getLayoutSet().getLayoutSetId());
101    
102                                    if (StagingUtil.isIncomplete(layout, recentLayoutSetBranchId)) {
103                                            jsonObject.put("incomplete", true);
104                                    }
105    
106                                    long layoutSetBranchId = layoutRevision.getLayoutSetBranchId();
107    
108                                    LayoutSetBranch layoutSetBranch =
109                                            LayoutSetBranchLocalServiceUtil.getLayoutSetBranch(
110                                                    layoutSetBranchId);
111    
112                                    LayoutBranch layoutBranch = layoutRevision.getLayoutBranch();
113    
114                                    if (!layoutBranch.isMaster()) {
115                                            jsonObject.put(
116                                                    "layoutBranchId", layoutBranch.getLayoutBranchId());
117                                            jsonObject.put("layoutBranchName", layoutBranch.getName());
118                                    }
119    
120                                    jsonObject.put(
121                                            "layoutRevisionId", layoutRevision.getLayoutRevisionId());
122                                    jsonObject.put("layoutSetBranchId", layoutSetBranchId);
123                                    jsonObject.put(
124                                            "layoutSetBranchName", layoutSetBranch.getName());
125                            }
126    
127                            jsonArray.put(jsonObject);
128                    }
129    
130                    return jsonArray.toString();
131            }
132    
133            protected List<Layout> getLayouts(HttpServletRequest request)
134                    throws Exception {
135    
136                    long groupId = ParamUtil.getLong(request, "groupId");
137                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
138                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
139                    boolean incomplete = ParamUtil.getBoolean(request, "incomplete", true);
140                    int start = ParamUtil.getInteger(request, "start");
141                    int end = start + PropsValues.LAYOUT_MANAGE_PAGES_INITIAL_CHILDREN;
142    
143                    return LayoutLocalServiceUtil.getLayouts(
144                            groupId, privateLayout, parentLayoutId, incomplete, start, end);
145            }
146    
147    }