1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.util;
21  
22  import com.liferay.portal.PortalException;
23  import com.liferay.portal.SystemException;
24  import com.liferay.portal.model.Layout;
25  import com.liferay.portal.model.LayoutConstants;
26  import com.liferay.portal.service.LayoutLocalServiceUtil;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  import java.util.Locale;
31  
32  /**
33   * <a href="LayoutLister.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   *
37   */
38  public class LayoutLister {
39  
40      public LayoutView getLayoutView(
41              long groupId, boolean privateLayout, String rootNodeName,
42              Locale locale)
43          throws PortalException, SystemException {
44  
45          _groupId = groupId;
46          _privateLayout = privateLayout;
47          _locale = locale;
48          _nodeId = 1;
49  
50          _list = new ArrayList<String>();
51  
52          _list.add(
53              "1|0|0|" + LayoutConstants.DEFAULT_PLID + "|" + rootNodeName +
54                  "|0");
55  
56          _createList(LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, _nodeId, 0);
57  
58          return new LayoutView(_list, _depth);
59      }
60  
61      private void _createList(
62              long parentLayoutId, int parentId, int depth)
63          throws PortalException, SystemException {
64  
65          List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
66              _groupId, _privateLayout, parentLayoutId);
67  
68          for (int i = 0; i < layouts.size(); i++) {
69              Layout layout = layouts.get(i);
70  
71              if (i == 0) {
72                  depth++;
73  
74                  if (depth > _depth) {
75                      _depth = depth;
76                  }
77              }
78  
79              StringBuilder sb = new StringBuilder();
80  
81              sb.append(++_nodeId).append("|");
82              sb.append(parentId).append("|");
83  
84              if ((i + 1) == layouts.size()) {
85                  sb.append("1");
86              }
87              else {
88                  sb.append("0");
89              }
90  
91              sb.append("|");
92              sb.append(layout.getPlid()).append("|");
93              sb.append(layout.getName(_locale)).append("|");
94              //sb.append("9");
95              sb.append("11");
96              sb.append("|");
97              sb.append(depth);
98  
99              _list.add(sb.toString());
100 
101             _createList(layout.getLayoutId(), _nodeId, depth);
102         }
103     }
104 
105     private long _groupId;
106     private boolean _privateLayout;
107     private Locale _locale;
108     private int _nodeId;
109     private List<String> _list;
110     private int _depth;
111 
112 }