001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
038     * @author Brian Wing Shun Chan
039     */
040    public class RandomLayoutAction extends Action {
041    
042            public void run(HttpServletRequest request, HttpServletResponse response)
043                    throws ActionException {
044    
045                    try {
046    
047                            // Do not randomize layout unless the user is logged in
048    
049                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
050                                    WebKeys.THEME_DISPLAY);
051    
052                            if (!themeDisplay.isSignedIn()) {
053                                    return;
054                            }
055    
056                            // Do not randomize layout unless the user is accessing the portal
057    
058                            String requestURI = GetterUtil.getString(request.getRequestURI());
059    
060                            if (!requestURI.endsWith("/portal/layout")) {
061                                    return;
062                            }
063    
064                            // Do not randomize layout unless the user is accessing a personal
065                            // layout
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    }