1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.events;
24  
25  import com.liferay.portal.kernel.events.Action;
26  import com.liferay.portal.kernel.events.ActionException;
27  import com.liferay.portal.kernel.log.Log;
28  import com.liferay.portal.kernel.log.LogFactoryUtil;
29  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
30  import com.liferay.portal.kernel.util.GetterUtil;
31  import com.liferay.portal.kernel.util.Randomizer;
32  import com.liferay.portal.model.ColorScheme;
33  import com.liferay.portal.model.Layout;
34  import com.liferay.portal.model.Theme;
35  import com.liferay.portal.service.LayoutServiceUtil;
36  import com.liferay.portal.service.ThemeLocalServiceUtil;
37  import com.liferay.portal.theme.ThemeDisplay;
38  import com.liferay.portal.util.WebKeys;
39  
40  import java.util.List;
41  
42  import javax.servlet.http.HttpServletRequest;
43  import javax.servlet.http.HttpServletResponse;
44  
45  /**
46   * <a href="RandomLookAndFeelAction.java.html"><b><i>View Source</i></b></a>
47   *
48   * @author Brian Wing Shun Chan
49   *
50   */
51  public class RandomLookAndFeelAction extends Action {
52  
53      public void run(HttpServletRequest request, HttpServletResponse response)
54          throws ActionException {
55  
56          try {
57  
58              // Do not randomize look and feel unless the user is logged in
59  
60              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
61                  WebKeys.THEME_DISPLAY);
62  
63              if (!themeDisplay.isSignedIn()) {
64                  return;
65              }
66  
67              // Do not randomize look and feel unless the user is accessing the
68              // portal
69  
70              String requestURI = GetterUtil.getString(request.getRequestURI());
71  
72              if (!requestURI.endsWith("/portal/layout")) {
73                  return;
74              }
75  
76              // Do not randomize look and feel unless the user is accessing a
77              // personal layout
78  
79              Layout layout = themeDisplay.getLayout();
80  
81              if (layout == null) {
82                  return;
83              }
84  
85              Randomizer randomizer = Randomizer.getInstance();
86  
87              boolean wapTheme = BrowserSnifferUtil.isWap(request);
88  
89              List<Theme> themes = ThemeLocalServiceUtil.getThemes(
90                  themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId(),
91                  themeDisplay.getUserId(), wapTheme);
92  
93              if (themes.size() > 0) {
94                  Theme theme = themes.get(randomizer.nextInt(themes.size()));
95  
96                  List<ColorScheme> colorSchemes = theme.getColorSchemes();
97  
98                  ColorScheme colorScheme = colorSchemes.get(
99                      randomizer.nextInt(colorSchemes.size()));
100 
101                 LayoutServiceUtil.updateLookAndFeel(
102                     layout.getGroupId(), layout.isPrivateLayout(),
103                     layout.getPlid(), theme.getThemeId(),
104                     colorScheme.getColorSchemeId(), layout.getCss(), wapTheme);
105 
106                 themeDisplay.setLookAndFeel(theme, colorScheme);
107 
108                 request.setAttribute(WebKeys.THEME, theme);
109                 request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
110             }
111         }
112         catch (Exception e) {
113             _log.error(e, e);
114 
115             throw new ActionException(e);
116         }
117     }
118 
119     private static Log _log =
120          LogFactoryUtil.getLog(RandomLookAndFeelAction.class);
121 
122 }