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