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