001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.events;
016    
017    import com.liferay.portal.kernel.events.Action;
018    import com.liferay.portal.kernel.events.ActionException;
019    import com.liferay.portal.kernel.log.Log;
020    import com.liferay.portal.kernel.log.LogFactoryUtil;
021    import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
022    import com.liferay.portal.model.ColorScheme;
023    import com.liferay.portal.model.Layout;
024    import com.liferay.portal.model.Theme;
025    import com.liferay.portal.model.impl.ColorSchemeImpl;
026    import com.liferay.portal.model.impl.ThemeImpl;
027    import com.liferay.portal.service.ThemeLocalServiceUtil;
028    import com.liferay.portal.theme.ThemeDisplay;
029    import com.liferay.portal.util.WebKeys;
030    
031    import javax.servlet.http.HttpServletRequest;
032    import javax.servlet.http.HttpServletResponse;
033    
034    /**
035     * @author Edward Han
036     */
037    public class ThemeServicePreAction extends Action {
038    
039            @Override
040            public void run(HttpServletRequest request, HttpServletResponse response)
041                    throws ActionException {
042    
043                    try {
044                            servicePre(request, response);
045                    }
046                    catch (Exception e) {
047                            throw new ActionException(e);
048                    }
049            }
050    
051            protected void servicePre(
052                            HttpServletRequest request, HttpServletResponse response)
053                    throws Exception {
054    
055                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
056                            WebKeys.THEME_DISPLAY);
057    
058                    Theme theme = themeDisplay.getTheme();
059                    ColorScheme colorScheme = themeDisplay.getColorScheme();
060    
061                    if (theme != null) {
062                            if (_log.isInfoEnabled()) {
063                                    _log.info("Theme is already set");
064                            }
065    
066                            return;
067                    }
068    
069                    Layout layout = themeDisplay.getLayout();
070    
071                    boolean wapTheme = BrowserSnifferUtil.isWap(request);
072    
073                    if (layout != null) {
074                            if (wapTheme) {
075                                    theme = layout.getWapTheme();
076                                    colorScheme = layout.getWapColorScheme();
077                            }
078                            else {
079                                    theme = layout.getTheme();
080                                    colorScheme = layout.getColorScheme();
081                            }
082                    }
083                    else {
084                            String themeId = null;
085                            String colorSchemeId = null;
086    
087                            if (wapTheme) {
088                                    themeId = ThemeImpl.getDefaultWapThemeId(
089                                            themeDisplay.getCompanyId());
090                                    colorSchemeId = ColorSchemeImpl.getDefaultWapColorSchemeId();
091                            }
092                            else {
093                                    themeId = ThemeImpl.getDefaultRegularThemeId(
094                                            themeDisplay.getCompanyId());
095                                    colorSchemeId =
096                                            ColorSchemeImpl.getDefaultRegularColorSchemeId();
097                            }
098    
099                            theme = ThemeLocalServiceUtil.getTheme(
100                                    themeDisplay.getCompanyId(), themeId, wapTheme);
101                            colorScheme = ThemeLocalServiceUtil.getColorScheme(
102                                    themeDisplay.getCompanyId(), theme.getThemeId(), colorSchemeId,
103                                    wapTheme);
104                    }
105    
106                    request.setAttribute(WebKeys.THEME, theme);
107                    request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
108    
109                    themeDisplay.setLookAndFeel(theme, colorScheme);
110            }
111    
112            private static Log _log = LogFactoryUtil.getLog(
113                    ThemeServicePreAction.class);
114    
115    }