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