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.rule.impl;
016    
017    import com.liferay.portal.kernel.mobile.device.Device;
018    import com.liferay.portal.kernel.mobile.device.rulegroup.rule.RuleHandler;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.UnicodeProperties;
021    import com.liferay.portal.kernel.util.Validator;
022    import com.liferay.portal.theme.ThemeDisplay;
023    import com.liferay.portlet.mobiledevicerules.model.MDRRule;
024    
025    import java.util.ArrayList;
026    import java.util.Collection;
027    import java.util.Collections;
028    
029    /**
030     * @author Edward Han
031     */
032    public class SimpleRuleHandler implements RuleHandler {
033    
034            public static String getHandlerType() {
035                    return SimpleRuleHandler.class.getName();
036            }
037    
038            public boolean evaluateRule(MDRRule mdrRule, ThemeDisplay themeDisplay) {
039                    Device device = themeDisplay.getDevice();
040    
041                    if (device == null) {
042                            return false;
043                    }
044    
045                    UnicodeProperties typeSettingsProperties =
046                            mdrRule.getTypeSettingsProperties();
047    
048                    boolean result = true;
049    
050                    String os = typeSettingsProperties.get("os");
051    
052                    if (Validator.isNotNull(os)) {
053                            if (os.equals(device.getOS())) {
054                                    result = true;
055                            }
056                            else {
057                                    result = false;
058                            }
059                    }
060    
061                    String tablet = typeSettingsProperties.get("tablet");
062    
063                    if (Validator.isNotNull(tablet)) {
064                            boolean tabletBoolean = GetterUtil.getBoolean(tablet);
065    
066                            if (result && (tabletBoolean == device.isTablet())) {
067                                    result = true;
068                            }
069                            else {
070                                    result = false;
071                            }
072                    }
073    
074                    return result;
075            }
076    
077            public Collection<String> getPropertyNames() {
078                    return _propertyNames;
079            }
080    
081            public String getType() {
082                    return getHandlerType();
083            }
084    
085            private static Collection<String> _propertyNames;
086    
087            static {
088                    _propertyNames = new ArrayList<String>(2);
089    
090                    _propertyNames.add("os");
091                    _propertyNames.add("tablet");
092    
093                    _propertyNames = Collections.unmodifiableCollection(_propertyNames);
094            }
095    
096    }