001    /**
002     * Copyright (c) 2000-2012 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.mobile.device.rulegroup.action.impl;
016    
017    import com.liferay.portal.kernel.bean.BeanReference;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.log.Log;
021    import com.liferay.portal.kernel.log.LogFactoryUtil;
022    import com.liferay.portal.kernel.util.GetterUtil;
023    import com.liferay.portal.kernel.util.UnicodeProperties;
024    import com.liferay.portal.model.Group;
025    import com.liferay.portal.model.Layout;
026    import com.liferay.portal.service.GroupLocalService;
027    import com.liferay.portal.service.LayoutLocalService;
028    import com.liferay.portal.service.LayoutLocalServiceUtil;
029    import com.liferay.portal.theme.ThemeDisplay;
030    import com.liferay.portal.util.PortalUtil;
031    import com.liferay.portal.util.WebKeys;
032    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
033    
034    import java.util.ArrayList;
035    import java.util.Collection;
036    import java.util.Collections;
037    
038    import javax.servlet.http.HttpServletRequest;
039    import javax.servlet.http.HttpServletResponse;
040    
041    /**
042     * @author Edward Han
043     */
044    public class SiteRedirectActionHandler extends BaseRedirectActionHandler {
045    
046            public static String getHandlerType() {
047                    return SiteRedirectActionHandler.class.getName();
048            }
049    
050            public Collection<String> getPropertyNames() {
051                    return _propertyNames;
052            }
053    
054            public String getType() {
055                    return getHandlerType();
056            }
057    
058            public void setGroupLocalService(GroupLocalService groupLocalService) {
059                    _groupLocalService = groupLocalService;
060            }
061    
062            public void setLayoutLocalService(LayoutLocalService layoutLocalService) {
063                    _layoutLocalService = layoutLocalService;
064            }
065    
066            @Override
067            protected String getURL(
068                            MDRAction mdrAction, HttpServletRequest request,
069                            HttpServletResponse response)
070                    throws PortalException, SystemException {
071    
072                    UnicodeProperties typeSettingsProperties =
073                            mdrAction.getTypeSettingsProperties();
074    
075                    long plid = GetterUtil.getLong(
076                            typeSettingsProperties.getProperty("plid"));
077    
078                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
079                            WebKeys.THEME_DISPLAY);
080    
081                    Layout themeDisplayLayout = themeDisplay.getLayout();
082    
083                    if (plid == themeDisplayLayout.getPlid()) {
084                            return null;
085                    }
086    
087                    Layout layout = _layoutLocalService.fetchLayout(plid);
088    
089                    long groupId = GetterUtil.getLong(
090                            typeSettingsProperties.getProperty("groupId"));
091    
092                    if ((layout != null) && (layout.getGroupId() != groupId)) {
093                            if (_log.isWarnEnabled()) {
094                                    _log.warn(
095                                            "Layout " + layout.getPlid() +
096                                                    " does not belong to group " + groupId);
097                            }
098    
099                            layout = null;
100                    }
101    
102                    if (layout == null) {
103                            if (_log.isWarnEnabled()) {
104                                    _log.warn("Using default public layout");
105                            }
106    
107                            Group group = null;
108    
109                            if (groupId != themeDisplayLayout.getGroupId()) {
110                                    group = _groupLocalService.fetchGroup(groupId);
111                            }
112    
113                            if (group == null) {
114                                    if (_log.isWarnEnabled()) {
115                                            _log.warn("No group found with group ID " + groupId);
116                                    }
117    
118                                    return null;
119                            }
120    
121                            layout = LayoutLocalServiceUtil.fetchLayout(
122                                    group.getDefaultPublicPlid());
123                    }
124    
125                    if (layout != null) {
126                            return PortalUtil.getLayoutURL(layout, themeDisplay);
127                    }
128    
129                    if (_log.isWarnEnabled()) {
130                            _log.warn("Unable to resolve default layout");
131                    }
132    
133                    return null;
134            }
135    
136            private static Log _log = LogFactoryUtil.getLog(
137                    SiteRedirectActionHandler.class);
138    
139            private static Collection<String> _propertyNames;
140    
141            @BeanReference(type = GroupLocalService.class)
142            private GroupLocalService _groupLocalService;
143    
144            @BeanReference(type = LayoutLocalService.class)
145            private LayoutLocalService _layoutLocalService;
146    
147            static {
148                    _propertyNames = new ArrayList<String>(2);
149    
150                    _propertyNames.add("groupId");
151                    _propertyNames.add("plid");
152    
153                    _propertyNames = Collections.unmodifiableCollection(_propertyNames);
154            }
155    
156    }