From e440bb3e763226381cafbc77af5c35e43765029d Mon Sep 17 00:00:00 2001 From: VladimirEliTokarev Date: Fri, 28 Oct 2016 19:55:51 +0300 Subject: [PATCH] Added configuration validation --- voting-booth-gui/build.gradle | 3 +++ .../VotingBoothConfiguration.java | 23 +++++++++++++++++++ .../voting/gui/managment/ChainBuilder.java | 2 ++ .../WelcomeSplashController.java | 19 ++++++++++++++- .../resources/view/welcome_splash_screen.fxml | 5 ++++ 5 files changed, 51 insertions(+), 1 deletion(-) diff --git a/voting-booth-gui/build.gradle b/voting-booth-gui/build.gradle index dca5053..98b5e4e 100644 --- a/voting-booth-gui/build.gradle +++ b/voting-booth-gui/build.gradle @@ -62,6 +62,9 @@ dependencies { // Jar that creates barcodes compile group: 'net.sourceforge.barbecue', name: 'barbecue', version: '1.5-beta1' + // Json configuration parsing for the test + testCompile group: 'org.json', name: 'json', version: '20160810' + testCompile 'junit:junit:4.+' runtime 'org.codehaus.groovy:groovy:2.4.+' diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfiguration.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfiguration.java index 9a1b73c..2e312e8 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfiguration.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/configuration/VotingBoothConfiguration.java @@ -8,5 +8,28 @@ import meerkat.protobuf.BallotQuestionUIElementOuterClass; public class VotingBoothConfiguration { public BallotQuestionUIElementOuterClass.BallotQuestionUIElement NameSelectionQuestion; public BallotQuestionUIElementOuterClass.BallotQuestionUIElement NameSelectionByPictureQuestion; + + /** + * Checks that the configuration that have been given is valud + * @return true if the configuration was valid else false + */ + public boolean IsConfigurationValid(){ + // check that both the ballot questions really exists + if (this.NameSelectionByPictureQuestion == null || this.NameSelectionQuestion == null) { + return false; + } + // check that both the questions answers exists + if (this.NameSelectionQuestion.getAnswers().getAnswers().getAnswersList() == null || + this.NameSelectionByPictureQuestion.getAnswers().getAnswers().getAnswersList() == null) { + return false; + } + // check that both the types of the questions answers are supported + if (BallotQuestionUIElementOuterClass.ValueType.forNumber(NameSelectionByPictureQuestion.getAnswers().getAnswersType()) == null || + BallotQuestionUIElementOuterClass.ValueType.forNumber(NameSelectionQuestion.getAnswers().getAnswersType()) == null ){ + return false; + } + return true; + } + } diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/managment/ChainBuilder.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/managment/ChainBuilder.java index ad554ba..4a3f7e4 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/managment/ChainBuilder.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/managment/ChainBuilder.java @@ -51,6 +51,8 @@ public class ChainBuilder { WelcomeSplashLoader welcomeSplashLoader = new WelcomeSplashLoader(primaryStage, configuration); TwoWayNode welcomeSplashController = welcomeSplashLoader.GetWelcomeSplash(); welcomeSplashController.SetVotersBallot(votersBallot); + // Calling the update method for the splash controller to run its graphics changes + welcomeSplashController.UpdateNode(); StraightChannelSectionLoader straightChannelSectionLoader = new StraightChannelSectionLoader(primaryStage, configuration); TwoWayNode straightChannelSectionController = straightChannelSectionLoader.GetStraightChannelSection(); diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/panels/welcome_splash/WelcomeSplashController.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/panels/welcome_splash/WelcomeSplashController.java index 50ab605..dc5a999 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/panels/welcome_splash/WelcomeSplashController.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/panels/welcome_splash/WelcomeSplashController.java @@ -1,6 +1,7 @@ package meerkat.voting.gui.panels.welcome_splash; import javafx.fxml.FXML; +import javafx.scene.control.Label; import javafx.scene.input.MouseEvent; import meerkat.voting.gui.managment.TwoWayNode; import org.slf4j.Logger; @@ -13,6 +14,10 @@ import org.slf4j.LoggerFactory; public class WelcomeSplashController extends TwoWayNode { private final Logger logger = LoggerFactory.getLogger(WelcomeSplashController.class); + private final String INVALID_CONFIGURATION_ERROR_TEXT = "The given configuration is invalid please run this voting booth with different configuraiton."; + + @FXML + private Label ErrorPanel; @FXML private void StartVotingProcess(MouseEvent mousePressed) { @@ -25,6 +30,18 @@ public class WelcomeSplashController extends TwoWayNode { @Override public void UpdateNode() { - // There is no questions relevant to this panel so this method does nothing + if (this.config == null || !this.config.IsConfigurationValid()){ + RepresentErrorToVoter(INVALID_CONFIGURATION_ERROR_TEXT); + } } + + /** + * Represents the given string to user in ErrorLabel + * @param message is the error string to represent to the user + */ + private void RepresentErrorToVoter(String message) { + ErrorPanel.setText(message); + this.currentStage.show(); + } + } diff --git a/voting-booth-gui/src/main/resources/view/welcome_splash_screen.fxml b/voting-booth-gui/src/main/resources/view/welcome_splash_screen.fxml index ebd4801..aa9140e 100644 --- a/voting-booth-gui/src/main/resources/view/welcome_splash_screen.fxml +++ b/voting-booth-gui/src/main/resources/view/welcome_splash_screen.fxml @@ -95,5 +95,10 @@ + +
+
+