001    /**
002     * Copyright (c) 2000-2010 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.GetterUtil;
023    import com.liferay.portal.kernel.util.Randomizer;
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.LayoutServiceUtil;
028    import com.liferay.portal.service.ThemeLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.WebKeys;
031    
032    import java.util.List;
033    
034    import javax.servlet.http.HttpServletRequest;
035    import javax.servlet.http.HttpServletResponse;
036    
037    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class RandomLookAndFeelAction extends Action {
041    
042            public void run(HttpServletRequest request, HttpServletResponse response)
043                    throws ActionException {
044    
045                    try {
046    
047                            // Do not randomize look and feel unless the user is logged in
048    
049                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
050                                    WebKeys.THEME_DISPLAY);
051    
052                            if (!themeDisplay.isSignedIn()) {
053                                    return;
054                            }
055    
056                            // Do not randomize look and feel unless the user is accessing the
057                            // portal
058    
059                            String requestURI = GetterUtil.getString(request.getRequestURI());
060    
061                            if (!requestURI.endsWith("/portal/layout")) {
062                                    return;
063                            }
064    
065                            // Do not randomize look and feel unless the user is accessing a
066                            // personal layout
067    
068                            Layout layout = themeDisplay.getLayout();
069    
070                            if (layout == null) {
071                                    return;
072                            }
073    
074                            Randomizer randomizer = Randomizer.getInstance();
075    
076                            boolean wapTheme = BrowserSnifferUtil.isWap(request);
077    
078                            List<Theme> themes = ThemeLocalServiceUtil.getThemes(
079                                    themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId(),
080                                    themeDisplay.getUserId(), wapTheme);
081    
082                            if (themes.size() > 0) {
083                                    Theme theme = themes.get(randomizer.nextInt(themes.size()));
084    
085                                    List<ColorScheme> colorSchemes = theme.getColorSchemes();
086    
087                                    ColorScheme colorScheme = colorSchemes.get(
088                                            randomizer.nextInt(colorSchemes.size()));
089    
090                                    LayoutServiceUtil.updateLookAndFeel(
091                                            layout.getGroupId(), layout.isPrivateLayout(),
092                                            layout.getPlid(), theme.getThemeId(),
093                                            colorScheme.getColorSchemeId(), layout.getCss(), wapTheme);
094    
095                                    themeDisplay.setLookAndFeel(theme, colorScheme);
096    
097                                    request.setAttribute(WebKeys.THEME, theme);
098                                    request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
099                            }
100                    }
101                    catch (Exception e) {
102                            _log.error(e, e);
103    
104                            throw new ActionException(e);
105                    }
106            }
107    
108            private static Log _log = LogFactoryUtil.getLog(
109                    RandomLookAndFeelAction.class);
110    
111    }