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