001    /**
002     * Copyright (c) 2000-2010 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.portlet.journal.util;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.StringBundler;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.model.Layout;
023    import com.liferay.portal.service.LayoutLocalServiceUtil;
024    import com.liferay.portal.theme.ThemeDisplay;
025    import com.liferay.portal.util.PortalUtil;
026    
027    import java.util.ArrayList;
028    import java.util.LinkedHashMap;
029    import java.util.List;
030    import java.util.Map;
031    
032    /**
033     * @author Alexander Chow
034     * @author Raymond Augé
035     */
036    public class TemplateNode extends LinkedHashMap<String, Object> {
037    
038            public TemplateNode(
039                    ThemeDisplay themeDisplay, String name, String data, String type) {
040    
041                    _themeDisplay = themeDisplay;
042    
043                    put("name", name);
044                    put("data", data);
045                    put("type", type);
046                    put("options", new ArrayList<String>());
047            }
048    
049            public void appendChild(TemplateNode child) {
050                    _children.put(child.getName(), child);
051                    put(child.getName(), child);
052            }
053    
054            public void appendChildren(List<TemplateNode> children) {
055                    for (TemplateNode child : children) {
056                            appendChild(child);
057                    }
058            }
059    
060            public void appendOption(String option) {
061                    getOptions().add(option);
062            }
063    
064            public void appendOptions(List<String> options) {
065                    getOptions().addAll(options);
066            }
067    
068            public void appendSibling(TemplateNode sibling) {
069                    _siblings.add(sibling);
070            }
071    
072            public TemplateNode getChild(String name) {
073                    return _children.get(name);
074            }
075    
076            public List<TemplateNode> getChildren() {
077                    return new ArrayList<TemplateNode>(_children.values());
078            }
079    
080            public String getData() {
081                    if (getType().equals("link_to_layout")) {
082                            String data = (String)get("data");
083    
084                            int pos = data.indexOf(StringPool.AT);
085    
086                            if (pos != -1) {
087                                    data = data.substring(0, pos);
088                            }
089    
090                            return data;
091                    }
092                    else {
093                            return (String)get("data");
094                    }
095            }
096    
097            public String getFriendlyUrl() {
098                    if (_themeDisplay == null) {
099                            return getUrl();
100                    }
101    
102                    if (getType().equals("link_to_layout")) {
103                            String layoutType = getLayoutType();
104    
105                            long layoutId = getLayoutId();
106    
107                            boolean privateLayout = layoutType.startsWith("private");
108    
109                            try {
110                                    Layout layout = LayoutLocalServiceUtil.getLayout(
111                                            _themeDisplay.getScopeGroupId(), privateLayout, layoutId);
112    
113                                    return PortalUtil.getLayoutFriendlyURL(layout, _themeDisplay);
114                            }
115                            catch (Exception e) {
116                                    if (_log.isDebugEnabled()) {
117                                            _log.debug(
118                                                    "Error finding friendly Url on page " +
119                                                            _themeDisplay.getURLCurrent(), e);
120                                    }
121    
122                                    return getUrl();
123                            }
124                    }
125    
126                    return StringPool.BLANK;
127            }
128    
129            public String getName() {
130                    return (String)get("name");
131            }
132    
133            public List<String> getOptions() {
134                    return (List<String>)get("options");
135            }
136    
137            public List<TemplateNode> getSiblings() {
138                    return _siblings;
139            }
140    
141            public String getType() {
142                    return (String)get("type");
143            }
144    
145            public String getUrl() {
146                    if (getType().equals("link_to_layout")) {
147                            StringBundler sb = new StringBundler(5);
148    
149                            String layoutType = getLayoutType();
150    
151                            if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_GROUP)) {
152                                    sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
153                            }
154                            else if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_USER)) {
155                                    sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
156                            }
157                            else if (layoutType.equals(_LAYOUT_TYPE_PUBLIC)) {
158                                    sb.append(PortalUtil.getPathFriendlyURLPublic());
159                            }
160                            else {
161                                    sb.append("@friendly_url_current@");
162                            }
163    
164                            sb.append(StringPool.SLASH);
165                            sb.append("@group_id@");
166                            sb.append(StringPool.SLASH);
167                            sb.append(getLayoutId());
168    
169                            return sb.toString();
170                    }
171    
172                    return StringPool.BLANK;
173            }
174    
175            protected long getLayoutId() {
176                    String data = (String)get("data");
177    
178                    int pos = data.indexOf(StringPool.AT);
179    
180                    if (pos != -1) {
181                            data = data.substring(0, pos);
182                    }
183    
184                    return GetterUtil.getLong(data);
185            }
186    
187            protected String getLayoutType() {
188                    String data = (String)get("data");
189    
190                    int pos = data.indexOf(StringPool.AT);
191    
192                    if (pos != -1) {
193                            data = data.substring(pos + 1);
194                    }
195    
196                    return data;
197            }
198    
199            private static final String _LAYOUT_TYPE_PRIVATE_GROUP = "private-group";
200    
201            private static final String _LAYOUT_TYPE_PRIVATE_USER = "private-user";
202    
203            private static final String _LAYOUT_TYPE_PUBLIC = "public";
204    
205            private static Log _log = LogFactoryUtil.getLog(TemplateNode.class);
206    
207            private Map<String, TemplateNode> _children =
208                    new LinkedHashMap<String, TemplateNode>();
209            private List<TemplateNode> _siblings = new ArrayList<TemplateNode>();
210            private ThemeDisplay _themeDisplay;
211    
212    }