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