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.util.GetterUtil;
22  import com.liferay.portal.kernel.util.Randomizer;
23  import com.liferay.portal.model.Group;
24  import com.liferay.portal.model.GroupConstants;
25  import com.liferay.portal.model.Layout;
26  import com.liferay.portal.model.LayoutTypePortlet;
27  import com.liferay.portal.service.GroupLocalServiceUtil;
28  import com.liferay.portal.service.LayoutLocalServiceUtil;
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="RandomLayoutAction.java.html"><b><i>View Source</i></b></a>
39   *
40   * @author Brian Wing Shun Chan
41   */
42  public class RandomLayoutAction extends Action {
43  
44      public void run(HttpServletRequest request, HttpServletResponse response)
45          throws ActionException {
46  
47          try {
48  
49              // Do not randomize layout 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 layout unless the user is accessing the portal
59  
60              String requestURI = GetterUtil.getString(request.getRequestURI());
61  
62              if (!requestURI.endsWith("/portal/layout")) {
63                  return;
64              }
65  
66              // Do not randomize layout unless the user is accessing a personal
67              // layout
68  
69              Layout layout = themeDisplay.getLayout();
70  
71              if (layout == null) {
72                  return;
73              }
74  
75              Group generalGuestGroup = GroupLocalServiceUtil.getGroup(
76                  themeDisplay.getCompanyId(), GroupConstants.GUEST);
77  
78              List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
79                  generalGuestGroup.getGroupId(), false);
80  
81              if (layouts.size() > 0) {
82                  Layout randomLayout = layouts.get(
83                      Randomizer.getInstance().nextInt(layouts.size()));
84  
85                  themeDisplay.setLayout(randomLayout);
86                  themeDisplay.setLayoutTypePortlet(
87                      (LayoutTypePortlet)randomLayout.getLayoutType());
88  
89                  request.setAttribute(WebKeys.LAYOUT, randomLayout);
90              }
91          }
92          catch (Exception e) {
93              _log.error(e, e);
94  
95              throw new ActionException(e);
96          }
97      }
98  
99      private static Log _log = LogFactoryUtil.getLog(
100         RandomLookAndFeelAction.class);
101 
102 }