V1_0.java [src/java/m/sq/stir] Revision:   Date:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package m.sq.stir;

import csip.ModelDataService;
import csip.annotations.*;
import static csip.annotations.State.RELEASED;
import javax.ws.rs.Path;
import static m.sq.Constants.*;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONObject;

/**
 * STIR service.
 *
 * @author od
 */
@Name("STIR")
@Description("STIR")
@State(RELEASED)
@Path("m/stir/1.0")
public class V1_0 extends ModelDataService {

  private double[] operation_stirs;
  private int duration;
  private double total_stir;
  private double average_annual_stir;


  @Override
  protected void preProcess() throws Exception {
    JSONObject management = parameter().getJSON(KEY_MANAGEMENT);
    duration = management.getInt(KEY_DURATION_IN_MAN);
    JSONArray operations = management.getJSONArray(KEY_OPERATIONS);
    operation_stirs = new double[operations.length()];
    for (int i = 0; i < operations.length(); i++) {
      JSONObject operation = operations.getJSONObject(i);
      operation_stirs[i] = operation.getDouble(KEY_STIR);
    }
  }


  @Override
  public void doProcess() {
    total_stir = 0;
    for (int i = 0; i < operation_stirs.length; i++) {
      total_stir += operation_stirs[i];
    }
    average_annual_stir = total_stir / duration;
  }


  @Override
  protected void postProcess() throws Exception {
    results().put(KEY_TOTAL_STIR, total_stir);
    results().put(KEY_AVERAGE_ANNUAL_STIR, average_annual_stir);
  }

}