package cn.ey.biz.module.system.framework.rule;

import java.util.ArrayList;
import java.util.List;

public class DefaultRuleFactory {

    public Rule createRule(RuleDefinition definition) {
        Rule rule;
        List<Rule> list = new ArrayList<>();
        if(definition.getAll() != null){              // all
            rule = new AndRule(list);
            definition.getAll().forEach(item -> {
                list.add(createRule(item));
            });
        }else if(definition.getAny() != null){        // any
            rule = new OrRule(list);
            definition.getAny().forEach(item -> {
                list.add(createRule(item));
            });
        }else{                                        // simple
            rule = new SimpleRule(definition.getName()
                    +definition.getOperator()
                    +"\""+definition.getValue()+"\"");
        }
        return rule;
    }

}
