001
014
015 package com.liferay.portal.events;
016
017 import com.liferay.portal.kernel.events.Action;
018 import com.liferay.portal.kernel.events.ActionException;
019 import com.liferay.portal.kernel.log.Log;
020 import com.liferay.portal.kernel.log.LogFactoryUtil;
021 import com.liferay.portal.kernel.util.GetterUtil;
022 import com.liferay.portal.kernel.util.Randomizer;
023 import com.liferay.portal.model.Group;
024 import com.liferay.portal.model.GroupConstants;
025 import com.liferay.portal.model.Layout;
026 import com.liferay.portal.model.LayoutTypePortlet;
027 import com.liferay.portal.service.GroupLocalServiceUtil;
028 import com.liferay.portal.service.LayoutLocalServiceUtil;
029 import com.liferay.portal.theme.ThemeDisplay;
030 import com.liferay.portal.util.WebKeys;
031
032 import java.util.List;
033
034 import javax.servlet.http.HttpServletRequest;
035 import javax.servlet.http.HttpServletResponse;
036
037
040 public class RandomLayoutAction extends Action {
041
042 public void run(HttpServletRequest request, HttpServletResponse response)
043 throws ActionException {
044
045 try {
046
047
048
049 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
050 WebKeys.THEME_DISPLAY);
051
052 if (!themeDisplay.isSignedIn()) {
053 return;
054 }
055
056
057
058 String requestURI = GetterUtil.getString(request.getRequestURI());
059
060 if (!requestURI.endsWith("/portal/layout")) {
061 return;
062 }
063
064
065
066
067 Layout layout = themeDisplay.getLayout();
068
069 if (layout == null) {
070 return;
071 }
072
073 Group generalGuestGroup = GroupLocalServiceUtil.getGroup(
074 themeDisplay.getCompanyId(), GroupConstants.GUEST);
075
076 List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
077 generalGuestGroup.getGroupId(), false);
078
079 if (layouts.size() > 0) {
080 Layout randomLayout = layouts.get(
081 Randomizer.getInstance().nextInt(layouts.size()));
082
083 themeDisplay.setLayout(randomLayout);
084 themeDisplay.setLayoutTypePortlet(
085 (LayoutTypePortlet)randomLayout.getLayoutType());
086
087 request.setAttribute(WebKeys.LAYOUT, randomLayout);
088 }
089 }
090 catch (Exception e) {
091 _log.error(e, e);
092
093 throw new ActionException(e);
094 }
095 }
096
097 private static Log _log = LogFactoryUtil.getLog(
098 RandomLookAndFeelAction.class);
099
100 }