aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/xyz/driver/pdsuidomain
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/xyz/driver/pdsuidomain')
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/entities/Document.scala4
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/entities/Intervention.scala193
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/entities/export/trial/ExportTrialWithLabels.scala7
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/fakes/entities/common.scala10
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/fakes/entities/trialcuration.scala17
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiIntervention.scala6
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala12
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiPartialIntervention.scala60
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiRecord.scala92
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiUpdateRecord.scala2
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/formats/json/sprayformats/intervention.scala92
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/services/InterventionService.scala36
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/services/rest/RestInterventionService.scala23
-rw-r--r--src/main/scala/xyz/driver/pdsuidomain/services/rest/RestTrialService.scala6
14 files changed, 480 insertions, 80 deletions
diff --git a/src/main/scala/xyz/driver/pdsuidomain/entities/Document.scala b/src/main/scala/xyz/driver/pdsuidomain/entities/Document.scala
index 1f73184..839fead 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/entities/Document.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/entities/Document.scala
@@ -14,6 +14,8 @@ import xyz.driver.pdsuicommon.validation.Validators
import xyz.driver.pdsuicommon.validation.Validators.Validator
import xyz.driver.pdsuidomain.entities.Document.Meta
+import scalaz.Equal
+
final case class ProviderType(id: LongId[ProviderType], name: String)
object ProviderType {
@@ -124,6 +126,8 @@ object DocumentType {
}
}
+ implicit def equal: Equal[DocumentType] = Equal.equal[DocumentType](_ == _)
+
implicit def toPhiString(x: DocumentType): PhiString = {
import x._
phi"DocumentType(id=$id, name=${Unsafe(name)})"
diff --git a/src/main/scala/xyz/driver/pdsuidomain/entities/Intervention.scala b/src/main/scala/xyz/driver/pdsuidomain/entities/Intervention.scala
index cb677cf..9ccc267 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/entities/Intervention.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/entities/Intervention.scala
@@ -2,10 +2,186 @@ package xyz.driver.pdsuidomain.entities
import xyz.driver.pdsuicommon.domain.{LongId, StringId}
import xyz.driver.pdsuicommon.logging._
+import xyz.driver.pdsuidomain.entities.InterventionType.DeliveryMethod
+import xyz.driver.pdsuidomain.entities.InterventionType.DeliveryMethod._
-final case class InterventionType(id: LongId[InterventionType], name: String)
+sealed trait InterventionType {
+ val id: LongId[InterventionType]
+ val name: String
+ val deliveryMethods: Set[DeliveryMethod]
+}
object InterventionType {
+
+ final case object RadiationTherapy extends InterventionType {
+ val id: LongId[InterventionType] = LongId[InterventionType](1)
+ val name: String = "Radiation therapy"
+ val deliveryMethods: Set[DeliveryMethod] = commonMethods
+ }
+
+ final case object Chemotherapy extends InterventionType {
+ val id: LongId[InterventionType] = LongId[InterventionType](2)
+ val name: String = "Chemotherapy"
+ val deliveryMethods: Set[DeliveryMethod] = commonMethods
+ }
+
+ final case object TargetedTherapy extends InterventionType {
+ val id: LongId[InterventionType] = LongId[InterventionType](3)
+ val name: String = "Targeted therapy"
+ val deliveryMethods: Set[DeliveryMethod] = commonMethods
+ }
+
+ final case object Immunotherapy extends InterventionType {
+ val id: LongId[InterventionType] = LongId[InterventionType](4)
+ val name: String = "Immunotherapy"
+ val deliveryMethods: Set[DeliveryMethod] = commonMethods
+ }
+
+ final case object Surgery extends InterventionType {
+ val id: LongId[InterventionType] = LongId[InterventionType](5)
+ val name: String = "Surgery"
+ val deliveryMethods: Set[DeliveryMethod] = commonMethods
+ }
+
+ final case object HormoneTherapy extends InterventionType {
+ val id: LongId[InterventionType] = LongId[InterventionType](6)
+ val name: String = "Hormone therapy"
+ val deliveryMethods: Set[DeliveryMethod] = commonMethods
+ }
+
+ final case object Other extends InterventionType {
+ val id: LongId[InterventionType] = LongId[InterventionType](7)
+ val name: String = "Other"
+ val deliveryMethods: Set[DeliveryMethod] = commonMethods
+ }
+
+ final case object Radiation extends InterventionType {
+ val id: LongId[InterventionType] = LongId[InterventionType](8)
+ val name: String = "Radiation"
+ val deliveryMethods: Set[DeliveryMethod] = Set(
+ ExternalRadiationTherapy,
+ Brachytherapy,
+ SystemicRadiationTherapyIV,
+ SystemicRadiationTherapyOral,
+ ProtonBeamTherapy
+ )
+ }
+
+ final case object SurgeryProcedure extends InterventionType {
+ val id: LongId[InterventionType] = LongId[InterventionType](9)
+ val name: String = "Surgery/Procedure"
+ val deliveryMethods: Set[DeliveryMethod] = Set(
+ RadioFrequencyAblationRFA,
+ Cryoablation,
+ TherapeuticConventionalSurgery,
+ RoboticAssistedLaparoscopicSurgery
+ )
+ }
+
+ def typeFromString: PartialFunction[String, InterventionType] = {
+ case "Radiation therapy" => RadiationTherapy
+ case "Chemotherapy" => Chemotherapy
+ case "Targeted therapy" => TargetedTherapy
+ case "Immunotherapy" => Immunotherapy
+ case "Surgery" => Surgery
+ case "Hormone therapy" => HormoneTherapy
+ case "Other" => Other
+ case "Radiation" => Radiation
+ case "Surgery/Procedure" => SurgeryProcedure
+ }
+
+ sealed trait DeliveryMethod
+ object DeliveryMethod {
+ case object IntravenousInfusionIV extends DeliveryMethod
+ case object IntramuscularInjection extends DeliveryMethod
+ case object SubcutaneousInjection extends DeliveryMethod
+ case object IntradermalInjection extends DeliveryMethod
+ case object SpinalInjection extends DeliveryMethod
+ case object Oral extends DeliveryMethod
+ case object Topical extends DeliveryMethod
+ case object TransdermalPatch extends DeliveryMethod
+ case object Inhalation extends DeliveryMethod
+ case object Rectal extends DeliveryMethod
+ case object ExternalRadiationTherapy extends DeliveryMethod
+ case object Brachytherapy extends DeliveryMethod
+ case object SystemicRadiationTherapyIV extends DeliveryMethod
+ case object SystemicRadiationTherapyOral extends DeliveryMethod
+ case object ProtonBeamTherapy extends DeliveryMethod
+ case object RadioFrequencyAblationRFA extends DeliveryMethod
+ case object Cryoablation extends DeliveryMethod
+ case object TherapeuticConventionalSurgery extends DeliveryMethod
+ case object RoboticAssistedLaparoscopicSurgery extends DeliveryMethod
+
+ def fromString: PartialFunction[String, DeliveryMethod] = {
+ case "Intravenous Infusion (IV)" => IntravenousInfusionIV
+ case "Intramuscular Injection" => IntramuscularInjection
+ case "Subcutaneous Injection" => SubcutaneousInjection
+ case "Intradermal Injection" => IntradermalInjection
+ case "Spinal Injection" => SpinalInjection
+ case "Oral" => Oral
+ case "Topical" => Topical
+ case "Transdermal Patch" => TransdermalPatch
+ case "Inhalation" => Inhalation
+ case "Rectal" => Rectal
+ case "External Radiation Therapy" => ExternalRadiationTherapy
+ case "Brachytherapy" => Brachytherapy
+ case "Systemic Radiation Therapy (IV)" => SystemicRadiationTherapyIV
+ case "Systemic Radiation Therapy (Oral)" => SystemicRadiationTherapyOral
+ case "Proton Beam Therapy" => ProtonBeamTherapy
+ case "Radio-Frequency Ablation (RFA)" => RadioFrequencyAblationRFA
+ case "Cryoablation" => Cryoablation
+ case "Therapeutic Conventional Surgery" => TherapeuticConventionalSurgery
+ case "Robotic Assisted Laparoscopic Surgery" => RoboticAssistedLaparoscopicSurgery
+ }
+
+ def methodToString(x: DeliveryMethod): String = x match {
+ case IntravenousInfusionIV => "Intravenous Infusion (IV)"
+ case IntramuscularInjection => "Intramuscular Injection"
+ case SubcutaneousInjection => "Subcutaneous Injection"
+ case IntradermalInjection => "Intradermal Injection"
+ case SpinalInjection => "Spinal Injection"
+ case Oral => "Oral"
+ case Topical => "Topical"
+ case TransdermalPatch => "Transdermal Patch"
+ case Inhalation => "Inhalation"
+ case Rectal => "Rectal"
+ case ExternalRadiationTherapy => "External Radiation Therapy"
+ case Brachytherapy => "Brachytherapy"
+ case SystemicRadiationTherapyIV => "Systemic Radiation Therapy (IV)"
+ case SystemicRadiationTherapyOral => "Systemic Radiation Therapy (Oral)"
+ case ProtonBeamTherapy => "Proton Beam Therapy"
+ case RadioFrequencyAblationRFA => "Radio-Frequency Ablation (RFA)"
+ case Cryoablation => "Cryoablation"
+ case TherapeuticConventionalSurgery => "Therapeutic Conventional Surgery"
+ case RoboticAssistedLaparoscopicSurgery => "Robotic Assisted Laparoscopic Surgery"
+ }
+ }
+
+ val commonMethods = Set[DeliveryMethod](
+ IntravenousInfusionIV,
+ IntramuscularInjection,
+ SubcutaneousInjection,
+ IntradermalInjection,
+ SpinalInjection,
+ Oral,
+ Topical,
+ TransdermalPatch,
+ Inhalation,
+ Rectal
+ )
+
+ val All: Map[LongId[InterventionType], InterventionType] = Map[LongId[InterventionType], InterventionType](
+ LongId[InterventionType](1) -> RadiationTherapy,
+ LongId[InterventionType](2) -> Chemotherapy,
+ LongId[InterventionType](3) -> TargetedTherapy,
+ LongId[InterventionType](4) -> Immunotherapy,
+ LongId[InterventionType](5) -> Surgery,
+ LongId[InterventionType](6) -> HormoneTherapy,
+ LongId[InterventionType](7) -> Other,
+ LongId[InterventionType](8) -> Radiation,
+ LongId[InterventionType](9) -> SurgeryProcedure
+ )
+
implicit def toPhiString(x: InterventionType): PhiString = {
import x._
phi"InterventionType(id=$id, name=${Unsafe(name)})"
@@ -29,12 +205,23 @@ final case class Intervention(id: LongId[Intervention],
originalType: Option[String],
dosage: String,
originalDosage: String,
- isActive: Boolean)
+ isActive: Boolean,
+ deliveryMethod: Option[String]) {
+ def deliveryMethodIsCorrect: Boolean = {
+ if (this.typeId.nonEmpty) {
+ this.deliveryMethod.nonEmpty &&
+ InterventionType.All
+ .getOrElse(this.typeId.get, throw new IllegalArgumentException(s"Not found Intervention type ${this.typeId}"))
+ .deliveryMethods
+ .contains(DeliveryMethod.fromString(this.deliveryMethod.get))
+ } else true
+ }
+}
object Intervention {
implicit def toPhiString(x: Intervention): PhiString = {
import x._
- phi"Intervention(id=$id, trialId=$trialId, name=${Unsafe(name)}, typeId=$typeId, isActive=$isActive)"
+ phi"Intervention(id=$id, trialId=$trialId, name=${Unsafe(name)}, typeId=$typeId, isActive=$isActive, deliveryMethod=${Unsafe(deliveryMethod)})"
}
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/entities/export/trial/ExportTrialWithLabels.scala b/src/main/scala/xyz/driver/pdsuidomain/entities/export/trial/ExportTrialWithLabels.scala
index 60b74ff..cf55694 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/entities/export/trial/ExportTrialWithLabels.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/entities/export/trial/ExportTrialWithLabels.scala
@@ -27,7 +27,7 @@ object ExportTrialWithLabels {
def fromRaw(rawData: List[RawTrialLabel]): ExportTrialWithLabels = {
val trials: Set[StringId[Trial]] = rawData.map(_.nctId)(breakOut)
- assert(trials.size == 1, "There are more than one trials in the rawData")
+ assert(trials.size == 1, "There are more than one trial in the rawData")
val trial = rawData.head
ExportTrialWithLabels(
@@ -43,9 +43,7 @@ object ExportTrialWithLabels {
ExportTrialArm(armId, rawTrials.head.armName)
}(breakOut),
criteria = rawData
- .groupBy { x =>
- (x.criterionId, x.labelId)
- }
+ .groupBy(x => (x.criterionId, x.labelId))
.map {
case (_, rawTrialLabels) =>
val armIds = rawTrialLabels.map(_.criterionArmId).toSet
@@ -53,5 +51,4 @@ object ExportTrialWithLabels {
}(breakOut)
)
}
-
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/common.scala b/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/common.scala
index 52d7b98..b259b07 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/common.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/common.scala
@@ -4,6 +4,7 @@ import java.time.{LocalDate, LocalDateTime, LocalTime}
import xyz.driver.pdsuicommon.domain.{LongId, StringId, UuidId}
import xyz.driver.pdsuidomain.entities.{Trial, TrialHistory}
+import scala.util.Random
object common {
import xyz.driver.core.generators
@@ -20,10 +21,11 @@ object common {
def nextLocalDateTime = LocalDateTime.of(nextLocalDate, LocalTime.MIDNIGHT)
- def nextLocalDate = {
- val date = generators.nextDate()
- LocalDate.of(date.year, date.month + 1, date.day + 1)
- }
+ def nextLocalDate = LocalDate.of(
+ 1970 + Random.nextInt(68),
+ 1 + Random.nextInt(12),
+ 1 + Random.nextInt(28) // all months have at least 28 days
+ )
def nextCondition = generators.oneOf[Trial.Condition](Trial.Condition.All)
diff --git a/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/trialcuration.scala b/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/trialcuration.scala
index ecb6e0a..fe5bf09 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/trialcuration.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/fakes/entities/trialcuration.scala
@@ -7,6 +7,7 @@ import xyz.driver.pdsuidomain.services.CriterionService.RichCriterion
object trialcuration {
import xyz.driver.core.generators
import common._
+ import xyz.driver.pdsuidomain.entities.InterventionType._
def nextTrial(): Trial = Trial(
id = nextStringId[Trial],
@@ -76,7 +77,8 @@ object trialcuration {
originalType = Option(generators.nextString()),
dosage = generators.nextString(),
originalDosage = generators.nextString(),
- isActive = generators.nextBoolean()
+ isActive = generators.nextBoolean(),
+ deliveryMethod = Option(generators.nextString())
)
def nextInterventionArm(interventionId: LongId[Intervention]): InterventionArm = InterventionArm(
@@ -129,9 +131,16 @@ object trialcuration {
name = generators.nextString()
)
- def nextInterventionType(): InterventionType = InterventionType(
- id = nextLongId[InterventionType],
- name = generators.nextString()
+ def nextInterventionType(): InterventionType = generators.oneOf[InterventionType](
+ RadiationTherapy,
+ Chemotherapy,
+ TargetedTherapy,
+ Immunotherapy,
+ Surgery,
+ HormoneTherapy,
+ Other,
+ Radiation,
+ SurgeryProcedure
)
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiIntervention.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiIntervention.scala
index f306a71..072ed25 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiIntervention.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiIntervention.scala
@@ -12,6 +12,7 @@ final case class ApiIntervention(id: Long,
isActive: Boolean,
arms: List[Long],
trialId: String,
+ deliveryMethod: Option[String],
originalName: String,
originalDosage: String,
originalType: Option[String]) {
@@ -26,7 +27,8 @@ final case class ApiIntervention(id: Long,
originalType = this.originalType.map(id => id.toString),
dosage = this.dosage,
originalDosage = this.originalDosage,
- isActive = this.isActive
+ isActive = this.isActive,
+ deliveryMethod = this.deliveryMethod
)
InterventionWithArms(intervention, this.arms.map { armId =>
@@ -47,6 +49,7 @@ object ApiIntervention {
(JsPath \ "isActive").format[Boolean] and
(JsPath \ "arms").format[List[Long]] and
(JsPath \ "trialId").format[String] and
+ (JsPath \ "deliveryMethod").formatNullable[String] and
(JsPath \ "originalName").format[String] and
(JsPath \ "originalDosage").format[String] and
(JsPath \ "originalType").formatNullable[String]
@@ -64,6 +67,7 @@ object ApiIntervention {
isActive = intervention.isActive,
arms = arms.map(_.armId.id),
trialId = intervention.trialId.id,
+ deliveryMethod = intervention.deliveryMethod,
originalName = intervention.originalName,
originalDosage = intervention.originalDosage,
originalType = intervention.originalType
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala
index ebef225..3db8bfa 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiInterventionType.scala
@@ -2,23 +2,25 @@ package xyz.driver.pdsuidomain.formats.json.intervention
import play.api.libs.functional.syntax._
import play.api.libs.json.{Format, JsPath}
-import xyz.driver.pdsuicommon.domain.LongId
import xyz.driver.pdsuidomain.entities.InterventionType
+import xyz.driver.pdsuidomain.entities.InterventionType.DeliveryMethod
-final case class ApiInterventionType(id: Long, name: String) {
+final case class ApiInterventionType(id: Long, name: String, deliveryMethods: List[String]) {
- def toDomain = InterventionType(id = LongId[InterventionType](id), name = name)
+ def toDomain = InterventionType.typeFromString(name)
}
object ApiInterventionType {
implicit val format: Format[ApiInterventionType] = (
(JsPath \ "id").format[Long] and
- (JsPath \ "name").format[String]
+ (JsPath \ "name").format[String] and
+ (JsPath \ "deliveryMethods").format[List[String]]
)(ApiInterventionType.apply, unlift(ApiInterventionType.unapply))
def fromDomain(interventionType: InterventionType) = ApiInterventionType(
id = interventionType.id.id,
- name = interventionType.name
+ name = interventionType.name,
+ deliveryMethods = interventionType.deliveryMethods.map(DeliveryMethod.methodToString).toList
)
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiPartialIntervention.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiPartialIntervention.scala
index aa55506..28a8555 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiPartialIntervention.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/intervention/ApiPartialIntervention.scala
@@ -1,42 +1,88 @@
package xyz.driver.pdsuidomain.formats.json.intervention
-import xyz.driver.pdsuicommon.domain.LongId
-import xyz.driver.pdsuidomain.entities.{InterventionArm, InterventionWithArms}
+import play.api.data.validation.Invalid
+import xyz.driver.pdsuicommon.domain.{LongId, StringId}
+import xyz.driver.pdsuidomain.entities.{Intervention, InterventionArm, InterventionWithArms, Trial}
import play.api.libs.functional.syntax._
import play.api.libs.json._
+import xyz.driver.pdsuicommon.json.JsonValidationException
+import xyz.driver.pdsuicommon.validation.{AdditionalConstraints, JsonValidationErrors}
-final case class ApiPartialIntervention(typeId: Option[Long],
+import scala.collection.breakOut
+import scala.util.Try
+
+final case class ApiPartialIntervention(name: Option[String],
+ trialId: Option[String],
+ typeId: Option[Long],
dosage: Option[String],
isActive: Option[Boolean],
+ deliveryMethod: Option[String],
arms: Option[List[Long]]) {
def applyTo(orig: InterventionWithArms): InterventionWithArms = {
val origIntervention = orig.intervention
- val draftArmList = arms.map(_.map(x => InterventionArm(LongId(x), orig.intervention.id)))
+ val draftArmList = arms.map(_.map(x => InterventionArm(armId = LongId(x), interventionId = orig.intervention.id)))
orig.copy(
intervention = origIntervention.copy(
+ name = name.getOrElse(origIntervention.name),
typeId = typeId.map(LongId(_)).orElse(origIntervention.typeId),
dosage = dosage.getOrElse(origIntervention.dosage),
- isActive = isActive.getOrElse(origIntervention.isActive)
+ isActive = isActive.getOrElse(origIntervention.isActive),
+ deliveryMethod = deliveryMethod.orElse(origIntervention.deliveryMethod)
),
arms = draftArmList.getOrElse(orig.arms)
)
}
+
+ def toDomain: Try[InterventionWithArms] = Try {
+ val validation = Map(JsPath \ "trialId" -> AdditionalConstraints.optionNonEmptyConstraint(trialId))
+
+ val validationErrors: JsonValidationErrors = validation.collect({
+ case (fieldName, e: Invalid) => (fieldName, e.errors)
+ })(breakOut)
+
+ if (validationErrors.isEmpty) {
+ InterventionWithArms(
+ intervention = Intervention(
+ id = LongId(0),
+ trialId = trialId.map(StringId[Trial]).get,
+ name = name.getOrElse(""),
+ originalName = name.getOrElse(""),
+ typeId = typeId.map(LongId(_)),
+ originalType = Option(""),
+ dosage = dosage.getOrElse(""),
+ originalDosage = dosage.getOrElse(""),
+ isActive = isActive.getOrElse(false),
+ deliveryMethod = deliveryMethod
+ ),
+ arms =
+ arms.map(_.map(x => InterventionArm(armId = LongId(x), interventionId = LongId(0)))).getOrElse(List.empty)
+ )
+ } else {
+ throw new JsonValidationException(validationErrors)
+ }
+ }
}
object ApiPartialIntervention {
private val reads: Reads[ApiPartialIntervention] = (
- (JsPath \ "typeId").readNullable[Long] and
+ (JsPath \ "name").readNullable[String] and
+ (JsPath \ "trialId").readNullable[String] and
+ (JsPath \ "typeId").readNullable[Long] and
(JsPath \ "dosage").readNullable[String] and
(JsPath \ "isActive").readNullable[Boolean] and
+ (JsPath \ "deliveryMethod").readNullable[String] and
(JsPath \ "arms").readNullable[List[Long]]
)(ApiPartialIntervention.apply _)
private val writes: Writes[ApiPartialIntervention] = (
- (JsPath \ "typeId").writeNullable[Long] and
+ (JsPath \ "name").writeNullable[String] and
+ (JsPath \ "trialId").writeNullable[String] and
+ (JsPath \ "typeId").writeNullable[Long] and
(JsPath \ "dosage").writeNullable[String] and
(JsPath \ "isActive").writeNullable[Boolean] and
+ (JsPath \ "deliveryMethod").writeNullable[String] and
(JsPath \ "arms").writeNullable[List[Long]]
)(unlift(ApiPartialIntervention.unapply))
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiRecord.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiRecord.scala
index b255892..e7b58cd 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiRecord.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiRecord.scala
@@ -13,49 +13,11 @@ import play.api.libs.json._
import xyz.driver.pdsuicommon.json.JsonSerializer
import xyz.driver.pdsuicommon.domain.{LongId, StringId, TextJson, UuidId}
-final case class ApiRecord(id: Long,
- patientId: String,
- caseId: Option[String],
- disease: String,
- physician: Option[String],
- lastUpdate: ZonedDateTime,
- status: String,
- previousStatus: Option[String],
- assignee: Option[String],
- previousAssignee: Option[String],
- lastActiveUser: Option[String],
- requestId: UUID,
- meta: String) {
-
- private def extractStatus(status: String): Status =
- Status
- .fromString(status)
- .getOrElse(
- throw new NoSuchElementException(s"Status $status not found")
- )
-
- def toDomain = MedicalRecord(
- id = LongId(this.id),
- status = extractStatus(this.status),
- previousStatus = this.previousStatus.map(extractStatus),
- assignee = this.assignee.map(StringId(_)),
- previousAssignee = this.previousAssignee.map(StringId(_)),
- lastActiveUserId = this.lastActiveUser.map(StringId(_)),
- patientId = UuidId(patientId),
- requestId = RecordRequestId(this.requestId),
- disease = this.disease,
- caseId = caseId.map(CaseId(_)),
- physician = this.physician,
- meta = Some(TextJson(JsonSerializer.deserialize[List[MedicalRecord.Meta]](this.meta))),
- predictedMeta = None,
- predictedDocuments = None,
- lastUpdate = this.lastUpdate.toLocalDateTime()
- )
-
-}
-
object ApiRecord {
+ private val emptyMeta: String =
+ "[]"
+
private val statusFormat = Format(
Reads.StringReads.filter(ValidationError("unknown status")) {
case x if MedicalRecordStatus.statusFromString.isDefinedAt(x) => true
@@ -95,6 +57,52 @@ object ApiRecord {
previousAssignee = record.previousAssignee.map(_.id),
lastActiveUser = record.lastActiveUserId.map(_.id),
requestId = record.requestId.id,
- meta = record.meta.map(x => JsonSerializer.serialize(x.content)).getOrElse("[]")
+ meta = record.meta.map(x => JsonSerializer.serialize(x.content)).getOrElse(emptyMeta)
+ )
+}
+
+final case class ApiRecord(id: Long,
+ patientId: String,
+ caseId: Option[String],
+ disease: String,
+ physician: Option[String],
+ lastUpdate: ZonedDateTime,
+ status: String,
+ previousStatus: Option[String],
+ assignee: Option[String],
+ previousAssignee: Option[String],
+ lastActiveUser: Option[String],
+ requestId: UUID,
+ meta: String) {
+
+ private def extractStatus(status: String): Status =
+ Status
+ .fromString(status)
+ .getOrElse(
+ throw new NoSuchElementException(s"Status $status not found")
+ )
+
+ def toDomain = MedicalRecord(
+ id = LongId(this.id),
+ status = extractStatus(this.status),
+ previousStatus = this.previousStatus.map(extractStatus),
+ assignee = this.assignee.map(StringId(_)),
+ previousAssignee = this.previousAssignee.map(StringId(_)),
+ lastActiveUserId = this.lastActiveUser.map(StringId(_)),
+ patientId = UuidId(patientId),
+ requestId = RecordRequestId(this.requestId),
+ disease = this.disease,
+ caseId = caseId.map(CaseId(_)),
+ physician = this.physician,
+ meta = {
+ if (this.meta == ApiRecord.emptyMeta) {
+ None
+ } else {
+ Some(TextJson(JsonSerializer.deserialize[List[MedicalRecord.Meta]](this.meta)))
+ }
+ },
+ predictedMeta = None,
+ predictedDocuments = None,
+ lastUpdate = this.lastUpdate.toLocalDateTime()
)
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiUpdateRecord.scala b/src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiUpdateRecord.scala
index 47bc493..05d5a60 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiUpdateRecord.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/formats/json/record/ApiUpdateRecord.scala
@@ -37,7 +37,7 @@ object ApiUpdateRecord {
JsSuccess(Json.stringify(x))
})
.map {
- case Tristate.Present("{}") => Tristate.Absent
+ case Tristate.Present("[]") => Tristate.Absent
case x => x
}
)(ApiUpdateRecord.apply _)
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 9314391..62cb9fa 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
@@ -1,14 +1,15 @@
package xyz.driver.pdsuidomain.formats.json.sprayformats
import spray.json._
-import xyz.driver.pdsuicommon.domain.LongId
+import xyz.driver.pdsuicommon.domain.{LongId, StringId}
+import xyz.driver.pdsuidomain.entities.InterventionType.DeliveryMethod
import xyz.driver.pdsuidomain.entities._
object intervention {
import DefaultJsonProtocol._
import common._
- implicit val interventionWriter: JsonWriter[InterventionWithArms] = new JsonWriter[InterventionWithArms] {
+ implicit val interventionFormat: JsonFormat[InterventionWithArms] = new RootJsonFormat[InterventionWithArms] {
override def write(obj: InterventionWithArms) =
JsObject(
"id" -> obj.intervention.id.toJson,
@@ -18,14 +19,72 @@ object intervention {
"isActive" -> obj.intervention.isActive.toJson,
"arms" -> obj.arms.map(_.armId).toJson,
"trialId" -> obj.intervention.trialId.toJson,
+ "deliveryMethod" -> obj.intervention.deliveryMethod.toJson,
"originalName" -> obj.intervention.originalName.toJson,
"originalDosage" -> obj.intervention.originalDosage.toJson,
"originalType" -> obj.intervention.originalType.toJson
)
+
+ override def read(json: JsValue): InterventionWithArms = json match {
+ case JsObject(fields) =>
+ val trialId = fields
+ .get("trialId")
+ .map(_.convertTo[StringId[Trial]])
+ .getOrElse(deserializationError(s"Intervention json object does not contain `trialId` field: $json"))
+
+ val typeId = fields
+ .get("typeId")
+ .map(_.convertTo[LongId[InterventionType]])
+
+ val name = fields
+ .get("name")
+ .map(_.convertTo[String])
+ .getOrElse("")
+
+ val dosage = fields
+ .get("dosage")
+ .map(_.convertTo[String])
+ .getOrElse("")
+
+ val isActive = fields
+ .get("isActive")
+ .exists(_.convertTo[Boolean])
+
+ val deliveryMethod = fields
+ .get("deliveryMethod")
+ .map(_.convertTo[String])
+
+ val arms = fields
+ .get("arms")
+ .map(_.convertTo[List[LongId[Arm]]].map(x => InterventionArm(armId = x, interventionId = LongId(0))))
+ .getOrElse(List.empty[InterventionArm])
+
+ InterventionWithArms(
+ intervention = Intervention(
+ id = LongId(0),
+ trialId = trialId,
+ name = name,
+ originalName = name,
+ typeId = typeId,
+ originalType = None,
+ dosage = dosage,
+ originalDosage = dosage,
+ isActive = isActive,
+ deliveryMethod = deliveryMethod
+ ),
+ arms = arms
+ )
+
+ case _ => deserializationError(s"Expected Json Object as create Intervention json, but got $json")
+ }
}
def applyUpdateToInterventionWithArms(json: JsValue, orig: InterventionWithArms): InterventionWithArms = json match {
case JsObject(fields) =>
+ val name = fields
+ .get("name")
+ .map(_.convertTo[String])
+
val typeId = fields
.get("typeId")
.map(_.convertTo[LongId[InterventionType]])
@@ -38,6 +97,10 @@ object intervention {
.get("isActive")
.map(_.convertTo[Boolean])
+ val deliveryMethod = fields
+ .get("deliveryMethod")
+ .map(_.convertTo[String])
+
val origIntervention = orig.intervention
val arms = fields
.get("arms")
@@ -45,9 +108,11 @@ object intervention {
orig.copy(
intervention = origIntervention.copy(
+ name = name.getOrElse(origIntervention.name),
typeId = typeId.orElse(origIntervention.typeId),
dosage = dosage.getOrElse(origIntervention.dosage),
- isActive = isActive.getOrElse(origIntervention.isActive)
+ isActive = isActive.getOrElse(origIntervention.isActive),
+ deliveryMethod = deliveryMethod.orElse(origIntervention.deliveryMethod)
),
arms = arms.getOrElse(orig.arms)
)
@@ -55,6 +120,25 @@ object intervention {
case _ => deserializationError(s"Expected Json Object as partial Intervention, but got $json")
}
- implicit val interventionTypeFormat: RootJsonFormat[InterventionType] = jsonFormat2(InterventionType.apply)
+ implicit val interventionTypeFormat: JsonFormat[InterventionType] = new RootJsonFormat[InterventionType] {
+ override def read(json: JsValue) = json match {
+ case JsObject(fields) =>
+ val name = fields
+ .get("name")
+ .map(_.convertTo[String])
+ .getOrElse(deserializationError(s"Intervention type json object does not contain `name` field: $json"))
+
+ InterventionType.typeFromString(name)
+
+ case _ => deserializationError(s"Expected Json Object as Intervention type, but got $json")
+ }
+
+ override def write(obj: InterventionType) =
+ JsObject(
+ "id" -> obj.id.toJson,
+ "name" -> obj.name.toJson,
+ "deliveryMethods" -> obj.deliveryMethods.map(DeliveryMethod.methodToString).toJson
+ )
+ }
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/InterventionService.scala b/src/main/scala/xyz/driver/pdsuidomain/services/InterventionService.scala
index 439e456..1e7c7f1 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/services/InterventionService.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/services/InterventionService.scala
@@ -61,6 +61,37 @@ object InterventionService {
final case class CommonError(userMessage: String) extends UpdateReply with DomainError
}
+ sealed trait CreateReply
+ object CreateReply {
+ final case class Created(x: InterventionWithArms) extends CreateReply
+
+ type Error = CreateReply with DomainError
+
+ case object AuthorizationError
+ extends CreateReply with DefaultAccessDeniedError with DomainError.AuthorizationError
+
+ final case class CommonError(userMessage: String) extends CreateReply with DomainError
+
+ implicit def toPhiString(reply: CreateReply): PhiString = reply match {
+ case Created(x) => phi"Created($x)"
+ case x: Error => DomainError.toPhiString(x)
+ }
+ }
+
+ sealed trait DeleteReply
+ object DeleteReply {
+ case object Deleted extends DeleteReply
+
+ type Error = DeleteReply with DomainError
+
+ case object NotFoundError extends DeleteReply with DefaultNotFoundError with DomainError.NotFoundError
+
+ case object AuthorizationError
+ extends DeleteReply with DefaultAccessDeniedError with DomainError.AuthorizationError
+
+ final case class CommonError(userMessage: String) extends DeleteReply with DomainError
+ }
+
}
trait InterventionService {
@@ -76,4 +107,9 @@ trait InterventionService {
def update(origIntervention: InterventionWithArms, draftIntervention: InterventionWithArms)(
implicit requestContext: AuthenticatedRequestContext): Future[UpdateReply]
+
+ def create(draftIntervention: InterventionWithArms)(
+ implicit requestContext: AuthenticatedRequestContext): Future[CreateReply]
+
+ def delete(id: LongId[Intervention])(implicit requestContext: AuthenticatedRequestContext): Future[DeleteReply]
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestInterventionService.scala b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestInterventionService.scala
index e593c3b..025a48a 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestInterventionService.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestInterventionService.scala
@@ -1,7 +1,6 @@
package xyz.driver.pdsuidomain.services.rest
import scala.concurrent.{ExecutionContext, Future}
-
import akka.http.scaladsl.marshalling.Marshal
import akka.http.scaladsl.model._
import akka.stream.Materializer
@@ -61,4 +60,26 @@ class RestInterventionService(transport: ServiceTransport, baseUri: Uri)(
}
}
+ def create(draftIntervention: InterventionWithArms)(
+ implicit requestContext: AuthenticatedRequestContext): Future[CreateReply] = {
+ for {
+ entity <- Marshal(ApiIntervention.fromDomain(draftIntervention)).to[RequestEntity]
+ request = HttpRequest(HttpMethods.POST, endpointUri(baseUri, "/v1/intervention")).withEntity(entity)
+ response <- transport.sendRequestGetResponse(requestContext)(request)
+ reply <- apiResponse[ApiIntervention](response)
+ } yield {
+ CreateReply.Created(reply.toDomain)
+ }
+ }
+
+ def delete(id: LongId[Intervention])(implicit requestContext: AuthenticatedRequestContext): Future[DeleteReply] = {
+ val request = HttpRequest(HttpMethods.DELETE, endpointUri(baseUri, s"/v1/intervention/$id"))
+ for {
+ response <- transport.sendRequestGetResponse(requestContext)(request)
+ _ <- apiResponse[HttpEntity](response)
+ } yield {
+ DeleteReply.Deleted
+ }
+ }
+
}
diff --git a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestTrialService.scala b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestTrialService.scala
index f826b98..b77e6df 100644
--- a/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestTrialService.scala
+++ b/src/main/scala/xyz/driver/pdsuidomain/services/rest/RestTrialService.scala
@@ -34,9 +34,9 @@ class RestTrialService(transport: ServiceTransport, baseUri: Uri)(implicit prote
}
}
- def getTrialWithLabels(trialId: StringId[Trial], condition: String)(
+ def getTrialWithLabels(trialId: StringId[Trial], disease: String)(
implicit requestContext: AuthenticatedRequestContext): Future[GetTrialWithLabelsReply] = {
- val request = HttpRequest(HttpMethods.GET, endpointUri(baseUri, s"/v1/export/trial/$trialId"))
+ val request = HttpRequest(HttpMethods.GET, endpointUri(baseUri, s"/v1/export/trial/$disease/$trialId"))
for {
response <- transport.sendRequestGetResponse(requestContext)(request)
reply <- apiResponse[ApiExportTrialWithLabels](response)
@@ -47,7 +47,7 @@ class RestTrialService(transport: ServiceTransport, baseUri: Uri)(implicit prote
def getPdfSource(trialId: StringId[Trial])(
implicit requestContext: AuthenticatedRequestContext): Future[Source[ByteString, NotUsed]] = {
- val request = HttpRequest(HttpMethods.GET, endpointUri(baseUri, s"/v1/trial/${trialId}/source"))
+ val request = HttpRequest(HttpMethods.GET, endpointUri(baseUri, s"/v1/trial/$trialId/source"))
for {
response <- transport.sendRequestGetResponse(requestContext)(request)
reply <- apiResponse[HttpEntity](response)