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.security.RandomUtil;
022 import com.liferay.portal.kernel.servlet.BrowserSnifferUtil;
023 import com.liferay.portal.kernel.util.GetterUtil;
024 import com.liferay.portal.model.ColorScheme;
025 import com.liferay.portal.model.Layout;
026 import com.liferay.portal.model.Theme;
027 import com.liferay.portal.service.LayoutServiceUtil;
028 import com.liferay.portal.service.ThemeLocalServiceUtil;
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 RandomLookAndFeelAction extends Action {
041
042 @Override
043 public void run(HttpServletRequest request, HttpServletResponse response)
044 throws ActionException {
045
046 try {
047
048
049
050 ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
051 WebKeys.THEME_DISPLAY);
052
053 if (!themeDisplay.isSignedIn()) {
054 return;
055 }
056
057
058
059
060 String requestURI = GetterUtil.getString(request.getRequestURI());
061
062 if (!requestURI.endsWith("/portal/layout")) {
063 return;
064 }
065
066
067
068
069 Layout layout = themeDisplay.getLayout();
070
071 if (layout == null) {
072 return;
073 }
074
075 boolean wapTheme = BrowserSnifferUtil.isWap(request);
076
077 List<Theme> themes = ThemeLocalServiceUtil.getThemes(
078 themeDisplay.getCompanyId(), themeDisplay.getScopeGroupId(),
079 themeDisplay.getUserId(), wapTheme);
080
081 if (themes.size() > 0) {
082 Theme theme = themes.get(RandomUtil.nextInt(themes.size()));
083
084 List<ColorScheme> colorSchemes = theme.getColorSchemes();
085
086 ColorScheme colorScheme = colorSchemes.get(
087 RandomUtil.nextInt(colorSchemes.size()));
088
089 LayoutServiceUtil.updateLookAndFeel(
090 layout.getGroupId(), layout.isPrivateLayout(),
091 layout.getPlid(), theme.getThemeId(),
092 colorScheme.getColorSchemeId(), layout.getCss(), wapTheme);
093
094 themeDisplay.setLookAndFeel(theme, colorScheme);
095
096 request.setAttribute(WebKeys.THEME, theme);
097 request.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
098 }
099 }
100 catch (Exception e) {
101 _log.error(e, e);
102
103 throw new ActionException(e);
104 }
105 }
106
107 private static Log _log = LogFactoryUtil.getLog(
108 RandomLookAndFeelAction.class);
109
110 }