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.events;
016    
017    import com.liferay.portal.kernel.events.Action;
018    import com.liferay.portal.kernel.log.Log;
019    import com.liferay.portal.kernel.log.LogFactoryUtil;
020    import com.liferay.portal.kernel.mobile.device.Device;
021    import com.liferay.portal.kernel.mobile.device.DeviceDetectionUtil;
022    import com.liferay.portal.kernel.mobile.device.UnknownDevice;
023    import com.liferay.portal.kernel.mobile.device.rulegroup.ActionHandlerManagerUtil;
024    import com.liferay.portal.kernel.mobile.device.rulegroup.RuleGroupProcessorUtil;
025    import com.liferay.portal.theme.ThemeDisplay;
026    import com.liferay.portal.util.WebKeys;
027    import com.liferay.portlet.mobiledevicerules.model.MDRAction;
028    import com.liferay.portlet.mobiledevicerules.model.MDRRuleGroupInstance;
029    import com.liferay.portlet.mobiledevicerules.service.MDRActionLocalServiceUtil;
030    
031    import java.util.List;
032    
033    import javax.servlet.http.HttpServletRequest;
034    import javax.servlet.http.HttpServletResponse;
035    import javax.servlet.http.HttpSession;
036    
037    /**
038     * @author Edward Han
039     */
040    public class DeviceServicePreAction extends Action {
041    
042            @Override
043            public void run(HttpServletRequest request, HttpServletResponse response) {
044                    HttpSession session = request.getSession();
045    
046                    ThemeDisplay themeDisplay = (ThemeDisplay)request.getAttribute(
047                            WebKeys.THEME_DISPLAY);
048    
049                    Device device = (Device)session.getAttribute(WebKeys.DEVICE);
050    
051                    if (device == null) {
052                            device = DeviceDetectionUtil.detectDevice(request);
053    
054                            session.setAttribute(WebKeys.DEVICE, device);
055                    }
056    
057                    themeDisplay.setDevice(device);
058    
059                    UnknownDevice unknownDevice = UnknownDevice.getInstance();
060    
061                    if (device.equals(unknownDevice)) {
062                            return;
063                    }
064    
065                    MDRRuleGroupInstance mdrRuleGroupInstance = null;
066    
067                    try {
068                            mdrRuleGroupInstance = RuleGroupProcessorUtil.evaluateRuleGroups(
069                                    themeDisplay);
070    
071                            if (_log.isDebugEnabled()) {
072                                    String logMessage =
073                                            "Rule group evaluation returned rule group instance ";
074    
075                                    if (mdrRuleGroupInstance != null) {
076                                            logMessage += mdrRuleGroupInstance.getRuleGroupInstanceId();
077                                    }
078                                    else {
079                                            logMessage += "null";
080                                    }
081    
082                                    _log.debug(logMessage);
083                            }
084                    }
085                    catch (Exception e) {
086                            if (_log.isWarnEnabled()) {
087                                    _log.warn("Unable to retrieve rule group", e);
088                            }
089    
090                            return;
091                    }
092    
093                    themeDisplay.setMDRRuleGroupInstance(mdrRuleGroupInstance);
094    
095                    if (mdrRuleGroupInstance == null) {
096                            return;
097                    }
098    
099                    try {
100                            List<MDRAction> mdrActions = MDRActionLocalServiceUtil.getActions(
101                                    mdrRuleGroupInstance.getRuleGroupInstanceId());
102    
103                            ActionHandlerManagerUtil.applyActions(
104                                    mdrActions, request, response);
105                    }
106                    catch (Exception e) {
107                            if (_log.isWarnEnabled()) {
108                                    _log.warn("Unable to apply device profile", e);
109                            }
110                    }
111            }
112    
113            private static Log _log = LogFactoryUtil.getLog(
114                    DeviceServicePreAction.class);
115    
116    }