Skip to content

Commit

Permalink
force operator to provide name
Browse files Browse the repository at this point in the history
  • Loading branch information
artoonie committed Jun 28, 2023
1 parent 64eddf7 commit 2dbc9b7
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 44 deletions.
35 changes: 15 additions & 20 deletions src/main/java/network/brightspots/rcv/GuiConfigController.java
Original file line number Diff line number Diff line change
Expand Up @@ -460,12 +460,11 @@ public void menuItemTabulateClicked() {
Pair<String, Boolean> 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!");
}
Expand All @@ -480,12 +479,10 @@ public void menuItemConvertToCdfClicked() {
Pair<String, Boolean> 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!");
}
Expand Down Expand Up @@ -953,19 +950,15 @@ 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 "
+ "operating this machine.");
dialog.setContentText("Name:");
Optional<String> 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() {
Expand Down Expand Up @@ -1588,9 +1581,11 @@ protected void setUpTaskCompletionTriggers(Task<Void> 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
Expand All @@ -1600,7 +1595,7 @@ protected Task<Void> createTask() {
@Override
protected Void call() {
TabulatorSession session = new TabulatorSession(configPath);
session.tabulate();
session.tabulate(operatorName);
return null;
}
};
Expand Down
27 changes: 6 additions & 21 deletions src/main/java/network/brightspots/rcv/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)!");
Expand All @@ -98,7 +82,8 @@ public static void main(String[] args) {
if (convertToCdf) {
session.convertToCdf();
} else {
session.tabulate();
operatorName = operatorName.trim();
session.tabulate(operatorName);
}
}

Expand Down
11 changes: 9 additions & 2 deletions src/main/java/network/brightspots/rcv/TabulatorSession.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,17 +137,24 @@ void convertToCdf() {
}

// Returns a List of exception class names that were thrown while tabulating.
List<String> tabulate() {
// Operator name is required for the audit logs.
List<String> tabulate(String operatorName) {
Logger.info("Starting tabulation session...");
List<String> 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 =
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/network/brightspots/rcv/TabulatorTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> exceptionsEncountered = session.tabulate();
List<String> exceptionsEncountered = session.tabulate("TEST");
if (expectedException != null) {
assertTrue(exceptionsEncountered.contains(expectedException));
return;
Expand Down

0 comments on commit 2dbc9b7

Please sign in to comment.