1
22
23 package com.liferay.portal.events;
24
25 import com.liferay.portal.kernel.events.Action;
26 import com.liferay.portal.kernel.events.ActionException;
27 import com.liferay.portal.kernel.log.Log;
28 import com.liferay.portal.kernel.log.LogFactoryUtil;
29 import com.liferay.portal.kernel.util.GetterUtil;
30 import com.liferay.portal.kernel.util.Randomizer;
31 import com.liferay.portal.model.Group;
32 import com.liferay.portal.model.GroupConstants;
33 import com.liferay.portal.model.Layout;
34 import com.liferay.portal.model.LayoutTypePortlet;
35 import com.liferay.portal.service.GroupLocalServiceUtil;
36 import com.liferay.portal.service.LayoutLocalServiceUtil;
37 import com.liferay.portal.theme.ThemeDisplay;
38 import com.liferay.portal.util.WebKeys;
39
40 import java.util.List;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44
45
51 public class RandomLayoutAction extends Action {
52
53 public void run(HttpServletRequest request, HttpServletResponse response)
54 throws ActionException {
55
56 try {
57
58
60 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
61 WebKeys.THEME_DISPLAY);
62
63 if (!themeDisplay.isSignedIn()) {
64 return;
65 }
66
67
69 String requestURI = GetterUtil.getString(request.getRequestURI());
70
71 if (!requestURI.endsWith("/portal/layout")) {
72 return;
73 }
74
75
78 Layout layout = themeDisplay.getLayout();
79
80 if (layout == null) {
81 return;
82 }
83
84 Group generalGuestGroup = GroupLocalServiceUtil.getGroup(
85 themeDisplay.getCompanyId(), GroupConstants.GUEST);
86
87 List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
88 generalGuestGroup.getGroupId(), false);
89
90 if (layouts.size() > 0) {
91 Layout randomLayout = layouts.get(
92 Randomizer.getInstance().nextInt(layouts.size()));
93
94 themeDisplay.setLayout(randomLayout);
95 themeDisplay.setLayoutTypePortlet(
96 (LayoutTypePortlet)randomLayout.getLayoutType());
97
98 request.setAttribute(WebKeys.LAYOUT, randomLayout);
99 }
100 }
101 catch (Exception e) {
102 _log.error(e, e);
103
104 throw new ActionException(e);
105 }
106 }
107
108 private static Log _log =
109 LogFactoryUtil.getLog(RandomLookAndFeelAction.class);
110
111 }