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