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