/*
 * Copyright © 2015 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.cdap.internal.app.workflow;

import co.cask.cdap.api.workflow.WorkflowAction;
import co.cask.cdap.api.workflow.WorkflowActionConfigurer;
import co.cask.cdap.internal.lang.Reflections;
import co.cask.cdap.internal.specification.DataSetFieldExtractor;
import co.cask.cdap.internal.specification.PropertyFieldExtractor;
import co.cask.cdap.internal.workflow.DefaultWorkflowActionSpecification;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
 * Default implementation of {@link WorkflowActionConfigurer}.
 */
public class DefaultWorkflowActionConfigurer implements WorkflowActionConfigurer {

  private final String className;
  private final Map<String, String> propertyFields;
  private final Set<String> datasetFields;

  private String name;
  private String description;
  private Map<String, String> properties;
  private Set<String> datasets;

  public DefaultWorkflowActionConfigurer(WorkflowAction workflowAction) {
    this.name = workflowAction.getClass().getSimpleName();
    this.className = workflowAction.getClass().getName();
    this.propertyFields = new HashMap<>();
    this.datasetFields = new HashSet<>();
    this.properties = new HashMap<>();
    this.datasets = new HashSet<>();

    Reflections.visit(workflowAction, workflowAction.getClass(), new PropertyFieldExtractor(propertyFields),
                      new DataSetFieldExtractor(datasetFields));
  }

  @Override
  public void setName(String name) {
    this.name = name;
  }

  @Override
  public void setDescription(String description) {
    this.description = description;
  }

  @Override
  public void setProperties(Map<String, String> properties) {
    this.properties = new HashMap<>(properties);
  }

  @Override
  public void useDatasets(Iterable<String> datasets) {
    for (String dataset : datasets) {
      this.datasets.add(dataset);
    }
  }

  public DefaultWorkflowActionSpecification createSpecification() {
    Map<String, String> properties = new HashMap<>(this.properties);
    properties.putAll(propertyFields);
    Set<String> datasets = new HashSet<>(this.datasets);
    datasets.addAll(datasetFields);
    return new DefaultWorkflowActionSpecification(className, name, description, properties, datasets);
  }
}

@@ -997,16 +997,100 @@
 ication;
+%0Aimport com.google.common.base.Preconditions;%0Aimport com.google.common.base.Strings;
 %0A%0Aimport
@@ -1718,32 +1718,59 @@
 etSimpleName();%0A
+    this.description = %22%22;%0A
     this.classNa
@@ -2209,93 +2209,307 @@
     
-this.name = name;%0A  %7D%0A%0A  @Override%0A  public void setDescription(String description) %7B
+Preconditions.checkArgument(!Strings.isNullOrEmpty(name), %22Name of the WorkflowAction cannot be null or empty%22);%0A    this.name = name;%0A  %7D%0A%0A  @Override%0A  public void setDescription(String description) %7B%0A    Preconditions.checkNotNull(description, %22Description of the WorkflowAction cannot be null%22);
 %0A   
@@ -2616,24 +2616,119 @@
 operties) %7B%0A
+    Preconditions.checkNotNull(properties, %22Properties of the WorkflowAction cannot be null%22);%0A
     this.pro
@@ -2832,24 +2832,115 @@
 datasets) %7B%0A
+    Preconditions.checkNotNull(datasets, %22Datasets of the WorkflowAction cannot be null%22);%0A
     for (Str

Set the default description to empty string