WEPS_CRLMOD_V1_0.java [src/java/m/util] 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.util;
import csip.ModelDataService;
import csip.annotations.*;
import static csip.annotations.ResourceType.*;
import java.util.ArrayList;
import javax.ws.rs.Path;
//import org.codehaus.jettison.json.JSONArray;
//import org.codehaus.jettison.json.JSONException;
//import org.codehaus.jettison.json.JSONObject;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import java.io.IOException;
import java.text.ParseException;
import java.util.List;
import org.json.XML;
//import org.json.JSONArray;
//import org.json.JSONException;
//import org.json.JSONObject;
/**
* This service returns data from a converted
*
* @author jf
*/
@Name("WEPSmanagementTranslate")
@Description("WEPS management translation")
@Path("m/util/wepstranslate/1.0")
@Polling(first = 2000, next = 2000)
// Output to capture
@Resource(file = "*stdout.txt *stderr.txt", type = OUTPUT)
/**
* This is translation service from a WEPS .manx (XML management file for WEPS)
* to a CRLMOD file that the WEPP interface can load.
*/
public class WEPS_CRLMOD_V1_0 extends ModelDataService {
static final String WEPS_XML_DATA = "xmldata";
static final String WEPS_FILENAME = "filename";
String wepsdata;
JSONObject wepsJson;
JSONObject outJson;
String wepsManName;
@Override
protected void doProcess() throws Exception {
wepsdata = parameter().getString(WEPS_XML_DATA);
wepsManName = parameter().getString(WEPS_FILENAME,"unknown");
try {
String temps = XML.toJSONObject(wepsdata).toString();
JSONObject temp = new JSONObject(temps);
wepsJson = temp;
// convert this WEPS JSON into a CRLMOD version, drop all
// WEPS specfic parameters
JSONObject mandb = wepsJson.getJSONObject("wepsmanDB");
JSONArray ops = mandb.getJSONArray("wepsmanvalue");
if (ops != null) {
outJson = new JSONObject();
JSONArray events = new JSONArray();
int lastYear = 1;
for (int i = 0; i < ops.length(); i++) {
JSONObject ev = new JSONObject();
JSONObject oneop = ops.optJSONObject(i);
String datestr = oneop.getString("date");
String parts[] = datestr.split("/");
int yr = Integer.parseInt(parts[2]);
lastYear = yr;
datestr = String.format("%04d", yr) + "-" + parts[1] + "-" + parts[0];
ev.put("date", datestr);
JSONObject wepsOp = oneop.getJSONObject("operationDB");
JSONObject thisOp = new JSONObject();
thisOp.put("name", wepsOp.getString("operationname"));
thisOp.put("id","999");
thisOp.put("OpGroup1","");
thisOp.put("begin_growth",false);
thisOp.put("kill_crop",false);
thisOp.put("add_residue",false);
thisOp.put("stir",0);
String cropName = "";
String targetYield = "";
String hyldunits = "";
String residuemass = "";
boolean hasInterval = false;
JSONArray actions = wepsOp.getJSONArray("actionvalue");
for (int j=0; j < actions.length(); j++) {
JSONObject param = actions.optJSONObject(j);
JSONObject p1 = param.optJSONObject("param");
if (p1 != null) {
String pName = p1.optString("name");
if (pName != null) {
if (pName.equals("gcropname")) {
cropName = p1.optString("value");
}
}
} else {
JSONArray p2 = param.optJSONArray("param");
if (p2 != null) {
for (int k=0; k < p2.length(); k++) {
JSONObject paramar = p2.optJSONObject(k);
String pname = paramar.optString("name");
if (pname != null) {
if (pname.equals("tgtyield")) {
targetYield = paramar.optString("value");
} else if (pname.equals("hyldunits")) {
hyldunits = paramar.optString("value");
} else if (pname.equals("rflatmass")) {
residuemass = paramar.optString("value");
} else if (pname.equals("harv_calib_flg")) {
int flg = paramar.optInt("value",0);
if (flg == 1) {
hasInterval = true;
}
}
}
}
}
}
}
if (cropName != "") {
if (!residuemass.equals("")) {
JSONObject thisResidue = new JSONObject();
thisResidue.put("name",cropName);
double res = Double.parseDouble(residuemass);
String residuemassLBS = String.format("%.2f",(res * 8921.79));
thisResidue.put("res_added",residuemassLBS);
thisResidue.put("resGroup1", "");
thisResidue.put("id","999");
ev.put("residue",thisResidue);
thisOp.put("defaultResidueAdded",residuemassLBS);
} else {
JSONObject thisCrop = new JSONObject();
thisCrop.put("name",cropName);
thisCrop.put("yieldUnit", hyldunits);
thisCrop.put("defaultYield", targetYield);
thisCrop.put("yield",targetYield);
thisCrop.put("cropGroup1", "");
thisCrop.put("id","999");
ev.put("crop",thisCrop);
thisOp.put("defaultResidueAdded",0);
}
} else {
thisOp.put("defaultResidueAdded",0);
}
ev.put("interval", hasInterval);
ev.put("operation",thisOp);
events.put(ev);
}
JSONObject rotObj = new JSONObject();
JSONObject rot2Obj = new JSONObject();
rotObj.put("duration",lastYear);
JSONArray manarr = new JSONArray();
JSONObject man1 = new JSONObject();
man1.put("path", "managements\\local");
man1.put("events",events);
man1.put("name",wepsManName);
manarr.put(man1);
rotObj.put("managements",manarr);
rot2Obj.put("rotation",rotObj);
results().put("crlmod",rot2Obj);
}
} catch (Exception e) {
results().put("Error", "Failed", "Failed XML to JSON conversion");
System.err.println("ERROR occured in WEPS conversion: converting XML to JSONObject.");
e.printStackTrace();
return;
}
}
@Override
protected void postProcess() throws Exception {
//results().put("WEPSJson", outJson, "A json format version of results");
//results().p
}
}