001    /**
002     * Copyright (c) 2000-present 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.model.ColorScheme;
022    import com.liferay.portal.kernel.model.Layout;
023    import com.liferay.portal.kernel.model.Theme;
024    import com.liferay.portal.kernel.service.ThemeLocalServiceUtil;
025    import com.liferay.portal.kernel.theme.ThemeDisplay;
026    import com.liferay.portal.kernel.util.ColorSchemeFactoryUtil;
027    import com.liferay.portal.kernel.util.ThemeFactoryUtil;
028    import com.liferay.portal.kernel.util.WebKeys;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.http.HttpServletResponse;
032    
033    /**
034     * @author Edward Han
035     */
036    public class ThemeServicePreAction extends Action {
037    
038            @Override
039            public void run(HttpServletRequest request, HttpServletResponse response)
040                    throws ActionException {
041    
042                    try {
043                            servicePre(request, response);
044                    }
045                    catch (Exception e) {
046                            throw new ActionException(e);
047                    }
048            }
049    
050            protected void servicePre(
051                            HttpServletRequest request, HttpServletResponse response)
052                    throws Exception {
053    
054                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
055                            WebKeys.THEME_DISPLAY);
056    
057                    Theme theme = themeDisplay.getTheme();
058                    ColorScheme colorScheme = themeDisplay.getColorScheme();
059    
060                    if (theme != null) {
061                            if (_log.isInfoEnabled()) {
062                                    _log.info("Theme is already set");
063                            }
064    
065                            return;
066                    }
067    
068                    Layout layout = themeDisplay.getLayout();
069    
070                    if (layout != null) {
071                            theme = layout.getTheme();
072                            colorScheme = layout.getColorScheme();
073                    }
074                    else {
075                            String themeId = ThemeFactoryUtil.getDefaultRegularThemeId(
076                                    themeDisplay.getCompanyId());
077                            String colorSchemeId =
078                                    ColorSchemeFactoryUtil.getDefaultRegularColorSchemeId();
079    
080                            theme = ThemeLocalServiceUtil.getTheme(
081                                    themeDisplay.getCompanyId(), themeId);
082                            colorScheme = ThemeLocalServiceUtil.getColorScheme(
083                                    themeDisplay.getCompanyId(), theme.getThemeId(), colorSchemeId);
084                    }
085    
086                    request.setAttribute(WebKeys.THEME, theme);
087                    request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
088    
089                    themeDisplay.setLookAndFeel(theme, colorScheme);
090            }
091    
092            private static final Log _log = LogFactoryUtil.getLog(
093                    ThemeServicePreAction.class);
094    
095    }