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