001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.PortalUtil;
030    import com.liferay.portal.util.WebKeys;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.http.HttpServletResponse;
034    
035    /**
036     * @author Edward Han
037     */
038    public class ThemeServicePreAction extends Action {
039    
040            @Override
041            public void run(HttpServletRequest request, HttpServletResponse response)
042                    throws ActionException {
043    
044                    try {
045                            servicePre(request, response);
046                    }
047                    catch (Exception e) {
048                            throw new ActionException(e);
049                    }
050            }
051    
052            protected void servicePre(
053                            HttpServletRequest request, HttpServletResponse response)
054                    throws Exception {
055    
056                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
057                            WebKeys.THEME_DISPLAY);
058    
059                    Theme theme = themeDisplay.getTheme();
060                    ColorScheme colorScheme = themeDisplay.getColorScheme();
061    
062                    if (theme != null) {
063                            if (_log.isInfoEnabled()) {
064                                    _log.info("Theme is already set");
065                            }
066    
067                            return;
068                    }
069    
070                    Layout layout = themeDisplay.getLayout();
071    
072                    boolean wapTheme = BrowserSnifferUtil.isWap(request);
073    
074                    if (layout != null) {
075                            if (wapTheme) {
076                                    theme = layout.getWapTheme();
077                                    colorScheme = layout.getWapColorScheme();
078                            }
079                            else {
080                                    theme = layout.getTheme();
081                                    colorScheme = layout.getColorScheme();
082                            }
083                    }
084                    else {
085                            String themeId = null;
086                            String colorSchemeId = null;
087    
088                            if (wapTheme) {
089                                    themeId = ThemeImpl.getDefaultWapThemeId(
090                                            themeDisplay.getCompanyId());
091                                    colorSchemeId = ColorSchemeImpl.getDefaultWapColorSchemeId();
092                            }
093                            else {
094                                    themeId = ThemeImpl.getDefaultRegularThemeId(
095                                            themeDisplay.getCompanyId());
096                                    colorSchemeId =
097                                            ColorSchemeImpl.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(
111                            PortalUtil.getPathContext(), theme, colorScheme);
112            }
113    
114            private static Log _log = LogFactoryUtil.getLog(
115                    ThemeServicePreAction.class);
116    
117    }