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