diff --git a/bigbluebutton-apps/src/main/java/org/bigbluebutton/conference/service/poll/PollService.java b/bigbluebutton-apps/src/main/java/org/bigbluebutton/conference/service/poll/PollService.java index e1f92e2306..c55a2b1b3c 100755 --- a/bigbluebutton-apps/src/main/java/org/bigbluebutton/conference/service/poll/PollService.java +++ b/bigbluebutton-apps/src/main/java/org/bigbluebutton/conference/service/poll/PollService.java @@ -18,16 +18,44 @@ */ package org.bigbluebutton.conference.service.poll; - import org.slf4j.Logger; +import org.bigbluebutton.core.api.IBigBlueButtonInGW; import org.red5.logging.Red5LoggerFactory; +import com.google.gson.Gson; +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; + + public class PollService { private static Logger log = Red5LoggerFactory.getLogger( PollService.class, "bigbluebutton" ); + private IBigBlueButtonInGW bbbInGW; + + public void setBigBlueButtonInGW(IBigBlueButtonInGW inGW) { + bbbInGW = inGW; + } public void createPoll(String msg){ System.out.println("*** PollService:: create poll \n" + msg + "\n"); + //{"title":"My sample poll","questions":[{"type":"MULTI_CHOICE","responses":["Answer 1","Answer 2","Answer 3"],"question":"What is my name?"}]} + + Gson gson = new Gson(); + + JsonParser parser = new JsonParser(); + JsonObject obj = parser.parse(msg).getAsJsonObject(); + String title = gson.fromJson(obj.get("title"), String.class); + + JsonArray questions = obj.get("questions").getAsJsonArray(); + //you can do a loop + JsonObject aquestion = questions.get(0).getAsJsonObject(); + String type = gson.fromJson(aquestion.get("type"), String.class); + + JsonArray responses = aquestion.get("responses").getAsJsonArray(); + String response = gson.fromJson(responses.get(0), String.class); + + System.out.println(title + ": " + type + " :" + response); } } diff --git a/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollApp.scala b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollApp.scala index 22b6b3f77f..e7768124d3 100755 --- a/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollApp.scala +++ b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollApp.scala @@ -25,28 +25,28 @@ class PollApp(meetingID: String, recorded: Boolean, outGW: MessageOutGateway) { } private def handleGetPolls(msg: GetPolls) { - val poll = new ArrayBuffer[PollVO] + val poll = new ArrayBuffer[PollVOOut] polls.values.foreach(p => { - val questions = new ArrayBuffer[QuestionVO] + val questions = new ArrayBuffer[QuestionVOOut] p.questions.foreach(q => { - val responses = new ArrayBuffer[ResponseVO] + val responses = new ArrayBuffer[ResponseVOOut] q.responses.foreach(response => { - val r = new ResponseVO(response.id, response.response) + val r = new ResponseVOOut(response.id, response.response) responses += r }) - val rArray = new Array[ResponseVO](responses.length) + val rArray = new Array[ResponseVOOut](responses.length) responses.copyToArray(rArray) - val quest = new QuestionVO(q.id, q.questionType, q.question, rArray) + val quest = new QuestionVOOut(q.id, q.questionType, q.question, rArray) questions += quest }) - val qArray = new Array[QuestionVO](questions.length) + val qArray = new Array[QuestionVOOut](questions.length) questions.copyToArray(qArray) - poll += new PollVO(p.id, p.title, qArray) + poll += new PollVOOut(p.id, p.title, qArray) }) - val pArray = new Array[PollVO](poll.length) + val pArray = new Array[PollVOOut](poll.length) poll.copyToArray(pArray) outGW.send(new GetPollsReplyOutMsg(meetingID, recorded, msg.requesterID, pArray)) } diff --git a/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollInGateway.scala b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollInGateway.scala new file mode 100755 index 0000000000..13dd84b021 --- /dev/null +++ b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollInGateway.scala @@ -0,0 +1,8 @@ +package org.bigbluebutton.core.apps.poll + + + +class PollInGateway { + + +} \ No newline at end of file diff --git a/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollMessageConverter.scala b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollMessageConverter.scala new file mode 100755 index 0000000000..4ac3affcf3 --- /dev/null +++ b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/PollMessageConverter.scala @@ -0,0 +1,46 @@ +package org.bigbluebutton.core.apps.poll + +import org.bigbluebutton.core.apps.poll.messages.PollVO +import com.google.gson.Gson +import com.google.gson.JsonParser +import org.bigbluebutton.core.apps.poll.messages.QuestionVO +import org.bigbluebutton.core.apps.poll.messages.ResponseVO +import org.bigbluebutton.core.util.RandomStringGenerator._ + +class PollMessageConverter { + + def convertCreatePollMessage(msg:String):PollVO = { + val gson = new Gson(); + val parser = new JsonParser(); + val obj = parser.parse(msg).getAsJsonObject(); + val title = gson.fromJson(obj.get("title"), classOf[String]); + + val questions = obj.get("questions").getAsJsonArray(); + + var cvoArray = Array[QuestionVO]() + + var i = 0 + for (i <- 0 to questions.size() - 1) { + val aquestion = questions.get(i).getAsJsonObject(); + val questionText = gson.fromJson(aquestion.get("question"), classOf[String]) + val qType = gson.fromJson(aquestion.get("type"), classOf[String]) + + val responses = aquestion.get("responses").getAsJsonArray(); + val rvoArray = Array[ResponseVO]() + + var j = 0 + for (j <- 0 to responses.size() - 1) { + val response = gson.fromJson(responses.get(0), classOf[String]); + rvoArray :+ new ResponseVO(j.toString, response) + } + + val questionType = if (qType.equalsIgnoreCase(QuestionType.MultiChoice.toString())) QuestionType.MultiChoice else QuestionType.MultiResponse + + cvoArray :+ new QuestionVO(i.toString, questionType, questionText, rvoArray) + } + + new PollVO(randomAlphanumericString(12), title, cvoArray) + } + + +} \ No newline at end of file diff --git a/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/messages/PollMessages.scala b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/messages/PollMessages.scala index 8ea1d0b5fc..07537746a5 100755 --- a/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/messages/PollMessages.scala +++ b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/apps/poll/messages/PollMessages.scala @@ -6,7 +6,7 @@ import org.bigbluebutton.core.api.InMessage import org.bigbluebutton.core.api.IOutMessage // Poll Messages -case class CreatePoll(meetingID:String, poll: PollVO, requesterID: String) extends InMessage +case class CreatePoll(meetingID: String, poll: PollVO, requesterID: String) extends InMessage case class UpdatePoll(meetingID: String, poll: PollVO) extends InMessage case class GetPolls(meetingID: String, requesterID: String) extends InMessage case class DestroyPoll(meetingID: String, pollID: String) extends InMessage @@ -21,10 +21,13 @@ case class ResponseVO(id: String, text: String) case class QuestionVO(id: String, questionType: QuestionType, question: String, responses: Array[ResponseVO]) case class PollVO(id: String, title: String, questions: Array[QuestionVO], preCreated: Boolean=false) +case class ResponseVOOut(id: String, text: String) +case class QuestionVOOut(id: String, questionType: QuestionType, question: String, responses: Array[ResponseVOOut]) +case class PollVOOut(id: String, title: String, questions: Array[QuestionVOOut], preCreated: Boolean=false) // Out Messages -case class GetPollResultReply(meetingID: String, recorded: Boolean, requesterID: String, pollVO: PollVO) extends IOutMessage -case class GetPollsReplyOutMsg(meetingID: String, recorded: Boolean, requesterID: String, polls: Array[PollVO]) extends IOutMessage +case class GetPollResultReply(meetingID: String, recorded: Boolean, requesterID: String, pollVO: PollVOOut) extends IOutMessage +case class GetPollsReplyOutMsg(meetingID: String, recorded: Boolean, requesterID: String, polls: Array[PollVOOut]) extends IOutMessage case class ClearPollFailed(meetingID: String, pollID: String, requesterID: String, reason: String) extends IOutMessage case class PollClearedOutMsg(meetingID: String, recorded: Boolean, pollID: String) extends IOutMessage case class PollStartedOutMsg(meetingID: String, recorded: Boolean, pollID: String) extends IOutMessage diff --git a/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/util/RandomStringGenerator.scala b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/util/RandomStringGenerator.scala new file mode 100755 index 0000000000..504ad4915f --- /dev/null +++ b/bigbluebutton-apps/src/main/scala/org/bigbluebutton/core/util/RandomStringGenerator.scala @@ -0,0 +1,16 @@ +package org.bigbluebutton.core.util + +object RandomStringGenerator { + + // Random generator + val random = new scala.util.Random + + // Generate a random string of length n from the given alphabet + def randomString(alphabet: String)(n: Int): String = + Stream.continually(random.nextInt(alphabet.size)).map(alphabet).take(n).mkString + + // Generate a random alphabnumeric string of length n + def randomAlphanumericString(n: Int) = + randomString("abcdefghijklmnopqrstuvwxyz0123456789")(n) +} + diff --git a/bigbluebutton-apps/src/test/resources/testng.xml b/bigbluebutton-apps/src/test/resources/testng.xml index c48045d804..6f9342510c 100755 --- a/bigbluebutton-apps/src/test/resources/testng.xml +++ b/bigbluebutton-apps/src/test/resources/testng.xml @@ -1,18 +1,17 @@ - + - - - - - + + + diff --git a/bigbluebutton-apps/src/test/scala/org/bigbluebutton/core/apps/poll/PollMessageConverterTest.scala b/bigbluebutton-apps/src/test/scala/org/bigbluebutton/core/apps/poll/PollMessageConverterTest.scala new file mode 100755 index 0000000000..d553ce7a37 --- /dev/null +++ b/bigbluebutton-apps/src/test/scala/org/bigbluebutton/core/apps/poll/PollMessageConverterTest.scala @@ -0,0 +1,22 @@ +package org.bigbluebutton.core.apps.poll + +import org.testng.annotations.BeforeClass +import org.testng.annotations.Test + +class PollMessageConverterTest { + + @BeforeClass + def setUp() { + // code that will be invoked when this test is instantiated + } + + @Test(groups = Array[String]( "unit" )) + def createPoll(){ + val msg = "{\"title\":\"My sample poll\",\"questions\":[{\"type\":\"MULTI_CHOICE\",\"responses\":[\"Answer 1\",\"Answer 2\",\"Answer 3\"],\"question\":\"What is my name?\"}]}"; + + val cut = new PollMessageConverter + val pvp = cut.convertCreatePollMessage(msg) + + assert(pvp.title.equals("My sample poll"), "Title not the same.") + } +} \ No newline at end of file