001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.model.LayoutTemplate;
021    import com.liferay.portal.kernel.model.Plugin;
022    import com.liferay.portal.kernel.servlet.ServletContextPool;
023    import com.liferay.portal.kernel.util.CharPool;
024    import com.liferay.portal.kernel.util.HttpUtil;
025    import com.liferay.portal.kernel.util.LocaleUtil;
026    import com.liferay.portal.kernel.util.PortalUtil;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    
031    import java.io.IOException;
032    
033    import java.util.ArrayList;
034    import java.util.List;
035    import java.util.Locale;
036    
037    import javax.servlet.ServletContext;
038    
039    /**
040     * @author Brian Wing Shun Chan
041     * @author Jorge Ferrer
042     */
043    public class LayoutTemplateImpl
044            extends PluginBaseImpl implements LayoutTemplate {
045    
046            public LayoutTemplateImpl() {
047                    this(null, null);
048            }
049    
050            public LayoutTemplateImpl(String layoutTemplateId) {
051                    this(layoutTemplateId, null);
052            }
053    
054            public LayoutTemplateImpl(String layoutTemplateId, String name) {
055                    _layoutTemplateId = layoutTemplateId;
056                    _name = name;
057            }
058    
059            @Override
060            public int compareTo(LayoutTemplate layoutTemplate) {
061                    if (layoutTemplate == null) {
062                            return -1;
063                    }
064    
065                    return getName().compareTo(layoutTemplate.getName());
066            }
067    
068            public boolean equals(LayoutTemplate layoutTemplate) {
069                    if (layoutTemplate == null) {
070                            return false;
071                    }
072    
073                    String layoutTemplateId = layoutTemplate.getLayoutTemplateId();
074    
075                    if (getLayoutTemplateId().equals(layoutTemplateId)) {
076                            return true;
077                    }
078                    else {
079                            return false;
080                    }
081            }
082    
083            @Override
084            public List<String> getColumns() {
085                    return _columns;
086            }
087    
088            @Override
089            public String getContent() {
090                    return _content;
091            }
092    
093            @Override
094            public String getContextPath() {
095                    if (!isWARFile()) {
096                            return PortalUtil.getPathContext();
097                    }
098    
099                    String servletContextName = getServletContextName();
100    
101                    if (ServletContextPool.containsKey(servletContextName)) {
102                            ServletContext servletContext = ServletContextPool.get(
103                                    servletContextName);
104    
105                            return servletContext.getContextPath();
106                    }
107    
108                    return StringPool.SLASH.concat(servletContextName);
109            }
110    
111            @Override
112            public String getLayoutTemplateId() {
113                    return _layoutTemplateId;
114            }
115    
116            @Override
117            public String getName() {
118                    return getName(LocaleUtil.getDefault());
119            }
120    
121            @Override
122            public String getName(Locale locale) {
123                    if (Validator.isNotNull(_name)) {
124                            return _name;
125                    }
126    
127                    String layoutTemplateId = StringUtil.replace(
128                            _layoutTemplateId, CharPool.UNDERLINE, CharPool.DASH);
129    
130                    return LanguageUtil.get(locale, "layout-template-" + layoutTemplateId);
131            }
132    
133            @Override
134            public String getPluginId() {
135                    return getLayoutTemplateId();
136            }
137    
138            @Override
139            public String getPluginType() {
140                    return Plugin.TYPE_LAYOUT_TEMPLATE;
141            }
142    
143            @Override
144            public String getServletContextName() {
145                    return _servletContextName;
146            }
147    
148            @Override
149            public boolean getStandard() {
150                    return _standard;
151            }
152    
153            @Override
154            public String getStaticResourcePath() {
155                    String proxyPath = PortalUtil.getPathProxy();
156    
157                    String contextPath = getContextPath();
158    
159                    if (!isWARFile()) {
160                            return contextPath;
161                    }
162    
163                    return proxyPath.concat(contextPath);
164            }
165    
166            @Override
167            public String getTemplatePath() {
168                    return _templatePath;
169            }
170    
171            @Override
172            public String getThemeId() {
173                    return _themeId;
174            }
175    
176            @Override
177            public String getThumbnailPath() {
178                    return _thumbnailPath;
179            }
180    
181            @Override
182            public String getUncachedContent() throws IOException {
183                    if (_servletContext == null) {
184                            if (_log.isDebugEnabled()) {
185                                    _log.debug(
186                                            "Cannot get latest content for " + _servletContextName +
187                                                    " " + getTemplatePath() +
188                                                            " because the servlet context is null");
189                            }
190    
191                            return _content;
192                    }
193    
194                    if (_log.isDebugEnabled()) {
195                            _log.debug(
196                                    "Getting latest content for " + _servletContextName + " " +
197                                            getTemplatePath());
198                    }
199    
200                    String content = HttpUtil.URLtoString(
201                            _servletContext.getResource(getTemplatePath()));
202    
203                    setContent(content);
204    
205                    return content;
206            }
207    
208            @Override
209            public boolean getWARFile() {
210                    return _warFile;
211            }
212    
213            @Override
214            public boolean hasSetContent() {
215                    return _setContent;
216            }
217    
218            @Override
219            public boolean isStandard() {
220                    return _standard;
221            }
222    
223            @Override
224            public boolean isWARFile() {
225                    return _warFile;
226            }
227    
228            @Override
229            public void setColumns(List<String> columns) {
230                    _columns = columns;
231            }
232    
233            @Override
234            public void setContent(String content) {
235                    _setContent = true;
236    
237                    _content = content;
238            }
239    
240            @Override
241            public void setName(String name) {
242                    _name = name;
243            }
244    
245            @Override
246            public void setServletContext(ServletContext servletContext) {
247                    _servletContext = servletContext;
248            }
249    
250            @Override
251            public void setServletContextName(String servletContextName) {
252                    _servletContextName = servletContextName;
253    
254                    if (Validator.isNotNull(_servletContextName)) {
255                            _warFile = true;
256                    }
257                    else {
258                            _warFile = false;
259                    }
260            }
261    
262            @Override
263            public void setStandard(boolean standard) {
264                    _standard = standard;
265            }
266    
267            @Override
268            public void setTemplatePath(String templatePath) {
269                    _templatePath = templatePath;
270            }
271    
272            @Override
273            public void setThemeId(String themeId) {
274                    _themeId = themeId;
275            }
276    
277            @Override
278            public void setThumbnailPath(String thumbnailPath) {
279                    _thumbnailPath = thumbnailPath;
280            }
281    
282            private static final Log _log = LogFactoryUtil.getLog(
283                    LayoutTemplateImpl.class);
284    
285            private List<String> _columns = new ArrayList<>();
286            private String _content;
287            private final String _layoutTemplateId;
288            private String _name;
289            private transient ServletContext _servletContext;
290            private String _servletContextName = StringPool.BLANK;
291            private boolean _setContent;
292            private boolean _standard;
293            private String _templatePath;
294            private String _themeId;
295            private String _thumbnailPath;
296            private boolean _warFile;
297    
298    }