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