1   /**
2    * Copyright (c) 2000-2007 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.util.StringPool;
26  import com.liferay.portal.kernel.util.Validator;
27  import com.liferay.portal.model.LayoutTemplate;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.util.Http;
30  
31  import java.io.IOException;
32  
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  import javax.servlet.ServletContext;
37  
38  import org.apache.commons.logging.Log;
39  import org.apache.commons.logging.LogFactory;
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 static final String PLUGIN_TYPE = "layout-template";
52  
53      public LayoutTemplateImpl() {
54      }
55  
56      public LayoutTemplateImpl(String layoutTemplateId) {
57          _layoutTemplateId = layoutTemplateId;
58      }
59  
60      public LayoutTemplateImpl(String layoutTemplateId, String name) {
61          _layoutTemplateId = layoutTemplateId;
62          _name = name;
63      }
64  
65      public String getLayoutTemplateId() {
66          return _layoutTemplateId;
67      }
68  
69      public String getPluginId() {
70          return getLayoutTemplateId();
71      }
72  
73      public String getPluginType() {
74          return PLUGIN_TYPE;
75      }
76  
77      public boolean getStandard() {
78          return _standard;
79      }
80  
81      public boolean isStandard() {
82          return _standard;
83      }
84  
85      public void setStandard(boolean standard) {
86          _standard = standard;
87      }
88  
89      public String getName() {
90          if (Validator.isNull(_name)) {
91              return _layoutTemplateId;
92          }
93          else {
94              return _name;
95          }
96      }
97  
98      public void setName(String name) {
99          _name = name;
100     }
101 
102     public String getTemplatePath() {
103         return _templatePath;
104     }
105 
106     public void setTemplatePath(String templatePath) {
107         _templatePath = templatePath;
108     }
109 
110     public String getWapTemplatePath() {
111         return _wapTemplatePath;
112     }
113 
114     public void setWapTemplatePath(String wapTemplatePath) {
115         _wapTemplatePath = wapTemplatePath;
116     }
117 
118     public String getThumbnailPath() {
119         return _thumbnailPath;
120     }
121 
122     public void setThumbnailPath(String thumbnailPath) {
123         _thumbnailPath = thumbnailPath;
124     }
125 
126     public String getContent() {
127         return _content;
128     }
129 
130     public void setContent(String content) {
131         _setContent = true;
132 
133         _content = content;
134     }
135 
136     public boolean hasSetContent() {
137         return _setContent;
138     }
139 
140     public String getUncachedContent() throws IOException {
141         if (_ctx == null) {
142             if (_log.isDebugEnabled()) {
143                 _log.debug(
144                     "Cannot get latest content for " + _servletContextName +
145                         " " + getTemplatePath() +
146                             " because the servlet context is null");
147             }
148 
149             return _content;
150         }
151 
152         if (_log.isDebugEnabled()) {
153             _log.debug(
154                 "Getting latest content for " + _servletContextName + " " +
155                     getTemplatePath());
156         }
157 
158         String content = Http.URLtoString(_ctx.getResource(getTemplatePath()));
159 
160         setContent(content);
161 
162         return content;
163     }
164 
165     public String getWapContent() {
166         return _wapContent;
167     }
168 
169     public void setWapContent(String wapContent) {
170         _setWapContent = true;
171 
172         _wapContent = wapContent;
173     }
174 
175     public boolean hasSetWapContent() {
176         return _setWapContent;
177     }
178 
179     public String getUncachedWapContent() throws IOException {
180         if (_ctx == null) {
181             if (_log.isDebugEnabled()) {
182                 _log.debug(
183                     "Cannot get latest WAP content for " + _servletContextName +
184                         " " + getWapTemplatePath() +
185                             " because the servlet context is null");
186             }
187 
188             return _wapContent;
189         }
190 
191         if (_log.isDebugEnabled()) {
192             _log.debug(
193                 "Getting latest WAP content for " + _servletContextName + " " +
194                     getWapTemplatePath());
195         }
196 
197         String wapContent = null;
198 
199         try {
200             wapContent = Http.URLtoString(
201                 _ctx.getResource(getWapTemplatePath()));
202         }
203         catch (Exception e) {
204             _log.error(
205                 "Unable to get content at WAP template path " +
206                     getWapTemplatePath() + ": " + e.getMessage());
207         }
208 
209         setWapContent(wapContent);
210 
211         return wapContent;
212     }
213 
214     public List getColumns() {
215         return _columns;
216     }
217 
218     public void setColumns(List columns) {
219         _columns = columns;
220     }
221 
222     public void setServletContext(ServletContext ctx) {
223         _ctx = ctx;
224     }
225 
226     public String getServletContextName() {
227         return _servletContextName;
228     }
229 
230     public void setServletContextName(String servletContextName) {
231         _servletContextName = servletContextName;
232 
233         if (Validator.isNotNull(_servletContextName)) {
234             _warFile = true;
235         }
236         else {
237             _warFile = false;
238         }
239     }
240 
241     public boolean getWARFile() {
242         return _warFile;
243     }
244 
245     public boolean isWARFile() {
246         return _warFile;
247     }
248 
249     public String getContextPath() {
250         if (isWARFile()) {
251             return StringPool.SLASH + getServletContextName();
252         }
253         else {
254             return PortalUtil.getPathContext();
255         }
256     }
257 
258     public int compareTo(Object obj) {
259         if (obj == null) {
260             return -1;
261         }
262 
263         LayoutTemplate layoutTemplate = (LayoutTemplate) obj;
264 
265         return getName().compareTo(layoutTemplate.getName());
266     }
267 
268     public boolean equals(Object obj) {
269         if (obj == null) {
270             return false;
271         }
272 
273         LayoutTemplate layoutTemplate = null;
274 
275         try {
276             layoutTemplate = (LayoutTemplate)obj;
277         }
278         catch (ClassCastException cce) {
279             return false;
280         }
281 
282         String layoutTemplateId = layoutTemplate.getLayoutTemplateId();
283 
284         if (getLayoutTemplateId().equals(layoutTemplateId)) {
285             return true;
286         }
287         else {
288             return false;
289         }
290     }
291 
292     private static Log _log = LogFactory.getLog(LayoutTemplateImpl.class);
293 
294     private String _layoutTemplateId;
295     private boolean _standard;
296     private String _name;
297     private String _templatePath;
298     private String _wapTemplatePath;
299     private String _thumbnailPath;
300     private String _content;
301     private boolean _setContent;
302     private String _wapContent;
303     private boolean _setWapContent;
304     private List _columns = new ArrayList();
305     private transient ServletContext _ctx;
306     private String _servletContextName;
307     private boolean _warFile;
308 
309 }