001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.events;
022:
023: import com.liferay.portal.kernel.events.Action;
024: import com.liferay.portal.kernel.events.ActionException;
025: import com.liferay.portal.kernel.servlet.BrowserSniffer;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.Randomizer;
028: import com.liferay.portal.model.ColorScheme;
029: import com.liferay.portal.model.Layout;
030: import com.liferay.portal.model.Theme;
031: import com.liferay.portal.service.LayoutServiceUtil;
032: import com.liferay.portal.service.impl.ThemeLocalUtil;
033: import com.liferay.portal.theme.ThemeDisplay;
034: import com.liferay.portal.util.PropsUtil;
035: import com.liferay.portal.util.WebKeys;
036:
037: import java.util.List;
038:
039: import javax.servlet.http.HttpServletRequest;
040: import javax.servlet.http.HttpServletResponse;
041:
042: import org.apache.commons.logging.Log;
043: import org.apache.commons.logging.LogFactory;
044:
045: /**
046: * <a href="RandomLookAndFeelAction.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class RandomLookAndFeelAction extends Action {
052:
053: public void run(HttpServletRequest req, HttpServletResponse res)
054: throws ActionException {
055:
056: try {
057:
058: // Disable caching for CSS and JavaScript
059:
060: PropsUtil.set(PropsUtil.LAST_MODIFIED_CHECK, "false");
061:
062: // Do not randomize look and feel unless the user is logged in
063:
064: ThemeDisplay themeDisplay = (ThemeDisplay) req
065: .getAttribute(WebKeys.THEME_DISPLAY);
066:
067: if (!themeDisplay.isSignedIn()) {
068: return;
069: }
070:
071: // Do not randomize look and feel unless the user is accessing the
072: // portal
073:
074: String requestURI = GetterUtil.getString(req
075: .getRequestURI());
076:
077: if (!requestURI.endsWith("/portal/layout")) {
078: return;
079: }
080:
081: // Do not randomize look and feel unless the user is accessing a
082: // personal layout
083:
084: Layout layout = themeDisplay.getLayout();
085:
086: if (layout == null) {
087: return;
088: }
089:
090: Randomizer randomizer = Randomizer.getInstance();
091:
092: boolean wapTheme = BrowserSniffer.is_wap_xhtml(req);
093:
094: List themes = ThemeLocalUtil.getThemes(themeDisplay
095: .getCompanyId(), themeDisplay.getPortletGroupId(),
096: themeDisplay.getUserId(), wapTheme);
097:
098: if (themes.size() > 0) {
099: Theme theme = (Theme) themes.get(randomizer
100: .nextInt(themes.size()));
101:
102: List colorSchemes = theme.getColorSchemes();
103:
104: ColorScheme colorScheme = (ColorScheme) colorSchemes
105: .get(randomizer.nextInt(colorSchemes.size()));
106:
107: LayoutServiceUtil.updateLookAndFeel(
108: layout.getGroupId(), layout.isPrivateLayout(),
109: layout.getPlid(), theme.getThemeId(),
110: colorScheme.getColorSchemeId(),
111: layout.getCss(), wapTheme);
112:
113: themeDisplay.setLookAndFeel(theme, colorScheme);
114:
115: req.setAttribute(WebKeys.THEME, theme);
116: req.setAttribute(WebKeys.COLOR_SCHEME, colorScheme);
117: }
118: } catch (Exception e) {
119: _log.error(e, e);
120:
121: throw new ActionException(e);
122: }
123: }
124:
125: private static Log _log = LogFactory
126: .getLog(RandomLookAndFeelAction.class);
127:
128: }
|