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