package edu.washington.escience.myriad.api.encoding;

import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Response.Status;

import com.google.common.base.Preconditions;

import edu.washington.escience.myriad.api.MyriaApiException;
import edu.washington.escience.myriad.operator.Operator;
import edu.washington.escience.myriad.operator.agg.MultiGroupByAggregate;

public class MultiGroupByAggregateEncoding extends OperatorEncoding<MultiGroupByAggregate> {

  public String argChild;
  public int[] argAggFields;
  public List<List<String>> argAggOperators;
  public int[] argGroupFields;

  @Override
  public void validate() throws MyriaApiException {
    super.validate();
    try {
      Preconditions.checkNotNull(argAggFields);
      Preconditions.checkNotNull(argAggOperators);
      Preconditions.checkNotNull(argGroupFields);
    } catch (Exception e) {
      throw new MyriaApiException(Status.BAD_REQUEST, "required fields: arg_aggFields, arg_groupFields, arg_opFields");
    }
  }

  @Override
  public MultiGroupByAggregate construct() {
    int[] ops = AggregateEncoding.deserializeAggregateOperator(argAggOperators);
    return new MultiGroupByAggregate(null, argAggFields, argGroupFields, ops);
  }

  @Override
  public void connect(Operator operator, Map<String, Operator> operators) {
    operator.setChildren(new Operator[] { operators.get(argChild) });
  }

}

@@ -299,16 +299,79 @@
 erator;%0A
+import edu.washington.escience.myriad.operator.agg.Aggregator;%0A
 import e
@@ -436,16 +436,16 @@
 regate;%0A
-
 %0Apublic 
@@ -1144,26 +1144,8 @@
 s = 
-AggregateEncoding.
 dese
@@ -1355,24 +1355,24 @@
 perators) %7B%0A
-
     operator
@@ -1434,10 +1434,1229 @@
 );%0A  %7D%0A%0A
+  /**%0A   * Deserializes Aggregate Operators.%0A   * %0A   * @param map the map containing all JSON data%0A   * @param field the name of the field we should get the data from%0A   * @return an array of operations specified.%0A   */%0A  private int%5B%5D deserializeAggregateOperator(final List%3CList%3CString%3E%3E ops) %7B%0A    int%5B%5D result = new int%5Bops.size()%5D;%0A    for (int i = 0; i %3C ops.size(); i++) %7B%0A      List%3CString%3E operatorForOneAggField = ops.get(i);%0A      int operations = 0x0;%0A      for (String operator : operatorForOneAggField) %7B%0A        switch (operator) %7B%0A          case %22AGG_OP_MIN%22:%0A            operations %7C= Aggregator.AGG_OP_MIN;%0A            break;%0A          case %22AGG_OP_MAX%22:%0A            operations %7C= Aggregator.AGG_OP_MAX;%0A            break;%0A          case %22AGG_OP_COUNT%22:%0A            operations %7C= Aggregator.AGG_OP_COUNT;%0A            break;%0A          case %22AGG_OP_SUM%22:%0A            operations %7C= Aggregator.AGG_OP_SUM;%0A            break;%0A          case %22AGG_OP_AVG%22:%0A            operations %7C= Aggregator.AGG_OP_AVG;%0A            break;%0A          case %22AGG_OP_STDEV%22:%0A            operations %7C= Aggregator.AGG_OP_STDEV;%0A            break;%0A%0A        %7D%0A        result%5Bi%5D = operations;%0A      %7D%0A    %7D%0A    return result;%0A  %7D%0A%0A
 %7D%0A

Move the deserialization of Aggregate operations to MultiGroupByAggregateEncoding class