1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.model.impl;
16  
17  import com.liferay.portal.freemarker.FreeMarkerTemplateLoader;
18  import com.liferay.portal.kernel.exception.SystemException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.util.ListUtil;
22  import com.liferay.portal.kernel.util.PropsKeys;
23  import com.liferay.portal.kernel.util.StringPool;
24  import com.liferay.portal.kernel.util.StringUtil;
25  import com.liferay.portal.kernel.util.Validator;
26  import com.liferay.portal.model.ColorScheme;
27  import com.liferay.portal.model.Plugin;
28  import com.liferay.portal.model.SpriteImage;
29  import com.liferay.portal.model.Theme;
30  import com.liferay.portal.theme.ThemeCompanyId;
31  import com.liferay.portal.theme.ThemeCompanyLimit;
32  import com.liferay.portal.theme.ThemeGroupLimit;
33  import com.liferay.portal.util.PortalUtil;
34  import com.liferay.portal.util.PrefsPropsUtil;
35  import com.liferay.portal.util.PropsValues;
36  import com.liferay.portal.velocity.VelocityResourceListener;
37  
38  import java.util.HashMap;
39  import java.util.Iterator;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Properties;
43  
44  /**
45   * <a href="ThemeImpl.java.html"><b><i>View Source</i></b></a>
46   *
47   * @author Brian Wing Shun Chan
48   */
49  public class ThemeImpl extends PluginBaseImpl implements Theme {
50  
51      /**
52       * @deprecated
53       */
54      public static String getDefaultRegularThemeId() {
55          return PortalUtil.getJsSafePortletId(
56              PropsValues.DEFAULT_REGULAR_THEME_ID);
57      }
58  
59      public static String getDefaultRegularThemeId(long companyId)
60          throws SystemException {
61  
62          String defaultRegularThemeId = PrefsPropsUtil.getString(
63              companyId, PropsKeys.DEFAULT_REGULAR_THEME_ID);
64  
65          return PortalUtil.getJsSafePortletId(defaultRegularThemeId);
66      }
67  
68      /**
69       * @deprecated
70       */
71      public static String getDefaultWapThemeId() {
72          return PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_WAP_THEME_ID);
73      }
74  
75      public static String getDefaultWapThemeId(long companyId)
76          throws SystemException {
77  
78          String defaultWapThemeId = PrefsPropsUtil.getString(
79              companyId, PropsKeys.DEFAULT_WAP_THEME_ID);
80  
81          return PortalUtil.getJsSafePortletId(defaultWapThemeId);
82      }
83  
84      public ThemeImpl() {
85      }
86  
87      public ThemeImpl(String themeId) {
88          _themeId = themeId;
89      }
90  
91      public ThemeImpl(String themeId, String name) {
92          _themeId = themeId;
93          _name = name;
94      }
95  
96      public String getThemeId() {
97          return _themeId;
98      }
99  
100     public String getPluginId() {
101         return getThemeId();
102     }
103 
104     public String getPluginType() {
105         return Plugin.TYPE_THEME;
106     }
107 
108     public ThemeCompanyLimit getThemeCompanyLimit() {
109         return _themeCompanyLimit;
110     }
111 
112     public void setThemeCompanyLimit(ThemeCompanyLimit themeCompanyLimit) {
113         _themeCompanyLimit = themeCompanyLimit;
114     }
115 
116     public boolean isCompanyAvailable(long companyId) {
117         return isAvailable(getThemeCompanyLimit(), companyId);
118     }
119 
120     public ThemeGroupLimit getThemeGroupLimit() {
121         return _themeGroupLimit;
122     }
123 
124     public void setThemeGroupLimit(ThemeGroupLimit themeGroupLimit) {
125         _themeGroupLimit = themeGroupLimit;
126     }
127 
128     public boolean isGroupAvailable(long groupId) {
129         return isAvailable(getThemeGroupLimit(), groupId);
130     }
131 
132     public long getTimestamp() {
133         return _timestamp;
134     }
135 
136     public void setTimestamp(long timestamp) {
137         _timestamp = timestamp;
138     }
139 
140     public String getName() {
141         return _name;
142     }
143 
144     public void setName(String name) {
145         _name = name;
146     }
147 
148     public String getRootPath() {
149         return _rootPath;
150     }
151 
152     public void setRootPath(String rootPath) {
153         _rootPath = rootPath;
154     }
155 
156     public String getTemplatesPath() {
157         return _templatesPath;
158     }
159 
160     public void setTemplatesPath(String templatesPath) {
161         _templatesPath = templatesPath;
162     }
163 
164     public String getCssPath() {
165         return _cssPath;
166     }
167 
168     public void setCssPath(String cssPath) {
169         _cssPath = cssPath;
170     }
171 
172     public String getImagesPath() {
173         return _imagesPath;
174     }
175 
176     public void setImagesPath(String imagesPath) {
177         _imagesPath = imagesPath;
178     }
179 
180     public String getJavaScriptPath() {
181         return _javaScriptPath;
182     }
183 
184     public void setJavaScriptPath(String javaScriptPath) {
185         _javaScriptPath = javaScriptPath;
186     }
187 
188     public String getVirtualPath() {
189         return _virtualPath;
190     }
191 
192     public void setVirtualPath(String virtualPath) {
193         if (_warFile && Validator.isNull(virtualPath)) {
194             virtualPath = PropsValues.THEME_VIRTUAL_PATH;
195         }
196 
197         _virtualPath = virtualPath;
198     }
199 
200     public String getTemplateExtension() {
201         return _templateExtension;
202     }
203 
204     public void setTemplateExtension(String templateExtension) {
205         _templateExtension = templateExtension;
206     }
207 
208     public Properties getSettings() {
209         return _settings;
210     }
211 
212     public String getSetting(String key) {
213         return _settings.getProperty(key);
214     }
215 
216     public void setSetting(String key, String value) {
217         _settings.setProperty(key, value);
218     }
219 
220     public boolean getWapTheme() {
221         return _wapTheme;
222     }
223 
224     public boolean isWapTheme() {
225         return _wapTheme;
226     }
227 
228     public void setWapTheme(boolean wapTheme) {
229         _wapTheme = wapTheme;
230     }
231 
232     public List<ColorScheme> getColorSchemes() {
233         List<ColorScheme> colorSchemes = ListUtil.fromCollection(
234             _colorSchemesMap.values());
235 
236         return ListUtil.sort(colorSchemes);
237     }
238 
239     public Map<String, ColorScheme> getColorSchemesMap() {
240         return _colorSchemesMap;
241     }
242 
243     public boolean hasColorSchemes() {
244         if (_colorSchemesMap.size() > 0) {
245             return true;
246         }
247         else {
248             return false;
249         }
250     }
251 
252     public SpriteImage getSpriteImage(String fileName) {
253         return _spriteImagesMap.get(fileName);
254     }
255 
256     public void setSpriteImages(
257         String spriteFileName, Properties spriteProperties) {
258 
259         Iterator<Map.Entry<Object, Object>> itr =
260             spriteProperties.entrySet().iterator();
261 
262         while (itr.hasNext()) {
263             Map.Entry<Object, Object> entry = itr.next();
264 
265             String key = (String)entry.getKey();
266             String value = (String)entry.getValue();
267 
268             int[] values = StringUtil.split(value, 0);
269 
270             int offset = values[0];
271             int height = values[1];
272             int width = values[2];
273 
274             SpriteImage spriteImage = new SpriteImage(
275                 spriteFileName, key, offset, height, width);
276 
277             _spriteImagesMap.put(key, spriteImage);
278         }
279     }
280 
281     public String getServletContextName() {
282         return _servletContextName;
283     }
284 
285     public void setServletContextName(String servletContextName) {
286         _servletContextName = servletContextName;
287 
288         if (Validator.isNotNull(_servletContextName)) {
289             _warFile = true;
290         }
291         else {
292             _warFile = false;
293         }
294     }
295 
296     public boolean getWARFile() {
297         return _warFile;
298     }
299 
300     public boolean isWARFile() {
301         return _warFile;
302     }
303 
304     public String getContextPath() {
305         String virtualPath = getVirtualPath();
306 
307         if (Validator.isNotNull(virtualPath)) {
308             return virtualPath;
309         }
310 
311         if (isWARFile()) {
312             return StringPool.SLASH.concat(getServletContextName());
313         }
314         else {
315             return PortalUtil.getPathContext();
316         }
317     }
318 
319     public String getFreeMarkerTemplateLoader() {
320         if (_loadFromServletContext) {
321             return FreeMarkerTemplateLoader.SERVLET_SEPARATOR;
322         }
323         else {
324             return FreeMarkerTemplateLoader.THEME_LOADER_SEPARATOR;
325         }
326     }
327 
328     public boolean getLoadFromServletContext() {
329         return _loadFromServletContext;
330     }
331 
332     public boolean isLoadFromServletContext() {
333         return _loadFromServletContext;
334     }
335 
336     public void setLoadFromServletContext(boolean loadFromServletContext) {
337         _loadFromServletContext = loadFromServletContext;
338     }
339 
340     public String getVelocityResourceListener() {
341         if (_loadFromServletContext) {
342             return VelocityResourceListener.SERVLET_SEPARATOR;
343         }
344         else {
345             return VelocityResourceListener.THEME_LOADER_SEPARATOR;
346         }
347     }
348 
349     public int compareTo(Theme theme) {
350         return getName().compareTo(theme.getName());
351     }
352 
353     public boolean equals(Object obj) {
354         if (obj == null) {
355             return false;
356         }
357 
358         Theme theme = null;
359 
360         try {
361             theme = (Theme)obj;
362         }
363         catch (ClassCastException cce) {
364             return false;
365         }
366 
367         String themeId = theme.getThemeId();
368 
369         if (getThemeId().equals(themeId)) {
370             return true;
371         }
372         else {
373             return false;
374         }
375     }
376 
377     protected boolean isAvailable(ThemeCompanyLimit limit, long id) {
378         boolean available = true;
379 
380         if (_log.isDebugEnabled()) {
381             _log.debug(
382                 "Check if theme " + getThemeId() + " is available for " + id);
383         }
384 
385         if (limit != null) {
386             List<ThemeCompanyId> includes = limit.getIncludes();
387             List<ThemeCompanyId> excludes = limit.getExcludes();
388 
389             if ((includes.size() != 0) && (excludes.size() != 0)) {
390 
391                 // Since includes and excludes are specified, check to
392                 // make sure the current company id is included and also
393                 // not excluded
394 
395                 if (_log.isDebugEnabled()) {
396                     _log.debug("Check includes and excludes");
397                 }
398 
399                 available = limit.isIncluded(id);
400 
401                 if (available) {
402                     available = !limit.isExcluded(id);
403                 }
404             }
405             else if ((includes.size() == 0) && (excludes.size() != 0)) {
406 
407                 // Since no includes are specified, check to make sure
408                 // the current company id is not excluded
409 
410                 if (_log.isDebugEnabled()) {
411                     _log.debug("Check excludes");
412                 }
413 
414                 available = !limit.isExcluded(id);
415             }
416             else if ((includes.size() != 0) && (excludes.size() == 0)) {
417 
418                 // Since no excludes are specified, check to make sure
419                 // the current company id is included
420 
421                 if (_log.isDebugEnabled()) {
422                     _log.debug("Check includes");
423                 }
424 
425                 available = limit.isIncluded(id);
426             }
427             else {
428 
429                 // Since no includes or excludes are specified, this
430                 // theme is available for every company
431 
432                 if (_log.isDebugEnabled()) {
433                     _log.debug("No includes or excludes set");
434                 }
435 
436                 available = true;
437             }
438         }
439 
440         if (_log.isDebugEnabled()) {
441             _log.debug(
442                 "Theme " + getThemeId() + " is " +
443                     (!available ? "NOT " : "") + "available for " + id);
444         }
445 
446         return available;
447     }
448 
449     private static Log _log = LogFactoryUtil.getLog(ThemeImpl.class);
450 
451     private String _themeId;
452     private ThemeCompanyLimit _themeCompanyLimit;
453     private ThemeGroupLimit _themeGroupLimit;
454     private long _timestamp;
455     private String _name;
456     private String _rootPath = "/";
457     private String _templatesPath = "${root-path}/templates";
458     private String _cssPath = "${root-path}/css";
459     private String _imagesPath = "${root-path}/images";
460     private String _javaScriptPath = "${root-path}/javascript";
461     private String _virtualPath = StringPool.BLANK;
462     private String _templateExtension = "vm";
463     private Properties _settings = new Properties();
464     private boolean _wapTheme;
465     private Map<String, ColorScheme> _colorSchemesMap =
466         new HashMap<String, ColorScheme>();
467     private Map<String, SpriteImage> _spriteImagesMap =
468         new HashMap<String, SpriteImage>();
469     private String _servletContextName = StringPool.BLANK;
470     private boolean _warFile;
471     private boolean _loadFromServletContext;
472 
473 }