1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.kernel.log.Log;
18  import com.liferay.portal.kernel.log.LogFactoryUtil;
19  import com.liferay.portal.kernel.util.HttpUtil;
20  import com.liferay.portal.kernel.util.StringPool;
21  import com.liferay.portal.kernel.util.Validator;
22  import com.liferay.portal.model.LayoutTemplate;
23  import com.liferay.portal.model.Plugin;
24  import com.liferay.portal.util.PortalUtil;
25  
26  import java.io.IOException;
27  
28  import java.util.ArrayList;
29  import java.util.List;
30  
31  import javax.servlet.ServletContext;
32  
33  /**
34   * <a href="LayoutTemplateImpl.java.html"><b><i>View Source</i></b></a>
35   *
36   * @author Brian Wing Shun Chan
37   * @author Jorge Ferrer
38   */
39  public class LayoutTemplateImpl
40       extends PluginBaseImpl implements LayoutTemplate {
41  
42      public LayoutTemplateImpl() {
43      }
44  
45      public LayoutTemplateImpl(String layoutTemplateId) {
46          _layoutTemplateId = layoutTemplateId;
47      }
48  
49      public LayoutTemplateImpl(String layoutTemplateId, String name) {
50          _layoutTemplateId = layoutTemplateId;
51          _name = name;
52      }
53  
54      public String getLayoutTemplateId() {
55          return _layoutTemplateId;
56      }
57  
58      public String getPluginId() {
59          return getLayoutTemplateId();
60      }
61  
62      public String getPluginType() {
63          return Plugin.TYPE_LAYOUT_TEMPLATE;
64      }
65  
66      public boolean getStandard() {
67          return _standard;
68      }
69  
70      public boolean isStandard() {
71          return _standard;
72      }
73  
74      public void setStandard(boolean standard) {
75          _standard = standard;
76      }
77  
78      public String getThemeId() {
79          return _themeId;
80      }
81  
82      public void setThemeId(String themeId) {
83          _themeId = themeId;
84      }
85  
86      public String getName() {
87          if (Validator.isNull(_name)) {
88              return _layoutTemplateId;
89          }
90          else {
91              return _name;
92          }
93      }
94  
95      public void setName(String name) {
96          _name = name;
97      }
98  
99      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 StringPool.SLASH + getServletContextName();
250         }
251         else {
252             return PortalUtil.getPathContext();
253         }
254     }
255 
256     public int compareTo(LayoutTemplate layoutTemplate) {
257         if (layoutTemplate == null) {
258             return -1;
259         }
260 
261         return getName().compareTo(layoutTemplate.getName());
262     }
263 
264     public boolean equals(LayoutTemplate layoutTemplate) {
265         if (layoutTemplate == null) {
266             return false;
267         }
268 
269         String layoutTemplateId = layoutTemplate.getLayoutTemplateId();
270 
271         if (getLayoutTemplateId().equals(layoutTemplateId)) {
272             return true;
273         }
274         else {
275             return false;
276         }
277     }
278 
279     private static Log _log = LogFactoryUtil.getLog(LayoutTemplateImpl.class);
280 
281     private String _layoutTemplateId;
282     private boolean _standard;
283     private String _themeId;
284     private String _name;
285     private String _templatePath;
286     private String _wapTemplatePath;
287     private String _thumbnailPath;
288     private String _content;
289     private boolean _setContent;
290     private String _wapContent;
291     private boolean _setWapContent;
292     private List<String> _columns = new ArrayList<String>();
293     private transient ServletContext _servletContext;
294     private String _servletContextName = StringPool.BLANK;
295     private boolean _warFile;
296 
297 }