PlantGro_datastructure.java [src/java/m/dssat/utils] Revision: default  Date:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package m.dssat.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author sidereus
 */
public class PlantGro_datastructure {

  List<CropGrowth> list_cg;

  public PlantGro_datastructure() {
    list_cg = new ArrayList<>();
  }

  public void add(String treatment, Map<String, Harvest> mapParams) {
    list_cg.add(new CropGrowth(treatment, mapParams));
  }

  public JSONObject get() {
    JSONObject obj = new JSONObject();
    List<JSONArray> r = list_cg.stream().map(cg -> cg.get()).collect(Collectors.toList());

    try {
      obj.put("key", "Plant Growth");
      obj.put("value", r);
    } catch (JSONException ex) {
      throw new RuntimeException(ex.getMessage());
    }
    return obj;
  }

  class CropGrowth {

    String treatment;
    List<GrowthAspect> params = new ArrayList<>();

    public CropGrowth(String treatment, Map<String, Harvest> mapParams) {
      this.treatment = treatment;
      mapParams.forEach((k, v) -> {
        params.add(new GrowthAspect(k, v));
      });
    }

    JSONArray get() {
      JSONObject obj = new JSONObject();
      JSONObject obj2 = new JSONObject();
      JSONArray arr = new JSONArray();
      List<JSONObject> r = params.stream().map(param -> param.get()).collect(Collectors.toList());
      try {
        obj.put("key", "Treatement");
        obj.put("value", treatment);
        obj2.put("key", "Yield");
        obj2.put("value",r);
        arr.put(obj);
        arr.put(obj2);
      } catch (JSONException ex) {
        throw new RuntimeException(ex.getMessage());
      }
      return arr;
    }

  }

  class GrowthAspect {

    String param;
    double value;
    int year;
    int doy;

    GrowthAspect(String param, Harvest h) {
      this.param = param;
      this.value = h.getYield();
      this.year = h.getYear();
      this.doy = h.getDoy();
      if (param.equals("UYAD")) {
        this.value = this.value * 1000 / (50.8023 * 2.47105);
      }
    }

    JSONObject get() {
      JSONObject obj = new JSONObject();
      try {
        obj.put("key", param);
        obj.put("year", year);
        obj.put("doy", doy);
        obj.put("value", value);
      } catch (JSONException ex) {
        throw new RuntimeException(ex.getMessage());
      }
      return obj;
    }

  }
}