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.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                    _groupId = groupId;
039                    _privateLayout = privateLayout;
040                    _locale = locale;
041                    _nodeId = 1;
042    
043                    _list = new ArrayList<String>();
044    
045                    _list.add(
046                            "1|0|0|" + LayoutConstants.DEFAULT_PLID + "|" + rootNodeName +
047                                    "|0");
048    
049                    _createList(LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, _nodeId, 0);
050    
051                    return new LayoutView(_list, _depth);
052            }
053    
054            private void _createList(
055                            long parentLayoutId, int parentId, int depth)
056                    throws PortalException, SystemException {
057    
058                    List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
059                            _groupId, _privateLayout, parentLayoutId);
060    
061                    for (int i = 0; i < layouts.size(); i++) {
062                            Layout layout = layouts.get(i);
063    
064                            if (i == 0) {
065                                    depth++;
066    
067                                    if (depth > _depth) {
068                                            _depth = depth;
069                                    }
070                            }
071    
072                            StringBundler sb = new StringBundler(13);
073    
074                            sb.append(++_nodeId);
075                            sb.append("|");
076                            sb.append(parentId);
077                            sb.append("|");
078    
079                            if ((i + 1) == layouts.size()) {
080                                    sb.append("1");
081                            }
082                            else {
083                                    sb.append("0");
084                            }
085    
086                            sb.append("|");
087                            sb.append(layout.getPlid());
088                            sb.append("|");
089                            sb.append(layout.getName(_locale));
090                            sb.append("|");
091                            //sb.append("9");
092                            sb.append("11");
093                            sb.append("|");
094                            sb.append(depth);
095    
096                            _list.add(sb.toString());
097    
098                            _createList(layout.getLayoutId(), _nodeId, depth);
099                    }
100            }
101    
102            private long _groupId;
103            private boolean _privateLayout;
104            private Locale _locale;
105            private int _nodeId;
106            private List<String> _list;
107            private int _depth;
108    
109    }