The new object which creates protobuf from jason working

android-scanner
VladimirEliTokarev 2016-10-04 15:50:00 +03:00
parent 80a0a2fc2c
commit 55279e2e3d
2 changed files with 21 additions and 12 deletions

View File

@ -16,11 +16,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
/** /**
* Created by Vladimir Eliezer Tokarev on 10/4/2016. * Created by Vladimir Eliezer Tokarev on 10/4/2016.
* This class creates QuestionBallotUIElement from given configuration file * This class creates QuestionBallotUIElement from given configuration file
*/ */
class VotingBoothConfigurationCreator { class VotingBoothConfigurationCreator {
private int CURRENT_NUMBER_OF_QUESTIONS_REPRESENTED_TO_VOTER = 2 ;
/** /**
* Creates VotingBoothConfiguarion object form file that given as paramter * Creates VotingBoothConfiguarion object form file that given as paramter
* @param configPath the path to the json configuration * @param configPath the path to the json configuration
@ -30,14 +33,20 @@ class VotingBoothConfigurationCreator {
VotingBoothConfiguration config = new VotingBoothConfiguration(); VotingBoothConfiguration config = new VotingBoothConfiguration();
JSONArray array = this.getJsonFromPath(configPath); 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"); throw new IOException("The configration has to many questions datas");
} else { } else {
try {
BallotQuestionUIElementOuterClass.BallotQuestionUIElement ballotQuestionUIElement = this.convertJSONObjToBallotQuestion(array.getJSONObject(0)); BallotQuestionUIElementOuterClass.BallotQuestionUIElement ballotQuestionUIElement = this.convertJSONObjToBallotQuestion(array.getJSONObject(0));
config.NameSelectionQuestion = ballotQuestionUIElement; config.NameSelectionQuestion = ballotQuestionUIElement;
ballotQuestionUIElement = this.convertJSONObjToBallotQuestion(array.getJSONObject(1)); ballotQuestionUIElement = this.convertJSONObjToBallotQuestion(array.getJSONObject(1));
config.NameSelectionByPictureQuestion = ballotQuestionUIElement; config.NameSelectionByPictureQuestion = ballotQuestionUIElement;
} }
catch (JSONException e) {
e.printStackTrace();
}
}
return config; return config;
} }
@ -77,19 +86,21 @@ class VotingBoothConfigurationCreator {
byteStrings.add(this.convertToByteStringBasedOnType(answersType, answersValues.getString(i))); byteStrings.add(this.convertToByteStringBasedOnType(answersType, answersValues.getString(i)));
} }
// Create the UIQuestion and set the values parsed from jason
BallotQuestionUIElementOuterClass.UIQuestion uiQuestion = BallotQuestionUIElementOuterClass.UIQuestion.newBuilder() BallotQuestionUIElementOuterClass.UIQuestion uiQuestion = BallotQuestionUIElementOuterClass.UIQuestion.newBuilder()
.setQuestionType(questionType) .setQuestionType(questionType)
.setQuestion(BallotQuestionUIElementOuterClass.Question.newBuilder() .setQuestion(BallotQuestionUIElementOuterClass.Question.newBuilder()
.setQuestion(questionValue)) .setQuestion(questionValue))
.build(); .build();
// Create UIAnswers and set the values parsed from jason
BallotQuestionUIElementOuterClass.UIAnswers.Builder uiAnswersBuilder = BallotQuestionUIElementOuterClass.UIAnswers.newBuilder() BallotQuestionUIElementOuterClass.UIAnswers.Builder uiAnswersBuilder = BallotQuestionUIElementOuterClass.UIAnswers.newBuilder()
.setAnswersType(answersType); .setAnswersType(answersType);
BallotQuestionUIElementOuterClass.ListOfAnswers.Builder listOfAnswers = BallotQuestionUIElementOuterClass.ListOfAnswers.newBuilder();
for(ByteString byteString : byteStrings){ for(ByteString byteString : byteStrings){
uiAnswersBuilder.setAnswers(BallotQuestionUIElementOuterClass.ListOfAnswers.newBuilder() listOfAnswers.addAnswers(byteString);
.addAnswers(byteString));
} }
uiAnswersBuilder.setAnswers(listOfAnswers);
return BallotQuestionUIElementOuterClass.BallotQuestionUIElement.newBuilder() return BallotQuestionUIElementOuterClass.BallotQuestionUIElement.newBuilder()
.setQuestion(uiQuestion) .setQuestion(uiQuestion)
@ -98,8 +109,6 @@ class VotingBoothConfigurationCreator {
.build(); .build();
} }
/** /**
* Tries to get convert the given question to ByteString else throws excetion * 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) * @param questionType the type of the question (can be 0 - text, 1 - audio, 2 - image)

View File

@ -19,11 +19,11 @@
"randomize": true, "randomize": true,
"question": { "question": {
"type": 0, "type": 0,
"question": "Select candidate picture." "questionValue": "Select candidate picture."
}, },
"answers": { "answers": {
"type": 2, "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/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/MichaelJackson.png",
"/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/AntonioBanderass.png" "/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/AntonioBanderass.png"