001    /**
002     * Copyright (c) 2000-2013 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.portal.util;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.util.StringBundler;
020    import com.liferay.portal.model.Layout;
021    import com.liferay.portal.model.LayoutConstants;
022    import com.liferay.portal.service.LayoutLocalServiceUtil;
023    
024    import java.util.ArrayList;
025    import java.util.List;
026    import java.util.Locale;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class LayoutLister {
032    
033            public LayoutView getLayoutView(
034                            long groupId, boolean privateLayout, String rootNodeName,
035                            Locale locale)
036                    throws PortalException, SystemException {
037    
038                    _locale = locale;
039                    _nodeId = 1;
040    
041                    _list = new ArrayList<String>();
042    
043                    _list.add(
044                            "1|0|0|" + LayoutConstants.DEFAULT_PLID + "|" + rootNodeName +
045                                    "|0");
046    
047                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
048                            groupId, privateLayout);
049    
050                    _createList(
051                            layouts, LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, _nodeId, 0);
052    
053                    return new LayoutView(_list, _depth);
054            }
055    
056            private void _createList(
057                            List<Layout> layouts, long parentLayoutId, int parentId, int depth)
058                    throws PortalException, SystemException {
059    
060                    List<Layout> matchedLayouts = new ArrayList<Layout>();
061    
062                    for (Layout layout : layouts) {
063                            if (layout.getParentLayoutId() == parentLayoutId) {
064                                    matchedLayouts.add(layout);
065                            }
066                    }
067    
068                    for (int i = 0; i < matchedLayouts.size(); i++) {
069                            Layout layout = matchedLayouts.get(i);
070    
071                            if (i == 0) {
072                                    depth++;
073    
074                                    if (depth > _depth) {
075                                            _depth = depth;
076                                    }
077                            }
078    
079                            StringBundler sb = new StringBundler(13);
080    
081                            sb.append(++_nodeId);
082                            sb.append("|");
083                            sb.append(parentId);
084                            sb.append("|");
085    
086                            if ((i + 1) == matchedLayouts.size()) {
087                                    sb.append("1");
088                            }
089                            else {
090                                    sb.append("0");
091                            }
092    
093                            sb.append("|");
094                            sb.append(layout.getPlid());
095                            sb.append("|");
096                            sb.append(layout.getName(_locale));
097                            sb.append("|");
098                            //sb.append("9");
099                            sb.append("11");
100                            sb.append("|");
101                            sb.append(depth);
102    
103                            _list.add(sb.toString());
104    
105                            _createList(layouts, layout.getLayoutId(), _nodeId, depth);
106                    }
107            }
108    
109            private int _depth;
110            private List<String> _list;
111            private Locale _locale;
112            private int _nodeId;
113    
114    }