001
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.servlet.ServletContextPool;
022 import com.liferay.portal.kernel.util.ContextPathUtil;
023 import com.liferay.portal.kernel.util.ListUtil;
024 import com.liferay.portal.kernel.util.PropsKeys;
025 import com.liferay.portal.kernel.util.StringPool;
026 import com.liferay.portal.kernel.util.StringUtil;
027 import com.liferay.portal.kernel.util.ThemeHelper;
028 import com.liferay.portal.kernel.util.Validator;
029 import com.liferay.portal.model.ColorScheme;
030 import com.liferay.portal.model.Plugin;
031 import com.liferay.portal.model.SpriteImage;
032 import com.liferay.portal.model.Theme;
033 import com.liferay.portal.model.ThemeSetting;
034 import com.liferay.portal.theme.ThemeCompanyId;
035 import com.liferay.portal.theme.ThemeCompanyLimit;
036 import com.liferay.portal.theme.ThemeGroupLimit;
037 import com.liferay.portal.util.PortalUtil;
038 import com.liferay.portal.util.PrefsPropsUtil;
039 import com.liferay.portal.util.PropsValues;
040 import com.liferay.portal.velocity.VelocityResourceListener;
041
042 import java.util.HashMap;
043 import java.util.Iterator;
044 import java.util.LinkedHashMap;
045 import java.util.List;
046 import java.util.Map;
047 import java.util.Properties;
048 import java.util.concurrent.ConcurrentHashMap;
049
050 import javax.servlet.ServletContext;
051
052
057 public class ThemeImpl extends PluginBaseImpl implements Theme {
058
059
062 public static String getDefaultRegularThemeId() {
063 return PortalUtil.getJsSafePortletId(
064 PropsValues.DEFAULT_REGULAR_THEME_ID);
065 }
066
067 public static String getDefaultRegularThemeId(long companyId)
068 throws SystemException {
069
070 String defaultRegularThemeId = PrefsPropsUtil.getString(
071 companyId, PropsKeys.DEFAULT_REGULAR_THEME_ID);
072
073 return PortalUtil.getJsSafePortletId(defaultRegularThemeId);
074 }
075
076
079 public static String getDefaultWapThemeId() {
080 return PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_WAP_THEME_ID);
081 }
082
083 public static String getDefaultWapThemeId(long companyId)
084 throws SystemException {
085
086 String defaultWapThemeId = PrefsPropsUtil.getString(
087 companyId, PropsKeys.DEFAULT_WAP_THEME_ID);
088
089 return PortalUtil.getJsSafePortletId(defaultWapThemeId);
090 }
091
092 public ThemeImpl() {
093 }
094
095 public ThemeImpl(String themeId) {
096 _themeId = themeId;
097 }
098
099 public ThemeImpl(String themeId, String name) {
100 _themeId = themeId;
101 _name = name;
102 }
103
104 public void addSetting(
105 String key, String value, boolean configurable, String type,
106 String[] options, String script) {
107
108 ThemeSetting themeSetting = new ThemeSettingImpl(
109 configurable, options, script, type, value);
110
111 _themeSettingsMap.put(key, themeSetting);
112 }
113
114 public int compareTo(Theme theme) {
115 return getName().compareTo(theme.getName());
116 }
117
118 @Override
119 public boolean equals(Object obj) {
120 if (obj == null) {
121 return false;
122 }
123
124 Theme theme = null;
125
126 try {
127 theme = (Theme)obj;
128 }
129 catch (ClassCastException cce) {
130 return false;
131 }
132
133 String themeId = theme.getThemeId();
134
135 if (getThemeId().equals(themeId)) {
136 return true;
137 }
138 else {
139 return false;
140 }
141 }
142
143 public List<ColorScheme> getColorSchemes() {
144 List<ColorScheme> colorSchemes = ListUtil.fromMapValues(
145 _colorSchemesMap);
146
147 return ListUtil.sort(colorSchemes);
148 }
149
150 public Map<String, ColorScheme> getColorSchemesMap() {
151 return _colorSchemesMap;
152 }
153
154 public Map<String, ThemeSetting> getConfigurableSettings() {
155 Map<String, ThemeSetting> configurableSettings =
156 new LinkedHashMap<String, ThemeSetting>();
157
158 for (Map.Entry<String, ThemeSetting> entry :
159 _themeSettingsMap.entrySet()) {
160
161 ThemeSetting themeSetting = entry.getValue();
162
163 if (themeSetting.isConfigurable()) {
164 configurableSettings.put(entry.getKey(), entry.getValue());
165 }
166 }
167
168 return configurableSettings;
169 }
170
171 public String getContextPath() {
172 if (!isWARFile()) {
173 return PortalUtil.getPathContext();
174 }
175
176 String servletContextName = getServletContextName();
177
178 if (ServletContextPool.containsKey(servletContextName)) {
179 ServletContext servletContext = ServletContextPool.get(
180 servletContextName);
181
182 return ContextPathUtil.getContextPath(servletContext);
183 }
184
185 return StringPool.SLASH.concat(servletContextName);
186 }
187
188 public String getCssPath() {
189 return _cssPath;
190 }
191
192 public String getDevice() {
193 if (isWapTheme()) {
194 return "wap";
195 }
196 else {
197 return "regular";
198 }
199 }
200
201 public String getFreeMarkerTemplateLoader() {
202 if (_loadFromServletContext) {
203 return FreeMarkerTemplateLoader.SERVLET_SEPARATOR;
204 }
205 else {
206 return FreeMarkerTemplateLoader.THEME_LOADER_SEPARATOR;
207 }
208 }
209
210 public String getImagesPath() {
211 return _imagesPath;
212 }
213
214 public String getJavaScriptPath() {
215 return _javaScriptPath;
216 }
217
218 public boolean getLoadFromServletContext() {
219 return _loadFromServletContext;
220 }
221
222 public String getName() {
223 return _name;
224 }
225
226 public String getPluginId() {
227 return getThemeId();
228 }
229
230 public String getPluginType() {
231 return Plugin.TYPE_THEME;
232 }
233
234 public String getResourcePath(
235 ServletContext servletContext, String portletId, String path) {
236
237 if (!PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED) {
238 return ThemeHelper.getResourcePath(
239 servletContext, this, portletId, path);
240 }
241
242 String key = path;
243
244 if (Validator.isNotNull(portletId)) {
245 key = path.concat(StringPool.POUND).concat(portletId);
246 }
247
248 String resourcePath = _resourcePathsMap.get(key);
249
250 if (resourcePath != null) {
251 return resourcePath;
252 }
253
254 resourcePath = ThemeHelper.getResourcePath(
255 servletContext, this, portletId, path);
256
257 _resourcePathsMap.put(key, resourcePath);
258
259 return resourcePath;
260 }
261
262 public String getRootPath() {
263 return _rootPath;
264 }
265
266 public String getServletContextName() {
267 return _servletContextName;
268 }
269
270 public String getSetting(String key) {
271 String value = null;
272
273 ThemeSetting themeSetting = _themeSettingsMap.get(key);
274
275 if (themeSetting != null) {
276 value = themeSetting.getValue();
277 }
278
279 return value;
280 }
281
282 public String[] getSettingOptions(String key) {
283 String[] options = null;
284
285 ThemeSetting themeSetting = _themeSettingsMap.get(key);
286
287 if (themeSetting != null) {
288 options = themeSetting.getOptions();
289 }
290
291 return options;
292 }
293
294 public Map<String, ThemeSetting> getSettings() {
295 return _themeSettingsMap;
296 }
297
298 public Properties getSettingsProperties() {
299 Properties properties = new Properties();
300
301 for (String key : _themeSettingsMap.keySet()) {
302 ThemeSetting setting = _themeSettingsMap.get(key);
303
304 if (setting != null) {
305 properties.setProperty(key, setting.getValue());
306 }
307 }
308
309 return properties;
310 }
311
312 public SpriteImage getSpriteImage(String fileName) {
313 return _spriteImagesMap.get(fileName);
314 }
315
316 public String getStaticResourcePath() {
317 String proxyPath = PortalUtil.getPathProxy();
318
319 String virtualPath = getVirtualPath();
320
321 if (Validator.isNotNull(virtualPath)) {
322 return proxyPath.concat(virtualPath);
323 }
324
325 String contextPath = getContextPath();
326
327 if (!isWARFile()) {
328 return contextPath;
329 }
330
331 return proxyPath.concat(contextPath);
332 }
333
334 public String getTemplateExtension() {
335 return _templateExtension;
336 }
337
338 public String getTemplatesPath() {
339 return _templatesPath;
340 }
341
342 public ThemeCompanyLimit getThemeCompanyLimit() {
343 return _themeCompanyLimit;
344 }
345
346 public ThemeGroupLimit getThemeGroupLimit() {
347 return _themeGroupLimit;
348 }
349
350 public String getThemeId() {
351 return _themeId;
352 }
353
354 public long getTimestamp() {
355 return _timestamp;
356 }
357
358 public String getVelocityResourceListener() {
359 if (_loadFromServletContext) {
360 return VelocityResourceListener.SERVLET_SEPARATOR;
361 }
362 else {
363 return VelocityResourceListener.THEME_LOADER_SEPARATOR;
364 }
365 }
366
367 public String getVirtualPath() {
368 return _virtualPath;
369 }
370
371 public boolean getWapTheme() {
372 return _wapTheme;
373 }
374
375 public boolean getWARFile() {
376 return _warFile;
377 }
378
379 public boolean hasColorSchemes() {
380 if (_colorSchemesMap.size() > 0) {
381 return true;
382 }
383 else {
384 return false;
385 }
386 }
387
388 @Override
389 public int hashCode() {
390 return _themeId.hashCode();
391 }
392
393 public boolean isCompanyAvailable(long companyId) {
394 return isAvailable(getThemeCompanyLimit(), companyId);
395 }
396
397 public boolean isGroupAvailable(long groupId) {
398 return isAvailable(getThemeGroupLimit(), groupId);
399 }
400
401 public boolean isLoadFromServletContext() {
402 return _loadFromServletContext;
403 }
404
405 public boolean isWapTheme() {
406 return _wapTheme;
407 }
408
409 public boolean isWARFile() {
410 return _warFile;
411 }
412
413 public boolean resourceExists(
414 ServletContext servletContext, String portletId, String path)
415 throws Exception {
416
417 if (!PropsValues.LAYOUT_TEMPLATE_CACHE_ENABLED) {
418 return ThemeHelper.resourceExists(
419 servletContext, this, portletId, path);
420 }
421
422 String key = path;
423
424 if (Validator.isNotNull(portletId)) {
425 key = path.concat(StringPool.POUND).concat(portletId);
426 }
427
428 Boolean resourceExists = _resourceExistsMap.get(key);
429
430 if (resourceExists != null) {
431 return resourceExists;
432 }
433
434 resourceExists = ThemeHelper.resourceExists(
435 servletContext, this, portletId, path);
436
437 _resourceExistsMap.put(key, resourceExists);
438
439 return resourceExists;
440 }
441
442 public void setCssPath(String cssPath) {
443 _cssPath = cssPath;
444 }
445
446 public void setImagesPath(String imagesPath) {
447 _imagesPath = imagesPath;
448 }
449
450 public void setJavaScriptPath(String javaScriptPath) {
451 _javaScriptPath = javaScriptPath;
452 }
453
454 public void setLoadFromServletContext(boolean loadFromServletContext) {
455 _loadFromServletContext = loadFromServletContext;
456 }
457
458 public void setName(String name) {
459 _name = name;
460 }
461
462 public void setRootPath(String rootPath) {
463 _rootPath = rootPath;
464 }
465
466 public void setServletContextName(String servletContextName) {
467 _servletContextName = servletContextName;
468
469 if (Validator.isNotNull(_servletContextName)) {
470 _warFile = true;
471 }
472 else {
473 _warFile = false;
474 }
475 }
476
477 public void setSetting(String key, String value) {
478 ThemeSetting themeSetting = _themeSettingsMap.get(key);
479
480 if (themeSetting != null) {
481 themeSetting.setValue(value);
482 }
483 else {
484 addSetting(key, value, false, null, null, null);
485 }
486 }
487
488 public void setSpriteImages(
489 String spriteFileName, Properties spriteProperties) {
490
491 Iterator<Map.Entry<Object, Object>> itr =
492 spriteProperties.entrySet().iterator();
493
494 while (itr.hasNext()) {
495 Map.Entry<Object, Object> entry = itr.next();
496
497 String key = (String)entry.getKey();
498 String value = (String)entry.getValue();
499
500 int[] values = StringUtil.split(value, 0);
501
502 int offset = values[0];
503 int height = values[1];
504 int width = values[2];
505
506 SpriteImage spriteImage = new SpriteImage(
507 spriteFileName, key, offset, height, width);
508
509 _spriteImagesMap.put(key, spriteImage);
510 }
511 }
512
513 public void setTemplateExtension(String templateExtension) {
514 _templateExtension = templateExtension;
515 }
516
517 public void setTemplatesPath(String templatesPath) {
518 _templatesPath = templatesPath;
519 }
520
521 public void setThemeCompanyLimit(ThemeCompanyLimit themeCompanyLimit) {
522 _themeCompanyLimit = themeCompanyLimit;
523 }
524
525 public void setThemeGroupLimit(ThemeGroupLimit themeGroupLimit) {
526 _themeGroupLimit = themeGroupLimit;
527 }
528
529 public void setTimestamp(long timestamp) {
530 _timestamp = timestamp;
531 }
532
533 public void setVirtualPath(String virtualPath) {
534 if (_warFile && Validator.isNull(virtualPath)) {
535 virtualPath = PropsValues.THEME_VIRTUAL_PATH;
536 }
537
538 _virtualPath = virtualPath;
539 }
540
541 public void setWapTheme(boolean wapTheme) {
542 _wapTheme = wapTheme;
543 }
544
545 protected boolean isAvailable(ThemeCompanyLimit limit, long id) {
546 boolean available = true;
547
548 if (_log.isDebugEnabled()) {
549 _log.debug(
550 "Check if theme " + getThemeId() + " is available for " + id);
551 }
552
553 if (limit != null) {
554 List<ThemeCompanyId> includes = limit.getIncludes();
555 List<ThemeCompanyId> excludes = limit.getExcludes();
556
557 if ((includes.size() != 0) && (excludes.size() != 0)) {
558
559
560
561
562
563 if (_log.isDebugEnabled()) {
564 _log.debug("Check includes and excludes");
565 }
566
567 available = limit.isIncluded(id);
568
569 if (available) {
570 available = !limit.isExcluded(id);
571 }
572 }
573 else if ((includes.size() == 0) && (excludes.size() != 0)) {
574
575
576
577
578 if (_log.isDebugEnabled()) {
579 _log.debug("Check excludes");
580 }
581
582 available = !limit.isExcluded(id);
583 }
584 else if ((includes.size() != 0) && (excludes.size() == 0)) {
585
586
587
588
589 if (_log.isDebugEnabled()) {
590 _log.debug("Check includes");
591 }
592
593 available = limit.isIncluded(id);
594 }
595 else {
596
597
598
599
600 if (_log.isDebugEnabled()) {
601 _log.debug("No includes or excludes set");
602 }
603
604 available = true;
605 }
606 }
607
608 if (_log.isDebugEnabled()) {
609 _log.debug(
610 "Theme " + getThemeId() + " is " +
611 (!available ? "NOT " : "") + "available for " + id);
612 }
613
614 return available;
615 }
616
617 private static Log _log = LogFactoryUtil.getLog(ThemeImpl.class);
618
619 private Map<String, ColorScheme> _colorSchemesMap =
620 new HashMap<String, ColorScheme>();
621 private String _cssPath = "${root-path}/css";
622 private String _imagesPath = "${root-path}/images";
623 private String _javaScriptPath = "${root-path}/js";
624 private boolean _loadFromServletContext;
625 private String _name;
626 private Map<String, Boolean> _resourceExistsMap =
627 new ConcurrentHashMap<String, Boolean>();
628 private Map<String, String> _resourcePathsMap =
629 new ConcurrentHashMap<String, String>();
630 private String _rootPath = "/";
631 private String _servletContextName = StringPool.BLANK;
632 private Map<String, ThemeSetting> _themeSettingsMap =
633 new LinkedHashMap<String, ThemeSetting>();
634 private Map<String, SpriteImage> _spriteImagesMap =
635 new HashMap<String, SpriteImage>();
636 private String _templateExtension = "vm";
637 private String _templatesPath = "${root-path}/templates";
638 private ThemeCompanyLimit _themeCompanyLimit;
639 private ThemeGroupLimit _themeGroupLimit;
640 private String _themeId;
641 private long _timestamp;
642 private String _virtualPath = StringPool.BLANK;
643 private boolean _wapTheme;
644 private boolean _warFile;
645
646 }