001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.model.impl;
022:
023: import com.liferay.portal.kernel.util.StringPool;
024: import com.liferay.portal.kernel.util.Validator;
025: import com.liferay.portal.model.LayoutTemplate;
026: import com.liferay.portal.util.PortalUtil;
027: import com.liferay.util.Http;
028:
029: import java.io.IOException;
030:
031: import java.util.ArrayList;
032: import java.util.List;
033:
034: import javax.servlet.ServletContext;
035:
036: import org.apache.commons.logging.Log;
037: import org.apache.commons.logging.LogFactory;
038:
039: /**
040: * <a href="LayoutTemplateImpl.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: * @author Jorge Ferrer
044: *
045: */
046: public class LayoutTemplateImpl extends PluginBaseImpl implements
047: LayoutTemplate {
048:
049: public static final String PLUGIN_TYPE = "layout-template";
050:
051: public LayoutTemplateImpl() {
052: }
053:
054: public LayoutTemplateImpl(String layoutTemplateId) {
055: _layoutTemplateId = layoutTemplateId;
056: }
057:
058: public LayoutTemplateImpl(String layoutTemplateId, String name) {
059: _layoutTemplateId = layoutTemplateId;
060: _name = name;
061: }
062:
063: public String getLayoutTemplateId() {
064: return _layoutTemplateId;
065: }
066:
067: public String getPluginId() {
068: return getLayoutTemplateId();
069: }
070:
071: public String getPluginType() {
072: return PLUGIN_TYPE;
073: }
074:
075: public boolean getStandard() {
076: return _standard;
077: }
078:
079: public boolean isStandard() {
080: return _standard;
081: }
082:
083: public void setStandard(boolean standard) {
084: _standard = standard;
085: }
086:
087: public String getName() {
088: if (Validator.isNull(_name)) {
089: return _layoutTemplateId;
090: } else {
091: return _name;
092: }
093: }
094:
095: public void setName(String name) {
096: _name = name;
097: }
098:
099: public String getTemplatePath() {
100: return _templatePath;
101: }
102:
103: public void setTemplatePath(String templatePath) {
104: _templatePath = templatePath;
105: }
106:
107: public String getWapTemplatePath() {
108: return _wapTemplatePath;
109: }
110:
111: public void setWapTemplatePath(String wapTemplatePath) {
112: _wapTemplatePath = wapTemplatePath;
113: }
114:
115: public String getThumbnailPath() {
116: return _thumbnailPath;
117: }
118:
119: public void setThumbnailPath(String thumbnailPath) {
120: _thumbnailPath = thumbnailPath;
121: }
122:
123: public String getContent() {
124: return _content;
125: }
126:
127: public void setContent(String content) {
128: _setContent = true;
129:
130: _content = content;
131: }
132:
133: public boolean hasSetContent() {
134: return _setContent;
135: }
136:
137: public String getUncachedContent() throws IOException {
138: if (_ctx == null) {
139: if (_log.isDebugEnabled()) {
140: _log.debug("Cannot get latest content for "
141: + _servletContextName + " " + getTemplatePath()
142: + " because the servlet context is null");
143: }
144:
145: return _content;
146: }
147:
148: if (_log.isDebugEnabled()) {
149: _log.debug("Getting latest content for "
150: + _servletContextName + " " + getTemplatePath());
151: }
152:
153: String content = Http.URLtoString(_ctx
154: .getResource(getTemplatePath()));
155:
156: setContent(content);
157:
158: return content;
159: }
160:
161: public String getWapContent() {
162: return _wapContent;
163: }
164:
165: public void setWapContent(String wapContent) {
166: _setWapContent = true;
167:
168: _wapContent = wapContent;
169: }
170:
171: public boolean hasSetWapContent() {
172: return _setWapContent;
173: }
174:
175: public String getUncachedWapContent() throws IOException {
176: if (_ctx == null) {
177: if (_log.isDebugEnabled()) {
178: _log.debug("Cannot get latest WAP content for "
179: + _servletContextName + " "
180: + getWapTemplatePath()
181: + " because the servlet context is null");
182: }
183:
184: return _wapContent;
185: }
186:
187: if (_log.isDebugEnabled()) {
188: _log.debug("Getting latest WAP content for "
189: + _servletContextName + " " + getWapTemplatePath());
190: }
191:
192: String wapContent = null;
193:
194: try {
195: wapContent = Http.URLtoString(_ctx
196: .getResource(getWapTemplatePath()));
197: } catch (Exception e) {
198: _log.error("Unable to get content at WAP template path "
199: + getWapTemplatePath() + ": " + e.getMessage());
200: }
201:
202: setWapContent(wapContent);
203:
204: return wapContent;
205: }
206:
207: public List getColumns() {
208: return _columns;
209: }
210:
211: public void setColumns(List columns) {
212: _columns = columns;
213: }
214:
215: public void setServletContext(ServletContext ctx) {
216: _ctx = ctx;
217: }
218:
219: public String getServletContextName() {
220: return _servletContextName;
221: }
222:
223: public void setServletContextName(String servletContextName) {
224: _servletContextName = servletContextName;
225:
226: if (Validator.isNotNull(_servletContextName)) {
227: _warFile = true;
228: } else {
229: _warFile = false;
230: }
231: }
232:
233: public boolean getWARFile() {
234: return _warFile;
235: }
236:
237: public boolean isWARFile() {
238: return _warFile;
239: }
240:
241: public String getContextPath() {
242: if (isWARFile()) {
243: return StringPool.SLASH + getServletContextName();
244: } else {
245: return PortalUtil.getPathContext();
246: }
247: }
248:
249: public int compareTo(Object obj) {
250: if (obj == null) {
251: return -1;
252: }
253:
254: LayoutTemplate layoutTemplate = (LayoutTemplate) obj;
255:
256: return getName().compareTo(layoutTemplate.getName());
257: }
258:
259: public boolean equals(Object obj) {
260: if (obj == null) {
261: return false;
262: }
263:
264: LayoutTemplate layoutTemplate = null;
265:
266: try {
267: layoutTemplate = (LayoutTemplate) obj;
268: } catch (ClassCastException cce) {
269: return false;
270: }
271:
272: String layoutTemplateId = layoutTemplate.getLayoutTemplateId();
273:
274: if (getLayoutTemplateId().equals(layoutTemplateId)) {
275: return true;
276: } else {
277: return false;
278: }
279: }
280:
281: private static Log _log = LogFactory
282: .getLog(LayoutTemplateImpl.class);
283:
284: private String _layoutTemplateId;
285: private boolean _standard;
286: private String _name;
287: private String _templatePath;
288: private String _wapTemplatePath;
289: private String _thumbnailPath;
290: private String _content;
291: private boolean _setContent;
292: private String _wapContent;
293: private boolean _setWapContent;
294: private List _columns = new ArrayList();
295: private transient ServletContext _ctx;
296: private String _servletContextName;
297: private boolean _warFile;
298:
299: }
|