1   /**
2    * Copyright (c) 2000-2010 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   *
12   *
13   */
14  
15  package com.liferay.portal.util;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.util.ParamUtil;
20  import com.liferay.portal.kernel.util.StringUtil;
21  import com.liferay.portal.model.Layout;
22  import com.liferay.portal.model.LayoutConstants;
23  import com.liferay.portal.service.LayoutLocalServiceUtil;
24  
25  import java.util.ArrayList;
26  import java.util.Arrays;
27  import java.util.List;
28  import java.util.Locale;
29  
30  import javax.servlet.http.HttpServletRequest;
31  
32  /**
33   * <a href="LayoutLister.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class LayoutLister {
38  
39      public LayoutView getLayoutView(
40              long groupId, boolean privateLayout, String rootNodeName,
41              Locale locale)
42          throws PortalException, SystemException {
43  
44          return getLayoutView(
45              null, groupId, privateLayout, rootNodeName, locale);
46      }
47  
48      public LayoutView getLayoutView(
49              HttpServletRequest request, long groupId, boolean privateLayout,
50              String rootNodeName,Locale locale)
51          throws PortalException, SystemException {
52  
53          _request = request;
54          _groupId = groupId;
55          _privateLayout = privateLayout;
56          _locale = locale;
57          _nodeId = 1;
58  
59          _list = new ArrayList<String>();
60  
61          _list.add(
62              "1|0|0|" + LayoutConstants.DEFAULT_PLID + "|" + rootNodeName +
63                  "|0");
64  
65          _createList(LayoutConstants.DEFAULT_PARENT_LAYOUT_ID, _nodeId, 0);
66  
67          return new LayoutView(_list, _depth);
68      }
69  
70      private void _createList(long parentLayoutId, int parentId, int depth)
71          throws PortalException, SystemException {
72  
73          List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
74              _groupId, _privateLayout, parentLayoutId);
75  
76          for (int i = 0; i < layouts.size(); i++) {
77              Layout layout = layouts.get(i);
78  
79              if (i == 0) {
80                  depth++;
81  
82                  if (depth > _depth) {
83                      _depth = depth;
84                  }
85              }
86  
87              StringBuilder sb = new StringBuilder();
88  
89              sb.append(++_nodeId).append("|");
90              sb.append(parentId).append("|");
91  
92              if ((i + 1) == layouts.size()) {
93                  sb.append("1");
94              }
95              else {
96                  sb.append("0");
97              }
98  
99              sb.append("|");
100             sb.append(layout.getPlid()).append("|");
101             sb.append(layout.getName(_locale)).append("|");
102             //sb.append("9");
103             sb.append("11");
104             sb.append("|");
105             sb.append(depth);
106 
107             _list.add(sb.toString());
108 
109             long[] openNodes = new long[0];
110 
111             if (_request != null) {
112                 String treeId = ParamUtil.getString(_request, "treeId");
113 
114                 openNodes = StringUtil.split(
115                     SessionTreeJSClicks.getOpenNodes(_request, treeId), 0L);
116             }
117 
118             if ((_request == null) ||
119                 (Arrays.binarySearch(openNodes, _nodeId) >= 0)) {
120 
121                 _createList(layout.getLayoutId(), _nodeId, depth);
122             }
123         }
124     }
125 
126     private HttpServletRequest _request;
127     private long _groupId;
128     private boolean _privateLayout;
129     private Locale _locale;
130     private int _nodeId;
131     private List<String> _list;
132     private int _depth;
133 
134 }