From 2dbc9b736a3fe86ac14e246924438359f6daba49 Mon Sep 17 00:00:00 2001 From: Armin Samii Date: Wed, 28 Jun 2023 14:16:46 -0400 Subject: [PATCH] force operator to provide name --- .../brightspots/rcv/GuiConfigController.java | 35 ++++++++----------- .../java/network/brightspots/rcv/Main.java | 27 ++++---------- .../brightspots/rcv/TabulatorSession.java | 11 ++++-- .../brightspots/rcv/TabulatorTests.java | 2 +- 4 files changed, 31 insertions(+), 44 deletions(-) diff --git a/src/main/java/network/brightspots/rcv/GuiConfigController.java b/src/main/java/network/brightspots/rcv/GuiConfigController.java index 53af989c5..568bfa378 100644 --- a/src/main/java/network/brightspots/rcv/GuiConfigController.java +++ b/src/main/java/network/brightspots/rcv/GuiConfigController.java @@ -460,12 +460,11 @@ public void menuItemTabulateClicked() { Pair filePathAndTempStatus = commitConfigToFileAndGetFilePath(); if (filePathAndTempStatus != null) { if (GuiContext.getInstance().getConfig() != null) { - if (askUserForName()) { - setGuiIsBusy(true); - TabulatorService service = new TabulatorService( - filePathAndTempStatus.getKey(), filePathAndTempStatus.getValue()); - setUpAndStartService(service); - } + String operatorName = askUserForName(); + setGuiIsBusy(true); + TabulatorService service = new TabulatorService( + filePathAndTempStatus.getKey(), operatorName, filePathAndTempStatus.getValue()); + setUpAndStartService(service); } else { Logger.warning("Please load a contest config file before attempting to tabulate!"); } @@ -480,12 +479,10 @@ public void menuItemConvertToCdfClicked() { Pair filePathAndTempStatus = commitConfigToFileAndGetFilePath(); if (filePathAndTempStatus != null) { if (GuiContext.getInstance().getConfig() != null) { - if (askUserForName()) { - setGuiIsBusy(true); - ConvertToCdfService service = new ConvertToCdfService( - filePathAndTempStatus.getKey(), filePathAndTempStatus.getValue()); - setUpAndStartService(service); - } + setGuiIsBusy(true); + ConvertToCdfService service = new ConvertToCdfService( + filePathAndTempStatus.getKey(), filePathAndTempStatus.getValue()); + setUpAndStartService(service); } else { Logger.warning("Please load a contest config file before attempting to convert to CDF!"); } @@ -953,7 +950,7 @@ private ConfigComparisonResult compareConfigs() { /** * Returns whether user entered a name. */ - private Boolean askUserForName() { + private String askUserForName() { TextInputDialog dialog = new TextInputDialog(); dialog.setTitle("Enter your name"); dialog.setHeaderText("For auditing purposes, enter the name(s) of everyone currently " @@ -961,11 +958,7 @@ private Boolean askUserForName() { dialog.setContentText("Name:"); Optional result = dialog.showAndWait(); - if (result.isPresent()) { - Logger.info("Name(s) of operators, as entered interactively: " + result.get()); - } - - return result.isPresent(); + return result.isPresent() ? result.get() : null; } private boolean checkForSaveAndContinue() { @@ -1588,9 +1581,11 @@ protected void setUpTaskCompletionTriggers(Task task, String failureMessag // TabulatorService runs a tabulation in the background private static class TabulatorService extends ConfigReaderService { + private String operatorName; - TabulatorService(String configPath, boolean deleteConfigOnCompletion) { + TabulatorService(String configPath, String operatorName, boolean deleteConfigOnCompletion) { super(configPath, deleteConfigOnCompletion); + this.operatorName = operatorName; } @Override @@ -1600,7 +1595,7 @@ protected Task createTask() { @Override protected Void call() { TabulatorSession session = new TabulatorSession(configPath); - session.tabulate(); + session.tabulate(operatorName); return null; } }; diff --git a/src/main/java/network/brightspots/rcv/Main.java b/src/main/java/network/brightspots/rcv/Main.java index 1ef02ff7d..63a5a6982 100644 --- a/src/main/java/network/brightspots/rcv/Main.java +++ b/src/main/java/network/brightspots/rcv/Main.java @@ -58,36 +58,20 @@ public static void main(String[] args) { CommandLine cmd = parseArgsForCli(args); String path = cmd.getOptionValue("cli"); - String name = cmd.getOptionValue("name"); + String operatorName = cmd.getOptionValue("name"); boolean convertToCdf = cmd.hasOption("convert-to-cdf"); - boolean validNameProvided = false; - if (name != null) { - // Name was provided via CLI arg - name = name.trim(); - if (!name.isEmpty()) { - validNameProvided = true; - Logger.info("Operator name(s), as entered via command-line argument: " + name); - } - } else { + if (operatorName == null) { // Name wasn't provided via CLI arg, so prompt user to enter Logger.info("Enter operator name(s), for auditing purposes:"); Scanner sc = new Scanner(System.in, StandardCharsets.UTF_8); if (sc.hasNextLine()) { - name = sc.nextLine(); - } - - if (name != null) { - name = name.trim(); - if (!name.isEmpty()) { - validNameProvided = true; - Logger.info("Operator name(s), as entered interactively: " + name); - } + operatorName = sc.nextLine(); } } - if (!validNameProvided) { + if (operatorName == null || operatorName.isEmpty()) { Logger.severe( "Must supply --name as a CLI argument, or run via an interactive shell and actually" + " provide operator name(s)!"); @@ -98,7 +82,8 @@ public static void main(String[] args) { if (convertToCdf) { session.convertToCdf(); } else { - session.tabulate(); + operatorName = operatorName.trim(); + session.tabulate(operatorName); } } diff --git a/src/main/java/network/brightspots/rcv/TabulatorSession.java b/src/main/java/network/brightspots/rcv/TabulatorSession.java index 6788672c9..e6544b5fe 100644 --- a/src/main/java/network/brightspots/rcv/TabulatorSession.java +++ b/src/main/java/network/brightspots/rcv/TabulatorSession.java @@ -137,17 +137,24 @@ void convertToCdf() { } // Returns a List of exception class names that were thrown while tabulating. - List tabulate() { + // Operator name is required for the audit logs. + List tabulate(String operatorName) { Logger.info("Starting tabulation session..."); List exceptionsEncountered = new LinkedList<>(); ContestConfig config = ContestConfig.loadContestConfig(configPath); checkConfigVersionMatchesApp(config); boolean tabulationSuccess = false; + boolean setUpLoggingSuccess = setUpLogging(config.getOutputDirectory()); - if (setUpLogging(config.getOutputDirectory()) && config.validate().isEmpty()) { + if (operatorName == null || operatorName.isEmpty()) { + Logger.severe("Operator name is required for the audit logs."); + exceptionsEncountered.add(TabulationAbortedException.class.toString()); + } else if (setUpLoggingSuccess && config.validate().isEmpty()) { Logger.info("Computer machine name: %s", Utils.getComputerName()); Logger.info("Computer user name: %s", Utils.getUserName()); + Logger.info("Operator name: %s", operatorName); Logger.info("Config file: %s", configPath); + try { Logger.fine("Begin config file contents:"); BufferedReader reader = diff --git a/src/test/java/network/brightspots/rcv/TabulatorTests.java b/src/test/java/network/brightspots/rcv/TabulatorTests.java index bf6856ec1..28c8a59f0 100644 --- a/src/test/java/network/brightspots/rcv/TabulatorTests.java +++ b/src/test/java/network/brightspots/rcv/TabulatorTests.java @@ -106,7 +106,7 @@ private static void runTabulationTest(String stem, String expectedException) { Logger.info("Running tabulation test: %s\nTabulating config file: %s...", stem, configPath); TabulatorSession session = new TabulatorSession(configPath); - List exceptionsEncountered = session.tabulate(); + List exceptionsEncountered = session.tabulate("TEST"); if (expectedException != null) { assertTrue(exceptionsEncountered.contains(expectedException)); return;