ROTWriter.java [src/writers] 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 writers;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.time.format.DateTimeFormatter;
import nodes.Event;
import nodes.Management;
import org.apache.commons.io.FileUtils;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import static utils.Constants.*;
/**
*
* @author Brad
*/
public class ROTWriter {
public void writeROTfile(Management man, String fileName) throws JSONException, IOException {
JSONObject rot = createROTJSON(man);
FileUtils.writeStringToFile(new File(fileName), rot.toString(4), Charset.defaultCharset());
}
public JSONObject createROTJSON(Management man) throws JSONException {
JSONObject rot = new JSONObject();
JSONArray jrotations = new JSONArray();
JSONObject jmanagementTemplate = new JSONObject();
JSONArray jman_data = new JSONArray();
JSONObject jman_datum;
JSONObject jcrop;
JSONObject jop;
JSONObject jres;
String name;
String id;
String yield;
rot.put(DURATION, man.getRotationYears());
rot.put(MANAGEMENTS, jrotations);
jrotations.put(jmanagementTemplate);
jmanagementTemplate.put(EVENTS, jman_data);
for (Event datum : man.getManagementData()) {
jman_datum = new JSONObject();
if (datum.getCrop() != null) {
jcrop = new JSONObject();
jcrop.put(NAME, datum.getCrop().getName());
jcrop.put(ID, datum.getCrop().getID());
jman_datum.put(YIELD, datum.getCrop().getYield());
//jcrop.put(DEFAULT_YIELD, String.valueOf(datum.getCrop().getYield()));
jman_datum.put(CROP, jcrop);
}
if (datum.getResidue() != null) {
jres = new JSONObject();
jres.put(NAME, datum.getResidue().getName());
jres.put(ID, datum.getResidue().getID());
jman_datum.put(RESIDUE, jres);
}
jman_datum.put(DATE, datum.getDate().format(DateTimeFormatter.ISO_DATE));
jop = new JSONObject();
jop.put(NAME, datum.getOperation().getName());
jop.put(ID, datum.getOperation().getID());
jman_datum.put(OPERATION, jop);
jman_datum.put(RES_ADDED, datum.getOperation().getResidueAmount());
jman_data.put(jman_datum);
}
return rot;
}
}