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