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.portlet.journal.util;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.GetterUtil;
20  import com.liferay.portal.kernel.util.StringBundler;
21  import com.liferay.portal.kernel.util.StringPool;
22  import com.liferay.portal.model.Layout;
23  import com.liferay.portal.service.LayoutLocalServiceUtil;
24  import com.liferay.portal.theme.ThemeDisplay;
25  import com.liferay.portal.util.PortalUtil;
26  
27  import java.util.ArrayList;
28  import java.util.LinkedHashMap;
29  import java.util.List;
30  import java.util.Map;
31  
32  /**
33   * <a href="TemplateNode.java.html"><b><i>View Source</i></b></a>
34   *
35   * @author Alexander Chow
36   * @author Raymond Augé
37   */
38  public class TemplateNode extends LinkedHashMap<String, Object> {
39  
40      public TemplateNode(
41          ThemeDisplay themeDisplay, String name, String data, String type) {
42  
43          _themeDisplay = themeDisplay;
44  
45          put("name", name);
46          put("data", data);
47          put("type", type);
48          put("options", new ArrayList<String>());
49      }
50  
51      public void appendChild(TemplateNode child) {
52          _children.put(child.getName(), child);
53          put(child.getName(), child);
54      }
55  
56      public void appendChildren(List<TemplateNode> children) {
57          for (TemplateNode child : children) {
58              appendChild(child);
59          }
60      }
61  
62      public void appendOption(String option) {
63          getOptions().add(option);
64      }
65  
66      public void appendOptions(List<String> options) {
67          getOptions().addAll(options);
68      }
69  
70      public void appendSibling(TemplateNode sibling) {
71          _siblings.add(sibling);
72      }
73  
74      public TemplateNode getChild(String name) {
75          return _children.get(name);
76      }
77  
78      public List<TemplateNode> getChildren() {
79          return new ArrayList<TemplateNode>(_children.values());
80      }
81  
82      public String getData() {
83          if (getType().equals("link_to_layout")) {
84              String data = (String)get("data");
85  
86              int pos = data.indexOf(StringPool.AT);
87  
88              if (pos != -1) {
89                  data = data.substring(0, pos);
90              }
91  
92              return data;
93          }
94          else {
95              return (String)get("data");
96          }
97      }
98  
99      public String getFriendlyUrl() {
100         if (_themeDisplay == null) {
101             return getUrl();
102         }
103 
104         if (getType().equals("link_to_layout")) {
105             String layoutType = getLayoutType();
106 
107             long layoutId = getLayoutId();
108 
109             boolean privateLayout = layoutType.startsWith("private");
110 
111             try {
112                 Layout layout = LayoutLocalServiceUtil.getLayout(
113                     _themeDisplay.getScopeGroupId(), privateLayout, layoutId);
114 
115                 return PortalUtil.getLayoutFriendlyURL(layout, _themeDisplay);
116             }
117             catch (Exception e) {
118                 if (_log.isDebugEnabled()) {
119                     _log.debug(
120                         "Error finding friendly Url on page " +
121                             _themeDisplay.getURLCurrent(), e);
122                 }
123 
124                 return getUrl();
125             }
126         }
127 
128         return StringPool.BLANK;
129     }
130 
131     public String getName() {
132         return (String)get("name");
133     }
134 
135     public List<String> getOptions() {
136         return (List<String>)get("options");
137     }
138 
139     public List<TemplateNode> getSiblings() {
140         return _siblings;
141     }
142 
143     public String getType() {
144         return (String)get("type");
145     }
146 
147     public String getUrl() {
148         if (getType().equals("link_to_layout")) {
149             StringBundler sb = new StringBundler(5);
150 
151             String layoutType = getLayoutType();
152 
153             if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_GROUP)) {
154                 sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
155             }
156             else if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_USER)) {
157                 sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
158             }
159             else if (layoutType.equals(_LAYOUT_TYPE_PUBLIC)) {
160                 sb.append(PortalUtil.getPathFriendlyURLPublic());
161             }
162             else {
163                 sb.append("@friendly_url_current@");
164             }
165 
166             sb.append(StringPool.SLASH);
167             sb.append("@group_id@");
168             sb.append(StringPool.SLASH);
169             sb.append(getLayoutId());
170 
171             return sb.toString();
172         }
173 
174         return StringPool.BLANK;
175     }
176 
177     protected long getLayoutId() {
178         String data = (String)get("data");
179 
180         int pos = data.indexOf(StringPool.AT);
181 
182         if (pos != -1) {
183             data = data.substring(0, pos);
184         }
185 
186         return GetterUtil.getLong(data);
187     }
188 
189     protected String getLayoutType() {
190         String data = (String)get("data");
191 
192         int pos = data.indexOf(StringPool.AT);
193 
194         if (pos != -1) {
195             data = data.substring(pos + 1);
196         }
197 
198         return data;
199     }
200 
201     private static final String _LAYOUT_TYPE_PRIVATE_GROUP = "private-group";
202 
203     private static final String _LAYOUT_TYPE_PRIVATE_USER = "private-user";
204 
205     private static final String _LAYOUT_TYPE_PUBLIC = "public";
206 
207     private static Log _log = LogFactoryUtil.getLog(TemplateNode.class);
208 
209     private Map<String, TemplateNode> _children =
210         new LinkedHashMap<String, TemplateNode>();
211     private List<TemplateNode> _siblings = new ArrayList<TemplateNode>();
212     private ThemeDisplay _themeDisplay;
213 
214 }