001    /**
002     * Copyright (c) 2000-2011 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.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.isLayoutToBeUpdatedFromSourcePrototype(layout)) {
083                                    Layout sourcePrototypeLayout =
084                                            LayoutTypePortletImpl.getSourcePrototypeLayout(layout);
085    
086                                    if (sourcePrototypeLayout != null) {
087                                            name = sourcePrototypeLayout.getName(
088                                                    themeDisplay.getLocale());
089                                    }
090                            }
091    
092                            jsonObject.put("name", name);
093    
094                            jsonObject.put("parentLayoutId", layout.getParentLayoutId());
095                            jsonObject.put("plid", layout.getPlid());
096                            jsonObject.put("priority", layout.getPriority());
097                            jsonObject.put("privateLayout", layout.isPrivateLayout());
098    
099                            if ((layoutAncestors != null) && layoutAncestors.contains(layout)) {
100                                    jsonObject.put("selLayoutAncestor", true);
101                            }
102    
103                            jsonObject.put("type", layout.getType());
104                            jsonObject.put("updateable", SitesUtil.isLayoutUpdateable(layout));
105                            jsonObject.put("uuid", layout.getUuid());
106    
107                            LayoutRevision layoutRevision = LayoutStagingUtil.getLayoutRevision(
108                                    layout);
109    
110                            if (layoutRevision != null) {
111                                    User user = themeDisplay.getUser();
112    
113                                    long recentLayoutSetBranchId =
114                                            StagingUtil.getRecentLayoutSetBranchId(
115                                                    user, layout.getLayoutSet().getLayoutSetId());
116    
117                                    if (StagingUtil.isIncomplete(layout, recentLayoutSetBranchId)) {
118                                            jsonObject.put("incomplete", true);
119                                    }
120    
121                                    long layoutSetBranchId = layoutRevision.getLayoutSetBranchId();
122    
123                                    LayoutSetBranch layoutSetBranch =
124                                            LayoutSetBranchLocalServiceUtil.getLayoutSetBranch(
125                                                    layoutSetBranchId);
126    
127                                    LayoutBranch layoutBranch = layoutRevision.getLayoutBranch();
128    
129                                    if (!layoutBranch.isMaster()) {
130                                            jsonObject.put(
131                                                    "layoutBranchId", layoutBranch.getLayoutBranchId());
132                                            jsonObject.put("layoutBranchName", layoutBranch.getName());
133                                    }
134    
135                                    jsonObject.put(
136                                            "layoutRevisionId", layoutRevision.getLayoutRevisionId());
137                                    jsonObject.put("layoutSetBranchId", layoutSetBranchId);
138                                    jsonObject.put(
139                                            "layoutSetBranchName", layoutSetBranch.getName());
140                            }
141    
142                            jsonArray.put(jsonObject);
143                    }
144    
145                    return jsonArray.toString();
146            }
147    
148            protected List<Layout> getLayouts(HttpServletRequest request)
149                    throws Exception {
150    
151                    long groupId = ParamUtil.getLong(request, "groupId");
152                    boolean privateLayout = ParamUtil.getBoolean(request, "privateLayout");
153                    long parentLayoutId = ParamUtil.getLong(request, "parentLayoutId");
154                    int start = ParamUtil.getInteger(request, "start");
155                    int end = start + PropsValues.LAYOUT_MANAGE_PAGES_INITIAL_CHILDREN;
156    
157                    return LayoutLocalServiceUtil.getLayouts(
158                            groupId, privateLayout, parentLayoutId, true, start, end);
159            }
160    
161    }