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

Add Design and add Loader and Save classes

parent be8a536f
No related branches found
No related tags found
No related merge requests found
Showing
with 157 additions and 5 deletions
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 = _
}
......@@ -14,5 +14,7 @@ class Pad() {
var contentType: String = _
var paths: List[Path] = List()
var design: Design = _
var page: Page = _
}
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
}
}
......@@ -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
......
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
}
}
......@@ -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
}
......
......@@ -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()
......
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
}
}
......@@ -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
}
......
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)
}
}
......@@ -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)
}
}
}
......@@ -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"
}
......@@ -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"
}
......@@ -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)
}
}
......@@ -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
......
......@@ -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)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment