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