aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala
diff options
context:
space:
mode:
authorJakob Odersky <jakob@odersky.com>2017-08-14 22:29:45 -0700
committerGitHub <noreply@github.com>2017-08-14 22:29:45 -0700
commit322ea28ecf5ad5f65d3376f3e97e004d229d4736 (patch)
treec405d10d70f4ec1f18ffa81bc01cd8da64bddcba /src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala
parentbe74ae7c5531af998d38b9de8052791f17b25341 (diff)
parent442579b27ccbac82cb001a5b02402a593d005977 (diff)
downloadrest-query-322ea28ecf5ad5f65d3376f3e97e004d229d4736.tar.gz
rest-query-322ea28ecf5ad5f65d3376f3e97e004d229d4736.tar.bz2
rest-query-322ea28ecf5ad5f65d3376f3e97e004d229d4736.zip
Merge pull request #18 from drivergroup/PDSUI-2188
PDSUI-2188 Create spray json formats for domain entities
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala')
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala90
1 files changed, 90 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala
new file mode 100644
index 0000000..c1751bf
--- /dev/null
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/trial.scala
@@ -0,0 +1,90 @@
+package xyz.driver.pdsuidomain.formats.json.sprayformats
+
+import java.time.{ZoneId, ZonedDateTime}
+
+import spray.json._
+import xyz.driver.core.json.EnumJsonFormat
+import xyz.driver.pdsuicommon.domain.{LongId, UuidId}
+import xyz.driver.pdsuidomain.entities._
+
+object trial {
+ import DefaultJsonProtocol._
+ import common._
+ import Trial._
+
+ implicit val trialStatusFormat = new EnumJsonFormat[Status](
+ "New" -> Status.New,
+ "ReviewSummary" -> Status.ReviewSummary,
+ "Summarized" -> Status.Summarized,
+ "PendingUpdate" -> Status.PendingUpdate,
+ "Update" -> Status.Update,
+ "ReviewCriteria" -> Status.ReviewCriteria,
+ "Done" -> Status.Done,
+ "Flagged" -> Status.Flagged,
+ "Archived" -> Status.Archived
+ )
+
+ implicit val conditionFormat = new EnumJsonFormat[Condition](
+ "Breast" -> Condition.Breast,
+ "Lung" -> Condition.Lung,
+ "Prostate" -> Condition.Prostate
+ )
+
+ implicit val trialWriter: JsonWriter[Trial] = new JsonWriter[Trial] {
+ override def write(obj: Trial) =
+ JsObject(
+ "id" -> obj.id.toJson,
+ "externalid" -> obj.externalId.toJson,
+ "lastUpdate" -> ZonedDateTime.of(obj.lastUpdate, ZoneId.of("Z")).toJson,
+ "status" -> obj.status.toJson,
+ "assignee" -> obj.assignee.toJson,
+ "previousStatus" -> obj.previousStatus.toJson,
+ "previousAssignee" -> obj.previousAssignee.toJson,
+ "lastActiveUser" -> obj.lastActiveUserId.toJson,
+ "condition" -> obj.condition.toJson,
+ "phase" -> obj.phase.toJson,
+ "hypothesisId" -> obj.hypothesisId.toJson,
+ "studyDesignId" -> obj.studyDesignId.toJson,
+ "originalStudyDesignId" -> obj.originalStudyDesign.toJson,
+ "isPartner" -> obj.isPartner.toJson,
+ "overview" -> obj.overview.toJson,
+ "overviewTemplate" -> obj.overviewTemplate.toJson,
+ "isUpdated" -> obj.isUpdated.toJson,
+ "title" -> obj.title.toJson,
+ "originalTitle" -> obj.originalTitle.toJson
+ )
+ }
+
+ def applyUpdateToTrial(json: JsValue, orig: Trial): Trial = json match {
+ case JsObject(fields) =>
+ val hypothesisId = fields
+ .get("hypothesisId")
+ .map(_.convertTo[Option[UuidId[Hypothesis]]])
+ .getOrElse(orig.hypothesisId)
+
+ val studyDesignId = fields
+ .get("studyDesignId")
+ .map(_.convertTo[Option[LongId[StudyDesign]]])
+ .getOrElse(orig.studyDesignId)
+
+ val overview = fields
+ .get("overview")
+ .map(_.convertTo[Option[String]])
+ .getOrElse(orig.overview)
+
+ val title = fields
+ .get("title")
+ .map(_.convertTo[Option[String]].getOrElse(""))
+ .getOrElse(orig.title)
+
+ orig.copy(
+ hypothesisId = hypothesisId,
+ studyDesignId = studyDesignId,
+ overview = overview,
+ title = title
+ )
+
+ case _ => deserializationError(s"Expected Json Object as Trial, but got $json")
+ }
+
+}