diff options
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain/formats')
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm/ApiCreateEligibilityArm.scala (renamed from src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm/ApiEligibilityCreateArm.scala) | 0 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/criterion.scala | 31 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/eligibilityarm.scala | 116 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala | 20 | ||||
-rw-r--r-- | src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/slotarm.scala | 2 |
5 files changed, 116 insertions, 53 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm/ApiEligibilityCreateArm.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm/ApiCreateEligibilityArm.scala index 087fed5..087fed5 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm/ApiEligibilityCreateArm.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/eligibilityarm/ApiCreateEligibilityArm.scala diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/criterion.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/criterion.scala index a43e92a..acf952d 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/criterion.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/criterion.scala @@ -10,7 +10,7 @@ object criterion { import DefaultJsonProtocol._ import common._ - implicit val criterionLabelWriter = new RootJsonWriter[CriterionLabel] { + implicit val criterionLabelWriter: RootJsonWriter[CriterionLabel] = new RootJsonWriter[CriterionLabel] { override def write(obj: CriterionLabel) = JsObject( "labelId" -> obj.labelId.toJson, "categoryId" -> obj.categoryId.toJson, @@ -26,20 +26,21 @@ object criterion { case JsObject(fields) => val labelId = fields .get("labelId") - .map(_.convertTo[LongId[Label]]) + .flatMap(_.convertTo[Option[LongId[Label]]]) val categoryId = fields .get("categoryId") - .map(_.convertTo[LongId[LabelCategory]]) + .flatMap(_.convertTo[Option[LongId[LabelCategory]]]) val value = fields .get("value") - .map(_.convertTo[String] match { + .flatMap(_.convertTo[Option[String]]) + .map { case "Yes" => true case "No" => false case other => deserializationError(s"Unknown `value` of CriterionLabel object: expected `yes` or `no`, but got $other") - }) + } val isDefining = fields .get("isDefining") @@ -103,7 +104,7 @@ object criterion { case _ => deserializationError(s"Expected Json Object as partial Criterion, but got $json") } - val richCriterionFormat = new RootJsonFormat[RichCriterion] { + implicit val richCriterionFormat: RootJsonFormat[RichCriterion] = new RootJsonFormat[RichCriterion] { override def write(obj: RichCriterion): JsValue = JsObject( "id" -> obj.criterion.id.toJson, @@ -125,7 +126,7 @@ object criterion { val text = fields .get("text") - .map(_.convertTo[String]) + .flatMap(_.convertTo[Option[String]]) val isCompound = fields .get("isCompound") @@ -133,24 +134,22 @@ object criterion { val meta = fields .get("meta") - .map(_.convertTo[String]) - .getOrElse("") + .flatMap(_.convertTo[Option[String]]) val inclusion = fields .get("inclusion") - .map(_.convertTo[Option[Boolean]]) - .getOrElse(None) + .flatMap(_.convertTo[Option[Boolean]]) val arms = fields .get("arms") - .map(_.convertTo[List[LongId[EligibilityArm]]]) - .getOrElse(List.empty[LongId[EligibilityArm]]) + .map(_.convertTo[Seq[LongId[EligibilityArm]]]) + .getOrElse(Seq.empty[LongId[EligibilityArm]]) val labels = fields .get("labels") - .map(_.convertTo[List[JsValue]]) + .map(_.convertTo[Seq[JsValue]]) .map(_.map(l => jsValueToCriterionLabel(l, LongId(0)))) - .getOrElse(List.empty[CriterionLabel]) + .getOrElse(Seq.empty[CriterionLabel]) RichCriterion( criterion = Criterion( @@ -158,7 +157,7 @@ object criterion { trialId = trialId, text = text, isCompound = isCompound, - meta = meta, + meta = meta.getOrElse(""), inclusion = inclusion ), armIds = arms, diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/eligibilityarm.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/eligibilityarm.scala index acb790a..039e088 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/eligibilityarm.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/eligibilityarm.scala @@ -1,44 +1,108 @@ package xyz.driver.pdsuidomain.formats.json.sprayformats 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.{EligibilityArm, Trial} +import xyz.driver.pdsuidomain.entities._ object eligibilityarm { + import DefaultJsonProtocol._ import common._ - def applyUpdateToArm(json: JsValue, orig: EligibilityArm): EligibilityArm = json match { - case JsObject(fields) => - val name = fields - .get("name") - .map(_.convertTo[String]) - .getOrElse(deserializationError(s"Arm json object does not contain `name` field: $json")) - orig.copy(name = name) + private def deserializationErrorFieldMessage(field: String, json: JsValue)(implicit className: String) = { + deserializationError(s"$className json object do not contain '$field' field: $json") + } - case _ => deserializationError(s"Expected Json Object as partial Arm, but got $json") + private def deserializationErrorEntityMessage(json: JsValue)(implicit className: String) = { + deserializationError(s"Expected Json Object as $className, but got $json") } - def eligibilityArmFormat: RootJsonFormat[EligibilityArm] = new RootJsonFormat[EligibilityArm] { - override def write(obj: EligibilityArm): JsValue = - JsObject( - "id" -> obj.id.toJson, - "name" -> obj.name.toJson, - "originalName" -> obj.originalName.toJson, - "trialId" -> obj.trialId.toJson - ) - - override def read(json: JsValue): EligibilityArm = json.asJsObject.getFields("trialId", "name") match { - case Seq(trialId, name) => - EligibilityArm( - id = LongId(0), - name = name.convertTo[String], - trialId = trialId.convertTo[StringId[Trial]], - originalName = name.convertTo[String] + 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)) - case _ => deserializationError(s"Expected Json Object as Arm, but got $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) + } + } } diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala index 2ced434..bd68566 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala @@ -9,7 +9,7 @@ object intervention { import DefaultJsonProtocol._ import common._ - implicit val interventionFormat: JsonFormat[InterventionWithArms] = new RootJsonFormat[InterventionWithArms] { + implicit def interventionFormat: RootJsonFormat[InterventionWithArms] = new RootJsonFormat[InterventionWithArms] { override def write(obj: InterventionWithArms) = JsObject( "id" -> obj.intervention.id.toJson, @@ -34,7 +34,7 @@ object intervention { val typeId = fields .get("typeId") - .map(_.convertTo[LongId[InterventionType]]) + .flatMap(_.convertTo[Option[LongId[InterventionType]]]) val name = fields .get("name") @@ -44,7 +44,6 @@ object intervention { val dosage = fields .get("dosage") .map(_.convertTo[String]) - .getOrElse("") val isActive = fields .get("isActive") @@ -52,23 +51,24 @@ object intervention { val deliveryMethod = fields .get("deliveryMethod") - .map(_.convertTo[String]) + .flatMap(_.convertTo[Option[String]]) val arms = fields .get("arms") - .map(_.convertTo[List[LongId[SlotArm]]].map(x => InterventionArm(armId = x, interventionId = LongId(0)))) + .map(_.convertTo[List[LongId[SlotArm]]]) + .map(_ map (x => InterventionArm(armId = x, interventionId = LongId(0L)))) .getOrElse(List.empty[InterventionArm]) InterventionWithArms( intervention = Intervention( - id = LongId(0), + id = LongId(0L), trialId = trialId, name = name, originalName = name, typeId = typeId, originalType = None, - dosage = dosage, - originalDosage = dosage, + dosage = dosage.getOrElse(""), + originalDosage = dosage.getOrElse(""), isActive = isActive, deliveryMethod = deliveryMethod ), @@ -120,8 +120,8 @@ object intervention { case _ => deserializationError(s"Expected Json Object as partial Intervention, but got $json") } - implicit val interventionTypeFormat: JsonFormat[InterventionType] = new RootJsonFormat[InterventionType] { - override def read(json: JsValue) = json match { + implicit def interventionTypeFormat: JsonFormat[InterventionType] = new RootJsonFormat[InterventionType] { + override def read(json: JsValue): InterventionType = json match { case JsObject(fields) => val name = fields .get("name") diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/slotarm.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/slotarm.scala index 192bd6a..1d74916 100644 --- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/slotarm.scala +++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/slotarm.scala @@ -19,7 +19,7 @@ object slotarm { case _ => deserializationError(s"Expected Json Object as partial Arm, but got $json") } - def slotArmFormat: RootJsonFormat[SlotArm] = new RootJsonFormat[SlotArm] { + implicit def slotArmFormat: RootJsonFormat[SlotArm] = new RootJsonFormat[SlotArm] { override def write(obj: SlotArm): JsValue = JsObject( "id" -> obj.id.toJson, |