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