1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.util.StringBundler;
20  import com.liferay.portal.model.Layout;
21  import com.liferay.portal.model.LayoutConstants;
22  import com.liferay.portal.service.LayoutLocalServiceUtil;
23  
24  import java.util.ArrayList;
25  import java.util.List;
26  import java.util.Locale;
27  
28  /**
29   * <a href="LayoutLister.java.html"><b><i>View Source</i></b></a>
30   *
31   * @author Brian Wing Shun Chan
32   */
33  public class LayoutLister {
34  
35      public LayoutView getLayoutView(
36              long groupId, boolean privateLayout, String rootNodeName,
37              Locale locale)
38          throws PortalException, SystemException {
39  
40          _groupId = groupId;
41          _privateLayout = privateLayout;
42          _locale = locale;
43          _nodeId = 1;
44  
45          _list = new ArrayList<String>();
46  
47          _list.add(
48              "1|0|0|" + LayoutConstants.DEFAULT_PLID + "|" + rootNodeName +
49                  "|0");
50  
51          _createList(LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, _nodeId, 0);
52  
53          return new LayoutView(_list, _depth);
54      }
55  
56      private void _createList(
57              long parentLayoutId, int parentId, int depth)
58          throws PortalException, SystemException {
59  
60          List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
61              _groupId, _privateLayout, parentLayoutId);
62  
63          for (int i = 0; i < layouts.size(); i++) {
64              Layout layout = layouts.get(i);
65  
66              if (i == 0) {
67                  depth++;
68  
69                  if (depth > _depth) {
70                      _depth = depth;
71                  }
72              }
73  
74              StringBundler sb = new StringBundler(13);
75  
76              sb.append(++_nodeId);
77              sb.append("|");
78              sb.append(parentId);
79              sb.append("|");
80  
81              if ((i + 1) == layouts.size()) {
82                  sb.append("1");
83              }
84              else {
85                  sb.append("0");
86              }
87  
88              sb.append("|");
89              sb.append(layout.getPlid());
90              sb.append("|");
91              sb.append(layout.getName(_locale));
92              sb.append("|");
93              //sb.append("9");
94              sb.append("11");
95              sb.append("|");
96              sb.append(depth);
97  
98              _list.add(sb.toString());
99  
100             _createList(layout.getLayoutId(), _nodeId, depth);
101         }
102     }
103 
104     private long _groupId;
105     private boolean _privateLayout;
106     private Locale _locale;
107     private int _nodeId;
108     private List<String> _list;
109     private int _depth;
110 
111 }