From 55279e2e3da7c892b6b5bc7dc0b5f063b8d207ac Mon Sep 17 00:00:00 2001 From: VladimirEliTokarev Date: Tue, 4 Oct 2016 15:50:00 +0300 Subject: [PATCH] The new object which creates protobuf from jason working --- .../gui/VotingBoothConfigurationCreator.java | 29 ++++++++++++------- ...ion.json => VotingBoothConfiguration.json} | 4 +-- 2 files changed, 21 insertions(+), 12 deletions(-) rename voting-booth-gui/src/main/resources/configuration/{VotingBioothConfiguration.json => VotingBoothConfiguration.json} (90%) diff --git a/voting-booth-gui/src/main/java/meerkat/voting/gui/VotingBoothConfigurationCreator.java b/voting-booth-gui/src/main/java/meerkat/voting/gui/VotingBoothConfigurationCreator.java index be81e91..ff5dd1b 100644 --- a/voting-booth-gui/src/main/java/meerkat/voting/gui/VotingBoothConfigurationCreator.java +++ b/voting-booth-gui/src/main/java/meerkat/voting/gui/VotingBoothConfigurationCreator.java @@ -16,11 +16,14 @@ import java.util.ArrayList; import java.util.List; import java.util.Scanner; + + /** * Created by Vladimir Eliezer Tokarev on 10/4/2016. * This class creates QuestionBallotUIElement from given configuration file */ class VotingBoothConfigurationCreator { + private int CURRENT_NUMBER_OF_QUESTIONS_REPRESENTED_TO_VOTER = 2 ; /** * Creates VotingBoothConfiguarion object form file that given as paramter * @param configPath the path to the json configuration @@ -30,13 +33,19 @@ class VotingBoothConfigurationCreator { VotingBoothConfiguration config = new VotingBoothConfiguration(); JSONArray array = this.getJsonFromPath(configPath); - if (array.length() > 2) { + if (array.length() > CURRENT_NUMBER_OF_QUESTIONS_REPRESENTED_TO_VOTER) { throw new IOException("The configration has to many questions datas"); } else { - BallotQuestionUIElementOuterClass.BallotQuestionUIElement ballotQuestionUIElement = this.convertJSONObjToBallotQuestion(array.getJSONObject(0)); - config.NameSelectionQuestion = ballotQuestionUIElement; - ballotQuestionUIElement = this.convertJSONObjToBallotQuestion(array.getJSONObject(1)); - config.NameSelectionByPictureQuestion = ballotQuestionUIElement; + try { + BallotQuestionUIElementOuterClass.BallotQuestionUIElement ballotQuestionUIElement = this.convertJSONObjToBallotQuestion(array.getJSONObject(0)); + config.NameSelectionQuestion = ballotQuestionUIElement; + + ballotQuestionUIElement = this.convertJSONObjToBallotQuestion(array.getJSONObject(1)); + config.NameSelectionByPictureQuestion = ballotQuestionUIElement; + } + catch (JSONException e) { + e.printStackTrace(); + } } return config; @@ -77,19 +86,21 @@ class VotingBoothConfigurationCreator { byteStrings.add(this.convertToByteStringBasedOnType(answersType, answersValues.getString(i))); } + // Create the UIQuestion and set the values parsed from jason BallotQuestionUIElementOuterClass.UIQuestion uiQuestion = BallotQuestionUIElementOuterClass.UIQuestion.newBuilder() .setQuestionType(questionType) .setQuestion(BallotQuestionUIElementOuterClass.Question.newBuilder() .setQuestion(questionValue)) .build(); + // Create UIAnswers and set the values parsed from jason BallotQuestionUIElementOuterClass.UIAnswers.Builder uiAnswersBuilder = BallotQuestionUIElementOuterClass.UIAnswers.newBuilder() .setAnswersType(answersType); + BallotQuestionUIElementOuterClass.ListOfAnswers.Builder listOfAnswers = BallotQuestionUIElementOuterClass.ListOfAnswers.newBuilder(); for(ByteString byteString : byteStrings){ - uiAnswersBuilder.setAnswers(BallotQuestionUIElementOuterClass.ListOfAnswers.newBuilder() - .addAnswers(byteString)); + listOfAnswers.addAnswers(byteString); } - + uiAnswersBuilder.setAnswers(listOfAnswers); return BallotQuestionUIElementOuterClass.BallotQuestionUIElement.newBuilder() .setQuestion(uiQuestion) @@ -98,8 +109,6 @@ class VotingBoothConfigurationCreator { .build(); } - - /** * Tries to get convert the given question to ByteString else throws excetion * @param questionType the type of the question (can be 0 - text, 1 - audio, 2 - image) diff --git a/voting-booth-gui/src/main/resources/configuration/VotingBioothConfiguration.json b/voting-booth-gui/src/main/resources/configuration/VotingBoothConfiguration.json similarity index 90% rename from voting-booth-gui/src/main/resources/configuration/VotingBioothConfiguration.json rename to voting-booth-gui/src/main/resources/configuration/VotingBoothConfiguration.json index 77e124f..c378bdd 100644 --- a/voting-booth-gui/src/main/resources/configuration/VotingBioothConfiguration.json +++ b/voting-booth-gui/src/main/resources/configuration/VotingBoothConfiguration.json @@ -19,11 +19,11 @@ "randomize": true, "question": { "type": 0, - "question": "Select candidate picture." + "questionValue": "Select candidate picture." }, "answers": { "type": 2, - "answers": [ + "answersValue": [ "/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/GeorgeBush.png", "/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/MichaelJackson.png", "/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/AntonioBanderass.png"