1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.events;
16  
17  import com.liferay.portal.kernel.events.Action;
18  import com.liferay.portal.kernel.events.ActionException;
19  import com.liferay.portal.kernel.log.Log;
20  import com.liferay.portal.kernel.log.LogFactoryUtil;
21  import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
22  import com.liferay.portal.kernel.util.GetterUtil;
23  import com.liferay.portal.kernel.util.Randomizer;
24  import com.liferay.portal.model.ColorScheme;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.model.Theme;
27  import com.liferay.portal.service.LayoutServiceUtil;
28  import com.liferay.portal.service.ThemeLocalServiceUtil;
29  import com.liferay.portal.theme.ThemeDisplay;
30  import com.liferay.portal.util.WebKeys;
31  
32  import java.util.List;
33  
34  import javax.servlet.http.HttpServletRequest;
35  import javax.servlet.http.HttpServletResponse;
36  
37  /**
38   * <a href="RandomLookAndFeelAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class RandomLookAndFeelAction extends Action {
43  
44      public void run(HttpServletRequest request, HttpServletResponse response)
45          throws ActionException {
46  
47          try {
48  
49              // Do not randomize look and feel unless the user is logged in
50  
51              ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
52                  WebKeys.THEME_DISPLAY);
53  
54              if (!themeDisplay.isSignedIn()) {
55                  return;
56              }
57  
58              // Do not randomize look and feel unless the user is accessing the
59              // portal
60  
61              String requestURI = GetterUtil.getString(request.getRequestURI());
62  
63              if (!requestURI.endsWith("/portal/layout")) {
64                  return;
65              }
66  
67              // Do not randomize look and feel unless the user is accessing a
68              // personal layout
69  
70              Layout layout = themeDisplay.getLayout();
71  
72              if (layout == null) {
73                  return;
74              }
75  
76              Randomizer randomizer = Randomizer.getInstance();
77  
78              boolean wapTheme = BrowserSnifferUtil.isWap(request);
79  
80              List<Theme> themes = ThemeLocalServiceUtil.getThemes(
81                  themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId(),
82                  themeDisplay.getUserId(), wapTheme);
83  
84              if (themes.size() > 0) {
85                  Theme theme = themes.get(randomizer.nextInt(themes.size()));
86  
87                  List<ColorScheme> colorSchemes = theme.getColorSchemes();
88  
89                  ColorScheme colorScheme = colorSchemes.get(
90                      randomizer.nextInt(colorSchemes.size()));
91  
92                  LayoutServiceUtil.updateLookAndFeel(
93                      layout.getGroupId(), layout.isPrivateLayout(),
94                      layout.getPlid(), theme.getThemeId(),
95                      colorScheme.getColorSchemeId(), layout.getCss(), wapTheme);
96  
97                  themeDisplay.setLookAndFeel(theme, colorScheme);
98  
99                  request.setAttribute(WebKeys.THEME, theme);
100                 request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
101             }
102         }
103         catch (Exception e) {
104             _log.error(e, e);
105 
106             throw new ActionException(e);
107         }
108     }
109 
110     private static Log _log = LogFactoryUtil.getLog(
111         RandomLookAndFeelAction.class);
112 
113 }