1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portal.model.impl;
21  
22  import com.liferay.portal.SystemException;
23  import com.liferay.portal.kernel.log.Log;
24  import com.liferay.portal.kernel.log.LogFactoryUtil;
25  import com.liferay.portal.kernel.util.ListUtil;
26  import com.liferay.portal.kernel.util.StringPool;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.Validator;
29  import com.liferay.portal.model.ColorScheme;
30  import com.liferay.portal.model.Plugin;
31  import com.liferay.portal.model.SpriteImage;
32  import com.liferay.portal.model.Theme;
33  import com.liferay.portal.theme.ThemeCompanyId;
34  import com.liferay.portal.theme.ThemeCompanyLimit;
35  import com.liferay.portal.theme.ThemeGroupLimit;
36  import com.liferay.portal.util.PortalUtil;
37  import com.liferay.portal.util.PrefsPropsUtil;
38  import com.liferay.portal.util.PropsKeys;
39  import com.liferay.portal.util.PropsValues;
40  import com.liferay.portal.velocity.VelocityResourceListener;
41  
42  import java.util.HashMap;
43  import java.util.Iterator;
44  import java.util.List;
45  import java.util.Map;
46  import java.util.Properties;
47  
48  /**
49   * <a href="ThemeImpl.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Brian Wing Shun Chan
52   *
53   */
54  public class ThemeImpl extends PluginBaseImpl implements Theme {
55  
56      /**
57       * @deprecated
58       */
59      public static String getDefaultRegularThemeId() {
60          return PortalUtil.getJsSafePortletId(
61              PropsValues.DEFAULT_REGULAR_THEME_ID);
62      }
63  
64      public static String getDefaultRegularThemeId(long companyId)
65          throws SystemException {
66  
67          String defaultRegularThemeId = PrefsPropsUtil.getString(
68              companyId, PropsKeys.DEFAULT_REGULAR_THEME_ID);
69  
70          return PortalUtil.getJsSafePortletId(defaultRegularThemeId);
71      }
72  
73      /**
74       * @deprecated
75       */
76      public static String getDefaultWapThemeId() {
77          return PortalUtil.getJsSafePortletId(PropsValues.DEFAULT_WAP_THEME_ID);
78      }
79  
80      public static String getDefaultWapThemeId(long companyId)
81          throws SystemException {
82  
83          String defaultWapThemeId = PrefsPropsUtil.getString(
84              companyId, PropsKeys.DEFAULT_WAP_THEME_ID);
85  
86          return PortalUtil.getJsSafePortletId(defaultWapThemeId);
87      }
88  
89      public ThemeImpl() {
90      }
91  
92      public ThemeImpl(String themeId) {
93          _themeId = themeId;
94      }
95  
96      public ThemeImpl(String themeId, String name) {
97          _themeId = themeId;
98          _name = name;
99      }
100 
101     public String getThemeId() {
102         return _themeId;
103     }
104 
105     public String getPluginId() {
106         return getThemeId();
107     }
108 
109     public String getPluginType() {
110         return Plugin.TYPE_THEME;
111     }
112 
113     public ThemeCompanyLimit getThemeCompanyLimit() {
114         return _themeCompanyLimit;
115     }
116 
117     public void setThemeCompanyLimit(ThemeCompanyLimit themeCompanyLimit) {
118         _themeCompanyLimit = themeCompanyLimit;
119     }
120 
121     public boolean isCompanyAvailable(long companyId) {
122         return isAvailable(getThemeCompanyLimit(), companyId);
123     }
124 
125     public ThemeGroupLimit getThemeGroupLimit() {
126         return _themeGroupLimit;
127     }
128 
129     public void setThemeGroupLimit(ThemeGroupLimit themeGroupLimit) {
130         _themeGroupLimit = themeGroupLimit;
131     }
132 
133     public boolean isGroupAvailable(long groupId) {
134         return isAvailable(getThemeGroupLimit(), groupId);
135     }
136 
137     public long getTimestamp() {
138         return _timestamp;
139     }
140 
141     public void setTimestamp(long timestamp) {
142         _timestamp = timestamp;
143     }
144 
145     public String getName() {
146         return _name;
147     }
148 
149     public void setName(String name) {
150         _name = name;
151     }
152 
153     public String getRootPath() {
154         return _rootPath;
155     }
156 
157     public void setRootPath(String rootPath) {
158         _rootPath = rootPath;
159     }
160 
161     public String getTemplatesPath() {
162         return _templatesPath;
163     }
164 
165     public void setTemplatesPath(String templatesPath) {
166         _templatesPath = templatesPath;
167     }
168 
169     public String getCssPath() {
170         return _cssPath;
171     }
172 
173     public void setCssPath(String cssPath) {
174         _cssPath = cssPath;
175     }
176 
177     public String getImagesPath() {
178         return _imagesPath;
179     }
180 
181     public void setImagesPath(String imagesPath) {
182         _imagesPath = imagesPath;
183     }
184 
185     public String getJavaScriptPath() {
186         return _javaScriptPath;
187     }
188 
189     public void setJavaScriptPath(String javaScriptPath) {
190         _javaScriptPath = javaScriptPath;
191     }
192 
193     public String getVirtualPath() {
194         return _virtualPath;
195     }
196 
197     public void setVirtualPath(String virtualPath) {
198         if (_warFile && Validator.isNull(virtualPath)) {
199             virtualPath = PropsValues.THEME_VIRTUAL_PATH;
200         }
201 
202         _virtualPath = virtualPath;
203     }
204 
205     public String getTemplateExtension() {
206         return _templateExtension;
207     }
208 
209     public void setTemplateExtension(String templateExtension) {
210         _templateExtension = templateExtension;
211     }
212 
213     public Properties getSettings() {
214         return _settings;
215     }
216 
217     public String getSetting(String key) {
218         return _settings.getProperty(key);
219     }
220 
221     public void setSetting(String key, String value) {
222         _settings.setProperty(key, value);
223     }
224 
225     public boolean getWapTheme() {
226         return _wapTheme;
227     }
228 
229     public boolean isWapTheme() {
230         return _wapTheme;
231     }
232 
233     public void setWapTheme(boolean wapTheme) {
234         _wapTheme = wapTheme;
235     }
236 
237     public List<ColorScheme> getColorSchemes() {
238         List<ColorScheme> colorSchemes = ListUtil.fromCollection(
239             _colorSchemesMap.values());
240 
241         return ListUtil.sort(colorSchemes);
242     }
243 
244     public Map<String, ColorScheme> getColorSchemesMap() {
245         return _colorSchemesMap;
246     }
247 
248     public boolean hasColorSchemes() {
249         if (_colorSchemesMap.size() > 0) {
250             return true;
251         }
252         else {
253             return false;
254         }
255     }
256 
257     public SpriteImage getSpriteImage(String fileName) {
258         return _spriteImagesMap.get(fileName);
259     }
260 
261     public void setSpriteImages(
262         String spriteFileName, Properties spriteProperties) {
263 
264         Iterator<Map.Entry<Object, Object>> itr =
265             spriteProperties.entrySet().iterator();
266 
267         while (itr.hasNext()) {
268             Map.Entry<Object, Object> entry = itr.next();
269 
270             String key = (String)entry.getKey();
271             String value = (String)entry.getValue();
272 
273             int[] values = StringUtil.split(value, 0);
274 
275             int offset = values[0];
276             int height = values[1];
277             int width = values[2];
278 
279             SpriteImage spriteImage = new SpriteImage(
280                 spriteFileName, key, offset, height, width);
281 
282             _spriteImagesMap.put(key, spriteImage);
283         }
284     }
285 
286     public String getServletContextName() {
287         return _servletContextName;
288     }
289 
290     public void setServletContextName(String servletContextName) {
291         _servletContextName = servletContextName;
292 
293         if (Validator.isNotNull(_servletContextName)) {
294             _warFile = true;
295         }
296         else {
297             _warFile = false;
298         }
299     }
300 
301     public boolean getWARFile() {
302         return _warFile;
303     }
304 
305     public boolean isWARFile() {
306         return _warFile;
307     }
308 
309     public String getContextPath() {
310         String virtualPath = getVirtualPath();
311 
312         if (Validator.isNotNull(virtualPath)) {
313             return virtualPath;
314         }
315 
316         if (isWARFile()) {
317             StringBuilder sb = new StringBuilder();
318 
319             sb.append(StringPool.SLASH);
320             sb.append(getServletContextName());
321 
322             return sb.toString();
323         }
324         else {
325             return PortalUtil.getPathContext();
326         }
327     }
328 
329     public boolean getLoadFromServletContext() {
330         return _loadFromServletContext;
331     }
332 
333     public boolean isLoadFromServletContext() {
334         return _loadFromServletContext;
335     }
336 
337     public void setLoadFromServletContext(boolean loadFromServletContext) {
338         _loadFromServletContext = loadFromServletContext;
339     }
340 
341     public String getVelocityResourceListener() {
342         if (_loadFromServletContext) {
343             return VelocityResourceListener.SERVLET_SEPARATOR;
344         }
345         else {
346             return VelocityResourceListener.THEME_LOADER_SEPARATOR;
347         }
348     }
349 
350     public int compareTo(Theme theme) {
351         return getName().compareTo(theme.getName());
352     }
353 
354     public boolean equals(Object obj) {
355         if (obj == null) {
356             return false;
357         }
358 
359         Theme theme = null;
360 
361         try {
362             theme = (Theme)obj;
363         }
364         catch (ClassCastException cce) {
365             return false;
366         }
367 
368         String themeId = theme.getThemeId();
369 
370         if (getThemeId().equals(themeId)) {
371             return true;
372         }
373         else {
374             return false;
375         }
376     }
377 
378     protected boolean isAvailable(ThemeCompanyLimit limit, long id) {
379         boolean available = true;
380 
381         if (_log.isDebugEnabled()) {
382             _log.debug(
383                 "Check if theme " + getThemeId() + " is available for " + id);
384         }
385 
386         if (limit != null) {
387             List<ThemeCompanyId> includes = limit.getIncludes();
388             List<ThemeCompanyId> excludes = limit.getExcludes();
389 
390             if ((includes.size() != 0) && (excludes.size() != 0)) {
391 
392                 // Since includes and excludes are specified, check to
393                 // make sure the current company id is included and also
394                 // not excluded
395 
396                 if (_log.isDebugEnabled()) {
397                     _log.debug("Check includes and excludes");
398                 }
399 
400                 available = limit.isIncluded(id);
401 
402                 if (available) {
403                     available = !limit.isExcluded(id);
404                 }
405             }
406             else if ((includes.size() == 0) && (excludes.size() != 0)) {
407 
408                 // Since no includes are specified, check to make sure
409                 // the current company id is not excluded
410 
411                 if (_log.isDebugEnabled()) {
412                     _log.debug("Check excludes");
413                 }
414 
415                 available = !limit.isExcluded(id);
416             }
417             else if ((includes.size() != 0) && (excludes.size() == 0)) {
418 
419                 // Since no excludes are specified, check to make sure
420                 // the current company id is included
421 
422                 if (_log.isDebugEnabled()) {
423                     _log.debug("Check includes");
424                 }
425 
426                 available = limit.isIncluded(id);
427             }
428             else {
429 
430                 // Since no includes or excludes are specified, this
431                 // theme is available for every company
432 
433                 if (_log.isDebugEnabled()) {
434                     _log.debug("No includes or excludes set");
435                 }
436 
437                 available = true;
438             }
439         }
440 
441         if (_log.isDebugEnabled()) {
442             _log.debug(
443                 "Theme " + getThemeId() + " is " +
444                     (!available ? "NOT " : "") + "available for " + id);
445         }
446 
447         return available;
448     }
449 
450     private static Log _log = LogFactoryUtil.getLog(ThemeImpl.class);
451 
452     private String _themeId;
453     private ThemeCompanyLimit _themeCompanyLimit;
454     private ThemeGroupLimit _themeGroupLimit;
455     private long _timestamp;
456     private String _name;
457     private String _rootPath = "/";
458     private String _templatesPath = "${root-path}/templates";
459     private String _cssPath = "${root-path}/css";
460     private String _imagesPath = "${root-path}/images";
461     private String _javaScriptPath = "${root-path}/javascript";
462     private String _virtualPath = StringPool.BLANK;
463     private String _templateExtension = "vm";
464     private Properties _settings = new Properties();
465     private boolean _wapTheme;
466     private Map<String, ColorScheme> _colorSchemesMap =
467         new HashMap<String, ColorScheme>();
468     private Map<String, SpriteImage> _spriteImagesMap =
469         new HashMap<String, SpriteImage>();
470     private String _servletContextName = StringPool.BLANK;
471     private boolean _warFile;
472     private boolean _loadFromServletContext;
473 
474 }