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.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            @Override
051            public Collection<String> getPropertyNames() {
052                    return _propertyNames;
053            }
054    
055            @Override
056            public String getType() {
057                    return getHandlerType();
058            }
059    
060            public void setGroupLocalService(GroupLocalService groupLocalService) {
061                    _groupLocalService = groupLocalService;
062            }
063    
064            public void setLayoutLocalService(LayoutLocalService layoutLocalService) {
065                    _layoutLocalService = layoutLocalService;
066            }
067    
068            @Override
069            protected String getURL(
070                            MDRAction mdrAction, HttpServletRequest request,
071                            HttpServletResponse response)
072                    throws PortalException, SystemException {
073    
074                    UnicodeProperties typeSettingsProperties =
075                            mdrAction.getTypeSettingsProperties();
076    
077                    long plid = GetterUtil.getLong(
078                            typeSettingsProperties.getProperty("plid"));
079    
080                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
081                            WebKeys.THEME_DISPLAY);
082    
083                    Layout themeDisplayLayout = themeDisplay.getLayout();
084    
085                    if (plid == themeDisplayLayout.getPlid()) {
086                            return null;
087                    }
088    
089                    Layout layout = _layoutLocalService.fetchLayout(plid);
090    
091                    long groupId = GetterUtil.getLong(
092                            typeSettingsProperties.getProperty("groupId"));
093    
094                    if ((layout != null) && (layout.getGroupId() != groupId)) {
095                            if (_log.isWarnEnabled()) {
096                                    _log.warn(
097                                            "Layout " + layout.getPlid() +
098                                                    " does not belong to group " + groupId);
099                            }
100    
101                            layout = null;
102                    }
103    
104                    if (layout == null) {
105                            if (_log.isWarnEnabled()) {
106                                    _log.warn("Using default public layout");
107                            }
108    
109                            Group group = null;
110    
111                            if (groupId != themeDisplayLayout.getGroupId()) {
112                                    group = _groupLocalService.fetchGroup(groupId);
113                            }
114    
115                            if (group == null) {
116                                    if (_log.isWarnEnabled()) {
117                                            _log.warn("No group found with group ID " + groupId);
118                                    }
119    
120                                    return null;
121                            }
122    
123                            layout = LayoutLocalServiceUtil.fetchLayout(
124                                    group.getDefaultPublicPlid());
125                    }
126    
127                    if (layout != null) {
128                            return PortalUtil.getLayoutURL(layout, themeDisplay);
129                    }
130    
131                    if (_log.isWarnEnabled()) {
132                            _log.warn("Unable to resolve default layout");
133                    }
134    
135                    return null;
136            }
137    
138            private static Log _log = LogFactoryUtil.getLog(
139                    SiteRedirectActionHandler.class);
140    
141            private static Collection<String> _propertyNames;
142    
143            @BeanReference(type = GroupLocalService.class)
144            private GroupLocalService _groupLocalService;
145    
146            @BeanReference(type = LayoutLocalService.class)
147            private LayoutLocalService _layoutLocalService;
148    
149            static {
150                    _propertyNames = new ArrayList<String>(2);
151    
152                    _propertyNames.add("groupId");
153                    _propertyNames.add("plid");
154    
155                    _propertyNames = Collections.unmodifiableCollection(_propertyNames);
156            }
157    
158    }