// Path: src/main/java/net/nandgr/cba/exception/BadRulesException.java
// public class BadRulesException extends Exception {
// 
//   public BadRulesException(String message) {
//     super(message);
//   }
// }
// 
// Path: src/main/java/net/nandgr/cba/custom/visitor/helper/RuleHelper.java
// public static boolean allEmpty(Method method) {
//   if (method == null) {
//     return false;
//   }
//   return StringUtils.isBlank(method.getName()) && method.getParameters() == null && StringUtils.isBlank(method.getVisibility());
// }


import java.util.Arrays;
import java.util.List;
import net.nandgr.cba.exception.BadRulesException;
import org.apache.commons.lang.StringUtils;
import static net.nandgr.cba.custom.visitor.helper.RuleHelper.allEmpty;
/*
 * Copyright (c) 2016-2017, Fernando Garcia
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.nandgr.cba.custom.model;



public class Rules {

  private List<Rule> rules;

  public List<Rule> getRules() {
    return rules;
  }

  public void setRules(List<Rule> rules) {
    this.rules = rules;
  }

  @Override
  public String toString() {
    return "Rules{" +
            "rules=" + Arrays.toString(rules.toArray()) +
            '}';
  }

  public void validateRules() throws BadRulesException {
    for (Rule rule : this.getRules()) {
      if (StringUtils.isBlank(rule.getName())) {
        throw new BadRulesException("Rule name cannot be blank.");
      }
      validateRule(rule);
    }
  }

  private static void validateRule(Rule rule) throws BadRulesException {
    if (rule.getInvocations() != null) {
      for (Invocation invocation : rule.getInvocations()) {

        Method notFrom = invocation.getNotFrom();
        checkEmpty(notFrom, "\"invocation.notFrom\" property cannot have all the fields blank.");

        Method from = invocation.getFrom();
        checkEmpty(from, "\"invocation.from\" property cannot have all the fields blank.");

// Path: src/main/java/net/nandgr/cba/exception/BadRulesException.java
// public class BadRulesException extends Exception {
// 
//   public BadRulesException(String message) {
//     super(message);
//   }
// }
// 
// Path: src/main/java/net/nandgr/cba/custom/visitor/helper/RuleHelper.java
// public static boolean allEmpty(Method method) {
//   if (method == null) {
//     return false;
//   }
//   return StringUtils.isBlank(method.getName()) && method.getParameters() == null && StringUtils.isBlank(method.getVisibility());
// }


// Path: src/main/java/net/nandgr/cba/custom/model/Rules.java
import java.util.Arrays;
import java.util.List;
import net.nandgr.cba.exception.BadRulesException;
import org.apache.commons.lang.StringUtils;
import static net.nandgr.cba.custom.visitor.helper.RuleHelper.allEmpty;

/*
 * Copyright (c) 2016-2017, Fernando Garcia
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package net.nandgr.cba.custom.model;



public class Rules {

  private List<Rule> rules;

  public List<Rule> getRules() {
    return rules;
  }

  public void setRules(List<Rule> rules) {
    this.rules = rules;
  }

  @Override
  public String toString() {
    return "Rules{" +
            "rules=" + Arrays.toString(rules.toArray()) +
            '}';
  }

  public void validateRules() throws BadRulesException {
    for (Rule rule : this.getRules()) {
      if (StringUtils.isBlank(rule.getName())) {
        throw new BadRulesException("Rule name cannot be blank.");
      }
      validateRule(rule);
    }
  }

  private static void validateRule(Rule rule) throws BadRulesException {
    if (rule.getInvocations() != null) {
      for (Invocation invocation : rule.getInvocations()) {

        Method notFrom = invocation.getNotFrom();
        checkEmpty(notFrom, "\"invocation.notFrom\" property cannot have all the fields blank.");

        Method from = invocation.getFrom();
        checkEmpty(from, "\"invocation.from\" property cannot have all the fields blank.");

        Method method = invocation.getMethod();