Added the Object which creates configurations based on json file

android-scanner
VladimirEliTokarev 2016-10-04 15:09:58 +03:00
parent 80a032d373
commit 80a0a2fc2c
3 changed files with 183 additions and 57 deletions

View File

@ -1,14 +1,9 @@
package meerkat.voting.gui;
import com.google.protobuf.ByteString;
import javafx.application.Application;
import javafx.stage.Stage;
import meerkat.protobuf.BallotQuestionUIElementOuterClass;
import org.json.JSONException;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
@ -20,58 +15,14 @@ import java.io.IOException;
public class Main extends Application {
/**
* Converts the image from given path to bytes array
* @param filepath the path to the wanted picture
* @return byte array of the image
* @throws IOException
* Creates the VotingBoothConfigurationCreator and creates configuration based on given path
* @return VotingBoothVonfiguration parsed from json file
* @throws IOException will be thrown if the configuraiton file doesnt exist
* @throws JSONException will be thrown if the json is unvalid
*/
private byte[] GetData(String filepath) throws IOException {
byte[] imageInByte;
BufferedImage originalImage = ImageIO.read(new File(filepath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "png", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
return imageInByte;
}
/**
* Creates configuration for example
* @return VotingBoothConfiguration object
* @throws IOException
*/
private VotingBoothConfiguration GetConfig() throws IOException {
VotingBoothConfiguration config = new VotingBoothConfiguration();
config.NameSelectionQuestion = BallotQuestionUIElementOuterClass.BallotQuestionUIElement.newBuilder()
.setQuestion(BallotQuestionUIElementOuterClass.UIQuestion.newBuilder()
.setQuestionType(0)
.setQuestion(BallotQuestionUIElementOuterClass.Question.newBuilder()
.setQuestion(ByteString.copyFromUtf8("Who you vote for?"))))
.setAnswers(BallotQuestionUIElementOuterClass.UIAnswers.newBuilder()
.setAnswersType(0)
.setAnswers(BallotQuestionUIElementOuterClass.ListOfAnswers.newBuilder()
.addAnswers(ByteString.copyFromUtf8("George Boosh"))
.addAnswers(ByteString.copyFromUtf8("Antonio Banderas"))
.addAnswers(ByteString.copyFromUtf8("Michal Jakson"))))
.setRandomizeListOrder(true).build();
config.NameSelectionByPictureQuestion = BallotQuestionUIElementOuterClass.BallotQuestionUIElement.newBuilder()
.setQuestion(BallotQuestionUIElementOuterClass.UIQuestion.newBuilder()
.setQuestionType(0)
.setQuestion(BallotQuestionUIElementOuterClass.Question.newBuilder()
.setQuestion(ByteString.copyFromUtf8("Who you vote for?"))))
.setAnswers(BallotQuestionUIElementOuterClass.UIAnswers.newBuilder()
.setAnswersType(2)
.setAnswers(BallotQuestionUIElementOuterClass.ListOfAnswers.newBuilder()
.addAnswers(ByteString.copyFrom(GetData("/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/GeorgeBush.png")))
.addAnswers(ByteString.copyFrom(GetData("/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/MichaelJackson.png")))
.addAnswers(ByteString.copyFrom(GetData("/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/images/AntonioBanderass.png")))))
.setRandomizeListOrder(true).build();
return config;
private VotingBoothConfiguration GetConfig() throws IOException, JSONException {
VotingBoothConfigurationCreator creator = new VotingBoothConfigurationCreator();
return creator.CreateConfiguration("/F:/vova/meerkat/meerkat-java/voting-booth-gui/src/main/resources/configuration/VotingBoothConfiguration.json");
}
@Override

View File

@ -0,0 +1,141 @@
package meerkat.voting.gui;
import com.google.protobuf.ByteString;
import meerkat.protobuf.BallotQuestionUIElementOuterClass;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
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 {
/**
* Creates VotingBoothConfiguarion object form file that given as paramter
* @param configPath the path to the json configuration
* @return configuraiton object
*/
VotingBoothConfiguration CreateConfiguration(String configPath) throws IOException, JSONException {
VotingBoothConfiguration config = new VotingBoothConfiguration();
JSONArray array = this.getJsonFromPath(configPath);
if (array.length() > 2) {
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;
}
return config;
}
/**
* Gets array of object that contains the data of the BallotAQuestions
* @param path the path to the configuration file
* @return JSONArray which represnets array of ballot Questions
* @throws FileNotFoundException will be thrown in the config file not exists
* @throws JSONException will be thrown if the parsing will fail
*/
private JSONArray getJsonFromPath(String path) throws FileNotFoundException, JSONException {
String content = new Scanner(new File(path)).useDelimiter("\\Z").next();
JSONObject jsonObject = new JSONObject(content);
return (JSONArray)jsonObject.get("questions");
}
/**
* converts the given JSONObject into BallotQuestionUIElement
* @param object the object to convert
* @return BallotQuestion parsed fron JSONObject
*/
private BallotQuestionUIElementOuterClass.BallotQuestionUIElement convertJSONObjToBallotQuestion(JSONObject object) throws JSONException, IOException {
boolean randomize = object.getBoolean("randomize");
// Question part
JSONObject question = object.getJSONObject("question");
int questionType = question.getInt("type");
ByteString questionValue = this.convertToByteStringBasedOnType(questionType, question.getString("questionValue"));
// Answers Part
JSONObject answers = object.getJSONObject("answers");
int answersType = answers.getInt("type");
JSONArray answersValues = answers.getJSONArray("answersValue");
List<ByteString> byteStrings = new ArrayList<>();
for (int i = 0 ; i < answersValues.length(); i ++){
byteStrings.add(this.convertToByteStringBasedOnType(answersType, answersValues.getString(i)));
}
BallotQuestionUIElementOuterClass.UIQuestion uiQuestion = BallotQuestionUIElementOuterClass.UIQuestion.newBuilder()
.setQuestionType(questionType)
.setQuestion(BallotQuestionUIElementOuterClass.Question.newBuilder()
.setQuestion(questionValue))
.build();
BallotQuestionUIElementOuterClass.UIAnswers.Builder uiAnswersBuilder = BallotQuestionUIElementOuterClass.UIAnswers.newBuilder()
.setAnswersType(answersType);
for(ByteString byteString : byteStrings){
uiAnswersBuilder.setAnswers(BallotQuestionUIElementOuterClass.ListOfAnswers.newBuilder()
.addAnswers(byteString));
}
return BallotQuestionUIElementOuterClass.BallotQuestionUIElement.newBuilder()
.setQuestion(uiQuestion)
.setAnswers(uiAnswersBuilder.build())
.setRandomizeListOrder(randomize)
.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)
* @param questionValue the values itseld (in case the type is 1 or 2 the question value will be a path to the file)
* @return ByteString which is or the value or the file in given path
* @throws IOException will be thrown if the given type is unsupported
*/
private ByteString convertToByteStringBasedOnType(int questionType, String questionValue) throws IOException {
if (questionType == BallotQuestionUIElementOuterClass.ValueType.TEXT_TYPE.getNumber()) {
return ByteString.copyFromUtf8(questionValue);
}
if (questionType == BallotQuestionUIElementOuterClass.ValueType.IMAGE_TYPE.getNumber()) {
return ByteString.copyFrom(getFileBytes(questionValue));
}
else {
throw new IOException("The given questio type doesnt supported in current version");
}
}
/**
* Converts the image from given path to bytes array
* @param filepath the path to the wanted picture
* @return byte array of the image
* @throws IOException will be thrown if the given file doesnt exist
*/
private byte[] getFileBytes(String filepath) throws IOException {
byte[] imageInByte;
BufferedImage originalImage = ImageIO.read(new File(filepath));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(originalImage, "png", baos);
baos.flush();
imageInByte = baos.toByteArray();
baos.close();
return imageInByte;
}
}

View File

@ -0,0 +1,34 @@
{
"questions": [
{
"randomize": true,
"question": {
"type": 0,
"questionValue": "Select candidate name."
},
"answers": {
"type": 0,
"answersValue": [
"George Boosh",
"Antonio Banderass",
"Michael Jakson"
]
}
},
{
"randomize": true,
"question": {
"type": 0,
"question": "Select candidate picture."
},
"answers": {
"type": 2,
"answers": [
"/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"
]
}
}
]
}