Util.java [src/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 utils;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import translators.Translator.FORMAT;
import static utils.Constants.*;

/**
 *
 * @author Brad
 */
public class Util {

  public static Map<String, JSONObject> mapOpResultsByName(JSONArray jfiles, FORMAT format) throws JSONException { // Could do this for keys too but names should match or we have a problem
    HashMap<String, JSONObject> files = new HashMap<>();
    JSONObject operation;

    for (int i = 0; i < jfiles.length(); i++) {
      operation = jfiles.getJSONObject(i);
      if (format == FORMAT.KEY) {
        files.put(operation.getString(NAME), operation);
      } else
        files.put(operation.getString(ID), operation);
    }

    return files;
  }

  public static Map<String, JSONObject> mapCropResultsByName(JSONArray jfiles, FORMAT format) throws JSONException { // Could do this for keys too but names should match or we have a problem
    HashMap<String, JSONObject> files = new HashMap<>();
    JSONObject crop;

    for (int i = 0; i < jfiles.length(); i++) {
      crop = jfiles.getJSONObject(i);
      if (format == FORMAT.KEY)
        files.put(crop.getString(NAME), crop);
      else
        files.put(crop.getString(ID), crop);
    }

    return files;
  }

  public static Map<String, JSONObject> mapResidueResultsByName(JSONArray jfiles, FORMAT format) throws JSONException { // Could do this for keys too but names should match or we have a problem
    HashMap<String, JSONObject> files = new HashMap<>();
    JSONObject res;

    for (int i = 0; i < jfiles.length(); i++) {
      res = jfiles.getJSONObject(i);
      if (format == FORMAT.KEY)
        files.put(res.getString(NAME), res);
      else
        files.put(res.getString(ID), res);
    }

    return files;
  }

  public static int getTillagePcodeNumber(String pCode) {
    List<String> codes = Arrays.asList("planter", "drill", "cultivator", "other");

    return codes.indexOf(pCode) + 1;
  }
}