diff --git a/src/main/resources/messages.json b/src/main/resources/messages.json new file mode 100644 index 0000000000000000000000000000000000000000..43c4f86714cfae2c05a023e0c6ec1c2eb260d821 --- /dev/null +++ b/src/main/resources/messages.json @@ -0,0 +1,31 @@ +Simple Property { + class name + object id + + field name + field value + field type + + operation = update +} + +Collection Add { + class name parent + class name child + + object id parent + object id child + + data [ + field name + field value + field type + ] + + operation add +} + +Collection Remove { + class name child + object id child +} \ No newline at end of file diff --git a/src/main/scala/de/tobias/playpad/server/server/project/ProjectHandler.scala b/src/main/scala/de/tobias/playpad/server/server/project/ProjectHandler.scala index cb1b8370aaa51e0c2eb86c3a5cad0c4f8a20914d..786d9f4c220c1d00fc8d0b641825b64d5521f3c1 100644 --- a/src/main/scala/de/tobias/playpad/server/server/project/ProjectHandler.scala +++ b/src/main/scala/de/tobias/playpad/server/server/project/ProjectHandler.scala @@ -1,13 +1,12 @@ package de.tobias.playpad.server.server.project import java.sql.Connection -import java.util.UUID import com.google.gson.{JsonObject, JsonParser} import com.j256.ormlite.dao.Dao import de.tobias.playpad.server.account import de.tobias.playpad.server.account.Account -import de.tobias.playpad.server.server.SqlHelper +import de.tobias.playpad.server.server.project.listener.{CollectionAddListener, CollectionRemoveListener, PropertyUpdateListener} import org.eclipse.jetty.websocket.api.Session import org.eclipse.jetty.websocket.api.annotations.{OnWebSocketClose, OnWebSocketConnect, OnWebSocketMessage, WebSocket} @@ -21,6 +20,12 @@ import scala.collection.{Map, mutable} // TODO mutable.HashSet --> Set private var sessions: Map[Account, mutable.HashSet[Session]] = new mutable.HashMap[Account, mutable.HashSet[Session]]() + private val listeners = Map( + "update" -> new PropertyUpdateListener(), + "col-add" -> new CollectionAddListener(), + "col-remove" -> new CollectionRemoveListener() + ) + @OnWebSocketConnect def onConnect(serverSession: Session): Unit = { val key = serverSession.getUpgradeRequest.getHeader("key") if (key == null) { @@ -55,59 +60,9 @@ import scala.collection.{Map, mutable} @OnWebSocketMessage def onMessage(serverSession: Session, text: String): Unit = { // Store in Database try { - val json = new JsonParser().parse(text) - json match { - case jsonObject: JsonObject => - val className = jsonObject.get("class").getAsString - val field = jsonObject.get("field").getAsString - val id = jsonObject.get("id").getAsString - - val valueType = Class.forName(jsonObject.get("type").getAsString) - - if (valueType == classOf[String]) { - val value = jsonObject.get("value").getAsString - - if (id.matches("[0-9]*")) { - val idInt = id.toInt - SqlHelper.insertOrUpdate(connection, className, idInt, field, value) - } else { - val uuid = UUID.fromString(id) - SqlHelper.insertOrUpdate(connection, className, uuid, field, value) - } - } else if (valueType == classOf[Boolean]) { - val value = jsonObject.get("value").getAsBoolean - - if (id.matches("[0-9]*")) { - val idInt = id.toInt - SqlHelper.insertOrUpdate(connection, className, idInt, field, value) - } else { - val uuid = UUID.fromString(id) - SqlHelper.insertOrUpdate(connection, className, uuid, field, value) - } - } else if (valueType == classOf[Integer]){ - val value = jsonObject.get("value").getAsInt - - if (id.matches("[0-9]*")) { - val idInt = id.toInt - SqlHelper.insertOrUpdate(connection, className, idInt, field, value) - } else { - val uuid = UUID.fromString(id) - SqlHelper.insertOrUpdate(connection, className, uuid, field, value) - } - } else if (valueType == classOf[Double]) { - val value = jsonObject.get("value").getAsDouble - - if (id.matches("[0-9]*")) { - val idInt = id.toInt - SqlHelper.insertOrUpdate(connection, className, idInt, field, value) - } else { - val uuid = UUID.fromString(id) - SqlHelper.insertOrUpdate(connection, className, uuid, field, value) - } - } - - case _ => - } + val json = new JsonParser().parse(text).asInstanceOf[JsonObject] + val listener = listeners(json.get("operation").getAsString) + listener.onChange(json, connection) // Push to clients val key = serverSession.getUpgradeRequest.getHeader("key") diff --git a/src/main/scala/de/tobias/playpad/server/server/project/listener/CollectionAddListener.scala b/src/main/scala/de/tobias/playpad/server/server/project/listener/CollectionAddListener.scala new file mode 100644 index 0000000000000000000000000000000000000000..0bc308885baa1f9d5211222091f988810dfbcda2 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/server/project/listener/CollectionAddListener.scala @@ -0,0 +1,79 @@ +package de.tobias.playpad.server.server.project.listener + +import java.sql.Connection +import java.util.UUID + +import com.google.gson.{JsonArray, JsonElement, JsonObject} +import de.tobias.playpad.server.server.SqlHelper + +import scala.collection.JavaConverters + +/** + * Created by tobias on 18.02.17. + */ +class CollectionAddListener extends Listener { + override def onChange(json: JsonObject, connection: Connection): Unit = { + val childClass = json.get("child_class").getAsString + val childId = json.get("child_id").getAsString + + val fields = JavaConverters.asScalaIterator(json.get("fields").asInstanceOf[JsonArray].iterator()) + fields.filter(_.isJsonObject) + .map(f => f.asInstanceOf[JsonObject]) + .foreach(jsonField => { + val fieldName = jsonField.get("field").getAsString + val valueType = Class.forName(jsonField.get("type").getAsString) + val jsonValue = jsonField.get("value") + setValue(connection, childClass, childId, fieldName, valueType, jsonValue) + }) + + // Foreign Key + val fieldName = s"${json.get("parent_class").getAsString.toLowerCase}_id" + val valueType = Class.forName(json.get("parent_type").getAsString) + val jsonValue = json.get("parent_id") + setValue(connection, childClass, childId, fieldName, valueType, jsonValue) + } + + private def setValue(connection: Connection, className: String, objectId: String, fieldName: String, valueType: Class[_], jsonValue: JsonElement) = { + if (valueType == classOf[String]) { + val value = jsonValue.getAsString + + if (objectId.matches("[0-9]*")) { + val idInt = objectId.toInt + SqlHelper.insertOrUpdate(connection, className, idInt, fieldName, value) + } else { + val uuid = UUID.fromString(objectId) + SqlHelper.insertOrUpdate(connection, className, uuid, fieldName, value) + } + } else if (valueType == classOf[Boolean]) { + val value = jsonValue.getAsBoolean + + if (objectId.matches("[0-9]*")) { + val idInt = objectId.toInt + SqlHelper.insertOrUpdate(connection, className, idInt, fieldName, value) + } else { + val uuid = UUID.fromString(objectId) + SqlHelper.insertOrUpdate(connection, className, uuid, fieldName, value) + } + } else if (valueType == classOf[Integer]) { + val value = jsonValue.getAsInt + + if (objectId.matches("[0-9]*")) { + val idInt = objectId.toInt + SqlHelper.insertOrUpdate(connection, className, idInt, fieldName, value) + } else { + val uuid = UUID.fromString(objectId) + SqlHelper.insertOrUpdate(connection, className, uuid, fieldName, value) + } + } else if (valueType == classOf[Double]) { + val value = jsonValue.getAsDouble + + if (objectId.matches("[0-9]*")) { + val idInt = objectId.toInt + SqlHelper.insertOrUpdate(connection, className, idInt, fieldName, value) + } else { + val uuid = UUID.fromString(objectId) + SqlHelper.insertOrUpdate(connection, className, uuid, fieldName, value) + } + } + } +} diff --git a/src/main/scala/de/tobias/playpad/server/server/project/listener/CollectionRemoveListener.scala b/src/main/scala/de/tobias/playpad/server/server/project/listener/CollectionRemoveListener.scala new file mode 100644 index 0000000000000000000000000000000000000000..20875eb7e976e0873ed4aa3c14896b46ef2f6e8d --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/server/project/listener/CollectionRemoveListener.scala @@ -0,0 +1,24 @@ +package de.tobias.playpad.server.server.project.listener +import java.sql.Connection +import java.util.UUID + +import com.google.gson.JsonObject +import de.tobias.playpad.server.server.SqlHelper + +/** + * Created by tobias on 18.02.17. + */ +class CollectionRemoveListener extends Listener { + override def onChange(json: JsonObject, connection: Connection): Unit = { + val childClass = json.get("child_class").getAsString + val childId = json.get("child_id").getAsString + + if (childId.matches("[0-9]*")) { + val idInt = childId.toInt + SqlHelper.delete(connection, childClass, idInt) + } else { + val uuid = UUID.fromString(childId) + SqlHelper.delete(connection, childClass, uuid) + } + } +} diff --git a/src/main/scala/de/tobias/playpad/server/server/project/listener/Listener.scala b/src/main/scala/de/tobias/playpad/server/server/project/listener/Listener.scala new file mode 100644 index 0000000000000000000000000000000000000000..7d8220b2ca75d70acca9433dc9452d9d8cd7b3d0 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/server/project/listener/Listener.scala @@ -0,0 +1,13 @@ +package de.tobias.playpad.server.server.project.listener + +import java.sql.Connection + +import com.google.gson.JsonObject + +/** + * Created by tobias on 18.02.17. + */ +trait Listener { + + def onChange(json: JsonObject, connection: Connection): Unit +} diff --git a/src/main/scala/de/tobias/playpad/server/server/project/listener/PropertyUpdateListener.scala b/src/main/scala/de/tobias/playpad/server/server/project/listener/PropertyUpdateListener.scala new file mode 100644 index 0000000000000000000000000000000000000000..33aba2d3d9bba6354bb3de6aa1442331326e9a8e --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/server/project/listener/PropertyUpdateListener.scala @@ -0,0 +1,66 @@ +package de.tobias.playpad.server.server.project.listener +import java.sql.Connection +import java.util.UUID + +import com.google.gson.JsonObject +import de.tobias.playpad.server.server.SqlHelper + +/** + * Created by tobias on 18.02.17. + */ +class PropertyUpdateListener extends Listener { + override def onChange(json: JsonObject, connection: Connection): Unit = { + json match { + case jsonObject: JsonObject => + val className = jsonObject.get("class").getAsString + val field = jsonObject.get("field").getAsString + val id = jsonObject.get("id").getAsString + + val valueType = Class.forName(jsonObject.get("type").getAsString) + + if (valueType == classOf[String]) { + val value = jsonObject.get("value").getAsString + + if (id.matches("[0-9]*")) { + val idInt = id.toInt + SqlHelper.insertOrUpdate(connection, className, idInt, field, value) + } else { + val uuid = UUID.fromString(id) + SqlHelper.insertOrUpdate(connection, className, uuid, field, value) + } + } else if (valueType == classOf[Boolean]) { + val value = jsonObject.get("value").getAsBoolean + + if (id.matches("[0-9]*")) { + val idInt = id.toInt + SqlHelper.insertOrUpdate(connection, className, idInt, field, value) + } else { + val uuid = UUID.fromString(id) + SqlHelper.insertOrUpdate(connection, className, uuid, field, value) + } + } else if (valueType == classOf[Integer]){ + val value = jsonObject.get("value").getAsInt + + if (id.matches("[0-9]*")) { + val idInt = id.toInt + SqlHelper.insertOrUpdate(connection, className, idInt, field, value) + } else { + val uuid = UUID.fromString(id) + SqlHelper.insertOrUpdate(connection, className, uuid, field, value) + } + } else if (valueType == classOf[Double]) { + val value = jsonObject.get("value").getAsDouble + + if (id.matches("[0-9]*")) { + val idInt = id.toInt + SqlHelper.insertOrUpdate(connection, className, idInt, field, value) + } else { + val uuid = UUID.fromString(id) + SqlHelper.insertOrUpdate(connection, className, uuid, field, value) + } + } + + case _ => + } + } +}