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