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.kernel.util.Validator;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.service.GroupLocalServiceUtil;
027    import com.liferay.portal.service.LayoutLocalServiceUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.PortalUtil;
030    
031    import java.util.ArrayList;
032    import java.util.LinkedHashMap;
033    import java.util.List;
034    import java.util.Map;
035    
036    /**
037     * @author Alexander Chow
038     * @author Raymond Aug??
039     */
040    public class TemplateNode extends LinkedHashMap<String, Object> {
041    
042            public TemplateNode(
043                    ThemeDisplay themeDisplay, String name, String data, String type) {
044    
045                    _themeDisplay = themeDisplay;
046    
047                    put("name", name);
048                    put("data", data);
049                    put("type", type);
050                    put("options", new ArrayList<String>());
051            }
052    
053            public void appendChild(TemplateNode templateNode) {
054                    _childTemplateNodes.put(templateNode.getName(), templateNode);
055    
056                    put(templateNode.getName(), templateNode);
057            }
058    
059            public void appendChildren(List<TemplateNode> templateNodes) {
060                    for (TemplateNode templateNode : templateNodes) {
061                            appendChild(templateNode);
062                    }
063            }
064    
065            public void appendOption(String option) {
066                    List<String> options = getOptions();
067    
068                    options.add(option);
069            }
070    
071            public void appendOptions(List<String> options) {
072                    List<String> curOptions = getOptions();
073    
074                    curOptions.addAll(options);
075            }
076    
077            public void appendSibling(TemplateNode templateNode) {
078                    _siblingTemplateNodes.add(templateNode);
079            }
080    
081            public TemplateNode getChild(String name) {
082                    return _childTemplateNodes.get(name);
083            }
084    
085            public List<TemplateNode> getChildren() {
086                    return new ArrayList<TemplateNode>(_childTemplateNodes.values());
087            }
088    
089            public String getData() {
090                    String type = getType();
091    
092                    if (type.equals("link_to_layout")) {
093                            String data = (String)get("data");
094    
095                            int pos = data.indexOf(CharPool.AT);
096    
097                            if (pos != -1) {
098                                    data = data.substring(0, pos);
099                            }
100    
101                            return data;
102                    }
103                    else {
104                            return (String)get("data");
105                    }
106            }
107    
108            public String getFriendlyUrl() {
109                    if (_themeDisplay == null) {
110                            return getUrl();
111                    }
112    
113                    String type = getType();
114    
115                    if (!type.equals("link_to_layout")) {
116                            return StringPool.BLANK;
117                    }
118    
119                    String layoutType = getLayoutType();
120    
121                    if (Validator.isNull(layoutType)) {
122                            return StringPool.BLANK;
123                    }
124    
125                    long groupId = getLayoutGroupId();
126    
127                    if (groupId == 0) {
128                            groupId = _themeDisplay.getScopeGroupId();
129                    }
130    
131                    boolean privateLayout = layoutType.startsWith("private");
132    
133                    try {
134                            Layout layout = LayoutLocalServiceUtil.getLayout(
135                                    groupId, privateLayout, getLayoutId());
136    
137                            return PortalUtil.getLayoutFriendlyURL(layout, _themeDisplay);
138                    }
139                    catch (Exception e) {
140                            if (_log.isDebugEnabled()) {
141                                    _log.debug(
142                                            "Error finding friendly URL on page " +
143                                                    _themeDisplay.getURLCurrent(),
144                                            e);
145                            }
146    
147                            return getUrl();
148                    }
149            }
150    
151            public String getName() {
152                    return (String)get("name");
153            }
154    
155            public List<String> getOptions() {
156                    return (List<String>)get("options");
157            }
158    
159            public List<TemplateNode> getSiblings() {
160                    return _siblingTemplateNodes;
161            }
162    
163            public String getType() {
164                    return (String)get("type");
165            }
166    
167            public String getUrl() {
168                    String type = getType();
169    
170                    if (!type.equals("link_to_layout")) {
171                            return StringPool.BLANK;
172                    }
173    
174                    StringBundler sb = new StringBundler(5);
175    
176                    String layoutType = getLayoutType();
177    
178                    if (Validator.isNull(layoutType)) {
179                            return StringPool.BLANK;
180                    }
181    
182                    if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_GROUP)) {
183                            sb.append(PortalUtil.getPathFriendlyURLPrivateGroup());
184                    }
185                    else if (layoutType.equals(_LAYOUT_TYPE_PRIVATE_USER)) {
186                            sb.append(PortalUtil.getPathFriendlyURLPrivateUser());
187                    }
188                    else if (layoutType.equals(_LAYOUT_TYPE_PUBLIC)) {
189                            sb.append(PortalUtil.getPathFriendlyURLPublic());
190                    }
191                    else {
192                            sb.append("@friendly_url_current@");
193                    }
194    
195                    sb.append(StringPool.SLASH);
196    
197                    try {
198                            Group group = GroupLocalServiceUtil.getGroup(getLayoutGroupId());
199    
200                            String name = group.getFriendlyURL();
201    
202                            name = name.substring(1);
203    
204                            sb.append(name);
205                    }
206                    catch (Exception e) {
207                            sb.append("@group_id@");
208                    }
209    
210                    sb.append(StringPool.SLASH);
211                    sb.append(getLayoutId());
212    
213                    return sb.toString();
214            }
215    
216            protected long getLayoutGroupId() {
217                    String data = (String)get("data");
218    
219                    int pos = data.lastIndexOf(CharPool.AT);
220    
221                    if (pos != -1) {
222                            data = data.substring(pos + 1);
223                    }
224    
225                    return GetterUtil.getLong(data);
226            }
227    
228            protected long getLayoutId() {
229                    String data = (String)get("data");
230    
231                    int pos = data.indexOf(CharPool.AT);
232    
233                    if (pos != -1) {
234                            data = data.substring(0, pos);
235                    }
236    
237                    return GetterUtil.getLong(data);
238            }
239    
240            protected String getLayoutType() {
241                    String data = (String)get("data");
242    
243                    int x = data.indexOf(CharPool.AT);
244                    int y = data.lastIndexOf(CharPool.AT);
245    
246                    if ((x != -1) && (y != -1)) {
247                            if (x == y) {
248                                    data = data.substring(x + 1);
249                            }
250                            else {
251                                    data = data.substring(x + 1, y);
252                            }
253                    }
254    
255                    return data;
256            }
257    
258            private static final String _LAYOUT_TYPE_PRIVATE_GROUP = "private-group";
259    
260            private static final String _LAYOUT_TYPE_PRIVATE_USER = "private-user";
261    
262            private static final String _LAYOUT_TYPE_PUBLIC = "public";
263    
264            private static Log _log = LogFactoryUtil.getLog(TemplateNode.class);
265    
266            private Map<String, TemplateNode> _childTemplateNodes =
267                    new LinkedHashMap<String, TemplateNode>();
268            private List<TemplateNode> _siblingTemplateNodes =
269                    new ArrayList<TemplateNode>();
270            private ThemeDisplay _themeDisplay;
271    
272    }