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