1   /**
2    * Copyright (c) 2000-2009 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.log.Log;
26  import com.liferay.portal.kernel.log.LogFactoryUtil;
27  import com.liferay.portal.kernel.util.ListUtil;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.ColorScheme;
32  import com.liferay.portal.model.Plugin;
33  import com.liferay.portal.model.SpriteImage;
34  import com.liferay.portal.model.Theme;
35  import com.liferay.portal.theme.ThemeCompanyId;
36  import com.liferay.portal.theme.ThemeCompanyLimit;
37  import com.liferay.portal.theme.ThemeGroupLimit;
38  import com.liferay.portal.util.PortalUtil;
39  import com.liferay.portal.util.PropsValues;
40  import com.liferay.portal.velocity.VelocityResourceListener;
41  
42  import java.util.HashMap;
43  import java.util.Iterator;
44  import java.util.List;
45  import java.util.Map;
46  import java.util.Properties;
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         return ListUtil.sort(colorSchemes);
217     }
218 
219     public Map<String, ColorScheme> getColorSchemesMap() {
220         return _colorSchemesMap;
221     }
222 
223     public boolean hasColorSchemes() {
224         if (_colorSchemesMap.size() > 0) {
225             return true;
226         }
227         else {
228             return false;
229         }
230     }
231 
232     public SpriteImage getSpriteImage(String fileName) {
233         return _spriteImagesMap.get(fileName);
234     }
235 
236     public void setSpriteImages(
237         String spriteFileName, Properties spriteProperties) {
238 
239         Iterator<Map.Entry<Object, Object>> itr =
240             spriteProperties.entrySet().iterator();
241 
242         while (itr.hasNext()) {
243             Map.Entry<Object, Object> entry = itr.next();
244 
245             String key = (String)entry.getKey();
246             String value = (String)entry.getValue();
247 
248             int[] values = StringUtil.split(value, 0);
249 
250             int offset = values[0];
251             int height = values[1];
252             int width = values[2];
253 
254             SpriteImage spriteImage = new SpriteImage(
255                 spriteFileName, key, offset, height, width);
256 
257             _spriteImagesMap.put(key, spriteImage);
258         }
259     }
260 
261     public String getServletContextName() {
262         return _servletContextName;
263     }
264 
265     public void setServletContextName(String servletContextName) {
266         _servletContextName = servletContextName;
267 
268         if (Validator.isNotNull(_servletContextName)) {
269             _warFile = true;
270         }
271         else {
272             _warFile = false;
273         }
274     }
275 
276     public boolean getWARFile() {
277         return _warFile;
278     }
279 
280     public boolean isWARFile() {
281         return _warFile;
282     }
283 
284     public String getContextPath() {
285         String virtualPath = getVirtualPath();
286 
287         if (Validator.isNotNull(virtualPath)) {
288             return virtualPath;
289         }
290 
291         if (isWARFile()) {
292             StringBuilder sb = new StringBuilder();
293 
294             sb.append(StringPool.SLASH);
295             sb.append(getServletContextName());
296 
297             return sb.toString();
298         }
299         else {
300             return PortalUtil.getPathContext();
301         }
302     }
303 
304     public boolean getLoadFromServletContext() {
305         return _loadFromServletContext;
306     }
307 
308     public boolean isLoadFromServletContext() {
309         return _loadFromServletContext;
310     }
311 
312     public void setLoadFromServletContext(boolean loadFromServletContext) {
313         _loadFromServletContext = loadFromServletContext;
314     }
315 
316     public String getVelocityResourceListener() {
317         if (_loadFromServletContext) {
318             return VelocityResourceListener.SERVLET_SEPARATOR;
319         }
320         else {
321             return VelocityResourceListener.THEME_LOADER_SEPARATOR;
322         }
323     }
324 
325     public int compareTo(Theme theme) {
326         return getName().compareTo(theme.getName());
327     }
328 
329     public boolean equals(Object obj) {
330         if (obj == null) {
331             return false;
332         }
333 
334         Theme theme = null;
335 
336         try {
337             theme = (Theme)obj;
338         }
339         catch (ClassCastException cce) {
340             return false;
341         }
342 
343         String themeId = theme.getThemeId();
344 
345         if (getThemeId().equals(themeId)) {
346             return true;
347         }
348         else {
349             return false;
350         }
351     }
352 
353     protected boolean isAvailable(ThemeCompanyLimit limit, long id) {
354         boolean available = true;
355 
356         if (_log.isDebugEnabled()) {
357             _log.debug(
358                 "Check if theme " + getThemeId() + " is available for " + id);
359         }
360 
361         if (limit != null) {
362             List<ThemeCompanyId> includes = limit.getIncludes();
363             List<ThemeCompanyId> excludes = limit.getExcludes();
364 
365             if ((includes.size() != 0) && (excludes.size() != 0)) {
366 
367                 // Since includes and excludes are specified, check to
368                 // make sure the current company id is included and also
369                 // not excluded
370 
371                 if (_log.isDebugEnabled()) {
372                     _log.debug("Check includes and excludes");
373                 }
374 
375                 available = limit.isIncluded(id);
376 
377                 if (available) {
378                     available = !limit.isExcluded(id);
379                 }
380             }
381             else if ((includes.size() == 0) && (excludes.size() != 0)) {
382 
383                 // Since no includes are specified, check to make sure
384                 // the current company id is not excluded
385 
386                 if (_log.isDebugEnabled()) {
387                     _log.debug("Check excludes");
388                 }
389 
390                 available = !limit.isExcluded(id);
391             }
392             else if ((includes.size() != 0) && (excludes.size() == 0)) {
393 
394                 // Since no excludes are specified, check to make sure
395                 // the current company id is included
396 
397                 if (_log.isDebugEnabled()) {
398                     _log.debug("Check includes");
399                 }
400 
401                 available = limit.isIncluded(id);
402             }
403             else {
404 
405                 // Since no includes or excludes are specified, this
406                 // theme is available for every company
407 
408                 if (_log.isDebugEnabled()) {
409                     _log.debug("No includes or excludes set");
410                 }
411 
412                 available = true;
413             }
414         }
415 
416         if (_log.isDebugEnabled()) {
417             _log.debug(
418                 "Theme " + getThemeId() + " is " +
419                     (!available ? "NOT " : "") + "available for " + id);
420         }
421 
422         return available;
423     }
424 
425     private static final String _DEFAULT_REGULAR_THEME_ID =
426         PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_REGULAR_THEME_ID);
427 
428     private static final String _DEFAULT_WAP_THEME_ID =
429         PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_WAP_THEME_ID);
430 
431     private static Log _log = LogFactoryUtil.getLog(ThemeImpl.class);
432 
433     private String _themeId;
434     private ThemeCompanyLimit _themeCompanyLimit;
435     private ThemeGroupLimit _themeGroupLimit;
436     private long _timestamp;
437     private String _name;
438     private String _rootPath = "/";
439     private String _templatesPath = "${root-path}/templates";
440     private String _cssPath = "${root-path}/css";
441     private String _imagesPath = "${root-path}/images";
442     private String _javaScriptPath = "${root-path}/javascript";
443     private String _virtualPath = StringPool.BLANK;
444     private String _templateExtension = "vm";
445     private Properties _settings = new Properties();
446     private boolean _wapTheme;
447     private Map<String, ColorScheme> _colorSchemesMap =
448         new HashMap<String, ColorScheme>();
449     private Map<String, SpriteImage> _spriteImagesMap =
450         new HashMap<String, SpriteImage>();
451     private String _servletContextName = StringPool.BLANK;
452     private boolean _warFile;
453     private boolean _loadFromServletContext;
454 
455 }