diff --git a/src/main/java/de/tobias/playpad/server/json/JsonCollection.java b/src/main/java/de/tobias/playpad/server/json/JsonCollection.java new file mode 100644 index 0000000000000000000000000000000000000000..e6e8dcc40c044f0538aa6e575685d8815d5aea56 --- /dev/null +++ b/src/main/java/de/tobias/playpad/server/json/JsonCollection.java @@ -0,0 +1,18 @@ +package de.tobias.playpad.server.json; + +import java.lang.annotation.*; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface JsonCollection { + + /** + * Name of the field for the json. + * + * @return name + */ + String value(); + + Class type(); +} diff --git a/src/main/java/de/tobias/playpad/server/json/JsonName.java b/src/main/java/de/tobias/playpad/server/json/JsonName.java new file mode 100644 index 0000000000000000000000000000000000000000..464aaf3d34f80d8717c07b7c2baaddece0819a85 --- /dev/null +++ b/src/main/java/de/tobias/playpad/server/json/JsonName.java @@ -0,0 +1,18 @@ +package de.tobias.playpad.server.json; + +import java.lang.annotation.*; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface JsonName { + + /** + * Name of the field for the json. + * + * @return name + */ + String value(); + + Class<? extends SerializerHandler> handler() default StringSerializerHandler.class; +} diff --git a/src/main/java/de/tobias/playpad/server/json/JsonObj.java b/src/main/java/de/tobias/playpad/server/json/JsonObj.java new file mode 100644 index 0000000000000000000000000000000000000000..5f49aedaf0781182d60e28bdebab4e7bbd9db25d --- /dev/null +++ b/src/main/java/de/tobias/playpad/server/json/JsonObj.java @@ -0,0 +1,17 @@ +package de.tobias.playpad.server.json; + +import java.lang.annotation.*; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface JsonObj { + + /** + * Name of the field for the json. + * + * @return name + */ + String value(); + +} diff --git a/src/main/java/de/tobias/playpad/server/json/JsonParent.java b/src/main/java/de/tobias/playpad/server/json/JsonParent.java new file mode 100644 index 0000000000000000000000000000000000000000..d01a6491ba6c2b74c63c80c1a1360ee7804583c0 --- /dev/null +++ b/src/main/java/de/tobias/playpad/server/json/JsonParent.java @@ -0,0 +1,9 @@ +package de.tobias.playpad.server.json; + +import java.lang.annotation.*; + +@Target(ElementType.FIELD) +@Retention(RetentionPolicy.RUNTIME) +@Documented +public @interface JsonParent { +} diff --git a/src/main/scala/de/tobias/playpad/server/json/JsonSerializer.scala b/src/main/scala/de/tobias/playpad/server/json/JsonSerializer.scala new file mode 100644 index 0000000000000000000000000000000000000000..5683ddecad638f5740c569df06c1c2713397f7c0 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/json/JsonSerializer.scala @@ -0,0 +1,113 @@ +package de.tobias.playpad.server.json + +import java.util + +import com.google.gson.{JsonArray, JsonObject} + +class JsonSerializer { + + def serialize(o: Any): JsonObject = { + val jsonObject = new JsonObject + o.getClass.getDeclaredFields + .filter(f => f.isAnnotationPresent(classOf[JsonName])) + .foreach(f => { + f.setAccessible(true) + val annotation = f.getAnnotation(classOf[JsonName]) + val value = f.get(o) + if (value != null) { + jsonObject.add(annotation.value(), annotation.handler().newInstance().serialize(value)) + } + }) + + o.getClass.getDeclaredFields + .filter(f => f.isAnnotationPresent(classOf[JsonObj])) + .foreach(f => { + f.setAccessible(true) + val annotation = f.getAnnotation(classOf[JsonObj]) + val value = f.get(o) + if (value != null) { + jsonObject.add(annotation.value(), serialize(value)) + } + }) + + o.getClass.getDeclaredFields + .filter(f => f.isAnnotationPresent(classOf[JsonCollection])) + .foreach(f => { + f.setAccessible(true) + val annotation = f.getAnnotation(classOf[JsonCollection]) + val value = f.get(o) + + val jsonValues = value match { + case seq: Seq[_] => seq.map(serialize) + } + val jsonArray = new JsonArray(jsonValues.size) + jsonValues.foreach(jsonArray.add) + jsonObject.add(annotation.value(), jsonArray) + }) + + jsonObject + } + + def deserialize[T](jsonObject: JsonObject, clazz: Class[T]): T = { + val obj = clazz.newInstance() + + clazz.getDeclaredFields + .filter(f => f.isAnnotationPresent(classOf[JsonName])) + .foreach(f => { + println(clazz + "\t" + f.getName) + f.setAccessible(true) + val annotation = f.getAnnotation(classOf[JsonName]) + val value = jsonObject.get(annotation.value()) + f.set(obj, annotation.handler().newInstance().deserialize(value)) + }) + + + clazz.getDeclaredFields + .filter(f => f.isAnnotationPresent(classOf[JsonObj])) + .foreach(f => { + f.setAccessible(true) + val annotation = f.getAnnotation(classOf[JsonObj]) + val jsonObj = jsonObject.getAsJsonObject(annotation.value()) + val value = if (jsonObj != null) { + val value = deserialize(jsonObj, f.getType) + setParent(value, obj) + value + } else { + null + } + f.set(obj, value) + }) + + clazz.getDeclaredFields + .filter(f => f.isAnnotationPresent(classOf[JsonCollection])) + .foreach(f => { + f.setAccessible(true) + val annotation = f.getAnnotation(classOf[JsonCollection]) + val jsonArray = jsonObject.getAsJsonArray(annotation.value()) + + val tempList = new util.ArrayList[Any]() + jsonArray.forEach(elem => { + val value = deserialize(elem.getAsJsonObject, annotation.`type`()) + setParent(value, obj) + tempList.add(value) + }) + + var col = f.get(obj) + col match { + case _: List[_] => tempList.forEach(e => col = e :: col.asInstanceOf[List[_]]) + } + f.set(obj, col) + }) + + obj + } + + private def setParent(value: Any, parent: Any): Unit = { + value.getClass.getDeclaredFields + .filter(f => f.isAnnotationPresent(classOf[JsonParent])) + .foreach(f => { + f.setAccessible(true) + f.set(value, parent) + }) + } +} diff --git a/src/main/scala/de/tobias/playpad/server/json/NumberSerializerHandler.scala b/src/main/scala/de/tobias/playpad/server/json/NumberSerializerHandler.scala new file mode 100644 index 0000000000000000000000000000000000000000..1e860fbacce55a9d9ddcad5500ee47cccfd4e7cf --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/json/NumberSerializerHandler.scala @@ -0,0 +1,15 @@ +package de.tobias.playpad.server.json + +import com.google.gson.{JsonElement, JsonPrimitive} + +class NumberSerializerHandler 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 { + case jsonPrimitive: JsonPrimitive => jsonPrimitive.getAsInt + case _ => null + } +} diff --git a/src/main/scala/de/tobias/playpad/server/json/SerializerHandler.scala b/src/main/scala/de/tobias/playpad/server/json/SerializerHandler.scala new file mode 100644 index 0000000000000000000000000000000000000000..1f4d1b2f0a985a58e2a1d26e5c3da035c5736ba8 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/json/SerializerHandler.scala @@ -0,0 +1,9 @@ +package de.tobias.playpad.server.json + +import com.google.gson.{JsonElement, JsonPrimitive} + +trait SerializerHandler { + def serialize(value: Any): JsonPrimitive + + def deserialize(jsonElement: JsonElement): Any +} diff --git a/src/main/scala/de/tobias/playpad/server/json/StringSerializerHandler.scala b/src/main/scala/de/tobias/playpad/server/json/StringSerializerHandler.scala new file mode 100644 index 0000000000000000000000000000000000000000..6d99562e1132def2c8a96c27a465310f26216a96 --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/json/StringSerializerHandler.scala @@ -0,0 +1,15 @@ +package de.tobias.playpad.server.json + +import com.google.gson.{JsonElement, JsonPrimitive} + +class StringSerializerHandler extends SerializerHandler { + override def serialize(value: Any): JsonPrimitive = value match { + case str: String => new JsonPrimitive(str) + case _ => null + } + + override def deserialize(jsonElement: JsonElement): String = jsonElement match { + case jsonPrimitive: JsonPrimitive => jsonPrimitive.getAsString + case _ => null + } +} diff --git a/src/main/scala/de/tobias/playpad/server/json/UUIDSerializerHandler.scala b/src/main/scala/de/tobias/playpad/server/json/UUIDSerializerHandler.scala new file mode 100644 index 0000000000000000000000000000000000000000..51afb490a9e08ab75e60dee6d38392bb15229fad --- /dev/null +++ b/src/main/scala/de/tobias/playpad/server/json/UUIDSerializerHandler.scala @@ -0,0 +1,17 @@ +package de.tobias.playpad.server.json + +import java.util.UUID + +import com.google.gson.{JsonElement, JsonPrimitive} + +class UUIDSerializerHandler extends SerializerHandler { + override def serialize(value: Any): JsonPrimitive = value match { + case uuid: UUID => new JsonPrimitive(uuid.toString) + case _ => null + } + + override def deserialize(jsonElement: JsonElement): UUID = jsonElement match { + case jsonPrimitive: JsonPrimitive => UUID.fromString(jsonPrimitive.getAsString) + case _ => null + } +} diff --git a/src/main/scala/de/tobias/playpad/server/project/Design.scala b/src/main/scala/de/tobias/playpad/server/project/Design.scala index 0dc6abbe26d5580de8e74d13b053c7d2bc57f7fe..c3b664433895609fe274f0e7ea0b602685ead683 100644 --- a/src/main/scala/de/tobias/playpad/server/project/Design.scala +++ b/src/main/scala/de/tobias/playpad/server/project/Design.scala @@ -2,14 +2,20 @@ package de.tobias.playpad.server.project import java.util.UUID +import de.tobias.playpad.server.json.{JsonName, JsonParent, UUIDSerializerHandler} + /** * Created by tobias on 23.02.17. */ class Design { + @JsonName(value = "id", handler = classOf[UUIDSerializerHandler]) var id: UUID = UUID.randomUUID() + @JsonName("background_color") var backgroundColor: String = _ + @JsonName("play_color") var playColor: String = _ + @JsonParent 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 9c4b0fc6ad2fc28c4e3b2412a5f10c24afd496a0..c42bc1a4f1011b5ddea9d6a01446244e4fb5f66f 100644 --- a/src/main/scala/de/tobias/playpad/server/project/Pad.scala +++ b/src/main/scala/de/tobias/playpad/server/project/Pad.scala @@ -2,19 +2,29 @@ package de.tobias.playpad.server.project import java.util.UUID +import de.tobias.playpad.server.json._ + /** * Created by tobias on 17.02.17. */ class Pad() { + @JsonName(value = "id", handler = classOf[UUIDSerializerHandler]) var id: UUID = UUID.randomUUID() + + @JsonName("name") var name: String = _ + @JsonName(value = "position", handler = classOf[NumberSerializerHandler]) var position: Int = _ + @JsonName("contentType") var contentType: String = _ + @JsonCollection(value = "paths", `type` = classOf[Path]) var paths: List[Path] = List() + @JsonObj("design") var design: Design = _ + @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 079602a8117901c7528478573f60bb477e621172..76236fa517e5b35ab77bd2c913763bf3d7a257dc 100644 --- a/src/main/scala/de/tobias/playpad/server/project/Page.scala +++ b/src/main/scala/de/tobias/playpad/server/project/Page.scala @@ -2,16 +2,24 @@ package de.tobias.playpad.server.project import java.util.UUID +import de.tobias.playpad.server.json._ + /** * Created by tobias on 17.02.17. */ class Page { + @JsonName(value = "id", handler = classOf[UUIDSerializerHandler]) var id: UUID = UUID.randomUUID() + @JsonName("name") var name: String = _ + @JsonName(value = "position", handler = classOf[NumberSerializerHandler]) var position: Int = _ + @JsonCollection(value = "pads", `type` = classOf[Pad]) var pads: List[Pad] = List() + + @JsonParent var project: Project = _ // FOREIGN KEY } diff --git a/src/main/scala/de/tobias/playpad/server/project/Path.scala b/src/main/scala/de/tobias/playpad/server/project/Path.scala index 1e8c2b01a22ee0debca1a0301f5d10c88c9c25cd..467de1700165e619e6fe73bf4b526121cb476d67 100644 --- a/src/main/scala/de/tobias/playpad/server/project/Path.scala +++ b/src/main/scala/de/tobias/playpad/server/project/Path.scala @@ -2,13 +2,18 @@ package de.tobias.playpad.server.project import java.util.UUID +import de.tobias.playpad.server.json.{JsonName, JsonParent, UUIDSerializerHandler} + /** * Created by tobias on 20.02.17. */ class Path { + @JsonName(value = "id", handler = classOf[UUIDSerializerHandler]) var id: UUID = UUID.randomUUID() + @JsonName("filename") var filename: String = _ + @JsonParent var pad: Pad = _ } diff --git a/src/main/scala/de/tobias/playpad/server/project/Project.scala b/src/main/scala/de/tobias/playpad/server/project/Project.scala index 7cd0475bde0230c894b8102ce8e7d09aa35636fd..fc28a9806b76db7e3a1074543801f275c396211c 100644 --- a/src/main/scala/de/tobias/playpad/server/project/Project.scala +++ b/src/main/scala/de/tobias/playpad/server/project/Project.scala @@ -2,15 +2,23 @@ package de.tobias.playpad.server.project import java.util.UUID -import de.tobias.playpad.server.account.Session +import de.tobias.playpad.server.json.{JsonCollection, JsonName, UUIDSerializerHandler} /** * Created by tobias on 17.02.17. */ class Project { - val projectReference = new ProjectReference + @JsonName(value = "id", handler = classOf[UUIDSerializerHandler]) + var id: UUID = UUID.randomUUID() + @JsonName("name") + var name: String = _ - var pages: List[Page] = _ + var accountId: Int = _ // Account that own this project + var lastModified: Long = _ + var session: String = _ + + @JsonCollection(value = "pages", `type` = classOf[Page]) + var pages: List[Page] = List() } diff --git a/src/main/scala/de/tobias/playpad/server/project/ProjectReference.scala b/src/main/scala/de/tobias/playpad/server/project/ProjectReference.scala deleted file mode 100644 index 1d15a978ba04c3c608721334d049f987cd02c766..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/ProjectReference.scala +++ /dev/null @@ -1,17 +0,0 @@ -package de.tobias.playpad.server.project - -import java.util.UUID - -/** - * Created by tobias on 19.02.17. - */ -class ProjectReference { - - var id: UUID = UUID.randomUUID() - var name: String = _ - - var accountId: Int = _ // Account that own this project - var lastModified: Long = _ - var session: String = _ - -} 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 deleted file mode 100644 index a5127e85c39a926d59d5f9087494d26245144abf..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/loader/json/DesignLoader.scala +++ /dev/null @@ -1,23 +0,0 @@ -package de.tobias.playpad.server.project.loader.json - -import java.util.UUID - -import com.google.gson.JsonObject -import de.tobias.playpad.server.project.utils.JsonDef -import de.tobias.playpad.server.project.{Design, Pad} - -/** - * 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 deleted file mode 100644 index bd55a1e36f4ad1ea872554f30eb821193924566d..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/loader/json/PadLoader.scala +++ /dev/null @@ -1,43 +0,0 @@ -package de.tobias.playpad.server.project.loader.json - -import java.util.UUID - -import com.google.gson.{JsonArray, JsonObject} -import de.tobias.playpad.server.project.utils.JsonDef._ -import de.tobias.playpad.server.project.{Pad, Page} - -import scala.collection.JavaConverters - -/** - * Created by tobias on 17.02.17. - */ -class PadLoader { - def load(jsonArray: JsonArray, page: Page): List[Pad] = { - val it = JavaConverters.asScalaIterator(jsonArray.iterator()) - val pads = it.filter(_.isJsonObject).map(i => { - val json = i.asInstanceOf[JsonObject] - - val pad = new Pad() - pad.id = UUID.fromString(json.get(PAD_ID).getAsString) - if (json.get(PAD_NAME) != null) - pad.name = json.get(PAD_NAME).getAsString - pad.position = json.get(PAD_POSITION).getAsInt - if (json.get(PAD_CONTENT_TYPE) != null) - pad.contentType = 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) - if (designJson != null) - pad.design = designLoader.load(designJson, pad) - - pad - }).toList - pads - } - -} diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/json/PageLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/json/PageLoader.scala deleted file mode 100644 index de0a0d3a0e0eafc46e4d76c69f5e4766d0b02a47..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/loader/json/PageLoader.scala +++ /dev/null @@ -1,32 +0,0 @@ -package de.tobias.playpad.server.project.loader.json - -import java.util.UUID - -import com.google.gson.{JsonArray, JsonObject} -import de.tobias.playpad.server.project.utils.JsonDef._ -import de.tobias.playpad.server.project.{Page, Project} - -import scala.collection.JavaConverters - -/** - * Created by tobias on 17.02.17. - */ -class PageLoader { - def load(jsonArray: JsonArray, project: Project): List[Page] = { - val it = JavaConverters.asScalaIterator(jsonArray.iterator()) - val pages = it.filter(_.isJsonObject).map(i => { - val json = i.asInstanceOf[JsonObject] - - val page = new Page() - page.id = UUID.fromString(json.get(PAGE_ID).getAsString) - page.name = json.get(PAGE_NAME).getAsString - - val padLoader = new PadLoader - page.pads = padLoader.load(json.get(PAGE_PADS).getAsJsonArray, page) - - page.project = project - page - }).toList - pages - } -} diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/json/PathLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/json/PathLoader.scala deleted file mode 100644 index cb1a6c0ca7864652a27b96eff2aea3fea970b2a8..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/loader/json/PathLoader.scala +++ /dev/null @@ -1,29 +0,0 @@ -package de.tobias.playpad.server.project.loader.json - -import java.util.UUID - -import com.google.gson.{JsonArray, JsonObject} -import de.tobias.playpad.server.project.utils.JsonDef._ -import de.tobias.playpad.server.project.{Pad, Path} - -import scala.collection.JavaConverters - -/** - * Created by tobias on 20.02.17. - */ -class PathLoader { - def load(jsonArray: JsonArray, pad: Pad): List[Path] = { - val it = JavaConverters.asScalaIterator(jsonArray.iterator()) - val paths = it.filter(_.isJsonObject).map(i => { - val json = i.asInstanceOf[JsonObject] - - val path = new Path() - path.id = UUID.fromString(json.get(PATH_ID).getAsString) - path.filename = json.get(PATH_PATH).getAsString - path.pad = pad - - path - }).toList - paths - } -} diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/json/ProjectLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/json/ProjectLoader.scala deleted file mode 100644 index be1a394e9667bc54c9a9f7fd72ea8ec80a0b8888..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/loader/json/ProjectLoader.scala +++ /dev/null @@ -1,23 +0,0 @@ -package de.tobias.playpad.server.project.loader.json - -import java.util.UUID - -import com.google.gson.JsonObject -import de.tobias.playpad.server.project.Project -import de.tobias.playpad.server.project.utils.JsonDef._ - -/** - * Created by tobias on 17.02.17. - */ -class ProjectLoader { - def load(json: JsonObject): Project = { - val project = new Project() - project.projectReference.id = UUID.fromString(json.get(PROJECT_ID).getAsString) - project.projectReference.name = json.get(PROJECT_NAME).getAsString - - val pageLoader = new PageLoader - project.pages = pageLoader.load(json.getAsJsonArray(PROJECT_PAGES), project) - - project - } -} diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/sql/PageLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/sql/PageLoader.scala index 30d44b6cf69c007896900d06c209195e134df98e..7ce80fa0e5aee425ae4fff4e4d85da8504940c73 100644 --- a/src/main/scala/de/tobias/playpad/server/project/loader/sql/PageLoader.scala +++ b/src/main/scala/de/tobias/playpad/server/project/loader/sql/PageLoader.scala @@ -13,7 +13,7 @@ class PageLoader(val connection: Connection) { def load(project: Project): List[Page] = { val sql = s"SELECT * FROM $PAGE WHERE $PAGE_PROJECT_REF = ?" val preparedStatement = connection.prepareStatement(sql) - preparedStatement.setString(1, project.projectReference.id.toString) + preparedStatement.setString(1, project.id.toString) val result = preparedStatement.executeQuery() var pages: List[Page] = List() diff --git a/src/main/scala/de/tobias/playpad/server/project/loader/sql/ProjectLoader.scala b/src/main/scala/de/tobias/playpad/server/project/loader/sql/ProjectLoader.scala index 24ed0a0376438a5c5299f80fd3881c0b3fb0fd00..47753991d6090ee0c51f6a359818cf4724ed1e6c 100644 --- a/src/main/scala/de/tobias/playpad/server/project/loader/sql/ProjectLoader.scala +++ b/src/main/scala/de/tobias/playpad/server/project/loader/sql/ProjectLoader.scala @@ -3,9 +3,8 @@ package de.tobias.playpad.server.project.loader.sql import java.sql.Connection import java.util.UUID -import de.tobias.playpad.server.account.Session +import de.tobias.playpad.server.project.Project import de.tobias.playpad.server.project.utils.SqlDef._ -import de.tobias.playpad.server.project.{Project, ProjectReference} /** * Created by tobias on 17.02.17. @@ -21,12 +20,11 @@ class ProjectLoader(val connection: Connection) { while (result.next()) { val project = new Project() - val projectReference = project.projectReference - projectReference.id = UUID.fromString(result.getString(PROJECT_ID)) - projectReference.name = result.getString(PROJECT_NAME) - projectReference.accountId = result.getInt(PROJECT_ACCOUNT_ID) - projectReference.lastModified = result.getLong(PROJECT_LAST_MODIFIED) - projectReference.session = result.getString(PROJECT_SESSION_KEY) + project.id = UUID.fromString(result.getString(PROJECT_ID)) + project.name = result.getString(PROJECT_NAME) + project.accountId = result.getInt(PROJECT_ACCOUNT_ID) + project.lastModified = result.getLong(PROJECT_LAST_MODIFIED) + project.session = result.getString(PROJECT_SESSION_KEY) val pageLoader = new PageLoader(connection) project.pages = pageLoader.load(project) @@ -40,24 +38,24 @@ class ProjectLoader(val connection: Connection) { projects } - def list(accountId: Int): List[ProjectReference] = { + def list(accountId: Int): List[Project] = { val sql = s"SELECT * FROM $PROJECT WHERE $PROJECT_ACCOUNT_ID = ?" val preparedStatement = connection.prepareStatement(sql) preparedStatement.setInt(1, accountId) val result = preparedStatement.executeQuery() - var projects: List[ProjectReference] = List() + var projects: List[Project] = List() while (result.next()) { - val projectReference = new ProjectReference() + val project = new Project() - projectReference.id = UUID.fromString(result.getString(PROJECT_ID)) - projectReference.name = result.getString(PROJECT_NAME) - projectReference.accountId = result.getInt(PROJECT_ACCOUNT_ID) - projectReference.lastModified = result.getLong(PROJECT_LAST_MODIFIED) - projectReference.session = result.getString(PROJECT_SESSION_KEY) + project.id = UUID.fromString(result.getString(PROJECT_ID)) + project.name = result.getString(PROJECT_NAME) + project.accountId = result.getInt(PROJECT_ACCOUNT_ID) + project.lastModified = result.getLong(PROJECT_LAST_MODIFIED) + project.session = result.getString(PROJECT_SESSION_KEY) - projects = projectReference :: projects + projects = project :: projects } result.close() 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 deleted file mode 100644 index 37144e12c4522091edc9996b23cfded863cf07b5..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/saver/json/DesignSaver.scala +++ /dev/null @@ -1,20 +0,0 @@ -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 deleted file mode 100644 index aec2e27cd2a45c13632f8da9ea78382266a4d333..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/saver/json/PadSaver.scala +++ /dev/null @@ -1,30 +0,0 @@ -package de.tobias.playpad.server.project.saver.json - -import com.google.gson.{JsonArray, JsonElement, JsonObject} -import de.tobias.playpad.server.project.Pad -import de.tobias.playpad.server.project.utils.JsonDef._ - -/** - * Created by tobias on 17.02.17. - */ -class PadSaver { - def save(pad: Pad): JsonElement = { - val jsonObject = new JsonObject() - - val pathSaver = new PathSaver() - 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/json/PageSaver.scala b/src/main/scala/de/tobias/playpad/server/project/saver/json/PageSaver.scala deleted file mode 100644 index 883b8b6503920c030e1e02d232d32d24dec3d2f8..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/saver/json/PageSaver.scala +++ /dev/null @@ -1,25 +0,0 @@ -package de.tobias.playpad.server.project.saver.json - -import com.google.gson.{JsonArray, JsonElement, JsonObject} -import de.tobias.playpad.server.project.Page -import de.tobias.playpad.server.project.utils.JsonDef._ - -/** - * Created by tobias on 17.02.17. - */ -class PageSaver { - def save(page: Page): JsonElement = { - val jsonObject = new JsonObject() - - val padSaver = new PadSaver() - val padArray = new JsonArray() - page.pads.foreach(pad => padArray.add(padSaver.save(pad))) - - jsonObject.addProperty(PAGE_ID, page.id.toString) - jsonObject.addProperty(PAGE_NAME, page.name) - jsonObject.addProperty(PAGE_POSITION, page.position) - jsonObject.add(PAGE_PADS, padArray) - - jsonObject - } -} diff --git a/src/main/scala/de/tobias/playpad/server/project/saver/json/PathSaver.scala b/src/main/scala/de/tobias/playpad/server/project/saver/json/PathSaver.scala deleted file mode 100644 index 7063c2598e8a803a82f4b68e329768e3dc2587c2..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/saver/json/PathSaver.scala +++ /dev/null @@ -1,19 +0,0 @@ -package de.tobias.playpad.server.project.saver.json - -import com.google.gson.{JsonElement, JsonObject} -import de.tobias.playpad.server.project.Path -import de.tobias.playpad.server.project.utils.JsonDef._ - -/** - * Created by tobias on 20.02.17. - */ -class PathSaver { - def save(path: Path): JsonElement = { - val jsonObject = new JsonObject() - - jsonObject.addProperty(PATH_ID, path.id.toString) - jsonObject.addProperty(PATH_PATH, path.filename) - - jsonObject - } -} diff --git a/src/main/scala/de/tobias/playpad/server/project/saver/json/ProjectSaver.scala b/src/main/scala/de/tobias/playpad/server/project/saver/json/ProjectSaver.scala deleted file mode 100644 index f4b11791ca578dc96bad263f4b93f60c65b75934..0000000000000000000000000000000000000000 --- a/src/main/scala/de/tobias/playpad/server/project/saver/json/ProjectSaver.scala +++ /dev/null @@ -1,24 +0,0 @@ -package de.tobias.playpad.server.project.saver.json - -import com.google.gson.{JsonArray, JsonObject} -import de.tobias.playpad.server.project.Project -import de.tobias.playpad.server.project.utils.JsonDef._ - -/** - * Created by tobias on 17.02.17. - */ -class ProjectSaver { - def save(project: Project): JsonObject = { - val jsonObject = new JsonObject() - - val pageSaver = new PageSaver() - val pageArray = new JsonArray - project.pages.foreach(page => pageArray.add(pageSaver.save(page))) - - jsonObject.addProperty(PROJECT_ID, project.projectReference.id.toString) - jsonObject.addProperty(PROJECT_NAME, project.projectReference.name) - jsonObject.add(PROJECT_PAGES, pageArray) - - jsonObject - } -} diff --git a/src/main/scala/de/tobias/playpad/server/project/saver/sql/PageSaver.scala b/src/main/scala/de/tobias/playpad/server/project/saver/sql/PageSaver.scala index 1933fc60b2ca84ed58dd9469de1360bc2580644a..ec28b8501dd38410456332e94a55092b45a4630a 100644 --- a/src/main/scala/de/tobias/playpad/server/project/saver/sql/PageSaver.scala +++ b/src/main/scala/de/tobias/playpad/server/project/saver/sql/PageSaver.scala @@ -13,7 +13,7 @@ class PageSaver(connection: Connection) { def save(page: Page): Unit = { SqlHelper.insertOrUpdate(connection, PAGE, page.id, PAGE_NAME, page.name) SqlHelper.insertOrUpdate(connection, PAGE, page.id, PAGE_POSITION, page.position) - SqlHelper.insertOrUpdate(connection, PAGE, page.id, PAGE_PROJECT_REF, page.project.projectReference.id) + SqlHelper.insertOrUpdate(connection, PAGE, page.id, PAGE_PROJECT_REF, page.project.id) val padSaver = new PadSaver(connection) page.pads.foreach(padSaver.save) diff --git a/src/main/scala/de/tobias/playpad/server/project/saver/sql/ProjectSaver.scala b/src/main/scala/de/tobias/playpad/server/project/saver/sql/ProjectSaver.scala index 994c64ce5f17da0bde23ea237471be4b83af50e7..8fcd2027e2769b4224bcd43b9aabcec3b552c17e 100644 --- a/src/main/scala/de/tobias/playpad/server/project/saver/sql/ProjectSaver.scala +++ b/src/main/scala/de/tobias/playpad/server/project/saver/sql/ProjectSaver.scala @@ -12,9 +12,8 @@ import de.tobias.playpad.server.server.SqlHelper */ class ProjectSaver(val connection: Connection) { def save(project: Project): Unit = { - val reference = project.projectReference - SqlHelper.insertOrUpdate(connection, PROJECT, reference.id, PROJECT_NAME, reference.name) - SqlHelper.insertOrUpdate(connection, PROJECT, reference.id, PROJECT_ACCOUNT_ID, reference.accountId) + SqlHelper.insertOrUpdate(connection, PROJECT, project.id, PROJECT_NAME, project.name) + SqlHelper.insertOrUpdate(connection, PROJECT, project.id, PROJECT_ACCOUNT_ID, project.accountId) val pageSaver = new PageSaver(connection) project.pages.foreach(pageSaver.save) 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 a705363c1dd68c0daa874540d8847fa05baecaa5..790235bc06a29da21135f6bfe880ac17920cf684 100644 --- a/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala +++ b/src/main/scala/de/tobias/playpad/server/server/SqlHelper.scala @@ -72,7 +72,7 @@ object SqlHelper { } def createTables(connection: Connection): Unit = { - def createTable(sql: String) = { + def createTable(sql: String): Unit = { val preparedStatement = connection.prepareStatement(sql) preparedStatement.execute() preparedStatement.close() @@ -83,6 +83,8 @@ object SqlHelper { | `id` varchar(48) NOT NULL DEFAULT '', | `name` varchar(255) DEFAULT NULL, | `account_id` int(11) DEFAULT NULL, + | `last_modified` bigint(11) DEFAULT NULL, + | `session_key` varchar(255) DEFAULT NULL, | PRIMARY KEY (`id`), | UNIQUE KEY `id` (`id`) |) ENGINE=InnoDB DEFAULT CHARSET=latin1;""".stripMargin) @@ -101,7 +103,7 @@ object SqlHelper { | `id` varchar(48) NOT NULL DEFAULT '', | `name` varchar(255) DEFAULT NULL, | `position` int(11) DEFAULT NULL, - | `page` int(11) DEFAULT NULL, + | `content_type` varchar(100) DEFAULT NULL, | `page_id` varchar(48) DEFAULT NULL, | PRIMARY KEY (`id`), | UNIQUE KEY `id` (`id`), @@ -110,7 +112,7 @@ object SqlHelper { createTable( """CREATE TABLE IF NOT EXISTS `Path` ( | `id` varchar(40) NOT NULL DEFAULT '', - | `path` text, + | `filename` text, | `pad_id` varchar(40) DEFAULT NULL, | PRIMARY KEY (`id`), | KEY `pad_id` (`pad_id`), diff --git a/src/main/scala/de/tobias/playpad/server/server/project/ProjectGet.scala b/src/main/scala/de/tobias/playpad/server/server/project/ProjectGet.scala index e4ca4153889acc45b356b88c700cbec686f5f6b9..f11f171ca6367d3ed058f7efe38e29eb8bf63aa4 100644 --- a/src/main/scala/de/tobias/playpad/server/server/project/ProjectGet.scala +++ b/src/main/scala/de/tobias/playpad/server/server/project/ProjectGet.scala @@ -5,8 +5,8 @@ import java.util.UUID import com.j256.ormlite.dao.Dao import de.tobias.playpad.server.account.Session +import de.tobias.playpad.server.json.JsonSerializer import de.tobias.playpad.server.project.loader.sql.ProjectLoader -import de.tobias.playpad.server.project.saver.json.ProjectSaver import de.tobias.playpad.server.server.{Result, Status} import spark.{Request, Response, Route} @@ -28,9 +28,8 @@ class ProjectGet(connection: Connection, sessionDao: Dao[Session, Int]) extends if (projects.size == 1) { val project = projects.head - if (project.projectReference.accountId == s.getAccount.id) { - val projectSaver = new ProjectSaver() - return projectSaver.save(project) + if (project.accountId == s.getAccount.id) { + return new JsonSerializer().serialize(project) } } new Result(Status.ERROR, "Project invalid") diff --git a/src/main/scala/de/tobias/playpad/server/server/project/ProjectPost.scala b/src/main/scala/de/tobias/playpad/server/server/project/ProjectPost.scala index 90fee8590e1102cdbecc886935e43dfdefc7922f..edb831b6844a181c6f42e6ba05887963c64c1723 100644 --- a/src/main/scala/de/tobias/playpad/server/server/project/ProjectPost.scala +++ b/src/main/scala/de/tobias/playpad/server/server/project/ProjectPost.scala @@ -5,7 +5,8 @@ import java.sql.Connection import com.google.gson.JsonParser import com.j256.ormlite.dao.Dao import de.tobias.playpad.server.account.Session -import de.tobias.playpad.server.project.loader.json.ProjectLoader +import de.tobias.playpad.server.json.JsonSerializer +import de.tobias.playpad.server.project.Project import de.tobias.playpad.server.project.saver.sql.ProjectSaver import de.tobias.playpad.server.server.{Result, Status} import spark.{Request, Response, Route} @@ -19,13 +20,13 @@ class ProjectPost(connection: Connection, sessionDao: Dao[Session, Int]) extends val session = Session.getSession(sessionKey, sessionDao) session match { case Some(s) => - val projectParam = request.queryParams("project") + val projectParam = request.body() val json = new JsonParser().parse(projectParam).getAsJsonObject - val projectLoader = new ProjectLoader - val project = projectLoader.load(json) - project.projectReference.accountId = s.getAccount.id + val project = new JsonSerializer().deserialize(json, classOf[Project]) + + project.accountId = s.getAccount.id val projectSaver = new ProjectSaver(connection) projectSaver.save(project) 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 4a9b4421d502d72ba919ebd933084a0f8aa3dd70..58b8518361ca76ea8cb197c447ed4b609e714ca7 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 @@ -94,17 +94,17 @@ import scala.collection.{Map, mutable} case json: JsonObject => session match { case Some(s) => - val cmd = json.get("cmd").getAsString - if (listeners.contains(cmd)) { - listeners(cmd).onChange(json, connection, s) - } - // Write last modification to project table val timeStemp = json.get("time").getAsLong 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_SESSION_KEY, s.key) + val cmd = json.get("cmd").getAsString + if (listeners.contains(cmd)) { + listeners(cmd).onChange(json, connection, s) + } + case None => serverSession.close(500, "Invalid Session") } case _ => serverSession.close(500, "Invalid Data")