From 793e1023e1f87a451568fbda4637073c054f468a Mon Sep 17 00:00:00 2001 From: tobias <thinkdifferent055@gmail.com> Date: Sun, 29 Oct 2017 16:34:19 +0100 Subject: [PATCH] Add pad settings; improve json serialization --- PlayWallServer.iml | 2 + pom.xml | 7 +++ .../json/BooleanSerializerHandler.scala | 15 ++++++ .../server/json/DoubleSerializerHandler.scala | 15 ++++++ .../json/DurationSerializerHandler.scala | 17 +++++++ ...ndler.scala => IntSerializerHandler.scala} | 6 +-- .../tobias/playpad/server/project/Pad.scala | 7 +-- .../tobias/playpad/server/project/Page.scala | 2 +- .../server/project/loader/sql/PadLoader.scala | 2 +- .../server/project/saver/sql/PadSaver.scala | 8 +-- .../server/project/settings/Fade.scala | 29 +++++++++++ .../playpad/server/server/SqlHelper.scala | 49 +++++++++++++++---- 12 files changed, 137 insertions(+), 22 deletions(-) create mode 100644 src/main/scala/de/tobias/playpad/server/json/BooleanSerializerHandler.scala create mode 100644 src/main/scala/de/tobias/playpad/server/json/DoubleSerializerHandler.scala create mode 100644 src/main/scala/de/tobias/playpad/server/json/DurationSerializerHandler.scala rename src/main/scala/de/tobias/playpad/server/json/{NumberSerializerHandler.scala => IntSerializerHandler.scala} (65%) create mode 100644 src/main/scala/de/tobias/playpad/server/project/settings/Fade.scala diff --git a/PlayWallServer.iml b/PlayWallServer.iml index 7f3dfa7..b9512b7 100644 --- a/PlayWallServer.iml +++ b/PlayWallServer.iml @@ -37,5 +37,7 @@ <orderEntry type="library" name="Maven: com.j256.ormlite:ormlite-jdbc:5.0" level="project" /> <orderEntry type="library" name="Maven: mysql:mysql-connector-java:6.0.5" level="project" /> <orderEntry type="library" name="Maven: com.google.code.gson:gson:2.8.2" level="project" /> + <orderEntry type="library" scope="TEST" name="Maven: junit:junit:4.12" level="project" /> + <orderEntry type="library" scope="TEST" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" /> </component> </module> \ No newline at end of file diff --git a/pom.xml b/pom.xml index c56f1c2..ce3fbc0 100644 --- a/pom.xml +++ b/pom.xml @@ -126,5 +126,12 @@ <artifactId>gson</artifactId> <version>LATEST</version> </dependency> + + <dependency> + <groupId>junit</groupId> + <artifactId>junit</artifactId> + <version>4.12</version> + <scope>test</scope> + </dependency> </dependencies> </project> \ No newline at end of file diff --git a/src/main/scala/de/tobias/playpad/server/json/BooleanSerializerHandler.scala b/src/main/scala/de/tobias/playpad/server/json/BooleanSerializerHandler.scala new file mode 100644 index 0000000..28f330e --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/json/BooleanSerializerHandler.scala @@ -0,0 +1,15 @@ +package de.tobias.playpad.server.json + +import com.google.gson.{JsonElement, JsonPrimitive} + +class BooleanSerializerHandler extends SerializerHandler { + override def serialize(value: Any): JsonPrimitive = value match { + case bool: Boolean => new JsonPrimitive(bool) + case _ => null + } + + override def deserialize(jsonElement: JsonElement): Boolean = jsonElement match { + case jsonPrimitive: JsonPrimitive => jsonPrimitive.getAsBoolean + case _ => false + } +} diff --git a/src/main/scala/de/tobias/playpad/server/json/DoubleSerializerHandler.scala b/src/main/scala/de/tobias/playpad/server/json/DoubleSerializerHandler.scala new file mode 100644 index 0000000..08bd93b --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/json/DoubleSerializerHandler.scala @@ -0,0 +1,15 @@ +package de.tobias.playpad.server.json + +import com.google.gson.{JsonElement, JsonPrimitive} + +class DoubleSerializerHandler extends SerializerHandler { + override def serialize(value: Any): JsonPrimitive = value match { + case number: Number => new JsonPrimitive(number) + case _ => null + } + + override def deserialize(jsonElement: JsonElement): Double = jsonElement match { + case jsonPrimitive: JsonPrimitive => jsonPrimitive.getAsDouble + case _ => 0 + } +} diff --git a/src/main/scala/de/tobias/playpad/server/json/DurationSerializerHandler.scala b/src/main/scala/de/tobias/playpad/server/json/DurationSerializerHandler.scala new file mode 100644 index 0000000..29689b9 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/json/DurationSerializerHandler.scala @@ -0,0 +1,17 @@ +package de.tobias.playpad.server.json + +import javafx.util.Duration + +import com.google.gson.{JsonElement, JsonPrimitive} + +class DurationSerializerHandler extends SerializerHandler { + override def serialize(value: Any): JsonPrimitive = value match { + case duration: Duration => new JsonPrimitive(duration.toMillis) + case _ => null + } + + override def deserialize(jsonElement: JsonElement): Duration = jsonElement match { + case jsonPrimitive: JsonPrimitive => new Duration(jsonPrimitive.getAsInt) + case _ => Duration.ZERO + } +} diff --git a/src/main/scala/de/tobias/playpad/server/json/NumberSerializerHandler.scala b/src/main/scala/de/tobias/playpad/server/json/IntSerializerHandler.scala similarity index 65% rename from src/main/scala/de/tobias/playpad/server/json/NumberSerializerHandler.scala rename to src/main/scala/de/tobias/playpad/server/json/IntSerializerHandler.scala index 1e860fb..38a9ea7 100644 --- a/src/main/scala/de/tobias/playpad/server/json/NumberSerializerHandler.scala +++ b/src/main/scala/de/tobias/playpad/server/json/IntSerializerHandler.scala @@ -2,14 +2,14 @@ package de.tobias.playpad.server.json import com.google.gson.{JsonElement, JsonPrimitive} -class NumberSerializerHandler extends SerializerHandler { +class IntSerializerHandler extends SerializerHandler { override def serialize(value: Any): JsonPrimitive = value match { case number: Number => new JsonPrimitive(number) case _ => null } - override def deserialize(jsonElement: JsonElement): Number = jsonElement match { + override def deserialize(jsonElement: JsonElement): Int = jsonElement match { case jsonPrimitive: JsonPrimitive => jsonPrimitive.getAsInt - case _ => null + case _ => 0 } } diff --git a/src/main/scala/de/tobias/playpad/server/project/Pad.scala b/src/main/scala/de/tobias/playpad/server/project/Pad.scala index c42bc1a..79cdce5 100644 --- a/src/main/scala/de/tobias/playpad/server/project/Pad.scala +++ b/src/main/scala/de/tobias/playpad/server/project/Pad.scala @@ -3,6 +3,7 @@ package de.tobias.playpad.server.project import java.util.UUID import de.tobias.playpad.server.json._ +import de.tobias.playpad.server.project.settings.PadSettings /** * Created by tobias on 17.02.17. @@ -14,7 +15,7 @@ class Pad() { @JsonName("name") var name: String = _ - @JsonName(value = "position", handler = classOf[NumberSerializerHandler]) + @JsonName(value = "position", handler = classOf[IntSerializerHandler]) var position: Int = _ @JsonName("contentType") @@ -22,8 +23,8 @@ class Pad() { @JsonCollection(value = "paths", `type` = classOf[Path]) var paths: List[Path] = List() - @JsonObj("design") - var design: Design = _ + @JsonObj("settings") + var padSettings: PadSettings = _ @JsonParent var page: Page = _ diff --git a/src/main/scala/de/tobias/playpad/server/project/Page.scala b/src/main/scala/de/tobias/playpad/server/project/Page.scala index 76236fa..fcfa6ee 100644 --- a/src/main/scala/de/tobias/playpad/server/project/Page.scala +++ b/src/main/scala/de/tobias/playpad/server/project/Page.scala @@ -14,7 +14,7 @@ class Page { @JsonName("name") var name: String = _ - @JsonName(value = "position", handler = classOf[NumberSerializerHandler]) + @JsonName(value = "position", handler = classOf[IntSerializerHandler]) var position: Int = _ @JsonCollection(value = "pads", `type` = classOf[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 0e6daff..e89f571 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 @@ -29,7 +29,7 @@ class PadLoader(val connection: Connection) { pad.paths = pathLoader.load(pad) val designLoader = new DesignLoader(connection) - pad.design = designLoader.load(pad) + // pad.design = designLoader.load(pad) TODO pad.page = page pads = pad :: pads 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 602ed13..1bba342 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 @@ -19,9 +19,9 @@ class PadSaver(val connection: Connection) { val pathSaver = new PathSaver(connection) pad.paths.foreach(pathSaver.save) - if (pad.design != null) { - val designSaver = new DesignSaver(connection) - designSaver.save(pad.design) - } + // if (pad.design != null) { + // val designSaver = new DesignSaver(connection) + // designSaver.save(pad.design) + // } TODO } } diff --git a/src/main/scala/de/tobias/playpad/server/project/settings/Fade.scala b/src/main/scala/de/tobias/playpad/server/project/settings/Fade.scala new file mode 100644 index 0000000..c1816f1 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/project/settings/Fade.scala @@ -0,0 +1,29 @@ +package de.tobias.playpad.server.project.settings + +import java.util.UUID +import javafx.util.Duration + +import de.tobias.playpad.server.json._ + +class Fade { + + @JsonName(value = "id", handler = classOf[UUIDSerializerHandler]) + var id: UUID = UUID.randomUUID() + + @JsonName(value = "fadeIn", handler = classOf[DurationSerializerHandler]) + var fadeIn: Duration = _ + @JsonName(value = "fadeOut", handler = classOf[DurationSerializerHandler]) + var fadeOut: Duration = _ + + @JsonName(value = "fadeInStart", handler = classOf[BooleanSerializerHandler]) + var fadeInStart: Boolean = _ + @JsonName(value = "fadeInPause", handler = classOf[BooleanSerializerHandler]) + var fadeInPause: Boolean = _ + @JsonName(value = "fadeOutPause", handler = classOf[BooleanSerializerHandler]) + var fadeOutPause: Boolean = _ + @JsonName(value = "fadeOutStop", handler = classOf[BooleanSerializerHandler]) + var fadeOutStop: Boolean = _ + + @JsonParent + var padSettings: PadSettings = _ +} \ No newline at end of file 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 790235b..c19a5c0 100644 --- a/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala +++ b/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala @@ -98,6 +98,41 @@ object SqlHelper { | UNIQUE KEY `id` (`id`), | CONSTRAINT `Page_ibfk_1` FOREIGN KEY (`project_id`) REFERENCES `Project` (`id`) ON DELETE CASCADE |) ENGINE=InnoDB DEFAULT CHARSET=latin1;""".stripMargin) + + createTable( + """CREATE TABLE IF NOT EXISTS `Fade` ( + | `id` varchar(48) NOT NULL DEFAULT '', + | `fadeIn` int(11) DEFAULT NULL, + | `fadeOut` int(11) DEFAULT NULL, + | `fadeInStart` tinyint(1) DEFAULT NULL, + | `fadeInPause` tinyint(1) DEFAULT NULL, + | `fadeOutPause` tinyint(1) DEFAULT NULL, + | `fadeOutStop` tinyint(1) DEFAULT NULL, + | PRIMARY KEY (`id`) + |) ENGINE=InnoDB DEFAULT CHARSET=latin1;""".stripMargin) + createTable( + """CREATE TABLE IF NOT EXISTS `Design` ( + | `id` varchar(40) NOT NULL DEFAULT '', + | `background_color` varchar(20) DEFAULT NULL, + | `play_color` varchar(20) DEFAULT NULL, + | PRIMARY KEY (`id`) + |) ENGINE=InnoDB DEFAULT CHARSET=latin1;""".stripMargin) + createTable( + """CREATE TABLE IF NOT EXISTS `PadSettings` ( + | `id` varchar(48) NOT NULL DEFAULT '', + | `volume` double DEFAULT NULL, + | `loop` tinyint(1) DEFAULT NULL, + | `timeMode` varchar(20) DEFAULT NULL, + | `fade` varchar(48) DEFAULT NULL, + | `warning` int(11) DEFAULT NULL, + | `design` varchar(48) DEFAULT NULL, + | PRIMARY KEY (`id`), + | KEY `fade` (`fade`), + | KEY `design` (`design`), + | CONSTRAINT `PadSettings_ibfk_2` FOREIGN KEY (`design`) REFERENCES `Design` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, + | CONSTRAINT `PadSettings_ibfk_1` FOREIGN KEY (`fade`) REFERENCES `Fade` (`id`) ON DELETE CASCADE ON UPDATE CASCADE + |) ENGINE=InnoDB DEFAULT CHARSET=latin1;""".stripMargin) + createTable( """CREATE TABLE IF NOT EXISTS `Pad` ( | `id` varchar(48) NOT NULL DEFAULT '', @@ -105,8 +140,12 @@ object SqlHelper { | `position` int(11) DEFAULT NULL, | `content_type` varchar(100) DEFAULT NULL, | `page_id` varchar(48) DEFAULT NULL, + | `settings_id` varchar(48) DEFAULT NULL, | PRIMARY KEY (`id`), | UNIQUE KEY `id` (`id`), + | KEY `Pad_ibfk_1` (`page_id`), + | KEY `settings_id` (`settings_id`), + | CONSTRAINT `Pad_ibfk_2` FOREIGN KEY (`settings_id`) REFERENCES `PadSettings` (`id`) ON DELETE CASCADE ON UPDATE CASCADE, | CONSTRAINT `Pad_ibfk_1` FOREIGN KEY (`page_id`) REFERENCES `Page` (`id`) ON DELETE CASCADE |) ENGINE=InnoDB DEFAULT CHARSET=latin1;""".stripMargin) createTable( @@ -118,15 +157,5 @@ object SqlHelper { | KEY `pad_id` (`pad_id`), | CONSTRAINT `Path_ibfk_1` FOREIGN KEY (`pad_id`) REFERENCES `Pad` (`id`) ON DELETE CASCADE ON UPDATE CASCADE |) ENGINE=InnoDB DEFAULT CHARSET=latin1;""".stripMargin) - createTable( - """CREATE TABLE IF NOT EXISTS `Design` ( - | `id` varchar(40) NOT NULL DEFAULT '', - | `background_color` varchar(20) DEFAULT NULL, - | `play_color` varchar(20) DEFAULT NULL, - | `pad_id` varchar(40) DEFAULT NULL, - | PRIMARY KEY (`id`), - | KEY `pad_id` (`pad_id`), - | CONSTRAINT `Design_ibfk_1` FOREIGN KEY (`pad_id`) REFERENCES `Pad` (`id`) ON DELETE CASCADE ON UPDATE CASCADE - |) ENGINE=InnoDB DEFAULT CHARSET=latin1;""".stripMargin) } } -- GitLab