From b586af4e14f9bf1452a4bd50df3fb6343f5603dc Mon Sep 17 00:00:00 2001
From: tobias <thinkdifferent055@gmail.com>
Date: Wed, 22 Feb 2017 22:39:30 +0100
Subject: [PATCH] Add Pad Update, Clear; Path Remove, Update, Add; Fixed Pad
 Add, Project Loader/Saver

---
 .../project/loader/json/PadLoader.scala       |  2 ++
 .../server/project/loader/sql/PadLoader.scala |  1 +
 .../server/project/saver/sql/PadSaver.scala   |  1 +
 .../server/project/utils/JsonDef.scala        |  2 +-
 .../playpad/server/project/utils/SqlDef.scala |  1 +
 .../playpad/server/server/SqlHelper.scala     |  6 +++-
 .../project/sync/ProjectSyncHandler.scala     | 15 +++++----
 .../sync/listener/pad/PadAddListener.scala    |  8 ++---
 ...eListener.scala => PadClearListener.scala} |  6 ++--
 .../sync/listener/pad/PadUpdateListener.scala | 33 +++++++++++++++++++
 .../sync/listener/path/PathAddListener.scala  |  9 +++--
 .../listener/path/PathRemoveListener.scala    | 21 ++++++++++++
 .../listener/path/PathUpdateListener.scala    | 27 +++++++++++++++
 13 files changed, 111 insertions(+), 21 deletions(-)
 rename src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/{PadRemoveListener.scala => PadClearListener.scala} (77%)
 create mode 100644 src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadUpdateListener.scala
 create mode 100644 src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathRemoveListener.scala
 create mode 100644 src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathUpdateListener.scala

diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/json/PadLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/json/PadLoader.scala
index b60384d..61621f5 100644
--- a/src/main/scala/de/tobias/playpad/server/project/loader/json/PadLoader.scala
+++ b/src/main/scala/de/tobias/playpad/server/project/loader/json/PadLoader.scala
@@ -20,6 +20,8 @@ class PadLoader {
 			val pad = new Pad()
 			pad.id = UUID.fromString(json.get(PAD_ID).getAsString)
 			pad.name = json.get(PAD_NAME).getAsString
+			pad.position = json.get(PAD_POSITION).getAsInt
+			pad.name = json.get(PAD_CONTENT_TYPE).getAsString
 			pad.page = page
 
 			pad
diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/sql/PadLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/sql/PadLoader.scala
index 19e0c13..cffdcbc 100644
--- a/src/main/scala/de/tobias/playpad/server/project/loader/sql/PadLoader.scala
+++ b/src/main/scala/de/tobias/playpad/server/project/loader/sql/PadLoader.scala
@@ -23,6 +23,7 @@ class PadLoader(val connection: Connection) {
 			pad.id = UUID.fromString(result.getString(PAD_ID))
 			pad.name = result.getString(PAD_NAME)
 			pad.position = result.getInt(PAD_POSITION)
+			pad.contentType = result.getString(PAD_CONTENT_TYPE)
 
 			val pathLoader = new PathLoader(connection)
 			pad.paths = pathLoader.load(pad)
diff --git a/src/main/scala/de/tobias/playpad/server/project/saver/sql/PadSaver.scala b/src/main/scala/de/tobias/playpad/server/project/saver/sql/PadSaver.scala
index 47a330a..ec52487 100644
--- a/src/main/scala/de/tobias/playpad/server/project/saver/sql/PadSaver.scala
+++ b/src/main/scala/de/tobias/playpad/server/project/saver/sql/PadSaver.scala
@@ -13,6 +13,7 @@ class PadSaver(val connection: Connection) {
 	def save(pad: Pad): Unit = {
 		SqlHelper.insertOrUpdate(connection, PAD, pad.id, PAD_NAME, pad.name)
 		SqlHelper.insertOrUpdate(connection, PAD, pad.id, PAD_POSITION, pad.position)
+		SqlHelper.insertOrUpdate(connection, PAD, pad.id, PAD_CONTENT_TYPE, pad.contentType)
 		SqlHelper.insertOrUpdate(connection, PAD, pad.id, PAD_PAGE_REF, pad.page.id)
 
 		val pathSaver = new PathSaver(connection)
diff --git a/src/main/scala/de/tobias/playpad/server/project/utils/JsonDef.scala b/src/main/scala/de/tobias/playpad/server/project/utils/JsonDef.scala
index 5bcefb1..3244043 100644
--- a/src/main/scala/de/tobias/playpad/server/project/utils/JsonDef.scala
+++ b/src/main/scala/de/tobias/playpad/server/project/utils/JsonDef.scala
@@ -21,6 +21,6 @@ object JsonDef {
 	val PAD_PATHS = "paths"
 
 	val PATH_ID = "id"
-	val PATH_PATH = "name"
+	val PATH_PATH = "path"
 
 }
diff --git a/src/main/scala/de/tobias/playpad/server/project/utils/SqlDef.scala b/src/main/scala/de/tobias/playpad/server/project/utils/SqlDef.scala
index 29d701c..9e11d7a 100644
--- a/src/main/scala/de/tobias/playpad/server/project/utils/SqlDef.scala
+++ b/src/main/scala/de/tobias/playpad/server/project/utils/SqlDef.scala
@@ -20,6 +20,7 @@ object SqlDef {
 	val PAD_ID = "id"
 	val PAD_NAME = "name"
 	val PAD_POSITION = "position"
+	val PAD_CONTENT_TYPE = "content_type"
 	val PAD_PAGE_REF = "page_id"
 
 	val PATH = "Path"
diff --git a/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala b/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala
index d1633a0..549a098 100644
--- a/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala
+++ b/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala
@@ -1,5 +1,6 @@
 package de.tobias.playpad.server.server
 
+import java.sql.Types.NULL
 import java.sql.{Connection, PreparedStatement}
 import java.util.UUID
 
@@ -42,6 +43,9 @@ object SqlHelper {
 			case value: UUID =>
 				preparedStatement.setString(2, value.toString)
 				preparedStatement.setString(3, value.toString)
+			case null =>
+				preparedStatement.setNull(2, NULL)
+				preparedStatement.setNull(3, NULL)
 
 		}
 
@@ -103,7 +107,7 @@ object SqlHelper {
 			  |) ENGINE=InnoDB DEFAULT CHARSET=latin1;""".stripMargin)
 		createTable(
 			"""CREATE TABLE `Path` (
-			  |  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
+			  |  `id` varchar(40) NOT NULL DEFAULT '',
 			  |  `path` text,
 			  |  `pad_id` varchar(40) DEFAULT NULL,
 			  |  PRIMARY KEY (`id`),
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 7e68623..1ec5f7a 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
@@ -6,9 +6,9 @@ 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.sync.listener.pad.{PadAddListener, PadRemoveListener}
+import de.tobias.playpad.server.server.project.sync.listener.pad.{PadAddListener, PadClearListener, 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
+import de.tobias.playpad.server.server.project.sync.listener.path.{PathAddListener, PathRemoveListener, PathUpdateListener}
 import de.tobias.playpad.server.server.project.sync.listener.project.{ProjectAddListener, ProjectRemoveListener, ProjectUpdateListener}
 import org.eclipse.jetty.websocket.api.Session
 import org.eclipse.jetty.websocket.api.annotations.{OnWebSocketClose, OnWebSocketConnect, OnWebSocketMessage, WebSocket}
@@ -35,10 +35,12 @@ import scala.collection.{Map, mutable}
 		"page-rm" -> new PageRemoveListener(),
 
 		"pad-add" -> new PadAddListener(),
-		//"pad-update" -> new PadUpdateListener(),
-		"pad-rm" -> new PadRemoveListener(),
+		"pad-update" -> new PadUpdateListener(),
+		"pad-clear" -> new PadClearListener(),
 
-		"path-add" -> new PathAddListener()
+		"path-add" -> new PathAddListener(),
+		"path-update" -> new PathUpdateListener(),
+		"path-rm" -> new PathRemoveListener()
 	)
 
 	@OnWebSocketConnect def onConnect(serverSession: Session): Unit = {
@@ -66,8 +68,7 @@ import scala.collection.{Map, mutable}
 
 		val session = account.Session.getSession(key, sessionDao)
 		session match {
-			case Some(s) =>
-				this.sessions(s.getAccount) -= serverSession
+			case Some(s) => this.sessions(s.getAccount) -= serverSession
 			case None => serverSession.close(500, "Invalid Key")
 		}
 	}
diff --git a/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadAddListener.scala b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadAddListener.scala
index c835bb0..e9aeff3 100644
--- a/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadAddListener.scala
+++ b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadAddListener.scala
@@ -15,12 +15,8 @@ import de.tobias.playpad.server.server.project.sync.listener.Listener
 class PadAddListener extends Listener {
 	override def onChange(json: JsonObject, connection: Connection, session: Session): Unit = {
 		val padId = UUID.fromString(json.get("id").getAsString)
-		val pageId = UUID.fromString(json.get("page").getAsString)
-		val padName = json.get("name").getAsString
-		val padPosition= json.get("position").getAsInt
 
-		SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padId, SqlDef.PAD_PAGE_REF, pageId)
-		SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padId, SqlDef.PAD_NAME, padName)
-		SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padId, SqlDef.PAD_POSITION, padPosition)
+		SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padId, SqlDef.PAD_NAME, "")
+		SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padId, SqlDef.PAD_CONTENT_TYPE, null)
 	}
 }
diff --git a/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadRemoveListener.scala b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadClearListener.scala
similarity index 77%
rename from src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadRemoveListener.scala
rename to src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadClearListener.scala
index 6f31c8f..92f29cf 100644
--- a/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadRemoveListener.scala
+++ b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadClearListener.scala
@@ -12,10 +12,10 @@ import de.tobias.playpad.server.server.project.sync.listener.Listener
 /**
   * Created by tobias on 19.02.17.
   */
-class PadRemoveListener extends Listener {
+class PadClearListener extends Listener {
 	override def onChange(json: JsonObject, connection: Connection, session: Session): Unit = {
-		val padId = UUID.fromString(json.get("id").getAsString)
+		val pageId = UUID.fromString(json.get("id").getAsString)
 
-		SqlHelper.delete(connection, SqlDef.PAD, padId)
+		SqlHelper.delete(connection, SqlDef.PAGE, pageId)
 	}
 }
diff --git a/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadUpdateListener.scala b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadUpdateListener.scala
new file mode 100644
index 0000000..fe4590e
--- /dev/null
+++ b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/pad/PadUpdateListener.scala
@@ -0,0 +1,33 @@
+package de.tobias.playpad.server.server.project.sync.listener.pad
+
+import java.sql.Connection
+import java.util.UUID
+
+import com.google.gson.JsonObject
+import de.tobias.playpad.server.account.Session
+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
+
+/**
+  * Created by tobias on 19.02.17.
+  */
+class PadUpdateListener extends Listener {
+	override def onChange(json: JsonObject, connection: Connection, session: Session): Unit = {
+		val padID = UUID.fromString(json.get("id").getAsString)
+		val pageId = UUID.fromString(json.get("page").getAsString)
+		val field = json.get("field").getAsString
+
+		SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padID, SqlDef.PAD_PAGE_REF, pageId)
+
+		field match {
+			case "name" => SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padID, SqlDef.PAD_NAME, json.get("value").getAsString)
+			case "position" => SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padID, SqlDef.PAD_POSITION, json.get("value").getAsInt)
+			case "contentType" => if (!json.get("value").isJsonNull) {
+				SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padID, SqlDef.PAD_CONTENT_TYPE, json.get("value").getAsString)
+			} else {
+				SqlHelper.insertOrUpdate(connection, SqlDef.PAD, padID, SqlDef.PAD_CONTENT_TYPE, null)
+			}
+		}
+	}
+}
diff --git a/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathAddListener.scala b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathAddListener.scala
index 6cdce90..33ab2d7 100644
--- a/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathAddListener.scala
+++ b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathAddListener.scala
@@ -16,9 +16,12 @@ class PathAddListener extends Listener {
 	override def onChange(json: JsonObject, connection: Connection, session: Session): Unit = {
 		val pathId = UUID.fromString(json.get("id").getAsString)
 		val padId = UUID.fromString(json.get("pad").getAsString)
-		val path = json.get("path").getAsString
 
-		SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PAD_PAGE_REF, padId)
-		SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PAD_NAME, path)
+		SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PATH_PAD, padId)
+
+		if (json.get("path") != null) {
+			val path = json.get("path").getAsString
+			SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PATH_NAME, path)
+		}
 	}
 }
