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