diff --git a/src/main/scala/de/tobias/playpad/server/project/Design.scala b/src/main/scala/de/tobias/playpad/server/project/Design.scala new file mode 100644 index 0000000000000000000000000000000000000000..0dc6abbe26d5580de8e74d13b053c7d2bc57f7fe --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/project/Design.scala @@ -0,0 +1,15 @@ +package de.tobias.playpad.server.project + +import java.util.UUID + +/** + * Created by tobias on 23.02.17. + */ +class Design { + + var id: UUID = UUID.randomUUID() + var backgroundColor: String = _ + var playColor: String = _ + + var pad: Pad = _ +} 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 9945e9f50aac83e0976d182ec1ee462996c41016..9c4b0fc6ad2fc28c4e3b2412a5f10c24afd496a0 100644 --- a/src/main/scala/de/tobias/playpad/server/project/Pad.scala +++ b/src/main/scala/de/tobias/playpad/server/project/Pad.scala @@ -14,5 +14,7 @@ class Pad() { var contentType: String = _ var paths: List[Path] = List() + var design: Design = _ + var page: Page = _ } diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/json/DesignLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/json/DesignLoader.scala new file mode 100644 index 0000000000000000000000000000000000000000..057a19d56f9924f9ba4f684277f9ead285ca1b10 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/project/loader/json/DesignLoader.scala @@ -0,0 +1,23 @@ +package de.tobias.playpad.server.project.loader.json + +import java.util.UUID + +import com.google.gson.JsonObject +import de.tobias.playpad.server.project.{Design, Pad} +import de.tobias.playpad.server.project.utils.JsonDef + +/** + * Created by tobias on 23.02.17. + */ +class DesignLoader { + def load(jsonObject: JsonObject, pad: Pad): Design = { + val design = new Design() + + design.id = UUID.fromString(jsonObject.get(JsonDef.DESIGN_ID).getAsString) + design.backgroundColor = jsonObject.get(JsonDef.DESIGN_BACKGROUND_COLOR).getAsString + design.playColor = jsonObject.get(JsonDef.DESIGN_PLAY_COLOR).getAsString + design.pad = pad + + design + } +} 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 61621f54d170ce2f7bb71b2221f9d25d82507f52..c3591c2c16383ba3ef167054310fbd595a3784cc 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 @@ -24,6 +24,14 @@ class PadLoader { pad.name = json.get(PAD_CONTENT_TYPE).getAsString pad.page = page + val pathLoader = new PathLoader + val pathJson = json.getAsJsonArray(PAD_PATHS) + pad.paths = pathLoader.load(pathJson, pad) + + val designLoader = new DesignLoader + val designJson = json.getAsJsonObject(PAD_DESIGN) + pad.design = designLoader.load(designJson, pad) + pad }).toList pads diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/sql/DesignLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/sql/DesignLoader.scala new file mode 100644 index 0000000000000000000000000000000000000000..96b503f0801804e1203b092c4237f126dc1be456 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/project/loader/sql/DesignLoader.scala @@ -0,0 +1,34 @@ +package de.tobias.playpad.server.project.loader.sql + +import java.sql.Connection +import java.util.UUID + +import de.tobias.playpad.server.project.utils.SqlDef._ +import de.tobias.playpad.server.project.{Design, Pad} + +/** + * Created by tobias on 23.02.17. + */ +class DesignLoader(val connection: Connection) { + def load(pad: Pad): Design = { + val sql = s"SELECT * FROM $DESIGN WHERE $DESIGN_PAD_REF = ?" + val preparedStatement = connection.prepareStatement(sql) + preparedStatement.setString(1, pad.id.toString) + val result = preparedStatement.executeQuery() + + while (result.next()) { + val design = new Design + design.id = UUID.fromString(result.getString(DESIGN_ID)) + design.backgroundColor = result.getString(DESIGN_BACKGROUND_COLOR) + design.playColor = result.getString(DESIGN_PLAY_COLOR) + design.pad = pad + + result.close() + preparedStatement.close() + + return design + } + + null + } +} 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 cffdcbc2b65fa71884772d7a1e3fb7f818bc9c7f..0e6daffbaa7536ef605c5bfff2e533bb062e4c55 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 @@ -28,6 +28,9 @@ class PadLoader(val connection: Connection) { val pathLoader = new PathLoader(connection) pad.paths = pathLoader.load(pad) + val designLoader = new DesignLoader(connection) + pad.design = designLoader.load(pad) + pad.page = page pads = pad :: pads } diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/sql/PathLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/sql/PathLoader.scala index edfdb7c1f83d0944b0d27107a641bd865e215636..b4da8fc96ced83ab06cb40fb3a591f2b6e8baa1e 100644 --- a/src/main/scala/de/tobias/playpad/server/project/loader/sql/PathLoader.scala +++ b/src/main/scala/de/tobias/playpad/server/project/loader/sql/PathLoader.scala @@ -11,7 +11,7 @@ import de.tobias.playpad.server.project.{Pad, Path} */ class PathLoader(val connection: Connection) { def load(pad: Pad): List[Path] = { - val sql = s"SELECT * FROM $PATH WHERE $PATH_PAD = ?" + val sql = s"SELECT * FROM $PATH WHERE $PATH_PAD_REF = ?" val preparedStatement = connection.prepareStatement(sql) preparedStatement.setString(1, pad.id.toString) val result = preparedStatement.executeQuery() diff --git a/src/main/scala/de/tobias/playpad/server/project/saver/json/DesignSaver.scala b/src/main/scala/de/tobias/playpad/server/project/saver/json/DesignSaver.scala new file mode 100644 index 0000000000000000000000000000000000000000..37144e12c4522091edc9996b23cfded863cf07b5 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/project/saver/json/DesignSaver.scala @@ -0,0 +1,20 @@ +package de.tobias.playpad.server.project.saver.json + +import com.google.gson.{JsonElement, JsonObject} +import de.tobias.playpad.server.project.Design +import de.tobias.playpad.server.project.utils.JsonDef._ + +/** + * Created by tobias on 23.02.17. + */ +class DesignSaver { + def save(design: Design): JsonElement = { + val json = new JsonObject() + if (design != null) { + json.addProperty(DESIGN_ID, design.id.toString) + json.addProperty(DESIGN_BACKGROUND_COLOR, design.backgroundColor) + json.addProperty(DESIGN_PLAY_COLOR, design.playColor) + } + json + } +} diff --git a/src/main/scala/de/tobias/playpad/server/project/saver/json/PadSaver.scala b/src/main/scala/de/tobias/playpad/server/project/saver/json/PadSaver.scala index 3a98a38d1bf273902fd7b77e565b9acc978a3726..aec2e27cd2a45c13632f8da9ea78382266a4d333 100644 --- a/src/main/scala/de/tobias/playpad/server/project/saver/json/PadSaver.scala +++ b/src/main/scala/de/tobias/playpad/server/project/saver/json/PadSaver.scala @@ -15,11 +15,15 @@ class PadSaver { val pathArray = new JsonArray() pad.paths.foreach(path => pathArray.add(pathSaver.save(path))) + val designSaver = new DesignSaver + val designJson = designSaver.save(pad.design) + jsonObject.addProperty(PAD_ID, pad.id.toString) jsonObject.addProperty(PAD_NAME, pad.name) jsonObject.addProperty(PAD_POSITION, pad.position) jsonObject.addProperty(PAD_CONTENT_TYPE, pad.contentType) jsonObject.add(PAD_PATHS, pathArray) + jsonObject.add(PAD_DESIGN, designJson) jsonObject } diff --git a/src/main/scala/de/tobias/playpad/server/project/saver/sql/DesignSaver.scala b/src/main/scala/de/tobias/playpad/server/project/saver/sql/DesignSaver.scala new file mode 100644 index 0000000000000000000000000000000000000000..3f465bb20dee4d9b5f9983ccaa57bb658c3be06d --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/project/saver/sql/DesignSaver.scala @@ -0,0 +1,18 @@ +package de.tobias.playpad.server.project.saver.sql + +import java.sql.Connection + +import de.tobias.playpad.server.project.Design +import de.tobias.playpad.server.project.utils.SqlDef._ +import de.tobias.playpad.server.server.SqlHelper + +/** + * Created by tobias on 23.02.17. + */ +class DesignSaver(val connection: Connection) { + def save(design: Design): Unit = { + SqlHelper.insertOrUpdate(connection, DESIGN, design.id, DESIGN_BACKGROUND_COLOR, design.backgroundColor) + SqlHelper.insertOrUpdate(connection, DESIGN, design.id, DESIGN_PLAY_COLOR, design.playColor) + SqlHelper.insertOrUpdate(connection, DESIGN, design.id, DESIGN_PAD_REF, design.pad.id) + } +} 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 ec524877aee4c6d33362c9ef71b74e0e421651f7..602ed13a81e898ea4f5e6750329b4ee9cda04667 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 @@ -18,5 +18,10 @@ 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) + } } } 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 3244043797ed8fbfc133eacf302bf683a6c9a226..c0c5ef90019d1fec2a98b455bac3deca47c36b42 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 @@ -19,8 +19,13 @@ object JsonDef { val PAD_POSITION = "position" val PAD_CONTENT_TYPE = "contentType" val PAD_PATHS = "paths" + val PAD_DESIGN = "design" val PATH_ID = "id" val PATH_PATH = "path" + val DESIGN_ID = "id" + val DESIGN_BACKGROUND_COLOR = "background_color" + val DESIGN_PLAY_COLOR = "play_color" + } 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 9e11d7a9fd107d147ee14e00306257d505e6e193..6db997c476c4d179798c451f121538014e8b0219 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 @@ -26,6 +26,12 @@ object SqlDef { val PATH = "Path" val PATH_ID = "id" val PATH_NAME = "path" - val PATH_PAD = "pad_id" + val PATH_PAD_REF = "pad_id" + + val DESIGN = "Design" + val DESIGN_ID = "id" + val DESIGN_PAD_REF = "pad_id" + val DESIGN_BACKGROUND_COLOR = "background_color" + val DESIGN_PLAY_COLOR = "play_color" } 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 549a098a6e9007af106ed6001382a4d4c02cabdb..6eeb5582e0c2df8b90d9d4b852ef347b65688627 100644 --- a/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala +++ b/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala @@ -99,7 +99,6 @@ object SqlHelper { | `name` varchar(255) DEFAULT NULL, | `position` int(11) DEFAULT NULL, | `page` int(11) DEFAULT NULL, - | `design_id` varchar(48) DEFAULT NULL, | `page_id` varchar(48) DEFAULT NULL, | PRIMARY KEY (`id`), | UNIQUE KEY `id` (`id`) @@ -114,5 +113,15 @@ 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 `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) } } 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 33ab2d7fa3f5311789197984e8893623b89e03ce..497e925dc96ba4a437b9fc0a99696798359b7fec 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 @@ -17,7 +17,7 @@ class PathAddListener extends Listener { val pathId = UUID.fromString(json.get("id").getAsString) val padId = UUID.fromString(json.get("pad").getAsString) - SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PATH_PAD, padId) + SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PATH_PAD_REF, padId) if (json.get("path") != null) { val path = json.get("path").getAsString 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 index b11aa7ae06f7c42f62873af262fbd3e1f3df6057..59be933edb4a9468c1a76f4d74a0bb1a8324889d 100644 --- 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 @@ -18,7 +18,7 @@ class PathUpdateListener extends Listener { val padId = UUID.fromString(json.get("pad").getAsString) val field = json.get("field").getAsString - SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PATH_PAD, padId) + SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PATH_PAD_REF, padId) field match { case "path" => SqlHelper.insertOrUpdate(connection, SqlDef.PATH, pathId, SqlDef.PATH_NAME, json.get("value").getAsString)