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.model.ColorScheme;
022 import com.liferay.portal.kernel.model.Layout;
023 import com.liferay.portal.kernel.model.Theme;
024 import com.liferay.portal.kernel.security.RandomUtil;
025 import com.liferay.portal.kernel.service.LayoutServiceUtil;
026 import com.liferay.portal.kernel.service.ThemeLocalServiceUtil;
027 import com.liferay.portal.kernel.theme.ThemeDisplay;
028 import com.liferay.portal.kernel.util.GetterUtil;
029 import com.liferay.portal.kernel.util.WebKeys;
030
031 import java.util.List;
032
033 import javax.servlet.http.HttpServletRequest;
034 import javax.servlet.http.HttpServletResponse;
035
036
039 public class RandomLookAndFeelAction extends Action {
040
041 @Override
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
059 String requestURI = GetterUtil.getString(request.getRequestURI());
060
061 if (!requestURI.endsWith("/portal/layout")) {
062 return;
063 }
064
065
066
067
068 Layout layout = themeDisplay.getLayout();
069
070 if (layout == null) {
071 return;
072 }
073
074 List<Theme> themes = ThemeLocalServiceUtil.getPageThemes(
075 themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId(),
076 themeDisplay.getUserId());
077
078 if (!themes.isEmpty()) {
079 Theme theme = themes.get(RandomUtil.nextInt(themes.size()));
080
081 List<ColorScheme> colorSchemes = theme.getColorSchemes();
082
083 ColorScheme colorScheme = colorSchemes.get(
084 RandomUtil.nextInt(colorSchemes.size()));
085
086 LayoutServiceUtil.updateLookAndFeel(
087 layout.getGroupId(), layout.isPrivateLayout(),
088 layout.getPlid(), theme.getThemeId(),
089 colorScheme.getColorSchemeId(), layout.getCss());
090
091 themeDisplay.setLookAndFeel(theme, colorScheme);
092
093 request.setAttribute(WebKeys.THEME, theme);
094 request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
095 }
096 }
097 catch (Exception e) {
098 _log.error(e, e);
099
100 throw new ActionException(e);
101 }
102 }
103
104 private static final Log _log = LogFactoryUtil.getLog(
105 RandomLookAndFeelAction.class);
106
107 }