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