The new object which creates protobuf from jason working
parent
80a0a2fc2c
commit
55279e2e3d
|
@ -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)
|
||||
|
|
|
@ -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"
|
Loading…
Reference in New Issue