001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.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 child) {
051                    _children.put(child.getName(), child);
052                    put(child.getName(), child);
053            }
054    
055            public void appendChildren(List<TemplateNode> children) {
056                    for (TemplateNode child : children) {
057                            appendChild(child);
058                    }
059            }
060    
061            public void appendOption(String option) {
062                    getOptions().add(option);
063            }
064    
065            public void appendOptions(List<String> options) {
066                    getOptions().addAll(options);
067            }
068    
069            public void appendSibling(TemplateNode sibling) {
070                    _siblings.add(sibling);
071            }
072    
073            public TemplateNode getChild(String name) {
074                    return _children.get(name);
075            }
076    
077            public List<TemplateNode> getChildren() {
078                    return new ArrayList<TemplateNode>(_children.values());
079            }
080    
081            public String getData() {
082                    if (getType().equals("link_to_layout")) {
083                            String data = (String)get("data");
084    
085                            int pos = data.indexOf(CharPool.AT);
086    
087                            if (pos != -1) {
088                                    data = data.substring(0, pos);
089                            }
090    
091                            return data;
092                    }
093                    else {
094                            return (String)get("data");
095                    }
096            }
097    
098            public String getFriendlyUrl() {
099                    if (_themeDisplay == null) {
100                            return getUrl();
101                    }
102    
103                    if (getType().equals("link_to_layout")) {
104                            String layoutType = getLayoutType();
105    
106                            long layoutId = getLayoutId();
107    
108                            boolean privateLayout = layoutType.startsWith("private");
109    
110                            try {
111                                    Layout layout = LayoutLocalServiceUtil.getLayout(
112                                            _themeDisplay.getScopeGroupId(), privateLayout, layoutId);
113    
114                                    return PortalUtil.getLayoutFriendlyURL(layout, _themeDisplay);
115                            }
116                            catch (Exception e) {
117                                    if (_log.isDebugEnabled()) {
118                                            _log.debug(
119                                                    "Error finding friendly Url on page " +
120                                                            _themeDisplay.getURLCurrent(), e);
121                                    }
122    
123                                    return getUrl();
124                            }
125                    }
126    
127                    return StringPool.BLANK;
128            }
129    
130            public String getName() {
131                    return (String)get("name");
132            }
133    
134            public List<String> getOptions() {
135                    return (List<String>)get("options");
136            }
137    
138            public List<TemplateNode> getSiblings() {
139                    return _siblings;
140            }
141    
142            public String getType() {
143                    return (String)get("type");
144            }
145    
146            public String getUrl() {
147                    if (getType().equals("link_to_layout")) {
148                            StringBundler sb = new StringBundler(5);
149    
150                            String layoutType = getLayoutType();
151    
152                            if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_GROUP)) {
153                                    sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
154                            }
155                            else if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_USER)) {
156                                    sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
157                            }
158                            else if (layoutType.equals(_LAYOUT_TYPE_PUBLIC)) {
159                                    sb.append(PortalUtil.getPathFriendlyURLPublic());
160                            }
161                            else {
162                                    sb.append("@friendly_url_current@");
163                            }
164    
165                            sb.append(StringPool.SLASH);
166                            sb.append("@group_id@");
167                            sb.append(StringPool.SLASH);
168                            sb.append(getLayoutId());
169    
170                            return sb.toString();
171                    }
172    
173                    return StringPool.BLANK;
174            }
175    
176            protected long getLayoutId() {
177                    String data = (String)get("data");
178    
179                    int pos = data.indexOf(CharPool.AT);
180    
181                    if (pos != -1) {
182                            data = data.substring(0, pos);
183                    }
184    
185                    return GetterUtil.getLong(data);
186            }
187    
188            protected String getLayoutType() {
189                    String data = (String)get("data");
190    
191                    int pos = data.indexOf(CharPool.AT);
192    
193                    if (pos != -1) {
194                            data = data.substring(pos + 1);
195                    }
196    
197                    return data;
198            }
199    
200            private static final String _LAYOUT_TYPE_PRIVATE_GROUP = "private-group";
201    
202            private static final String _LAYOUT_TYPE_PRIVATE_USER = "private-user";
203    
204            private static final String _LAYOUT_TYPE_PUBLIC = "public";
205    
206            private static Log _log = LogFactoryUtil.getLog(TemplateNode.class);
207    
208            private Map<String, TemplateNode> _children =
209                    new LinkedHashMap<String, TemplateNode>();
210            private List<TemplateNode> _siblings = new ArrayList<TemplateNode>();
211            private ThemeDisplay _themeDisplay;
212    
213    }