/*
 * Copyright 2013 Cloudera Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cloudera.cdk.morphline.stdlib;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

import com.cloudera.cdk.morphline.api.Command;
import com.cloudera.cdk.morphline.api.CommandBuilder;
import com.cloudera.cdk.morphline.api.MorphlineContext;
import com.cloudera.cdk.morphline.api.MorphlineRuntimeException;
import com.cloudera.cdk.morphline.api.Record;
import com.cloudera.cdk.morphline.base.AbstractCommand;
import com.typesafe.config.Config;

/**
 * A tryRules command consists of zero or more rules.
 * 
 * A rule consists of zero or more commands.
 * 
 * The rules of a tryRules command are processed in top-down order. If one of the commands in a rule
 * fails, the tryRules command stops processing of this rule, backtracks and tries the next rule,
 * and so on, until a rule is found that runs all its commands to completion without failure (the
 * rule succeeds). If a rule succeeds the remaining rules of the current tryRules command are
 * skipped. If no rule succeeds the record remains unchanged, but a warning may be issued (the
 * warning can be turned off) or an exception may be thrown (which is logged and ignored in
 * production mode).
 * 
 * Because a command can itself be a tryRules command, there can be tryRules commands with commands,
 * nested inside tryRules, inside tryRules, recursively. This helps to implement arbitrarily complex
 * functionality for advanced usage.
 */
public final class TryRulesBuilder implements CommandBuilder {

  @Override
  public Collection<String> getNames() {
    return Collections.singletonList("tryRules");
  }
  
  @Override
  public Command build(Config config, Command parent, Command child, MorphlineContext context) {
    return new TryRules(config, parent, child, context);
  }
  
  
  ///////////////////////////////////////////////////////////////////////////////
  // Nested classes:
  ///////////////////////////////////////////////////////////////////////////////
  private static final class TryRules extends AbstractCommand {

    private final List<Command> childRules = new ArrayList();
    private final boolean throwExceptionIfAllRulesFailed;
    private final boolean catchExceptions;
    private final boolean copyRecords;
    
    public TryRules(Config config, Command parent, Command child, MorphlineContext context) {
      super(config, parent, child, context);
      this.throwExceptionIfAllRulesFailed = getConfigs().getBoolean(config, "throwExceptionIfAllRulesFailed", true);
      this.catchExceptions = getConfigs().getBoolean(config, "catchExceptions", false);
      this.copyRecords = getConfigs().getBoolean(config, "copyRecords", true);
      
      List<? extends Config> ruleConfigs = getConfigs().getConfigList(config, "rules", Collections.EMPTY_LIST);
      for (Config ruleConfig : ruleConfigs) {
        List<Command> commands = buildCommandChain(ruleConfig, "commands", child, true);
        if (commands.size() > 0) {
          childRules.add(commands.get(0));
        }
      }
      validateArguments();
    }
    
    @Override
    protected void doNotify(Record notification) {
      for (Command childRule : childRules) {
        childRule.notify(notification);
      }
      super.doNotify(notification);
    }
  
    @Override
    protected boolean doProcess(Record record) {
      for (Command childRule : childRules) {
        Record copy = copyRecords ? record.copy() : record;
        if (!catchExceptions) {
          if (childRule.process(copy)) {
            return true; // rule was executed successfully; no need to try the other remaining rules
          }          
        } else {
          try {
            if (childRule.process(copy)) {
              return true; // rule was executed successfully; no need to try the other remaining rules
            }
          } catch (RuntimeException e) {
            LOG.warn("tryRules command caught rule exception. Continuing to try other remaining rules", e);
            // continue and try the other remaining rules
          }
        }
      }
      LOG.warn("tryRules command found no successful rule");
      if (throwExceptionIfAllRulesFailed) {
        throw new MorphlineRuntimeException("tryRules command found no successful rule for record: " + record);
      }
      return false;
    }
    
  }
  
}

@@ -1057,32 +1057,67 @@
 bstractCommand;%0A
+import com.codahale.metrics.Meter;%0A
 import com.types
@@ -1135,16 +1135,16 @@
 Config;%0A
-
 %0A/**%0A * 
@@ -2895,16 +2895,61 @@
 ecords;%0A
+    private final Meter numExceptionsCaught;%0A
     %0A   
@@ -3741,24 +3741,85 @@
 rguments();%0A
+      numExceptionsCaught = getMeter(%22numExceptionsCaught%22);%0A
     %7D%0A    %0A 
@@ -4569,32 +4569,32 @@
 s%0A            %7D%0A
-
           %7D catc
@@ -4614,24 +4614,64 @@
 eption e) %7B%0A
+            numExceptionsCaught.mark();%0A
             

add metric to monitor the number of exceptions that were caught in tryRules command