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 deleted file mode 100644 index 8a6d0fdeec3a01be51ed22dc7978cd286d91584b..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/server/project/ProjectHandler.scala +++ /dev/null @@ -1,85 +0,0 @@ -package de.tobias.playpad.server.server.project - -import java.sql.Connection - -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.project.listener.{CollectionAddListener, CollectionRemoveListener, PropertyUpdateListener} -import org.eclipse.jetty.websocket.api.Session -import org.eclipse.jetty.websocket.api.annotations.{OnWebSocketClose, OnWebSocketConnect, OnWebSocketMessage, WebSocket} - -import scala.collection.{Map, mutable} - -/** - * Created by tobias on 13.02.17. - */ -@Deprecated -@WebSocket class ProjectHandler(sessionDao: Dao[account.Session, Int], connection: Connection) { - - // 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) { - serverSession.close(500, "Invalid Key") - } - - val sessions = sessionDao.queryForEq("key", key) - if (sessions.size() == 1) { - val session = sessions.get(0) - if (!this.sessions.contains(session.getAccount)) { - this.sessions += (session.getAccount -> new mutable.HashSet[Session]()) - } - this.sessions(session.getAccount) += serverSession - } else { - serverSession.close(500, "Invalid Key") - } - } - - @OnWebSocketClose def onClose(serverSession: Session, status: Int, reason: String): Unit = { - val key = serverSession.getUpgradeRequest.getHeader("key") - if (key == null) { - serverSession.close(500, "Invalid Key") - } - - val sessions = sessionDao.queryForEq("key", key) - if (sessions.size() == 1) { - val session = sessions.get(0) - this.sessions(session.getAccount) -= serverSession - } - } - - @OnWebSocketMessage def onMessage(serverSession: Session, text: String): Unit = { - // Store in Database - try { - 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") - if (key == null) { - serverSession.close(500, "Invalid Key") - } - - val sessions = sessionDao.queryForEq("key", key) - if (sessions.size() == 1) { - val session = sessions.get(0) - this.sessions(session.getAccount) - .filter(s => s != serverSession) - .foreach(s => s.getRemote.sendStringByFuture(text)) - } - } catch { - case e: Exception => e.printStackTrace() - } - } -} \ No newline at end of file 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 deleted file mode 100644 index a6b1867b7667ce6b974a1cf29fe78184fc853acb..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/server/project/listener/CollectionAddListener.scala +++ /dev/null @@ -1,80 +0,0 @@ -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. - */ -@Deprecated -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 deleted file mode 100644 index 202f36e1a860b7137e2d61499a041c6daa326f6d..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/server/project/listener/CollectionRemoveListener.scala +++ /dev/null @@ -1,26 +0,0 @@ -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. - */ -@Deprecated -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 deleted file mode 100644 index d964ed1a29329bcd3e45a0907f4ca1d057257d0e..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/server/project/listener/Listener.scala +++ /dev/null @@ -1,14 +0,0 @@ -package de.tobias.playpad.server.server.project.listener - -import java.sql.Connection - -import com.google.gson.JsonObject - -/** - * Created by tobias on 18.02.17. - */ -@Deprecated -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 deleted file mode 100644 index 5222d5d5e4dcbc3f640846327863dcae5e748e9a..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/server/project/listener/PropertyUpdateListener.scala +++ /dev/null @@ -1,68 +0,0 @@ -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. - */ -@Deprecated -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 _ => - } - } -} diff --git a/src/main/scala/de/tobias/playpad/server/server/project/sync/ProjectSyncHandler.scala b/src/main/scala/de/tobias/playpad/server/server/project/sync/ProjectSyncHandler.scala index f3901eee9e6141a68ae99efcf766cd9ccb76a37f..a653df8beec3ac72bccf4ab60b20a3191f80d2cf 100644 --- a/src/main/scala/de/tobias/playpad/server/server/project/sync/ProjectSyncHandler.scala +++ b/src/main/scala/de/tobias/playpad/server/server/project/sync/ProjectSyncHandler.scala @@ -10,27 +10,21 @@ import de.tobias.playpad.server.account.Account import de.tobias.playpad.server.project.utils.SqlDef import de.tobias.playpad.server.server.SqlHelper import de.tobias.playpad.server.server.project.sync.listener.Listener -import de.tobias.playpad.server.server.project.sync.listener.design.{DesignAddListener, DesignUpdateListener} -import de.tobias.playpad.server.server.project.sync.listener.pad.settings.{PadSettingsAddListener, PadSettingsUpdateListener} -import de.tobias.playpad.server.server.project.sync.listener.pad.{PadAddListener, PadClearListener, PadRemoveListener, PadUpdateListener} -import de.tobias.playpad.server.server.project.sync.listener.page.{PageAddListener, PageRemoveListener, PageUpdateListener} -import de.tobias.playpad.server.server.project.sync.listener.path.{PathAddListener, PathRemoveListener} -import de.tobias.playpad.server.server.project.sync.listener.project.{ProjectAddListener, ProjectRemoveListener, ProjectUpdateListener} import io.github.classgraph.ClassGraph import org.eclipse.jetty.websocket.api.Session import org.eclipse.jetty.websocket.api.annotations.{OnWebSocketClose, OnWebSocketConnect, OnWebSocketMessage, WebSocket} -import scala.jdk.CollectionConverters._ import scala.collection.{Map, mutable} +import scala.jdk.CollectionConverters._ /** * Created by tobias on 19.02.17. */ -@WebSocket class ProjectSyncHandler(sessionDao: Dao[account.Session, Int], connection: Connection) { +@WebSocket +class ProjectSyncHandler(sessionDao: Dao[account.Session, Int], connection: Connection) { val SESSION_KEY_HEADER = "key" - // TODO mutable.HashSet --> Set private var sessions: Map[Account, mutable.HashSet[Session]] = new mutable.HashMap[Account, mutable.HashSet[Session]]() private val listeners = { @@ -51,7 +45,8 @@ import scala.collection.{Map, mutable} }).toMap } - @OnWebSocketConnect def onConnect(serverSession: Session): Unit = { + @OnWebSocketConnect + def onConnect(serverSession: Session): Unit = { val key = serverSession.getUpgradeRequest.getHeader(SESSION_KEY_HEADER) if (key == null) { serverSession.close(500, "Invalid Key") @@ -68,7 +63,8 @@ import scala.collection.{Map, mutable} } } - @OnWebSocketClose def onClose(serverSession: Session, status: Int, reason: String): Unit = { + @OnWebSocketClose + def onClose(serverSession: Session, status: Int, reason: String): Unit = { val key = serverSession.getUpgradeRequest.getHeader(SESSION_KEY_HEADER) if (key == null) { serverSession.close(500, "Invalid Key") @@ -81,7 +77,8 @@ import scala.collection.{Map, mutable} } } - @OnWebSocketMessage def onMessage(serverSession: Session, text: String): Unit = { + @OnWebSocketMessage + def onMessage(serverSession: Session, text: String): Unit = { println(text) // Store in Database try { @@ -96,9 +93,9 @@ import scala.collection.{Map, mutable} session match { case Some(s) => // Write last modification to project table - val timeStemp = json.get("time").getAsLong + val timeStamp = json.get("time").getAsLong val projectRef = UUID.fromString(json.get("project").getAsString) - SqlHelper.insertOrUpdate(connection, SqlDef.PROJECT, projectRef, SqlDef.PROJECT_LAST_MODIFIED, timeStemp) + SqlHelper.insertOrUpdate(connection, SqlDef.PROJECT, projectRef, SqlDef.PROJECT_LAST_MODIFIED, timeStamp) SqlHelper.insertOrUpdate(connection, SqlDef.PROJECT, projectRef, SqlDef.PROJECT_SESSION_KEY, s.key) val cmd = json.get("cmd").getAsString