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