001    /**
002     * Copyright (c) 2000-2013 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.security.RandomUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
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            @Override
043            public void run(HttpServletRequest request, HttpServletResponse response)
044                    throws ActionException {
045    
046                    try {
047    
048                            // Do not randomize layout unless the user is logged in
049    
050                            ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
051                                    WebKeys.THEME_DISPLAY);
052    
053                            if (!themeDisplay.isSignedIn()) {
054                                    return;
055                            }
056    
057                            // Do not randomize layout unless the user is accessing the portal
058    
059                            String requestURI = GetterUtil.getString(request.getRequestURI());
060    
061                            if (!requestURI.endsWith("/portal/layout")) {
062                                    return;
063                            }
064    
065                            // Do not randomize layout unless the user is accessing a personal
066                            // layout
067    
068                            Layout layout = themeDisplay.getLayout();
069    
070                            if (layout == null) {
071                                    return;
072                            }
073    
074                            Group generalGuestGroup = GroupLocalServiceUtil.getGroup(
075                                    themeDisplay.getCompanyId(), GroupConstants.GUEST);
076    
077                            List<Layout> layouts = LayoutLocalServiceUtil.getLayouts(
078                                    generalGuestGroup.getGroupId(), false);
079    
080                            if (layouts.size() > 0) {
081                                    Layout randomLayout = layouts.get(
082                                            RandomUtil.nextInt(layouts.size()));
083    
084                                    themeDisplay.setLayout(randomLayout);
085                                    themeDisplay.setLayoutTypePortlet(
086                                            (LayoutTypePortlet)randomLayout.getLayoutType());
087    
088                                    request.setAttribute(WebKeys.LAYOUT, randomLayout);
089                            }
090                    }
091                    catch (Exception e) {
092                            _log.error(e, e);
093    
094                            throw new ActionException(e);
095                    }
096            }
097    
098            private static Log _log = LogFactoryUtil.getLog(RandomLayoutAction.class);
099    
100    }