diff options
author | Kseniya Tomskikh <ktomskih@datamonsters.co> | 2017-10-30 16:29:46 +0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-10-30 16:29:46 +0700 |
commit | 604fbf0a7a082bc440c0778abd6f90005b210c16 (patch) | |
tree | fd846d42448b2d3f432056898f0c3433475ea0da /src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm.scala | |
parent | 8811d60442d060097027b33784ece9a704458e1d (diff) | |
parent | d92a2795a02b9711aa9aed92a770f85377b31460 (diff) | |
download | rest-query-604fbf0a7a082bc440c0778abd6f90005b210c16.tar.gz rest-query-604fbf0a7a082bc440c0778abd6f90005b210c16.tar.bz2 rest-query-604fbf0a7a082bc440c0778abd6f90005b210c16.zip |
Merge pull request #54 from drivergroup/PDSUI-2336v0.11.0
PDSUI-2336 Cleanup
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm.scala')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm.scala | 108 |
1 files changed, 108 insertions, 0 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm.scala new file mode 100644 index 0000000..f31e6bd --- /dev/null +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm.scala @@ -0,0 +1,108 @@ +package xyz.driver.pdsuidomain.formats.json + +import spray.json._ +import xyz.driver.entities.patient.CancerType +import xyz.driver.formats.json.patient._ +import xyz.driver.pdsuicommon.domain.{LongId, StringId} +import xyz.driver.pdsuidomain.entities._ + +object eligibilityarm { + + import DefaultJsonProtocol._ + import common._ + + private def deserializationErrorFieldMessage(field: String, json: JsValue)(implicit className: String) = { + deserializationError(s"$className json object do not contain '$field' field: $json") + } + + private def deserializationErrorEntityMessage(json: JsValue)(implicit className: String) = { + deserializationError(s"Expected Json Object as $className, but got $json") + } + + implicit def eligibilityArmWithDiseasesWriter: RootJsonWriter[EligibilityArmWithDiseases] = + new RootJsonWriter[EligibilityArmWithDiseases] { + override def write(obj: EligibilityArmWithDiseases): JsValue = { + JsObject( + "id" -> obj.eligibilityArm.id.toJson, + "name" -> obj.eligibilityArm.name.toJson, + "originalName" -> obj.eligibilityArm.originalName.toJson, + "trialId" -> obj.eligibilityArm.trialId.toJson, + "diseases" -> obj.eligibilityArmDiseases.map(_.disease.toJson).toJson + ) + } + } + + implicit def eligibilityArmWithDiseasesReader: RootJsonReader[EligibilityArmWithDiseases] = { + new RootJsonReader[EligibilityArmWithDiseases] { + implicit val className: String = "create EligibilityArmWithDiseases" + + override def read(json: JsValue): EligibilityArmWithDiseases = { + json match { + case JsObject(fields) => + val name = fields + .get("name") + .map(_.convertTo[String]) + .getOrElse(deserializationErrorFieldMessage("name", json)) + + val trialId = fields + .get("trialId") + .map(_.convertTo[StringId[Trial]]) + .getOrElse(deserializationErrorFieldMessage("trialId", json)) + + val diseases = fields + .get("diseases") + .map(_.convertTo[Seq[String]]) + .getOrElse(deserializationErrorFieldMessage("diseases", json)) + + val eligibilityArm = EligibilityArm( + id = LongId(0), + name = name, + trialId = trialId, + originalName = name + ) + + EligibilityArmWithDiseases( + eligibilityArm, + diseases.map { disease => + val condition = CancerType + .fromString(disease) + .getOrElse(throw new NoSuchElementException(s"unknown condition $disease")) + EligibilityArmDisease(eligibilityArm.id, condition) + } + ) + case _ => deserializationErrorEntityMessage(json) + } + } + } + } + + def applyUpdateToEligibilityArmWithDiseases(json: JsValue, + orig: EligibilityArmWithDiseases): EligibilityArmWithDiseases = { + implicit val className: String = "update EligibilityArmWithDiseases" + json match { + case JsObject(fields) => + val name = fields + .get("name") + .map(_.convertTo[String]) + .getOrElse(orig.eligibilityArm.name) + + val diseases = fields + .get("diseases") + .map(_.convertTo[Seq[CancerType]]) + .getOrElse(orig.eligibilityArmDiseases.map(_.disease)) + + orig.copy( + eligibilityArm = orig.eligibilityArm + .copy(name = name), + eligibilityArmDiseases = orig.eligibilityArmDiseases + .zip(diseases) + .map { + case (eligibilityArmDisease, disease) => + eligibilityArmDisease.copy(disease = disease) + } + ) + + case _ => deserializationErrorEntityMessage(json) + } + } +} |