CokeyConverter.java [src/cokeyconverter] Revision: 2459bea6de7258e6530d779b884b4fdaa31fd039 Date: Mon Jul 10 11:53:49 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 java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
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 CokeyConverter {
/**
* @param args the command line arguments
* @throws java.lang.Exception
*/
public static void main(String[] args) throws SQLException {
ServiceArchive mongoArchive;
String dbLocation;
String ipAddress;
String fileSUIDs;
String newRequestLoc;
if (args.length >= 3) {
ArrayList<ModelArchive> results = null;
Connection sourceDb = Database.getConnection(
"jdbc:sqlserver://129.82.20.129:1433;databaseName=conservation_resources;user=rset-r;password=cs1prs3t",
"com.microsoft.sqlserver.jdbc.SQLServerDriver");
Connection destDb = Database.getConnection(
"jdbc:sqlserver://129.82.20.129:1433;databaseName=sdmOnline;user=sa;password=csurams#1",
"com.microsoft.sqlserver.jdbc.SQLServerDriver");
SoilComponentTranslator soilFinder = new SoilComponentTranslator(sourceDb, destDb);
dbLocation = args[0];
mongoArchive = new ServiceArchive(dbLocation);
newRequestLoc = args[3];
if (args[1].contains(".txt")) {
results = new ArrayList<>();
String suid;
try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(args[1])));) {
while ((suid = reader.readLine()) != null) {
ModelArchive archive = mongoArchive.getArchiveBySUID(suid);
results.add(archive);
}
} catch (FileNotFoundException ex) {
System.err.println("Cannot open the file specified to read the SUID list: " + args[1]);
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
System.exit(-1);
} catch (IOException ex) {
System.err.println("Cannot read the file specified to read the SUID list:" + args[1]);
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
System.exit(-1);
} catch (Exception ex) {
System.err.println("Cannot proceed with suid list file, unknown error");
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
System.exit(-1);
}
} else {
try {
results = mongoArchive.getArchiveByReqIP(args[1], args[2]);
} catch (Exception ex) {
System.err.println("Cannot search archive for this IP: " + args[1] + ", and service call: " + args[2]);
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
}
}
for (ModelArchive archive : results) {
String cokey = "";
try {
archive.setOriginalRequest(mongoArchive.getServiceRequest(archive.getSUID()));
cokey = archive.getOldCokey();
try {
archive.setOriginalResponse(mongoArchive.getServiceRequest(archive.getSUID()));
try {
archive.setNewCokey(soilFinder.translate(archive));
Client newRequest = new Client();
try {
archive.setNewResponse(newRequest.doPOST(newRequestLoc, archive.getNewRequest()));
writeFile(archive.getSUID() + ".json", archive.getNewRequest().toString());
writeFile(archive.getSUID() + "-original" + ".json", archive.getOriginalRequest().toString());
writeFile(archive.getSUID() + "-response.json", archive.getNewResponse().toString());
writeFile(archive.getSUID() + "-original-response" + ".json", archive.getOriginalResponse().toString());
} catch (Exception ex) {
System.err.println("Cannot call new service for suid:" + archive.getSUID() + ", for service: " + args[3]);
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (JSONException ex) {
System.err.println("Cannot translate the SSURGO cokey: " + cokey);
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (Exception ex) {
System.err.println("Cannot get archived service response of original request for suid: " + archive.getSUID());
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
}
} catch (Exception ex) {
System.err.println("Cannot get archived service request for suid: " + archive.getSUID());
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
}
// TODO:
// Should we clear this archive's local copy of request and response to save memory here??
}
try {
mongoArchive.shutdown();
} catch (Exception ex) {
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
}
System.out.println("SUID,latitude, longitude,Old Cokey,result,New Cokey,result");
if ((null != results) && (results.size() > 0)) {
for (ModelArchive archive : results) {
try {
System.out.println(archive.getSUID() + "," + archive.getLatitude() + "," + archive.getLongitude() + "," + archive.getOldCokey() + ",," + archive.getNewCokey() + ",");
} catch (JSONException ex) {
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
} else {
System.out.println("ERROR: No input arguments specified.\nPlease specify the URI location of the Archive to search and either the IP address to search for or a filename of a file containing a list of SUID's to search for. The filename must end in '.txt'");
System.out.println("Usage:");
System.out.println("\tCokeyConverter <Archive URI> <[ip address] | [filename]> <new service URI>");
System.out.println("\t\tWhere <Archive URI> is a valid host name port number combination.");
System.out.println("\t\tAnd [ip address] is a valid IPv4 address");
System.out.println("\t\tOr [filename] points to a file that contains a list of SUID's, one per line, to search for.");
System.out.println("\t\t\tAdditionally, a file may contain only one SUID, and if so, then the search will be for all records containing the ip address used in the SUID specified, and found after the date of the SUID found, for the service specified for that SUID.");
}
}
private static void writeFile(String filename, String data) {
try (Writer writer = new BufferedWriter(new OutputStreamWriter(
new FileOutputStream(filename), "utf-8"))) {
writer.write(data);
} catch (UnsupportedEncodingException ex) {
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
}
}
public static Map<String, JSONObject> preprocess(JSONArray params) throws JSONException {
Map<String, JSONObject> p = new HashMap<>();
for (int i = 0; i < params.length(); i++) {
JSONObject o = params.getJSONObject(i);
p.put(o.getString("name"), o);
}
return p;
}
}