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