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.Collections;
39  import java.util.HashMap;
40  import java.util.List;
41  import java.util.Map;
42  import java.util.Properties;
43  
44  import org.apache.commons.logging.Log;
45  import org.apache.commons.logging.LogFactory;
46  
47  /**
48   * <a href="ThemeImpl.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class ThemeImpl extends PluginBaseImpl implements 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_THEME;
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<ColorScheme> getColorSchemes() {
212         List<ColorScheme> colorSchemes = ListUtil.fromCollection(
213             _colorSchemesMap.values());
214 
215         Collections.sort(colorSchemes);
216 
217         return colorSchemes;
218     }
219 
220     public Map<String, ColorScheme> getColorSchemesMap() {
221         return _colorSchemesMap;
222     }
223 
224     public boolean hasColorSchemes() {
225         if (_colorSchemesMap.size() > 0) {
226             return true;
227         }
228         else {
229             return false;
230         }
231     }
232 
233     public String getServletContextName() {
234         return _servletContextName;
235     }
236 
237     public void setServletContextName(String servletContextName) {
238         _servletContextName = servletContextName;
239 
240         if (Validator.isNotNull(_servletContextName)) {
241             _warFile = true;
242         }
243         else {
244             _warFile = false;
245         }
246     }
247 
248     public boolean getWARFile() {
249         return _warFile;
250     }
251 
252     public boolean isWARFile() {
253         return _warFile;
254     }
255 
256     public String getContextPath() {
257         String virtualPath = getVirtualPath();
258 
259         if (Validator.isNotNull(virtualPath)) {
260             return virtualPath;
261         }
262 
263         if (isWARFile()) {
264             StringBuilder sb = new StringBuilder();
265 
266             sb.append(StringPool.SLASH);
267             sb.append(getServletContextName());
268 
269             return sb.toString();
270         }
271         else {
272             return PortalUtil.getPathContext();
273         }
274     }
275 
276     public boolean getLoadFromServletContext() {
277         return _loadFromServletContext;
278     }
279 
280     public boolean isLoadFromServletContext() {
281         return _loadFromServletContext;
282     }
283 
284     public void setLoadFromServletContext(boolean loadFromServletContext) {
285         _loadFromServletContext = loadFromServletContext;
286     }
287 
288     public String getVelocityResourceListener() {
289         if (_loadFromServletContext) {
290             return VelocityResourceListener.SERVLET_SEPARATOR;
291         }
292         else {
293             return VelocityResourceListener.THEME_LOADER_SEPARATOR;
294         }
295     }
296 
297     public int compareTo(Theme theme) {
298         return getName().compareTo(theme.getName());
299     }
300 
301     public boolean equals(Object obj) {
302         if (obj == null) {
303             return false;
304         }
305 
306         Theme theme = null;
307 
308         try {
309             theme = (Theme)obj;
310         }
311         catch (ClassCastException cce) {
312             return false;
313         }
314 
315         String themeId = theme.getThemeId();
316 
317         if (getThemeId().equals(themeId)) {
318             return true;
319         }
320         else {
321             return false;
322         }
323     }
324 
325     protected boolean isAvailable(ThemeCompanyLimit limit, long id) {
326         boolean available = true;
327 
328         if (_log.isDebugEnabled()) {
329             _log.debug(
330                 "Check if theme " + getThemeId() + " is available for " + id);
331         }
332 
333         if (limit != null) {
334             List<ThemeCompanyId> includes = limit.getIncludes();
335             List<ThemeCompanyId> excludes = limit.getExcludes();
336 
337             if ((includes.size() != 0) && (excludes.size() != 0)) {
338 
339                 // Since includes and excludes are specified, check to
340                 // make sure the current company id is included and also
341                 // not excluded
342 
343                 if (_log.isDebugEnabled()) {
344                     _log.debug("Check includes and excludes");
345                 }
346 
347                 available = limit.isIncluded(id);
348 
349                 if (available) {
350                     available = !limit.isExcluded(id);
351                 }
352             }
353             else if ((includes.size() == 0) && (excludes.size() != 0)) {
354 
355                 // Since no includes are specified, check to make sure
356                 // the current company id is not excluded
357 
358                 if (_log.isDebugEnabled()) {
359                     _log.debug("Check excludes");
360                 }
361 
362                 available = !limit.isExcluded(id);
363             }
364             else if ((includes.size() != 0) && (excludes.size() == 0)) {
365 
366                 // Since no excludes are specified, check to make sure
367                 // the current company id is included
368 
369                 if (_log.isDebugEnabled()) {
370                     _log.debug("Check includes");
371                 }
372 
373                 available = limit.isIncluded(id);
374             }
375             else {
376 
377                 // Since no includes or excludes are specified, this
378                 // theme is available for every company
379 
380                 if (_log.isDebugEnabled()) {
381                     _log.debug("No includes or excludes set");
382                 }
383 
384                 available = true;
385             }
386         }
387 
388         if (_log.isDebugEnabled()) {
389             _log.debug(
390                 "Theme " + getThemeId() + " is " +
391                     (!available ? "NOT " : "") + "available for " + id);
392         }
393 
394         return available;
395     }
396 
397     private static final String _DEFAULT_REGULAR_THEME_ID =
398         PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_REGULAR_THEME_ID);
399 
400     private static final String _DEFAULT_WAP_THEME_ID =
401         PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_WAP_THEME_ID);
402 
403     private static Log _log = LogFactory.getLog(ThemeImpl.class);
404 
405     private String _themeId;
406     private ThemeCompanyLimit _themeCompanyLimit;
407     private ThemeGroupLimit _themeGroupLimit;
408     private long _timestamp;
409     private String _name;
410     private String _rootPath = "/";
411     private String _templatesPath = "${root-path}/templates";
412     private String _cssPath = "${root-path}/css";
413     private String _imagesPath = "${root-path}/images";
414     private String _javaScriptPath = "${root-path}/javascript";
415     private String _virtualPath = StringPool.BLANK;
416     private String _templateExtension = "vm";
417     private Properties _settings = new Properties();
418     private boolean _wapTheme;
419     private Map<String, ColorScheme> _colorSchemesMap =
420         new HashMap<String, ColorScheme>();
421     private String _servletContextName = StringPool.BLANK;
422     private boolean _warFile;
423     private boolean _loadFromServletContext;
424 
425 }