CopyArchiveFiles.java [tools/CopyArchiveFiles/src/copyarchivefiles] Revision: 881bb2ce49f94beddba0611dc75b3c1f655ce3f8  Date: Tue Dec 17 11:40:32 MST 2019
/*
 * 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 copyarchivefiles;

import archives.ServiceArchive;
import java.io.BufferedReader;
import java.io.File;
import java.nio.file.Files;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import models.ModelArchive;
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 CopyArchiveFiles {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws SQLException, Exception {
        ServiceArchive mongoArchive;
        String dbLocation;
        ArrayList<ModelArchive> models = null;

        if (args.length == 1) {
            if (args[0].contains(".txt")) {
                // Then we have a list of suids to fetch.

                boolean fileDone = false;
                int lineNumber = 0;
                int maxThreads = 15;

                File inputFile = new File(args[0]);
                BufferedReader inputStream = Files.newBufferedReader(inputFile.toPath());

                dbLocation = inputStream.readLine();

                if (null == dbLocation) {
                    printCommandLineHelp();
                    throw new Exception("No mongo archive db Location found in first line of text file.");
                }

                String[] params = dbLocation.split(",");

                dbLocation = params[0];
                if (params.length > 1) {
                    maxThreads = Integer.parseInt(params[1].trim());
                }

                while (!fileDone) {
                    int count = 0;
                    String[] suid = new String[maxThreads];

                    while ((count < maxThreads) && (!fileDone)) {
                        String inLine = inputStream.readLine();

                        if (null != inLine) {
                            lineNumber++;
                            suid[count] = inLine.trim();
                            count++;
                        } else {
                            fileDone = true;
                        }
                    }

                    CountDownLatch latch = new CountDownLatch(count);
                    for (int i = 0; i < count; i++) {
                        ArchiveThread archiveThread = new ArchiveThread(suid[i], dbLocation, latch);
                        archiveThread.start();
                    }

                    latch.await();
                }
                
                System.out.println("Processed " + lineNumber + " requests for an archived result zip file.");
            } else {
                printCommandLineHelp();
            }
        } else {
            printCommandLineHelp();
        }
    }

    protected static void printCommandLineHelp() {
        System.out.println("ERROR: No input arguments specified.\nPlease specify 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("\nCopyArchiveFiles <filename>");
        System.out.println("\t\tWhere <filename> is a valid text file containing on line 1 the mongodb location connection string");
        System.out.println("\t\t\t and an optional comma separated value containing the number of threads to use, ");
        System.out.println("\t\t\tand every subsequent line a SUID value for a valid archived model result file to retrieve.");
    }

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