1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.model.impl;
24  
25  import com.liferay.portal.kernel.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.HttpUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.Validator;
30  import com.liferay.portal.model.LayoutTemplate;
31  import com.liferay.portal.model.Plugin;
32  import com.liferay.portal.util.PortalUtil;
33  
34  import java.io.IOException;
35  
36  import java.util.ArrayList;
37  import java.util.List;
38  
39  import javax.servlet.ServletContext;
40  
41  /**
42   * <a href="LayoutTemplateImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   * @author Jorge Ferrer
46   *
47   */
48  public class LayoutTemplateImpl
49       extends PluginBaseImpl implements LayoutTemplate {
50  
51      public LayoutTemplateImpl() {
52      }
53  
54      public LayoutTemplateImpl(String layoutTemplateId) {
55          _layoutTemplateId = layoutTemplateId;
56      }
57  
58      public LayoutTemplateImpl(String layoutTemplateId, String name) {
59          _layoutTemplateId = layoutTemplateId;
60          _name = name;
61      }
62  
63      public String getLayoutTemplateId() {
64          return _layoutTemplateId;
65      }
66  
67      public String getPluginId() {
68          return getLayoutTemplateId();
69      }
70  
71      public String getPluginType() {
72          return Plugin.TYPE_LAYOUT_TEMPLATE;
73      }
74  
75      public boolean getStandard() {
76          return _standard;
77      }
78  
79      public boolean isStandard() {
80          return _standard;
81      }
82  
83      public void setStandard(boolean standard) {
84          _standard = standard;
85      }
86  
87      public String getThemeId() {
88          return _themeId;
89      }
90  
91      public void setThemeId(String themeId) {
92          _themeId = themeId;
93      }
94  
95      public String getName() {
96          if (Validator.isNull(_name)) {
97              return _layoutTemplateId;
98          }
99          else {
100             return _name;
101         }
102     }
103 
104     public void setName(String name) {
105         _name = name;
106     }
107 
108     public String getTemplatePath() {
109         return _templatePath;
110     }
111 
112     public void setTemplatePath(String templatePath) {
113         _templatePath = templatePath;
114     }
115 
116     public String getWapTemplatePath() {
117         return _wapTemplatePath;
118     }
119 
120     public void setWapTemplatePath(String wapTemplatePath) {
121         _wapTemplatePath = wapTemplatePath;
122     }
123 
124     public String getThumbnailPath() {
125         return _thumbnailPath;
126     }
127 
128     public void setThumbnailPath(String thumbnailPath) {
129         _thumbnailPath = thumbnailPath;
130     }
131 
132     public String getContent() {
133         return _content;
134     }
135 
136     public void setContent(String content) {
137         _setContent = true;
138 
139         _content = content;
140     }
141 
142     public boolean hasSetContent() {
143         return _setContent;
144     }
145 
146     public String getUncachedContent() throws IOException {
147         if (_servletContext == null) {
148             if (_log.isDebugEnabled()) {
149                 _log.debug(
150                     "Cannot get latest content for " + _servletContextName +
151                         " " + getTemplatePath() +
152                             " because the servlet context is null");
153             }
154 
155             return _content;
156         }
157 
158         if (_log.isDebugEnabled()) {
159             _log.debug(
160                 "Getting latest content for " + _servletContextName + " " +
161                     getTemplatePath());
162         }
163 
164         String content = HttpUtil.URLtoString(
165             _servletContext.getResource(getTemplatePath()));
166 
167         setContent(content);
168 
169         return content;
170     }
171 
172     public String getWapContent() {
173         return _wapContent;
174     }
175 
176     public void setWapContent(String wapContent) {
177         _setWapContent = true;
178 
179         _wapContent = wapContent;
180     }
181 
182     public boolean hasSetWapContent() {
183         return _setWapContent;
184     }
185 
186     public String getUncachedWapContent() {
187         if (_servletContext == null) {
188             if (_log.isDebugEnabled()) {
189                 _log.debug(
190                     "Cannot get latest WAP content for " + _servletContextName +
191                         " " + getWapTemplatePath() +
192                             " because the servlet context is null");
193             }
194 
195             return _wapContent;
196         }
197 
198         if (_log.isDebugEnabled()) {
199             _log.debug(
200                 "Getting latest WAP content for " + _servletContextName + " " +
201                     getWapTemplatePath());
202         }
203 
204         String wapContent = null;
205 
206         try {
207             wapContent = HttpUtil.URLtoString(
208                 _servletContext.getResource(getWapTemplatePath()));
209         }
210         catch (Exception e) {
211             _log.error(
212                 "Unable to get content at WAP template path " +
213                     getWapTemplatePath() + ": " + e.getMessage());
214         }
215 
216         setWapContent(wapContent);
217 
218         return wapContent;
219     }
220 
221     public List<String> getColumns() {
222         return _columns;
223     }
224 
225     public void setColumns(List<String> columns) {
226         _columns = columns;
227     }
228 
229     public void setServletContext(ServletContext servletContext) {
230         _servletContext = servletContext;
231     }
232 
233     public String getServletContextName() {
234         return _servletContextName;
235     }
236 
237     public void setServletContextName(String servletContextName) {
238         _servletContextName = servletContextName;
239 
240         if (Validator.isNotNull(_servletContextName)) {
241             _warFile = true;
242         }
243         else {
244             _warFile = false;
245         }
246     }
247 
248     public boolean getWARFile() {
249         return _warFile;
250     }
251 
252     public boolean isWARFile() {
253         return _warFile;
254     }
255 
256     public String getContextPath() {
257         if (isWARFile()) {
258             return StringPool.SLASH + getServletContextName();
259         }
260         else {
261             return PortalUtil.getPathContext();
262         }
263     }
264 
265     public int compareTo(LayoutTemplate layoutTemplate) {
266         if (layoutTemplate == null) {
267             return -1;
268         }
269 
270         return getName().compareTo(layoutTemplate.getName());
271     }
272 
273     public boolean equals(LayoutTemplate layoutTemplate) {
274         if (layoutTemplate == null) {
275             return false;
276         }
277 
278         String layoutTemplateId = layoutTemplate.getLayoutTemplateId();
279 
280         if (getLayoutTemplateId().equals(layoutTemplateId)) {
281             return true;
282         }
283         else {
284             return false;
285         }
286     }
287 
288     private static Log _log = LogFactoryUtil.getLog(LayoutTemplateImpl.class);
289 
290     private String _layoutTemplateId;
291     private boolean _standard;
292     private String _themeId;
293     private String _name;
294     private String _templatePath;
295     private String _wapTemplatePath;
296     private String _thumbnailPath;
297     private String _content;
298     private boolean _setContent;
299     private String _wapContent;
300     private boolean _setWapContent;
301     private List<String> _columns = new ArrayList<String>();
302     private transient ServletContext _servletContext;
303     private String _servletContextName = StringPool.BLANK;
304     private boolean _warFile;
305 
306 }