CokeyConverter.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.ModelArchive.FAILED_MESSAGE;
import static cokeyconverter.ModelArchive.SLOPE_DEGRAD;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
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.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
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 {

    static int MAX_COUNT = 100;
    static int MAX_THREADS = 7;
    static int MAX_THREAD_WAIT = 1200; //in seconds

    /**
     * @param args the command line arguments
     * @throws java.sql.SQLException
     */
    public static void main(String[] args) throws SQLException, Exception {
        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];
            if (dbLocation.contains("mongodb://")) {
                mongoArchive = new MongoArchive(dbLocation);
            } else {
                mongoArchive = new FileArchive(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);
                }

            }

            int Count = 0;
            ExecutorService taskExecutor = Executors.newFixedThreadPool(MAX_THREADS);

            File csvFile = new File("Cokey_Results.csv");
            if (csvFile.exists()) {
                csvFile.delete();
            }
            csvFile.createNewFile();

            try (FileWriter fileWriter = new FileWriter(csvFile.getAbsoluteFile(), true);
                    BufferedWriter buffWriter = new BufferedWriter(fileWriter);) {

                buffWriter.write("SUID,latitude, longitude,Old Cokey,Old Result <" + SLOPE_DEGRAD + ">,New Cokey,New Result<" + SLOPE_DEGRAD + ">");
            }

            if ((null != results) && (results.size() > 0)) {
                for (ModelArchive archive : results) {
                    String cokey = "";
                    Count++;
                    if (Count > MAX_COUNT) {
                        break;
                    }
                    try {
                        archive.setOriginalRequest(mongoArchive.getServiceRequest(archive.getSUID()));
                        cokey = archive.getOldCokey();
                        try {
                            archive.setOriginalResponse(mongoArchive.getServiceResponse(archive.getSUID()));
                            try {
                                archive.setNewCokey(soilFinder.translate(archive));
                                taskExecutor.execute(new NewServiceCall(newRequestLoc, archive, csvFile));
                            } 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);
                    }
                }
            }

            try {
                mongoArchive.shutdown();
            } catch (Exception ex) {
                Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
            }

            taskExecutor.shutdown();
            try {
                taskExecutor.awaitTermination(MAX_THREAD_WAIT, TimeUnit.SECONDS);
            } catch (InterruptedException ex) {
                Logger.getLogger(CokeyConverter.class.getName()).log(Level.SEVERE, null, ex);
            }

//            if ((null != results) && (results.size() > 0)) {
//                Count = 0;
//                File csvFile = new File("Cokey_Results.csv");
//                if (csvFile.exists()) {
//                    csvFile.delete();
//                }
//                csvFile.createNewFile();
//
//                try (FileWriter fileWriter = new FileWriter(csvFile.getAbsoluteFile(), true);
//                        BufferedWriter buffWriter = new BufferedWriter(fileWriter);) {
//
//                    buffWriter.write("SUID,latitude, longitude,Old Cokey,Old Result <" + SLOPE_DEGRAD + ">,New Cokey,New Result<" + SLOPE_DEGRAD + ">");
//
//                    for (ModelArchive archive : results) {
//                        Count++;
//                        if (Count > MAX_COUNT) {
//                            break;
//                        }
//                        String[] result = archive.getRusle2CompareSLOPE_DEGRAD();
//                        String outLine = archive.getSUID() + "," + archive.getLatitude() + "," + archive.getLongitude() + "," + archive.getOldCokey() + "," + result[0] + "," + archive.getNewCokey() + "," + result[1] + "\n";
//
//                        buffWriter.write(outLine);
//
//                        if (result[0].equalsIgnoreCase(FAILED_MESSAGE) || result[1].equalsIgnoreCase(FAILED_MESSAGE)) {
//                            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());
//                        }
//                    }
//                }
//            }
        } 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;
    }

}