aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala
diff options
context:
space:
mode:
authorKseniya Tomskikh <ktomskih@datamonsters.co>2017-08-04 17:57:12 +0600
committerKseniya Tomskikh <ktomskih@datamonsters.co>2017-08-11 14:41:17 +0600
commitef9517e1b8f599fbdd15c474cf7dfea61e803c2f (patch)
treec31a139d35dab0c10c70bb7afef2bca8e48f0fee /src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala
parent5519c219e2404cb19b6116dee90b40b5e5e2a720 (diff)
downloadrest-query-ef9517e1b8f599fbdd15c474cf7dfea61e803c2f.tar.gz
rest-query-ef9517e1b8f599fbdd15c474cf7dfea61e803c2f.tar.bz2
rest-query-ef9517e1b8f599fbdd15c474cf7dfea61e803c2f.zip
PDSUI-2188 Created and fixed spray json formats for tric and rep
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala')
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala170
1 files changed, 170 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala
new file mode 100644
index 0000000..20cf6af
--- /dev/null
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/document.scala
@@ -0,0 +1,170 @@
+package xyz.driver.pdsuidomain.formats.json.sprayformats
+
+import java.time.{LocalDate, LocalDateTime}
+
+import spray.json._
+import xyz.driver.core.json.EnumJsonFormat
+import xyz.driver.pdsuicommon.domain.{LongId, TextJson}
+import xyz.driver.pdsuidomain.entities._
+
+object document {
+ import DefaultJsonProtocol._
+ import Document._
+ import common._
+
+ implicit val documentStatusFormat = new EnumJsonFormat[Status](
+ "New" -> Status.New,
+ "Organized" -> Status.Organized,
+ "Extracted" -> Status.Extracted,
+ "Done" -> Status.Done,
+ "Flagged" -> Status.Flagged,
+ "Archived" -> Status.Archived
+ )
+
+ implicit val requiredTypeFormat = new EnumJsonFormat[RequiredType](
+ "OPN" -> RequiredType.OPN,
+ "PN" -> RequiredType.PN
+ )
+
+ implicit val documentMetaFormat: RootJsonFormat[Meta] = jsonFormat3(Meta.apply)
+
+ implicit val fullDocumentMetaFormat = new RootJsonFormat[TextJson[Meta]] {
+ override def write(obj: TextJson[Meta]): JsValue = obj.content.toJson
+ override def read(json: JsValue) = TextJson(documentMetaFormat.read(json))
+ }
+
+ def applyUpdateToDocument(json: JsValue, orig: Document): Document = json match {
+ case JsObject(fields) =>
+ val physician = fields
+ .get("physician")
+ .map(_.convertTo[String])
+
+ val typeId = if (fields.contains("typeId")) {
+ fields
+ .get("typeId")
+ .map(_.convertTo[LongId[DocumentType]])
+ } else orig.typeId
+
+ val provider = if (fields.contains("provider")) {
+ fields
+ .get("provider")
+ .map(_.convertTo[String])
+ } else orig.providerName
+
+ val providerTypeId = if (fields.contains("providerTypeId")) {
+ fields
+ .get("providerTypeId")
+ .map(_.convertTo[LongId[ProviderType]])
+ } else orig.providerTypeId
+
+ val meta = if (fields.contains("meta")) {
+ fields
+ .get("meta")
+ .map(_.convertTo[TextJson[Meta]])
+ } else orig.meta
+
+ val startDate = if (fields.contains("startDate")) {
+ fields
+ .get("startDate")
+ .map(_.convertTo[LocalDate])
+ } else orig.startDate
+
+ val endDate = if (fields.contains("endDate")) {
+ fields
+ .get("endDate")
+ .map(_.convertTo[LocalDate])
+ } else orig.endDate
+
+ orig.copy(
+ physician = physician.orElse(orig.physician),
+ typeId = typeId,
+ providerName = provider,
+ providerTypeId = providerTypeId,
+ meta = meta,
+ startDate = startDate,
+ endDate = endDate
+ )
+
+ case _ => deserializationError(s"Expected Json Object as partial Document, but got $json")
+ }
+
+ implicit val documentFormat: RootJsonFormat[Document] = new RootJsonFormat[Document] {
+ override def write(document: Document): JsValue =
+ JsObject(
+ "id" -> document.id.id.toJson,
+ "recordId" -> document.recordId.toJson,
+ "physician" -> document.physician.toJson,
+ "typeId" -> document.typeId.toJson,
+ "provider" -> document.providerName.toJson,
+ "providerTypeId" -> document.providerTypeId.toJson,
+ "requiredType" -> document.requiredType.toJson,
+ "startDate" -> document.startDate.toJson,
+ "endDate" -> document.endDate.toJson,
+ "status" -> document.status.toJson,
+ "previousStatus" -> document.previousStatus.toJson,
+ "assignee" -> document.assignee.toJson,
+ "previousAssignee" -> document.previousAssignee.toJson,
+ "meta" -> document.meta.toJson,
+ "lastActiveUser" -> document.lastActiveUserId.toJson,
+ "lastUpdate" -> document.lastUpdate.toJson
+ )
+
+ override def read(json: JsValue): Document = json match {
+ case JsObject(fields) =>
+ val recordId = fields
+ .get("recordId")
+ .map(_.convertTo[LongId[MedicalRecord]])
+ .getOrElse(deserializationError(s"Document create json object does not contain `recordId` field: $json"))
+
+ val physician = fields
+ .get("physician")
+ .map(_.convertTo[String])
+
+ val typeId = fields
+ .get("typeId")
+ .map(_.convertTo[LongId[DocumentType]])
+
+ val provider = fields
+ .get("provider")
+ .map(_.convertTo[String])
+
+ val providerTypeId = fields
+ .get("providerTypeId")
+ .map(_.convertTo[LongId[ProviderType]])
+
+ val meta = fields
+ .get("meta")
+ .map(_.convertTo[TextJson[Meta]])
+
+ val startDate = fields
+ .get("startDate")
+ .map(_.convertTo[LocalDate])
+
+ val endDate = fields
+ .get("endDate")
+ .map(_.convertTo[LocalDate])
+
+ Document(
+ id = LongId(0),
+ recordId = recordId,
+ status = Document.Status.New,
+ physician = physician,
+ typeId = typeId,
+ startDate = startDate,
+ endDate = endDate,
+ providerName = provider,
+ providerTypeId = providerTypeId,
+ requiredType = None,
+ meta = meta,
+ previousStatus = None,
+ assignee = None,
+ previousAssignee = None,
+ lastActiveUserId = None,
+ lastUpdate = LocalDateTime.MIN
+ )
+
+ case _ => deserializationError(s"Expected Json Object as Document, but got $json")
+ }
+ }
+
+}