001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.model.impl;
016    
017    import com.liferay.portal.freemarker.FreeMarkerTemplateLoader;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.util.ListUtil;
022    import com.liferay.portal.kernel.util.PropsKeys;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.kernel.util.StringUtil;
025    import com.liferay.portal.kernel.util.Validator;
026    import com.liferay.portal.model.ColorScheme;
027    import com.liferay.portal.model.Plugin;
028    import com.liferay.portal.model.SpriteImage;
029    import com.liferay.portal.model.Theme;
030    import com.liferay.portal.theme.ThemeCompanyId;
031    import com.liferay.portal.theme.ThemeCompanyLimit;
032    import com.liferay.portal.theme.ThemeGroupLimit;
033    import com.liferay.portal.util.PortalUtil;
034    import com.liferay.portal.util.PrefsPropsUtil;
035    import com.liferay.portal.util.PropsValues;
036    import com.liferay.portal.velocity.VelocityResourceListener;
037    
038    import java.util.HashMap;
039    import java.util.Iterator;
040    import java.util.List;
041    import java.util.Map;
042    import java.util.Properties;
043    
044    /**
045     * @author Brian Wing Shun Chan
046     */
047    public class ThemeImpl extends PluginBaseImpl implements Theme {
048    
049            /**
050             * @deprecated
051             */
052            public static String getDefaultRegularThemeId() {
053                    return PortalUtil.getJsSafePortletId(
054                            PropsValues.DEFAULT_REGULAR_THEME_ID);
055            }
056    
057            public static String getDefaultRegularThemeId(long companyId)
058                    throws SystemException {
059    
060                    String defaultRegularThemeId = PrefsPropsUtil.getString(
061                            companyId, PropsKeys.DEFAULT_REGULAR_THEME_ID);
062    
063                    return PortalUtil.getJsSafePortletId(defaultRegularThemeId);
064            }
065    
066            /**
067             * @deprecated
068             */
069            public static String getDefaultWapThemeId() {
070                    return PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_WAP_THEME_ID);
071            }
072    
073            public static String getDefaultWapThemeId(long companyId)
074                    throws SystemException {
075    
076                    String defaultWapThemeId = PrefsPropsUtil.getString(
077                            companyId, PropsKeys.DEFAULT_WAP_THEME_ID);
078    
079                    return PortalUtil.getJsSafePortletId(defaultWapThemeId);
080            }
081    
082            public ThemeImpl() {
083            }
084    
085            public ThemeImpl(String themeId) {
086                    _themeId = themeId;
087            }
088    
089            public ThemeImpl(String themeId, String name) {
090                    _themeId = themeId;
091                    _name = name;
092            }
093    
094            public String getThemeId() {
095                    return _themeId;
096            }
097    
098            public String getPluginId() {
099                    return getThemeId();
100            }
101    
102            public String getPluginType() {
103                    return Plugin.TYPE_THEME;
104            }
105    
106            public ThemeCompanyLimit getThemeCompanyLimit() {
107                    return _themeCompanyLimit;
108            }
109    
110            public void setThemeCompanyLimit(ThemeCompanyLimit themeCompanyLimit) {
111                    _themeCompanyLimit = themeCompanyLimit;
112            }
113    
114            public boolean isCompanyAvailable(long companyId) {
115                    return isAvailable(getThemeCompanyLimit(), companyId);
116            }
117    
118            public ThemeGroupLimit getThemeGroupLimit() {
119                    return _themeGroupLimit;
120            }
121    
122            public void setThemeGroupLimit(ThemeGroupLimit themeGroupLimit) {
123                    _themeGroupLimit = themeGroupLimit;
124            }
125    
126            public boolean isGroupAvailable(long groupId) {
127                    return isAvailable(getThemeGroupLimit(), groupId);
128            }
129    
130            public long getTimestamp() {
131                    return _timestamp;
132            }
133    
134            public void setTimestamp(long timestamp) {
135                    _timestamp = timestamp;
136            }
137    
138            public String getName() {
139                    return _name;
140            }
141    
142            public void setName(String name) {
143                    _name = name;
144            }
145    
146            public String getRootPath() {
147                    return _rootPath;
148            }
149    
150            public void setRootPath(String rootPath) {
151                    _rootPath = rootPath;
152            }
153    
154            public String getTemplatesPath() {
155                    return _templatesPath;
156            }
157    
158            public void setTemplatesPath(String templatesPath) {
159                    _templatesPath = templatesPath;
160            }
161    
162            public String getCssPath() {
163                    return _cssPath;
164            }
165    
166            public void setCssPath(String cssPath) {
167                    _cssPath = cssPath;
168            }
169    
170            public String getImagesPath() {
171                    return _imagesPath;
172            }
173    
174            public void setImagesPath(String imagesPath) {
175                    _imagesPath = imagesPath;
176            }
177    
178            public String getJavaScriptPath() {
179                    return _javaScriptPath;
180            }
181    
182            public void setJavaScriptPath(String javaScriptPath) {
183                    _javaScriptPath = javaScriptPath;
184            }
185    
186            public String getVirtualPath() {
187                    return _virtualPath;
188            }
189    
190            public void setVirtualPath(String virtualPath) {
191                    if (_warFile && Validator.isNull(virtualPath)) {
192                            virtualPath = PropsValues.THEME_VIRTUAL_PATH;
193                    }
194    
195                    _virtualPath = virtualPath;
196            }
197    
198            public String getTemplateExtension() {
199                    return _templateExtension;
200            }
201    
202            public void setTemplateExtension(String templateExtension) {
203                    _templateExtension = templateExtension;
204            }
205    
206            public Properties getSettings() {
207                    return _settings;
208            }
209    
210            public String getSetting(String key) {
211                    return _settings.getProperty(key);
212            }
213    
214            public void setSetting(String key, String value) {
215                    _settings.setProperty(key, value);
216            }
217    
218            public boolean getWapTheme() {
219                    return _wapTheme;
220            }
221    
222            public boolean isWapTheme() {
223                    return _wapTheme;
224            }
225    
226            public void setWapTheme(boolean wapTheme) {
227                    _wapTheme = wapTheme;
228            }
229    
230            public List<ColorScheme> getColorSchemes() {
231                    List<ColorScheme> colorSchemes = ListUtil.fromCollection(
232                            _colorSchemesMap.values());
233    
234                    return ListUtil.sort(colorSchemes);
235            }
236    
237            public Map<String, ColorScheme> getColorSchemesMap() {
238                    return _colorSchemesMap;
239            }
240    
241            public boolean hasColorSchemes() {
242                    if (_colorSchemesMap.size() > 0) {
243                            return true;
244                    }
245                    else {
246                            return false;
247                    }
248            }
249    
250            public SpriteImage getSpriteImage(String fileName) {
251                    return _spriteImagesMap.get(fileName);
252            }
253    
254            public void setSpriteImages(
255                    String spriteFileName, Properties spriteProperties) {
256    
257                    Iterator<Map.Entry<Object, Object>> itr =
258                            spriteProperties.entrySet().iterator();
259    
260                    while (itr.hasNext()) {
261                            Map.Entry<Object, Object> entry = itr.next();
262    
263                            String key = (String)entry.getKey();
264                            String value = (String)entry.getValue();
265    
266                            int[] values = StringUtil.split(value, 0);
267    
268                            int offset = values[0];
269                            int height = values[1];
270                            int width = values[2];
271    
272                            SpriteImage spriteImage = new SpriteImage(
273                                    spriteFileName, key, offset, height, width);
274    
275                            _spriteImagesMap.put(key, spriteImage);
276                    }
277            }
278    
279            public String getServletContextName() {
280                    return _servletContextName;
281            }
282    
283            public void setServletContextName(String servletContextName) {
284                    _servletContextName = servletContextName;
285    
286                    if (Validator.isNotNull(_servletContextName)) {
287                            _warFile = true;
288                    }
289                    else {
290                            _warFile = false;
291                    }
292            }
293    
294            public boolean getWARFile() {
295                    return _warFile;
296            }
297    
298            public boolean isWARFile() {
299                    return _warFile;
300            }
301    
302            public String getContextPath() {
303                    String virtualPath = getVirtualPath();
304    
305                    if (Validator.isNotNull(virtualPath)) {
306                            return virtualPath;
307                    }
308    
309                    if (isWARFile()) {
310                            return StringPool.SLASH.concat(getServletContextName());
311                    }
312                    else {
313                            return PortalUtil.getPathContext();
314                    }
315            }
316    
317            public String getFreeMarkerTemplateLoader() {
318                    if (_loadFromServletContext) {
319                            return FreeMarkerTemplateLoader.SERVLET_SEPARATOR;
320                    }
321                    else {
322                            return FreeMarkerTemplateLoader.THEME_LOADER_SEPARATOR;
323                    }
324            }
325    
326            public boolean getLoadFromServletContext() {
327                    return _loadFromServletContext;
328            }
329    
330            public boolean isLoadFromServletContext() {
331                    return _loadFromServletContext;
332            }
333    
334            public void setLoadFromServletContext(boolean loadFromServletContext) {
335                    _loadFromServletContext = loadFromServletContext;
336            }
337    
338            public String getVelocityResourceListener() {
339                    if (_loadFromServletContext) {
340                            return VelocityResourceListener.SERVLET_SEPARATOR;
341                    }
342                    else {
343                            return VelocityResourceListener.THEME_LOADER_SEPARATOR;
344                    }
345            }
346    
347            public int compareTo(Theme theme) {
348                    return getName().compareTo(theme.getName());
349            }
350    
351            public boolean equals(Object obj) {
352                    if (obj == null) {
353                            return false;
354                    }
355    
356                    Theme theme = null;
357    
358                    try {
359                            theme = (Theme)obj;
360                    }
361                    catch (ClassCastException cce) {
362                            return false;
363                    }
364    
365                    String themeId = theme.getThemeId();
366    
367                    if (getThemeId().equals(themeId)) {
368                            return true;
369                    }
370                    else {
371                            return false;
372                    }
373            }
374    
375            public int hashCode() {
376                    return _themeId.hashCode();
377            }
378    
379            protected boolean isAvailable(ThemeCompanyLimit limit, long id) {
380                    boolean available = true;
381    
382                    if (_log.isDebugEnabled()) {
383                            _log.debug(
384                                    "Check if theme " + getThemeId() + " is available for " + id);
385                    }
386    
387                    if (limit != null) {
388                            List<ThemeCompanyId> includes = limit.getIncludes();
389                            List<ThemeCompanyId> excludes = limit.getExcludes();
390    
391                            if ((includes.size() != 0) && (excludes.size() != 0)) {
392    
393                                    // Since includes and excludes are specified, check to
394                                    // make sure the current company id is included and also
395                                    // not excluded
396    
397                                    if (_log.isDebugEnabled()) {
398                                            _log.debug("Check includes and excludes");
399                                    }
400    
401                                    available = limit.isIncluded(id);
402    
403                                    if (available) {
404                                            available = !limit.isExcluded(id);
405                                    }
406                            }
407                            else if ((includes.size() == 0) && (excludes.size() != 0)) {
408    
409                                    // Since no includes are specified, check to make sure
410                                    // the current company id is not excluded
411    
412                                    if (_log.isDebugEnabled()) {
413                                            _log.debug("Check excludes");
414                                    }
415    
416                                    available = !limit.isExcluded(id);
417                            }
418                            else if ((includes.size() != 0) && (excludes.size() == 0)) {
419    
420                                    // Since no excludes are specified, check to make sure
421                                    // the current company id is included
422    
423                                    if (_log.isDebugEnabled()) {
424                                            _log.debug("Check includes");
425                                    }
426    
427                                    available = limit.isIncluded(id);
428                            }
429                            else {
430    
431                                    // Since no includes or excludes are specified, this
432                                    // theme is available for every company
433    
434                                    if (_log.isDebugEnabled()) {
435                                            _log.debug("No includes or excludes set");
436                                    }
437    
438                                    available = true;
439                            }
440                    }
441    
442                    if (_log.isDebugEnabled()) {
443                            _log.debug(
444                                    "Theme " + getThemeId() + " is " +
445                                            (!available ? "NOT " : "") + "available for " + id);
446                    }
447    
448                    return available;
449            }
450    
451            private static Log _log = LogFactoryUtil.getLog(ThemeImpl.class);
452    
453            private String _themeId;
454            private ThemeCompanyLimit _themeCompanyLimit;
455            private ThemeGroupLimit _themeGroupLimit;
456            private long _timestamp;
457            private String _name;
458            private String _rootPath = "/";
459            private String _templatesPath = "${root-path}/templates";
460            private String _cssPath = "${root-path}/css";
461            private String _imagesPath = "${root-path}/images";
462            private String _javaScriptPath = "${root-path}/js";
463            private String _virtualPath = StringPool.BLANK;
464            private String _templateExtension = "vm";
465            private Properties _settings = new Properties();
466            private boolean _wapTheme;
467            private Map<String, ColorScheme> _colorSchemesMap =
468                    new HashMap<String, ColorScheme>();
469            private Map<String, SpriteImage> _spriteImagesMap =
470                    new HashMap<String, SpriteImage>();
471            private String _servletContextName = StringPool.BLANK;
472            private boolean _warFile;
473            private boolean _loadFromServletContext;
474    
475    }