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