1   /**
2    * Copyright (c) 2000-2008 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.HttpUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.LayoutTemplate;
29  import com.liferay.portal.model.Plugin;
30  import com.liferay.portal.util.PortalUtil;
31  
32  import java.io.IOException;
33  
34  import java.util.ArrayList;
35  import java.util.List;
36  
37  import javax.servlet.ServletContext;
38  
39  import org.apache.commons.logging.Log;
40  import org.apache.commons.logging.LogFactory;
41  
42  /**
43   * <a href="LayoutTemplateImpl.java.html"><b><i>View Source</i></b></a>
44   *
45   * @author Brian Wing Shun Chan
46   * @author Jorge Ferrer
47   *
48   */
49  public class LayoutTemplateImpl
50       extends PluginBaseImpl implements LayoutTemplate {
51  
52      public LayoutTemplateImpl() {
53      }
54  
55      public LayoutTemplateImpl(String layoutTemplateId) {
56          _layoutTemplateId = layoutTemplateId;
57      }
58  
59      public LayoutTemplateImpl(String layoutTemplateId, String name) {
60          _layoutTemplateId = layoutTemplateId;
61          _name = name;
62      }
63  
64      public String getLayoutTemplateId() {
65          return _layoutTemplateId;
66      }
67  
68      public String getPluginId() {
69          return getLayoutTemplateId();
70      }
71  
72      public String getPluginType() {
73          return Plugin.TYPE_LAYOUT_TEMPLATE;
74      }
75  
76      public boolean getStandard() {
77          return _standard;
78      }
79  
80      public boolean isStandard() {
81          return _standard;
82      }
83  
84      public void setStandard(boolean standard) {
85          _standard = standard;
86      }
87  
88      public String getName() {
89          if (Validator.isNull(_name)) {
90              return _layoutTemplateId;
91          }
92          else {
93              return _name;
94          }
95      }
96  
97      public void setName(String name) {
98          _name = name;
99      }
100 
101     public String getTemplatePath() {
102         return _templatePath;
103     }
104 
105     public void setTemplatePath(String templatePath) {
106         _templatePath = templatePath;
107     }
108 
109     public String getWapTemplatePath() {
110         return _wapTemplatePath;
111     }
112 
113     public void setWapTemplatePath(String wapTemplatePath) {
114         _wapTemplatePath = wapTemplatePath;
115     }
116 
117     public String getThumbnailPath() {
118         return _thumbnailPath;
119     }
120 
121     public void setThumbnailPath(String thumbnailPath) {
122         _thumbnailPath = thumbnailPath;
123     }
124 
125     public String getContent() {
126         return _content;
127     }
128 
129     public void setContent(String content) {
130         _setContent = true;
131 
132         _content = content;
133     }
134 
135     public boolean hasSetContent() {
136         return _setContent;
137     }
138 
139     public String getUncachedContent() throws IOException {
140         if (_servletContext == null) {
141             if (_log.isDebugEnabled()) {
142                 _log.debug(
143                     "Cannot get latest content for " + _servletContextName +
144                         " " + getTemplatePath() +
145                             " because the servlet context is null");
146             }
147 
148             return _content;
149         }
150 
151         if (_log.isDebugEnabled()) {
152             _log.debug(
153                 "Getting latest content for " + _servletContextName + " " +
154                     getTemplatePath());
155         }
156 
157         String content = HttpUtil.URLtoString(
158             _servletContext.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() {
180         if (_servletContext == 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 = HttpUtil.URLtoString(
201                 _servletContext.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<String> getColumns() {
215         return _columns;
216     }
217 
218     public void setColumns(List<String> columns) {
219         _columns = columns;
220     }
221 
222     public void setServletContext(ServletContext servletContext) {
223         _servletContext = servletContext;
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(LayoutTemplate layoutTemplate) {
259         if (layoutTemplate == null) {
260             return -1;
261         }
262 
263         return getName().compareTo(layoutTemplate.getName());
264     }
265 
266     public boolean equals(LayoutTemplate layoutTemplate) {
267         if (layoutTemplate == null) {
268             return false;
269         }
270 
271         String layoutTemplateId = layoutTemplate.getLayoutTemplateId();
272 
273         if (getLayoutTemplateId().equals(layoutTemplateId)) {
274             return true;
275         }
276         else {
277             return false;
278         }
279     }
280 
281     private static Log _log = LogFactory.getLog(LayoutTemplateImpl.class);
282 
283     private String _layoutTemplateId;
284     private boolean _standard;
285     private String _name;
286     private String _templatePath;
287     private String _wapTemplatePath;
288     private String _thumbnailPath;
289     private String _content;
290     private boolean _setContent;
291     private String _wapContent;
292     private boolean _setWapContent;
293     private List<String> _columns = new ArrayList<String>();
294     private transient ServletContext _servletContext;
295     private String _servletContextName;
296     private boolean _warFile;
297 
298 }