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