001    /**
002     * Copyright (c) 2000-2012 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.model.impl;
016    
017    import com.liferay.portal.kernel.log.Log;
018    import com.liferay.portal.kernel.log.LogFactoryUtil;
019    import com.liferay.portal.kernel.servlet.ServletContextPool;
020    import com.liferay.portal.kernel.util.ContextPathUtil;
021    import com.liferay.portal.kernel.util.HttpUtil;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.portal.model.LayoutTemplate;
025    import com.liferay.portal.model.Plugin;
026    import com.liferay.portal.util.PortalUtil;
027    
028    import java.io.IOException;
029    
030    import java.util.ArrayList;
031    import java.util.List;
032    
033    import javax.servlet.ServletContext;
034    
035    /**
036     * @author Brian Wing Shun Chan
037     * @author Jorge Ferrer
038     */
039    public class LayoutTemplateImpl
040             extends PluginBaseImpl implements LayoutTemplate {
041    
042            public LayoutTemplateImpl() {
043            }
044    
045            public LayoutTemplateImpl(String layoutTemplateId) {
046                    _layoutTemplateId = layoutTemplateId;
047            }
048    
049            public LayoutTemplateImpl(String layoutTemplateId, String name) {
050                    _layoutTemplateId = layoutTemplateId;
051                    _name = name;
052            }
053    
054            public int compareTo(LayoutTemplate layoutTemplate) {
055                    if (layoutTemplate == null) {
056                            return -1;
057                    }
058    
059                    return getName().compareTo(layoutTemplate.getName());
060            }
061    
062            public boolean equals(LayoutTemplate layoutTemplate) {
063                    if (layoutTemplate == null) {
064                            return false;
065                    }
066    
067                    String layoutTemplateId = layoutTemplate.getLayoutTemplateId();
068    
069                    if (getLayoutTemplateId().equals(layoutTemplateId)) {
070                            return true;
071                    }
072                    else {
073                            return false;
074                    }
075            }
076    
077            public List<String> getColumns() {
078                    return _columns;
079            }
080    
081            public String getContent() {
082                    return _content;
083            }
084    
085            public String getContextPath() {
086                    if (!isWARFile()) {
087                            return PortalUtil.getPathContext();
088                    }
089    
090                    String servletContextName = getServletContextName();
091    
092                    if (ServletContextPool.containsKey(servletContextName)) {
093                            ServletContext servletContext = ServletContextPool.get(
094                                    servletContextName);
095    
096                            return ContextPathUtil.getContextPath(servletContext);
097                    }
098    
099                    return StringPool.SLASH.concat(servletContextName);
100            }
101    
102            public String getLayoutTemplateId() {
103                    return _layoutTemplateId;
104            }
105    
106            public String getName() {
107                    if (Validator.isNull(_name)) {
108                            return _layoutTemplateId;
109                    }
110                    else {
111                            return _name;
112                    }
113            }
114    
115            public String getPluginId() {
116                    return getLayoutTemplateId();
117            }
118    
119            public String getPluginType() {
120                    return Plugin.TYPE_LAYOUT_TEMPLATE;
121            }
122    
123            public String getServletContextName() {
124                    return _servletContextName;
125            }
126    
127            public boolean getStandard() {
128                    return _standard;
129            }
130    
131            public String getStaticResourcePath() {
132                    String proxyPath = PortalUtil.getPathProxy();
133    
134                    String contextPath = getContextPath();
135    
136                    if (!isWARFile()) {
137                            return contextPath;
138                    }
139    
140                    return proxyPath.concat(contextPath);
141            }
142    
143            public String getTemplatePath() {
144                    return _templatePath;
145            }
146    
147            public String getThemeId() {
148                    return _themeId;
149            }
150    
151            public String getThumbnailPath() {
152                    return _thumbnailPath;
153            }
154    
155            public String getUncachedContent() throws IOException {
156                    if (_servletContext == null) {
157                            if (_log.isDebugEnabled()) {
158                                    _log.debug(
159                                            "Cannot get latest content for " + _servletContextName +
160                                                    " " + getTemplatePath() +
161                                                            " because the servlet context is null");
162                            }
163    
164                            return _content;
165                    }
166    
167                    if (_log.isDebugEnabled()) {
168                            _log.debug(
169                                    "Getting latest content for " + _servletContextName + " " +
170                                            getTemplatePath());
171                    }
172    
173                    String content = HttpUtil.URLtoString(
174                            _servletContext.getResource(getTemplatePath()));
175    
176                    setContent(content);
177    
178                    return content;
179            }
180    
181            public String getUncachedWapContent() {
182                    if (_servletContext == null) {
183                            if (_log.isDebugEnabled()) {
184                                    _log.debug(
185                                            "Cannot get latest WAP content for " + _servletContextName +
186                                                    " " + getWapTemplatePath() +
187                                                            " because the servlet context is null");
188                            }
189    
190                            return _wapContent;
191                    }
192    
193                    if (_log.isDebugEnabled()) {
194                            _log.debug(
195                                    "Getting latest WAP content for " + _servletContextName + " " +
196                                            getWapTemplatePath());
197                    }
198    
199                    String wapContent = null;
200    
201                    try {
202                            wapContent = HttpUtil.URLtoString(
203                                    _servletContext.getResource(getWapTemplatePath()));
204                    }
205                    catch (Exception e) {
206                            _log.error(
207                                    "Unable to get content at WAP template path " +
208                                            getWapTemplatePath() + ": " + e.getMessage());
209                    }
210    
211                    setWapContent(wapContent);
212    
213                    return wapContent;
214            }
215    
216            public String getWapContent() {
217                    return _wapContent;
218            }
219    
220            public String getWapTemplatePath() {
221                    return _wapTemplatePath;
222            }
223    
224            public boolean getWARFile() {
225                    return _warFile;
226            }
227    
228            public boolean hasSetContent() {
229                    return _setContent;
230            }
231    
232            public boolean hasSetWapContent() {
233                    return _setWapContent;
234            }
235    
236            public boolean isStandard() {
237                    return _standard;
238            }
239    
240            public boolean isWARFile() {
241                    return _warFile;
242            }
243    
244            public void setColumns(List<String> columns) {
245                    _columns = columns;
246            }
247    
248            public void setContent(String content) {
249                    _setContent = true;
250    
251                    _content = content;
252            }
253    
254            public void setName(String name) {
255                    _name = name;
256            }
257    
258            public void setServletContext(ServletContext servletContext) {
259                    _servletContext = servletContext;
260            }
261    
262            public void setServletContextName(String servletContextName) {
263                    _servletContextName = servletContextName;
264    
265                    if (Validator.isNotNull(_servletContextName)) {
266                            _warFile = true;
267                    }
268                    else {
269                            _warFile = false;
270                    }
271            }
272    
273            public void setStandard(boolean standard) {
274                    _standard = standard;
275            }
276    
277            public void setTemplatePath(String templatePath) {
278                    _templatePath = templatePath;
279            }
280    
281            public void setThemeId(String themeId) {
282                    _themeId = themeId;
283            }
284    
285            public void setThumbnailPath(String thumbnailPath) {
286                    _thumbnailPath = thumbnailPath;
287            }
288    
289            public void setWapContent(String wapContent) {
290                    _setWapContent = true;
291    
292                    _wapContent = wapContent;
293            }
294    
295            public void setWapTemplatePath(String wapTemplatePath) {
296                    _wapTemplatePath = wapTemplatePath;
297            }
298    
299            private static Log _log = LogFactoryUtil.getLog(LayoutTemplateImpl.class);
300    
301            private List<String> _columns = new ArrayList<String>();
302            private String _content;
303            private String _layoutTemplateId;
304            private String _name;
305            private transient ServletContext _servletContext;
306            private String _servletContextName = StringPool.BLANK;
307            private boolean _setContent;
308            private boolean _setWapContent;
309            private boolean _standard;
310            private String _templatePath;
311            private String _themeId;
312            private String _thumbnailPath;
313            private String _wapContent;
314            private String _wapTemplatePath;
315            private boolean _warFile;
316    
317    }