/*
 * Copyright © 2016 Cask Data, 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 co.cask.wrangler.steps.column;

import co.cask.wrangler.api.AbstractStep;
import co.cask.wrangler.api.PipelineContext;
import co.cask.wrangler.api.Record;
import co.cask.wrangler.api.StepException;
import co.cask.wrangler.api.Usage;

import java.util.List;

/**
 * Wrangle Step for renaming a column.
 */
@Usage(
  directive = "rename",
  usage = "rename <old> <new>",
  description = "Rename a column name."
)
public class Rename extends AbstractStep {
  // Columns of the columns that needs to be renamed.
  private String oldcol;

  // Columns of the column to be renamed to.
  private String newcol;

  public Rename(int lineno, String detail, String oldcol, String newcol) {
    super(lineno, detail);
    this.oldcol = oldcol;
    this.newcol = newcol;
  }

  /**
   * Renames the column from 'old' to 'new'.
   * If the source column doesn't exist, then it will return the record as it.
   *
   * @param records Input {@link Record} to be wrangled by this step.
   * @param context Specifies the context of the pipeline.
   * @return Transformed {@link Record} with column name modified.
   * @throws StepException Thrown when there is no 'source' column in the record.
   */
  @Override
  public List<Record> execute(List<Record> records, PipelineContext context) throws StepException {
    for (Record record : records) {
      int idx = record.find(oldcol);
      if (idx != -1) {
        record.setColumn(idx, newcol);
      } else {
        throw new StepException(toString() + " : '" +
                                  oldcol + "' column is not defined in the record or the record has no column."
        );
      }
    }
    return records;
  }
}

@@ -1894,16 +1894,43 @@
 ption %7B%0A
+    boolean found = false;%0A
     for 
@@ -1953,24 +1953,24 @@
  records) %7B%0A
-
       int id
@@ -2066,19 +2066,56 @@
     
-%7D else %7B%0A  
+  found = true;%0A      %7D%0A    %7D%0A    if (!found) %7B%0A
     
@@ -2140,16 +2140,25 @@
 ception(
+%0A        
 toString
@@ -2174,42 +2174,8 @@
 '%22 +
-%0A                                 
  old
@@ -2259,20 +2259,10 @@
     
-  );%0A      %7D
+);
 %0A   

Rename now can handle nulls