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.StringMaker;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.Validator;
28  import com.liferay.portal.model.ColorScheme;
29  import com.liferay.portal.model.Plugin;
30  import com.liferay.portal.model.Theme;
31  import com.liferay.portal.theme.ThemeCompanyId;
32  import com.liferay.portal.theme.ThemeCompanyLimit;
33  import com.liferay.portal.theme.ThemeGroupLimit;
34  import com.liferay.portal.util.PortalUtil;
35  import com.liferay.portal.util.PropsValues;
36  import com.liferay.portal.velocity.VelocityResourceListener;
37  import com.liferay.util.ListUtil;
38  
39  import java.util.Collections;
40  import java.util.HashMap;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Properties;
44  
45  import org.apache.commons.logging.Log;
46  import org.apache.commons.logging.LogFactory;
47  
48  /**
49   * <a href="ThemeImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class ThemeImpl extends PluginBaseImpl implements Theme {
55  
56      public static String getDefaultRegularThemeId() {
57          return _DEFAULT_REGULAR_THEME_ID;
58      }
59  
60      public static String getDefaultWapThemeId() {
61          return _DEFAULT_WAP_THEME_ID;
62      }
63  
64      public ThemeImpl() {
65      }
66  
67      public ThemeImpl(String themeId) {
68          _themeId = themeId;
69      }
70  
71      public ThemeImpl(String themeId, String name) {
72          _themeId = themeId;
73          _name = name;
74      }
75  
76      public String getThemeId() {
77          return _themeId;
78      }
79  
80      public String getPluginId() {
81          return getThemeId();
82      }
83  
84      public String getPluginType() {
85          return Plugin.TYPE_THEME;
86      }
87  
88      public ThemeCompanyLimit getThemeCompanyLimit() {
89          return _themeCompanyLimit;
90      }
91  
92      public void setThemeCompanyLimit(ThemeCompanyLimit themeCompanyLimit) {
93          _themeCompanyLimit = themeCompanyLimit;
94      }
95  
96      public boolean isCompanyAvailable(long companyId) {
97          return isAvailable(getThemeCompanyLimit(), companyId);
98      }
99  
100     public ThemeGroupLimit getThemeGroupLimit() {
101         return _themeGroupLimit;
102     }
103 
104     public void setThemeGroupLimit(ThemeGroupLimit themeGroupLimit) {
105         _themeGroupLimit = themeGroupLimit;
106     }
107 
108     public boolean isGroupAvailable(long groupId) {
109         return isAvailable(getThemeGroupLimit(), groupId);
110     }
111 
112     public long getTimestamp() {
113         return _timestamp;
114     }
115 
116     public void setTimestamp(long timestamp) {
117         _timestamp = timestamp;
118     }
119 
120     public String getName() {
121         return _name;
122     }
123 
124     public void setName(String name) {
125         _name = name;
126     }
127 
128     public String getRootPath() {
129         return _rootPath;
130     }
131 
132     public void setRootPath(String rootPath) {
133         _rootPath = rootPath;
134     }
135 
136     public String getTemplatesPath() {
137         return _templatesPath;
138     }
139 
140     public void setTemplatesPath(String templatesPath) {
141         _templatesPath = templatesPath;
142     }
143 
144     public String getCssPath() {
145         return _cssPath;
146     }
147 
148     public void setCssPath(String cssPath) {
149         _cssPath = cssPath;
150     }
151 
152     public String getImagesPath() {
153         return _imagesPath;
154     }
155 
156     public void setImagesPath(String imagesPath) {
157         _imagesPath = imagesPath;
158     }
159 
160     public String getJavaScriptPath() {
161         return _javaScriptPath;
162     }
163 
164     public void setJavaScriptPath(String javaScriptPath) {
165         _javaScriptPath = javaScriptPath;
166     }
167 
168     public String getVirtualPath() {
169         return _virtualPath;
170     }
171 
172     public void setVirtualPath(String virtualPath) {
173         if (_warFile && Validator.isNull(virtualPath)) {
174             virtualPath = PropsValues.THEME_VIRTUAL_PATH;
175         }
176 
177         _virtualPath = virtualPath;
178     }
179 
180     public String getTemplateExtension() {
181         return _templateExtension;
182     }
183 
184     public void setTemplateExtension(String templateExtension) {
185         _templateExtension = templateExtension;
186     }
187 
188     public Properties getSettings() {
189         return _settings;
190     }
191 
192     public String getSetting(String key) {
193         return _settings.getProperty(key);
194     }
195 
196     public void setSetting(String key, String value) {
197         _settings.setProperty(key, value);
198     }
199 
200     public boolean getWapTheme() {
201         return _wapTheme;
202     }
203 
204     public boolean isWapTheme() {
205         return _wapTheme;
206     }
207 
208     public void setWapTheme(boolean wapTheme) {
209         _wapTheme = wapTheme;
210     }
211 
212     public List<ColorScheme> getColorSchemes() {
213         List<ColorScheme> colorSchemes = ListUtil.fromCollection(
214             _colorSchemesMap.values());
215 
216         Collections.sort(colorSchemes);
217 
218         return colorSchemes;
219     }
220 
221     public Map<String, ColorScheme> getColorSchemesMap() {
222         return _colorSchemesMap;
223     }
224 
225     public boolean hasColorSchemes() {
226         if (_colorSchemesMap.size() > 0) {
227             return true;
228         }
229         else {
230             return false;
231         }
232     }
233 
234     public String getServletContextName() {
235         return _servletContextName;
236     }
237 
238     public void setServletContextName(String servletContextName) {
239         _servletContextName = servletContextName;
240 
241         if (Validator.isNotNull(_servletContextName)) {
242             _warFile = true;
243         }
244         else {
245             _warFile = false;
246         }
247     }
248 
249     public boolean getWARFile() {
250         return _warFile;
251     }
252 
253     public boolean isWARFile() {
254         return _warFile;
255     }
256 
257     public String getContextPath() {
258         String virtualPath = getVirtualPath();
259 
260         if (Validator.isNotNull(virtualPath)) {
261             return virtualPath;
262         }
263 
264         if (isWARFile()) {
265             StringMaker sm = new StringMaker();
266 
267             sm.append(StringPool.SLASH);
268             sm.append(getServletContextName());
269 
270             return sm.toString();
271         }
272         else {
273             return PortalUtil.getPathContext();
274         }
275     }
276 
277     public boolean getLoadFromServletContext() {
278         return _loadFromServletContext;
279     }
280 
281     public boolean isLoadFromServletContext() {
282         return _loadFromServletContext;
283     }
284 
285     public void setLoadFromServletContext(boolean loadFromServletContext) {
286         _loadFromServletContext = loadFromServletContext;
287     }
288 
289     public String getVelocityResourceListener() {
290         if (_loadFromServletContext) {
291             return VelocityResourceListener.SERVLET_SEPARATOR;
292         }
293         else {
294             return VelocityResourceListener.THEME_LOADER_SEPARATOR;
295         }
296     }
297 
298     public int compareTo(Theme theme) {
299         return getName().compareTo(theme.getName());
300     }
301 
302     public boolean equals(Object obj) {
303         if (obj == null) {
304             return false;
305         }
306 
307         Theme theme = null;
308 
309         try {
310             theme = (Theme)obj;
311         }
312         catch (ClassCastException cce) {
313             return false;
314         }
315 
316         String themeId = theme.getThemeId();
317 
318         if (getThemeId().equals(themeId)) {
319             return true;
320         }
321         else {
322             return false;
323         }
324     }
325 
326     protected boolean isAvailable(ThemeCompanyLimit limit, long id) {
327         boolean available = true;
328 
329         if (_log.isDebugEnabled()) {
330             _log.debug(
331                 "Check if theme " + getThemeId() + " is available for " + id);
332         }
333 
334         if (limit != null) {
335             List<ThemeCompanyId> includes = limit.getIncludes();
336             List<ThemeCompanyId> excludes = limit.getExcludes();
337 
338             if ((includes.size() != 0) && (excludes.size() != 0)) {
339 
340                 // Since includes and excludes are specified, check to
341                 // make sure the current company id is included and also
342                 // not excluded
343 
344                 if (_log.isDebugEnabled()) {
345                     _log.debug("Check includes and excludes");
346                 }
347 
348                 available = limit.isIncluded(id);
349 
350                 if (available) {
351                     available = !limit.isExcluded(id);
352                 }
353             }
354             else if ((includes.size() == 0) && (excludes.size() != 0)) {
355 
356                 // Since no includes are specified, check to make sure
357                 // the current company id is not excluded
358 
359                 if (_log.isDebugEnabled()) {
360                     _log.debug("Check excludes");
361                 }
362 
363                 available = !limit.isExcluded(id);
364             }
365             else if ((includes.size() != 0) && (excludes.size() == 0)) {
366 
367                 // Since no excludes are specified, check to make sure
368                 // the current company id is included
369 
370                 if (_log.isDebugEnabled()) {
371                     _log.debug("Check includes");
372                 }
373 
374                 available = limit.isIncluded(id);
375             }
376             else {
377 
378                 // Since no includes or excludes are specified, this
379                 // theme is available for every company
380 
381                 if (_log.isDebugEnabled()) {
382                     _log.debug("No includes or excludes set");
383                 }
384 
385                 available = true;
386             }
387         }
388 
389         if (_log.isDebugEnabled()) {
390             _log.debug(
391                 "Theme " + getThemeId() + " is " +
392                     (!available ? "NOT " : "") + "available for " + id);
393         }
394 
395         return available;
396     }
397 
398     private static final String _DEFAULT_REGULAR_THEME_ID =
399         PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_REGULAR_THEME_ID);
400 
401     private static final String _DEFAULT_WAP_THEME_ID =
402         PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_WAP_THEME_ID);
403 
404     private static Log _log = LogFactory.getLog(ThemeImpl.class);
405 
406     private String _themeId;
407     private ThemeCompanyLimit _themeCompanyLimit;
408     private ThemeGroupLimit _themeGroupLimit;
409     private long _timestamp;
410     private String _name;
411     private String _rootPath = "/";
412     private String _templatesPath = "${root-path}/templates";
413     private String _cssPath = "${root-path}/css";
414     private String _imagesPath = "${root-path}/images";
415     private String _javaScriptPath = "${root-path}/javascript";
416     private String _virtualPath = StringPool.BLANK;
417     private String _templateExtension = "vm";
418     private Properties _settings = new Properties();
419     private boolean _wapTheme;
420     private Map<String, ColorScheme> _colorSchemesMap =
421         new HashMap<String, ColorScheme>();
422     private String _servletContextName = StringPool.BLANK;
423     private boolean _warFile;
424     private boolean _loadFromServletContext;
425 
426 }