Fuel.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.Client;
import csip.Config;
import static csip.ModelDataService.ERROR;
import static csip.ModelDataService.KEY_METAINFO;
import static csip.ModelDataService.KEY_NAME;
import static csip.ModelDataService.KEY_PARAMETER;
import static csip.ModelDataService.KEY_RESULT;
import static csip.ModelDataService.KEY_STATUS;
import static csip.ModelDataService.KEY_VALUE;
import csip.ServiceException;
import csip.SessionLogger;
import static m.Constants.KEY_CONVERT;
import static m.Constants.KEY_FUELS;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
/**
*
* @author Brad
*/
public class Fuel {
private String fuelType;
private double conversion;
private double cost;
public String getFuelType() {
return fuelType;
}
public void setFuelType(String fuelType) {
this.fuelType = fuelType;
}
public double getConversion() {
return conversion;
}
public double getCost() {
return cost;
}
public void setCost(double cost) {
this.cost = cost;
}
public String getFuelConversion(SessionLogger LOG) throws JSONException, Exception{
JSONObject req;
JSONObject meta;
String error;
JSONArray paramObj;
JSONObject fuelobject;
JSONArray resultArr;
JSONArray fuelArr;
JSONObject lmod;
JSONObject fuelObj;
String warning = "";
req = new JSONObject();
meta = new JSONObject();
paramObj = new JSONArray();
fuelobject = new JSONObject();
fuelobject.put(KEY_NAME, "keywords");
fuelobject.put(KEY_VALUE, fuelType);
req.put(KEY_METAINFO, meta);
req.put(KEY_PARAMETER, paramObj);
paramObj.put(fuelobject);
LOG.info("FUEL Request: " + req.toString());
JSONObject soilsResponse = new Client().doPOST(Config.getString("fuels", "http://csip.engr.colostate.edu:8083/csip-lmod/d/fuel/1.0"), req);
LOG.info("FUEL Response: " + soilsResponse.toString());
JSONObject responseMeta = soilsResponse.getJSONObject(KEY_METAINFO);
if (!responseMeta.getString(KEY_STATUS).equalsIgnoreCase("Failed")) {
resultArr = soilsResponse.getJSONArray(KEY_RESULT);
lmod = resultArr.getJSONObject(0); // assuming only one lmod object wull come back because of Lucas's design
fuelArr = lmod.getJSONObject(KEY_VALUE).getJSONArray(KEY_FUELS);
if(fuelArr.length() > 1){
warning += "More than one fuel matched your fuel type.";
}
if(fuelArr.length() == 0){
throw new ServiceException("There are no fuels in LMOD that match "+fuelType);
}
fuelObj = fuelArr.getJSONObject(0);
conversion = fuelObj.getDouble(KEY_CONVERT);
}else{
error = responseMeta.getString(ERROR);
throw new ServiceException("Failed to get fuel type "+ fuelType +". WIth error "+ error);
}
return warning;
}
}