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