1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    *
5    *
6    *
7    * The contents of this file are subject to the terms of the Liferay Enterprise
8    * Subscription License ("License"). You may not use this file except in
9    * compliance with the License. You can obtain a copy of the License by
10   * contacting Liferay, Inc. See the License for the specific language governing
11   * permissions and limitations under the License, including but not limited to
12   * distribution rights 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  public class LayoutTemplateImpl
48       extends PluginBaseImpl implements LayoutTemplate {
49  
50      public LayoutTemplateImpl() {
51      }
52  
53      public LayoutTemplateImpl(String layoutTemplateId) {
54          _layoutTemplateId = layoutTemplateId;
55      }
56  
57      public LayoutTemplateImpl(String layoutTemplateId, String name) {
58          _layoutTemplateId = layoutTemplateId;
59          _name = name;
60      }
61  
62      public String getLayoutTemplateId() {
63          return _layoutTemplateId;
64      }
65  
66      public String getPluginId() {
67          return getLayoutTemplateId();
68      }
69  
70      public String getPluginType() {
71          return Plugin.TYPE_LAYOUT_TEMPLATE;
72      }
73  
74      public boolean getStandard() {
75          return _standard;
76      }
77  
78      public boolean isStandard() {
79          return _standard;
80      }
81  
82      public void setStandard(boolean standard) {
83          _standard = standard;
84      }
85  
86      public String getThemeId() {
87          return _themeId;
88      }
89  
90      public void setThemeId(String themeId) {
91          _themeId = themeId;
92      }
93  
94      public String getName() {
95          if (Validator.isNull(_name)) {
96              return _layoutTemplateId;
97          }
98          else {
99              return _name;
100         }
101     }
102 
103     public void setName(String name) {
104         _name = name;
105     }
106 
107     public String getTemplatePath() {
108         return _templatePath;
109     }
110 
111     public void setTemplatePath(String templatePath) {
112         _templatePath = templatePath;
113     }
114 
115     public String getWapTemplatePath() {
116         return _wapTemplatePath;
117     }
118 
119     public void setWapTemplatePath(String wapTemplatePath) {
120         _wapTemplatePath = wapTemplatePath;
121     }
122 
123     public String getThumbnailPath() {
124         return _thumbnailPath;
125     }
126 
127     public void setThumbnailPath(String thumbnailPath) {
128         _thumbnailPath = thumbnailPath;
129     }
130 
131     public String getContent() {
132         return _content;
133     }
134 
135     public void setContent(String content) {
136         _setContent = true;
137 
138         _content = content;
139     }
140 
141     public boolean hasSetContent() {
142         return _setContent;
143     }
144 
145     public String getUncachedContent() throws IOException {
146         if (_servletContext == null) {
147             if (_log.isDebugEnabled()) {
148                 _log.debug(
149                     "Cannot get latest content for " + _servletContextName +
150                         " " + getTemplatePath() +
151                             " because the servlet context is null");
152             }
153 
154             return _content;
155         }
156 
157         if (_log.isDebugEnabled()) {
158             _log.debug(
159                 "Getting latest content for " + _servletContextName + " " +
160                     getTemplatePath());
161         }
162 
163         String content = HttpUtil.URLtoString(
164             _servletContext.getResource(getTemplatePath()));
165 
166         setContent(content);
167 
168         return content;
169     }
170 
171     public String getWapContent() {
172         return _wapContent;
173     }
174 
175     public void setWapContent(String wapContent) {
176         _setWapContent = true;
177 
178         _wapContent = wapContent;
179     }
180 
181     public boolean hasSetWapContent() {
182         return _setWapContent;
183     }
184 
185     public String getUncachedWapContent() {
186         if (_servletContext == null) {
187             if (_log.isDebugEnabled()) {
188                 _log.debug(
189                     "Cannot get latest WAP content for " + _servletContextName +
190                         " " + getWapTemplatePath() +
191                             " because the servlet context is null");
192             }
193 
194             return _wapContent;
195         }
196 
197         if (_log.isDebugEnabled()) {
198             _log.debug(
199                 "Getting latest WAP content for " + _servletContextName + " " +
200                     getWapTemplatePath());
201         }
202 
203         String wapContent = null;
204 
205         try {
206             wapContent = HttpUtil.URLtoString(
207                 _servletContext.getResource(getWapTemplatePath()));
208         }
209         catch (Exception e) {
210             _log.error(
211                 "Unable to get content at WAP template path " +
212                     getWapTemplatePath() + ": " + e.getMessage());
213         }
214 
215         setWapContent(wapContent);
216 
217         return wapContent;
218     }
219 
220     public List<String> getColumns() {
221         return _columns;
222     }
223 
224     public void setColumns(List<String> columns) {
225         _columns = columns;
226     }
227 
228     public void setServletContext(ServletContext servletContext) {
229         _servletContext = servletContext;
230     }
231 
232     public String getServletContextName() {
233         return _servletContextName;
234     }
235 
236     public void setServletContextName(String servletContextName) {
237         _servletContextName = servletContextName;
238 
239         if (Validator.isNotNull(_servletContextName)) {
240             _warFile = true;
241         }
242         else {
243             _warFile = false;
244         }
245     }
246 
247     public boolean getWARFile() {
248         return _warFile;
249     }
250 
251     public boolean isWARFile() {
252         return _warFile;
253     }
254 
255     public String getContextPath() {
256         if (isWARFile()) {
257             return StringPool.SLASH + getServletContextName();
258         }
259         else {
260             return PortalUtil.getPathContext();
261         }
262     }
263 
264     public int compareTo(LayoutTemplate layoutTemplate) {
265         if (layoutTemplate == null) {
266             return -1;
267         }
268 
269         return getName().compareTo(layoutTemplate.getName());
270     }
271 
272     public boolean equals(LayoutTemplate layoutTemplate) {
273         if (layoutTemplate == null) {
274             return false;
275         }
276 
277         String layoutTemplateId = layoutTemplate.getLayoutTemplateId();
278 
279         if (getLayoutTemplateId().equals(layoutTemplateId)) {
280             return true;
281         }
282         else {
283             return false;
284         }
285     }
286 
287     private static Log _log = LogFactoryUtil.getLog(LayoutTemplateImpl.class);
288 
289     private String _layoutTemplateId;
290     private boolean _standard;
291     private String _themeId;
292     private String _name;
293     private String _templatePath;
294     private String _wapTemplatePath;
295     private String _thumbnailPath;
296     private String _content;
297     private boolean _setContent;
298     private String _wapContent;
299     private boolean _setWapContent;
300     private List<String> _columns = new ArrayList<String>();
301     private transient ServletContext _servletContext;
302     private String _servletContextName = StringPool.BLANK;
303     private boolean _warFile;
304 
305 }