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.util.GetterUtil;
026: import com.liferay.portal.kernel.util.Randomizer;
027: import com.liferay.portal.model.Group;
028: import com.liferay.portal.model.Layout;
029: import com.liferay.portal.model.LayoutTypePortlet;
030: import com.liferay.portal.model.impl.GroupImpl;
031: import com.liferay.portal.service.GroupLocalServiceUtil;
032: import com.liferay.portal.service.LayoutLocalServiceUtil;
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="RandomLayoutAction.java.html"><b><i>View Source</i></b></a>
047: *
048: * @author Brian Wing Shun Chan
049: *
050: */
051: public class RandomLayoutAction 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 layout 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 layout unless the user is accessing the portal
072:
073: String requestURI = GetterUtil.getString(req
074: .getRequestURI());
075:
076: if (!requestURI.endsWith("/portal/layout")) {
077: return;
078: }
079:
080: // Do not randomize layout unless the user is accessing a personal
081: // layout
082:
083: Layout layout = themeDisplay.getLayout();
084:
085: if (layout == null) {
086: return;
087: }
088:
089: Group generalGuestGroup = GroupLocalServiceUtil.getGroup(
090: themeDisplay.getCompanyId(), GroupImpl.GUEST);
091:
092: List layouts = LayoutLocalServiceUtil.getLayouts(
093: generalGuestGroup.getGroupId(), false);
094:
095: if (layouts.size() > 0) {
096: Layout randomLayout = (Layout) layouts.get(Randomizer
097: .getInstance().nextInt(layouts.size()));
098:
099: themeDisplay.setLayout(randomLayout);
100: themeDisplay
101: .setLayoutTypePortlet((LayoutTypePortlet) randomLayout
102: .getLayoutType());
103:
104: req.setAttribute(WebKeys.LAYOUT, randomLayout);
105: }
106: } catch (Exception e) {
107: _log.error(e, e);
108:
109: throw new ActionException(e);
110: }
111: }
112:
113: private static Log _log = LogFactory
114: .getLog(RandomLookAndFeelAction.class);
115:
116: }
|