001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.templateparser;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.CharPool;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.StringBundler;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.service.LayoutLocalServiceUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.util.ArrayList;
029    import java.util.LinkedHashMap;
030    import java.util.List;
031    import java.util.Map;
032    
033    /**
034     * @author Alexander Chow
035     * @author Raymond Aug??
036     */
037    public class TemplateNode extends LinkedHashMap<String, Object> {
038    
039            public TemplateNode(
040                    ThemeDisplay themeDisplay, String name, String data, String type) {
041    
042                    _themeDisplay = themeDisplay;
043    
044                    put("name", name);
045                    put("data", data);
046                    put("type", type);
047                    put("options", new ArrayList<String>());
048            }
049    
050            public void appendChild(TemplateNode templateNode) {
051                    _childTemplateNodes.put(templateNode.getName(), templateNode);
052    
053                    put(templateNode.getName(), templateNode);
054            }
055    
056            public void appendChildren(List<TemplateNode> templateNodes) {
057                    for (TemplateNode templateNode : templateNodes) {
058                            appendChild(templateNode);
059                    }
060            }
061    
062            public void appendOption(String option) {
063                    List<String> options = getOptions();
064    
065                    options.add(option);
066            }
067    
068            public void appendOptions(List<String> options) {
069                    List<String> curOptions = getOptions();
070    
071                    curOptions.addAll(options);
072            }
073    
074            public void appendSibling(TemplateNode templateNode) {
075                    _siblingTemplateNodes.add(templateNode);
076            }
077    
078            public TemplateNode getChild(String name) {
079                    return _childTemplateNodes.get(name);
080            }
081    
082            public List<TemplateNode> getChildren() {
083                    return new ArrayList<TemplateNode>(_childTemplateNodes.values());
084            }
085    
086            public String getData() {
087                    String type = getType();
088    
089                    if (type.equals("link_to_layout")) {
090                            String data = (String)get("data");
091    
092                            int pos = data.indexOf(CharPool.AT);
093    
094                            if (pos != -1) {
095                                    data = data.substring(0, pos);
096                            }
097    
098                            return data;
099                    }
100                    else {
101                            return (String)get("data");
102                    }
103            }
104    
105            public String getFriendlyUrl() {
106                    if (_themeDisplay == null) {
107                            return getUrl();
108                    }
109    
110                    String type = getType();
111    
112                    if (!type.equals("link_to_layout")) {
113                            return StringPool.BLANK;
114                    }
115    
116                    String layoutType = getLayoutType();
117    
118                    boolean privateLayout = layoutType.startsWith("private");
119    
120                    try {
121                            Layout layout = LayoutLocalServiceUtil.getLayout(
122                                    _themeDisplay.getScopeGroupId(), privateLayout, getLayoutId());
123    
124                            return PortalUtil.getLayoutFriendlyURL(layout, _themeDisplay);
125                    }
126                    catch (Exception e) {
127                            if (_log.isDebugEnabled()) {
128                                    _log.debug(
129                                            "Error finding friendly URL on page " +
130                                                    _themeDisplay.getURLCurrent(),
131                                            e);
132                            }
133    
134                            return getUrl();
135                    }
136            }
137    
138            public String getName() {
139                    return (String)get("name");
140            }
141    
142            public List<String> getOptions() {
143                    return (List<String>)get("options");
144            }
145    
146            public List<TemplateNode> getSiblings() {
147                    return _siblingTemplateNodes;
148            }
149    
150            public String getType() {
151                    return (String)get("type");
152            }
153    
154            public String getUrl() {
155                    String type = getType();
156    
157                    if (!type.equals("link_to_layout")) {
158                            return StringPool.BLANK;
159                    }
160    
161                    StringBundler sb = new StringBundler(5);
162    
163                    String layoutType = getLayoutType();
164    
165                    if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_GROUP)) {
166                            sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
167                    }
168                    else if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_USER)) {
169                            sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
170                    }
171                    else if (layoutType.equals(_LAYOUT_TYPE_PUBLIC)) {
172                            sb.append(PortalUtil.getPathFriendlyURLPublic());
173                    }
174                    else {
175                            sb.append("@friendly_url_current@");
176                    }
177    
178                    sb.append(StringPool.SLASH);
179                    sb.append("@group_id@");
180                    sb.append(StringPool.SLASH);
181                    sb.append(getLayoutId());
182    
183                    return sb.toString();
184            }
185    
186            protected long getLayoutId() {
187                    String data = (String)get("data");
188    
189                    int pos = data.indexOf(CharPool.AT);
190    
191                    if (pos != -1) {
192                            data = data.substring(0, pos);
193                    }
194    
195                    return GetterUtil.getLong(data);
196            }
197    
198            protected String getLayoutType() {
199                    String data = (String)get("data");
200    
201                    int pos = data.indexOf(CharPool.AT);
202    
203                    if (pos != -1) {
204                            data = data.substring(pos + 1);
205                    }
206    
207                    return data;
208            }
209    
210            private static final String _LAYOUT_TYPE_PRIVATE_GROUP = "private-group";
211    
212            private static final String _LAYOUT_TYPE_PRIVATE_USER = "private-user";
213    
214            private static final String _LAYOUT_TYPE_PUBLIC = "public";
215    
216            private static Log _log = LogFactoryUtil.getLog(TemplateNode.class);
217    
218            private Map<String, TemplateNode> _childTemplateNodes =
219                    new LinkedHashMap<String, TemplateNode>();
220            private List<TemplateNode> _siblingTemplateNodes =
221                    new ArrayList<TemplateNode>();
222            private ThemeDisplay _themeDisplay;
223    
224    }