Skip to content
Snippets Groups Projects
Commit 03f6bc67 authored by Tobias Ullerich's avatar Tobias Ullerich
Browse files

Remove deprecated classes

parent a707103f
No related branches found
No related tags found
No related merge requests found
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
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)
}
}
}
}
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)
}
}
}
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
}
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 _ =>
}
}
}
...@@ -10,27 +10,21 @@ import de.tobias.playpad.server.account.Account ...@@ -10,27 +10,21 @@ import de.tobias.playpad.server.account.Account
import de.tobias.playpad.server.project.utils.SqlDef import de.tobias.playpad.server.project.utils.SqlDef
import de.tobias.playpad.server.server.SqlHelper 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.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 io.github.classgraph.ClassGraph
import org.eclipse.jetty.websocket.api.Session import org.eclipse.jetty.websocket.api.Session
import org.eclipse.jetty.websocket.api.annotations.{OnWebSocketClose, OnWebSocketConnect, OnWebSocketMessage, WebSocket} import org.eclipse.jetty.websocket.api.annotations.{OnWebSocketClose, OnWebSocketConnect, OnWebSocketMessage, WebSocket}
import scala.jdk.CollectionConverters._
import scala.collection.{Map, mutable} import scala.collection.{Map, mutable}
import scala.jdk.CollectionConverters._
/** /**
* Created by tobias on 19.02.17. * 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" 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 var sessions: Map[Account, mutable.HashSet[Session]] = new mutable.HashMap[Account, mutable.HashSet[Session]]()
private val listeners = { private val listeners = {
...@@ -51,7 +45,8 @@ import scala.collection.{Map, mutable} ...@@ -51,7 +45,8 @@ import scala.collection.{Map, mutable}
}).toMap }).toMap
} }
@OnWebSocketConnect def onConnect(serverSession: Session): Unit = { @OnWebSocketConnect
def onConnect(serverSession: Session): Unit = {
val key = serverSession.getUpgradeRequest.getHeader(SESSION_KEY_HEADER) val key = serverSession.getUpgradeRequest.getHeader(SESSION_KEY_HEADER)
if (key == null) { if (key == null) {
serverSession.close(500, "Invalid Key") serverSession.close(500, "Invalid Key")
...@@ -68,7 +63,8 @@ import scala.collection.{Map, mutable} ...@@ -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) val key = serverSession.getUpgradeRequest.getHeader(SESSION_KEY_HEADER)
if (key == null) { if (key == null) {
serverSession.close(500, "Invalid Key") serverSession.close(500, "Invalid Key")
...@@ -81,7 +77,8 @@ import scala.collection.{Map, mutable} ...@@ -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) println(text)
// Store in Database // Store in Database
try { try {
...@@ -96,9 +93,9 @@ import scala.collection.{Map, mutable} ...@@ -96,9 +93,9 @@ import scala.collection.{Map, mutable}
session match { session match {
case Some(s) => case Some(s) =>
// Write last modification to project table // 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) 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) SqlHelper.insertOrUpdate(connection, SqlDef.PROJECT, projectRef, SqlDef.PROJECT_SESSION_KEY, s.key)
val cmd = json.get("cmd").getAsString val cmd = json.get("cmd").getAsString
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment