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