1
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
42 public class RandomLayoutAction extends Action {
43
44 public void run(HttpServletRequest request, HttpServletResponse response)
45 throws ActionException {
46
47 try {
48
49
51 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
52 WebKeys.THEME_DISPLAY);
53
54 if (!themeDisplay.isSignedIn()) {
55 return;
56 }
57
58
60 String requestURI = GetterUtil.getString(request.getRequestURI());
61
62 if (!requestURI.endsWith("/portal/layout")) {
63 return;
64 }
65
66
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 }