diff --git a/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathRemoveListener.scala b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathRemoveListener.scala
new file mode 100644
index 0000000..3705e0c
--- /dev/null
+++ b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathRemoveListener.scala
@@ -0,0 +1,21 @@
+package de.tobias.playpad.server.server.project.sync.listener.path
+
+import java.sql.Connection
+import java.util.UUID
+
+import com.google.gson.JsonObject
+import de.tobias.playpad.server.account.Session
+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
+
+/**
+  * Created by tobias on 19.02.17.
+  */
+class PathRemoveListener extends Listener {
+	override def onChange(json: JsonObject, connection: Connection, session: Session): Unit = {
+		val pageId = UUID.fromString(json.get("id").getAsString)
+
+		SqlHelper.delete(connection, SqlDef.PATH, pageId)
+	}
+}
diff --git a/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathUpdateListener.scala b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathUpdateListener.scala
new file mode 100644
index 0000000..b11aa7a
--- /dev/null
+++ b/src/main/scala/de/tobias/playpad/server/server/project/sync/listener/path/PathUpdateListener.scala
@@ -0,0 +1,27 @@
+package de.tobias.playpad.server.server.project.sync.listener.path
+
+import java.sql.Connection
+import java.util.UUID
+
+import com.google.gson.JsonObject
+import de.tobias.playpad.server.account.Session
+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
+
+/**
+  * Created by tobias on 19.02.17.
+  */
+class PathUpdateListener extends Listener {
+	override def onChange(json: JsonObject, connection: Connection, session: Session): Unit = {
+		val pathId = UUID.fromString(json.get("id").getAsString)
+		val padId = UUID.fromString(json.get("pad").getAsString)
+		val field = json.get("field").getAsString
+
+		SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PATH_PAD, padId)
+
+		field match {
+			case "path" => SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PATH_NAME, json.get("value").getAsString)
+		}
+	}
+}
-- 
GitLab