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