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