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 getName() {
88          if (Validator.isNull(_name)) {
89              return _layoutTemplateId;
90          }
91          else {
92              return _name;
93          }
94      }
95  
96      public void setName(String name) {
97          _name = name;
98      }
99  
100     public String getTemplatePath() {
101         return _templatePath;
102     }
103 
104     public void setTemplatePath(String templatePath) {
105         _templatePath = templatePath;
106     }
107 
108     public String getWapTemplatePath() {
109         return _wapTemplatePath;
110     }
111 
112     public void setWapTemplatePath(String wapTemplatePath) {
113         _wapTemplatePath = wapTemplatePath;
114     }
115 
116     public String getThumbnailPath() {
117         return _thumbnailPath;
118     }
119 
120     public void setThumbnailPath(String thumbnailPath) {
121         _thumbnailPath = thumbnailPath;
122     }
123 
124     public String getContent() {
125         return _content;
126     }
127 
128     public void setContent(String content) {
129         _setContent = true;
130 
131         _content = content;
132     }
133 
134     public boolean hasSetContent() {
135         return _setContent;
136     }
137 
138     public String getUncachedContent() throws IOException {
139         if (_servletContext == null) {
140             if (_log.isDebugEnabled()) {
141                 _log.debug(
142                     "Cannot get latest content for " + _servletContextName +
143                         " " + getTemplatePath() +
144                             " because the servlet context is null");
145             }
146 
147             return _content;
148         }
149 
150         if (_log.isDebugEnabled()) {
151             _log.debug(
152                 "Getting latest content for " + _servletContextName + " " +
153                     getTemplatePath());
154         }
155 
156         String content = HttpUtil.URLtoString(
157             _servletContext.getResource(getTemplatePath()));
158 
159         setContent(content);
160 
161         return content;
162     }
163 
164     public String getWapContent() {
165         return _wapContent;
166     }
167 
168     public void setWapContent(String wapContent) {
169         _setWapContent = true;
170 
171         _wapContent = wapContent;
172     }
173 
174     public boolean hasSetWapContent() {
175         return _setWapContent;
176     }
177 
178     public String getUncachedWapContent() {
179         if (_servletContext == null) {
180             if (_log.isDebugEnabled()) {
181                 _log.debug(
182                     "Cannot get latest WAP content for " + _servletContextName +
183                         " " + getWapTemplatePath() +
184                             " because the servlet context is null");
185             }
186 
187             return _wapContent;
188         }
189 
190         if (_log.isDebugEnabled()) {
191             _log.debug(
192                 "Getting latest WAP content for " + _servletContextName + " " +
193                     getWapTemplatePath());
194         }
195 
196         String wapContent = null;
197 
198         try {
199             wapContent = HttpUtil.URLtoString(
200                 _servletContext.getResource(getWapTemplatePath()));
201         }
202         catch (Exception e) {
203             _log.error(
204                 "Unable to get content at WAP template path " +
205                     getWapTemplatePath() + ": " + e.getMessage());
206         }
207 
208         setWapContent(wapContent);
209 
210         return wapContent;
211     }
212 
213     public List<String> getColumns() {
214         return _columns;
215     }
216 
217     public void setColumns(List<String> columns) {
218         _columns = columns;
219     }
220 
221     public void setServletContext(ServletContext servletContext) {
222         _servletContext = servletContext;
223     }
224 
225     public String getServletContextName() {
226         return _servletContextName;
227     }
228 
229     public void setServletContextName(String servletContextName) {
230         _servletContextName = servletContextName;
231 
232         if (Validator.isNotNull(_servletContextName)) {
233             _warFile = true;
234         }
235         else {
236             _warFile = false;
237         }
238     }
239 
240     public boolean getWARFile() {
241         return _warFile;
242     }
243 
244     public boolean isWARFile() {
245         return _warFile;
246     }
247 
248     public String getContextPath() {
249         if (isWARFile()) {
250             return StringPool.SLASH + getServletContextName();
251         }
252         else {
253             return PortalUtil.getPathContext();
254         }
255     }
256 
257     public int compareTo(LayoutTemplate layoutTemplate) {
258         if (layoutTemplate == null) {
259             return -1;
260         }
261 
262         return getName().compareTo(layoutTemplate.getName());
263     }
264 
265     public boolean equals(LayoutTemplate layoutTemplate) {
266         if (layoutTemplate == null) {
267             return false;
268         }
269 
270         String layoutTemplateId = layoutTemplate.getLayoutTemplateId();
271 
272         if (getLayoutTemplateId().equals(layoutTemplateId)) {
273             return true;
274         }
275         else {
276             return false;
277         }
278     }
279 
280     private static Log _log = LogFactoryUtil.getLog(LayoutTemplateImpl.class);
281 
282     private String _layoutTemplateId;
283     private boolean _standard;
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;
295     private boolean _warFile;
296 
297 }