ModelArchive.java [src/cokeyconverter] 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 cokeyconverter;

import static cokeyconverter.CokeyConverter.preprocess;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;

/**
 *
 * @author <a href="mailto:shaun.case@colostate.edu">Shaun Case</a>
 */
public class ModelArchive {

    public static String SLOPE_DEGRAD = "SLOPE_DEGRAD";
    public static String FAILED_MESSAGE = "SERVICE FAILED";

    private String service;
    private String status;
    private String ctime; // creation time
    private String etime; // expiration time
    private String req_ip;
    private String suid;
    private String filename;
    private String oldCokey = "";
    private String newCokey = "";
    private double latitude, longitude;
    private JSONObject originalRequest;
    private JSONObject originalResponse;
    private JSONObject newRequest;
    private JSONObject newResponse;

    ModelArchive(String suid, String ctime, String etime, String service, String status, String req_ip, String filename) {
        this.ctime = ctime;
        this.etime = etime;
        this.service = service;
        this.status = status;
        this.req_ip = req_ip;
        this.suid = suid;
        this.filename = filename;
    }

    ModelArchive(JSONObject inputData) throws JSONException {
        suid = inputData.getString("_id");
        ctime = inputData.getString("ctime");
        etime = inputData.getString("etime");
        service = inputData.getString("service");
        status = inputData.getString("status");
        req_ip = inputData.getString("req_ip");
        filename = suid + ".zip";
    }

    String getOriginalResponse(String lookup) {
        String ret_val = ((originalResponseFailed()) ? FAILED_MESSAGE : "");

        JSONArray results = originalResponse.optJSONArray("result");

        if (null != results) {
            for (int i = 0; i < results.length(); i++) {
                JSONObject jsonObject = results.optJSONObject(i);
                if (null != jsonObject) {
                    if (jsonObject.optString("name") != null) {
                        if (jsonObject.optString("name").equalsIgnoreCase(lookup)) {
                            if (jsonObject.optString("value") != null) {
                                ret_val = jsonObject.optString("value");
                            }
                        }
                    }
                }
            }
        }

        return ret_val;
    }

    String getNewResponse(String lookup) {
        String ret_val = ((originalResponseFailed()) ? FAILED_MESSAGE : "");

        JSONArray results = newResponse.optJSONArray("result");
        if (null != results) {
            for (int i = 0; i < results.length(); i++) {
                JSONObject jsonObject = results.optJSONObject(i);
                if (null != jsonObject) {
                    if (jsonObject.optString("name") != null) {
                        if (jsonObject.optString("name").equalsIgnoreCase(lookup)) {
                            if (jsonObject.optString("value") != null) {
                                ret_val = jsonObject.optString("value");
                            }
                        }
                    }
                }
            }
        }

        return ret_val;
    }
    
    public synchronized void clearStorage(){                
        this.newRequest = null;
        this.newResponse = null;
        this.originalRequest = null;
        this.originalResponse = null;
        System.gc();
    }
    
    void setOriginalRequest(JSONObject value) throws JSONException {
        originalRequest = value;
        newRequest = new JSONObject(value.toString());

        JSONArray parameters = originalRequest.getJSONArray("parameter");
        Map<String, JSONObject> parameterMap = preprocess(parameters);

        latitude = parameterMap.get("latitude").getDouble("value");
        longitude = parameterMap.get("longitude").getDouble("value");
        if (parameterMap.containsKey("soilPtr")) {
            oldCokey = parameterMap.get("soilPtr").getString("value");
        } else {
            oldCokey = parameterMap.get("soil").getString("value");
        }
        oldCokey = oldCokey.replace("[", "").replace("]", "").replace('"', ' ').trim();
    }

    boolean originalResponseFailed() {
        boolean ret_val = true;
        JSONObject metaInfo = originalResponse.optJSONObject("metainfo");

        if (null != metaInfo) {
            String status = metaInfo.optString("status");

            if ((null != status) && (!status.isEmpty())) {
                ret_val = !status.equalsIgnoreCase("finished");
            }
        }

        return ret_val;
    }

    boolean newResponseFailed() {
        boolean ret_val = true;
        JSONObject metaInfo = newResponse.optJSONObject("metainfo");

        if (null != metaInfo) {
            String status = metaInfo.optString("status");

            if ((null != status) && (!status.isEmpty())) {
                ret_val = !status.equalsIgnoreCase("finished");
            }
        }

        return ret_val;
    }

    String[] getRusle2CompareSLOPE_DEGRAD() throws JSONException {
        String[] ret_val = new String[2];

        ret_val[0] = this.getOriginalResponse(SLOPE_DEGRAD);
        ret_val[1] = this.getNewResponse(SLOPE_DEGRAD);

        return ret_val;
    }

    double getLatitude() throws JSONException {
        return latitude;
    }

    double getLongitude() throws JSONException {
        return longitude;
    }

    JSONObject getNewRequest() {
        return newRequest;
    }

    JSONObject getOriginalRequest() {
        return originalRequest;
    }

    JSONObject getNewResponse() {
        return newResponse;
    }

    JSONObject getOriginalResponse() {
        return originalResponse;
    }

    String getNewCokey() {
        return newCokey;
    }

    void setNewCokey(String value) throws JSONException {
        //TODO:  Set newRequest cokey value here also...
        newCokey = value;
        JSONArray parameters = newRequest.getJSONArray("parameter");
        Map<String, JSONObject> parameterMap = preprocess(parameters);
        JSONObject tempSoilPtr = null;
        if (parameterMap.containsKey("soilPtr")) {
            tempSoilPtr = parameterMap.get("soilPtr");
        } else {
            tempSoilPtr = parameterMap.get("soil");
        }
        if (null != tempSoilPtr) {
            tempSoilPtr.remove("value");
            tempSoilPtr.append("value", newCokey);

            newRequest.getJSONObject("metainfo").append("original-request-suid", suid);
        }

    }

    void setNewResponse(JSONObject value) {
        newResponse = value;
    }

    String getOldCokey() throws JSONException {
        return oldCokey;
    }

    void setOldCokey(String value) {
        oldCokey = value;
    }

    void setOriginalResponse(JSONObject value) {
        originalResponse = value;
    }

    String getReqIP() {
        return req_ip;
    }

    void setReqIP(String req_ip) {
        this.req_ip = req_ip;
    }

    String getStatus() {
        return status;
    }

    void setStatus(String status) {
        this.status = status;
    }

    String getService() {
        return service;
    }

    void setService(String service) {
        this.service = service;
    }

    String getSUID() {
        return suid;
    }

    void setSUID(String suid) {
        this.suid = suid;
    }

    String getCtime() {
        return ctime;
    }

    void setCtime(String ctime) {
        this.ctime = ctime;
    }

    void setEtime(String etime) {
        this.etime = etime;
    }

    String getEtime() {
        return etime;
    }
}