V1_0.java [src/java/m/ecat] 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.ecat;
import csip.ModelDataService;
import csip.ServiceException;
import csip.annotations.*;
import csip.utils.JSONUtils;
import javax.ws.rs.Path;
import static m.Constants.*;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import rotation_utils.utils.TranslatorException;
/**
*
* @author Brad
* @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
*/
@Name("Energy-01: Compute Tillage Energy Consumption Awareness Output")
@Description("This service sums fuel use for each farming operation in a crop rotation, makes any necessary conversion to fuel type, and calculates rotation and crop interval fuel consumption and cost.")
@Path("m/ecat/1.0")
public class V1_0 extends ModelDataService {
private ECAT ecat;
private String fuelType;
private double fuelConversion;
@Override
public void preProcess() throws ServiceException, TranslatorException, JSONException, Exception {
double acres;
double fuelCost;
Fuel fuel;
String warning;
JSONObject crlmod = getJSONParam(KEY_CRLMOD);
acres = getDoubleParam(AOA_ACRES);
fuelCost = getDoubleParam(FUEL_COST);
fuelType = getStringParam(FUEL_TYPE);
fuel = new Fuel();
fuel.setFuelType(fuelType);
fuel.setCost(fuelCost);
warning = fuel.getFuelConversion(LOG);
if (!warning.isEmpty()) {
setMetainfoWarning(warning);
}
ecat = new ECAT();
ecat.setAcres(acres);
ecat.setFuel(fuel);
if (null != crlmod) {
JSONArray rotations = crlmod.optJSONArray(KEY_ROTATION_FILES);
if (null != rotations) {
if (rotations.length() > 0) {
JSONObject rotation = rotations.getJSONObject(0).optJSONObject(KEY_ROTATION);
if (null != rotation) {
ecat.readManagements(rotation); // going to assume 1 rotation for now. May need to change this later.
} else {
throw new ServiceException("No rotation JSON object, " + KEY_ROTATION + ", found in the input JSON.");
}
}else{
throw new ServiceException("Rotation Files section found that contains no rotations.");
}
} else {
throw new ServiceException("No rotation files JSON object, " + KEY_ROTATION_FILES + ", found in the input JSON.");
}
} else {
throw new ServiceException("No CR_LMOD JSON object, " + KEY_CRLMOD + ", found in the input JSON.");
}
}
@Override
public void postProcess() throws ECATException {
this.putResult(TOTAL_FUEL_USAGE, ecat.getTotalFuelUsage(), "Total fuel use for management.", "gallons");
this.putResult(INTERVAL_FUEL_USAGE, ecat.getIntervalFuelUsage(), "Interval fuel use for crop intervals.", "gallons");
this.putResult(TOTAL_FUEL_COST, ecat.getTotalFuelCost(), "Total fuel use for management.", "USD");
this.putResult(INTERVAL_FUEL_COST, ecat.getIntervalFuelCost(), "Interval fuel use for crop intervals.", "USD");
}
}