1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
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 Trial._
import common._
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 trialWriter: RootJsonWriter[Trial] = new RootJsonWriter[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,
"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")
}
}
|