ModelArchive.java [src/cokeyconverter] Revision: c6d9f7ec837bbc6b7d0f03ea445a94dd21f30520  Date: Wed Jul 12 15:48:21 MDT 2017
/*
 * 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 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 {

    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";        
    }

    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();
    }

    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;
    }
}