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