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            @Override
041            public boolean evaluateRule(MDRRule mdrRule, ThemeDisplay themeDisplay) {
042                    Device device = themeDisplay.getDevice();
043    
044                    if ((device == null) || Validator.isNull(device.getOS())) {
045                            return false;
046                    }
047    
048                    UnicodeProperties typeSettingsProperties =
049                            mdrRule.getTypeSettingsProperties();
050    
051                    boolean result = true;
052    
053                    String os = typeSettingsProperties.get("os");
054    
055                    if (Validator.isNotNull(os)) {
056                            String[] operatingSystems = StringUtil.split(os);
057    
058                            if (ArrayUtil.contains(operatingSystems, device.getOS())) {
059                                    result = true;
060                            }
061                            else {
062                                    result = false;
063                            }
064                    }
065    
066                    String tablet = typeSettingsProperties.get("tablet");
067    
068                    if (Validator.isNotNull(tablet)) {
069                            boolean tabletBoolean = GetterUtil.getBoolean(tablet);
070    
071                            if (result && (tabletBoolean == device.isTablet())) {
072                                    result = true;
073                            }
074                            else {
075                                    result = false;
076                            }
077                    }
078    
079                    return result;
080            }
081    
082            @Override
083            public Collection<String> getPropertyNames() {
084                    return _propertyNames;
085            }
086    
087            @Override
088            public String getType() {
089                    return getHandlerType();
090            }
091    
092            private static Collection<String> _propertyNames;
093    
094            static {
095                    _propertyNames = new ArrayList<String>(2);
096    
097                    _propertyNames.add("os");
098                    _propertyNames.add("tablet");
099    
100                    _propertyNames = Collections.unmodifiableCollection(_propertyNames);
101            }
102    
103    }