001    /**
002     * Copyright (c) 2000-2013 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.kernel.util.ColorSchemeFactoryUtil;
023    import com.liferay.portal.kernel.util.ThemeFactoryUtil;
024    import com.liferay.portal.model.ColorScheme;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.model.Theme;
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 = ThemeFactoryUtil.getDefaultWapThemeId(
089                                            themeDisplay.getCompanyId());
090                                    colorSchemeId =
091                                            ColorSchemeFactoryUtil.getDefaultWapColorSchemeId();
092                            }
093                            else {
094                                    themeId = ThemeFactoryUtil.getDefaultRegularThemeId(
095                                            themeDisplay.getCompanyId());
096                                    colorSchemeId =
097                                            ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
098                            }
099    
100                            theme = ThemeLocalServiceUtil.getTheme(
101                                    themeDisplay.getCompanyId(), themeId, wapTheme);
102                            colorScheme = ThemeLocalServiceUtil.getColorScheme(
103                                    themeDisplay.getCompanyId(), theme.getThemeId(), colorSchemeId,
104                                    wapTheme);
105                    }
106    
107                    request.setAttribute(WebKeys.THEME, theme);
108                    request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
109    
110                    themeDisplay.setLookAndFeel(theme, colorScheme);
111            }
112    
113            private static Log _log = LogFactoryUtil.getLog(
114                    ThemeServicePreAction.class);
115    
116    }