diff --git a/workflow/src/main/java/com/google/android/fhir/workflow/activity/ActivityFlow.kt b/workflow/src/main/java/com/google/android/fhir/workflow/activity/ActivityFlow.kt index cb1bb6c429..9c2aa702d3 100644 --- a/workflow/src/main/java/com/google/android/fhir/workflow/activity/ActivityFlow.kt +++ b/workflow/src/main/java/com/google/android/fhir/workflow/activity/ActivityFlow.kt @@ -210,7 +210,7 @@ private constructor( Intent.ORDER -> OrderPhase(repository, requestResource) else -> throw IllegalArgumentException( - "Couldn't create the flow for ${requestResource.getIntent().name} intent. Supported intents are 'proposal', 'plan', 'order'", + "Couldn't create the flow for ${requestResource.getIntent().code} intent. Supported intents are 'proposal', 'plan' and 'order'.", ) } } else { diff --git a/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/event/PerformPhase.kt b/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/event/PerformPhase.kt index 33f6224300..0e1a7cb08e 100644 --- a/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/event/PerformPhase.kt +++ b/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/event/PerformPhase.kt @@ -145,7 +145,7 @@ class PerformPhase>(val repository: Repository, e: E) : val inputRequest = (inputPhase as BaseRequestPhase<*>).getRequest() check(inputRequest.getIntent() in AllowedIntents) { - "Event can't be created for a request with ${inputRequest.getIntent().name} intent." + "Event can't be created for a request with ${inputRequest.getIntent().code} intent." } check(inputRequest.getStatus() == Status.ACTIVE) { diff --git a/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/request/OrderPhase.kt b/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/request/OrderPhase.kt index 49f9b71d71..dcb65d8fd1 100644 --- a/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/request/OrderPhase.kt +++ b/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/request/OrderPhase.kt @@ -57,7 +57,7 @@ class OrderPhase>(repository: Repository, r: R) : val inputRequest = (inputPhase as Phase.RequestPhase<*>).getRequest() check(inputRequest.getIntent() in AllowedIntents) { - "Order can't be created for a request with ${inputRequest.getIntent().name} intent." + "Order can't be created for a request with ${inputRequest.getIntent().code} intent." } check(inputRequest.getStatus() == Status.ACTIVE) { @@ -110,7 +110,7 @@ class OrderPhase>(repository: Repository, r: R) : } require(inputOrder.getIntent() == Intent.ORDER) { - "Input request has '${inputOrder.getIntent().name}' intent." + "Input request has '${inputOrder.getIntent().code}' intent." } require(inputOrder.getStatus() in AllowedStatusForPhaseStart) { diff --git a/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/request/PlanPhase.kt b/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/request/PlanPhase.kt index 0e6cf0c731..99079a8665 100644 --- a/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/request/PlanPhase.kt +++ b/workflow/src/main/java/com/google/android/fhir/workflow/activity/phase/request/PlanPhase.kt @@ -52,7 +52,7 @@ class PlanPhase>(repository: Repository, r: R) : val inputRequest = (inputPhase as Phase.RequestPhase<*>).getRequest() check(inputRequest.getIntent() == Intent.PROPOSAL) { - "Plan can't be created for a request with ${inputRequest.getIntent().name} intent." + "Plan can't be created for a request with ${inputRequest.getIntent().code} intent." } check(inputRequest.getStatus() == Status.ACTIVE) { @@ -107,7 +107,7 @@ class PlanPhase>(repository: Repository, r: R) : } require(draftPlan.getIntent() == Intent.PLAN) { - "Input request has '${draftPlan.getIntent().name}' intent." + "Input request has '${draftPlan.getIntent().code}' intent." } require(draftPlan.getStatus() in AllowedStatusForPhaseStart) { diff --git a/workflow/src/main/java/com/google/android/fhir/workflow/activity/resource/request/CPGCommunicationRequest.kt b/workflow/src/main/java/com/google/android/fhir/workflow/activity/resource/request/CPGCommunicationRequest.kt index f8dfb32684..c5d359fa64 100644 --- a/workflow/src/main/java/com/google/android/fhir/workflow/activity/resource/request/CPGCommunicationRequest.kt +++ b/workflow/src/main/java/com/google/android/fhir/workflow/activity/resource/request/CPGCommunicationRequest.kt @@ -41,7 +41,7 @@ class CPGCommunicationRequest(override val resource: CommunicationRequest) : resource.getExtensionByUrl("http://hl7.org/fhir/StructureDefinition/request-intent")?.let { Intent.of(it.value?.primitiveValue()) } - ?: Intent.NULL + ?: Intent.of(null) override fun setStatus(status: Status, reason: String?) { resource.status = CommunicationRequest.CommunicationRequestStatus.fromCode(status.string) diff --git a/workflow/src/main/java/com/google/android/fhir/workflow/activity/resource/request/CPGRequestResource.kt b/workflow/src/main/java/com/google/android/fhir/workflow/activity/resource/request/CPGRequestResource.kt index 93cbcd774e..d758746191 100644 --- a/workflow/src/main/java/com/google/android/fhir/workflow/activity/resource/request/CPGRequestResource.kt +++ b/workflow/src/main/java/com/google/android/fhir/workflow/activity/resource/request/CPGRequestResource.kt @@ -55,9 +55,9 @@ sealed class CPGRequestResource(internal open val resource: R) where R : Reso val logicalId: String get() = resource.logicalId - abstract fun setIntent(intent: Intent) + internal abstract fun setIntent(intent: Intent) - abstract fun getIntent(): Intent + internal abstract fun getIntent(): Intent abstract fun setStatus(status: Status, reason: String? = null) @@ -73,7 +73,7 @@ sealed class CPGRequestResource(internal open val resource: R) where R : Reso internal abstract fun copy(): CPGRequestResource - fun copy(id: String, status: Status, intent: Intent): CPGRequestResource { + internal fun copy(id: String, status: Status, intent: Intent): CPGRequestResource { val parent: CPGRequestResource = this return copy().apply { resource.idElement = IdType.of(resource).setValue(id) @@ -143,32 +143,28 @@ sealed class CPGRequestResource(internal open val resource: R) where R : Reso } } -enum class Intent(val code: String?) { - PROPOSAL("proposal"), - PLAN("plan"), - DIRECTIVE("directive"), - ORDER("order"), - ORIGINALORDER("original-order"), - REFLEXORDER("reflex-order"), - FILLERORDER("filler-order"), - INSTANCEORDER("instance-order"), - OPTION("option"), - NULL(null), - ; +/** + * PROPOSAL, PLAN and ORDER are the only intents we are interested in. All the other Request Intent + * values are represented by OTHER. See + * [codesystem-request-intent](https://www.hl7.org/FHIR/codesystem-request-intent.html) for the list + * of intents. + */ +internal sealed class Intent(val code: String?) { + data object PROPOSAL : Intent("proposal") + + data object PLAN : Intent("plan") + + data object ORDER : Intent("order") + + class OTHER(code: String?) : Intent(code) companion object { fun of(code: String?): Intent { return when (code) { "proposal" -> PROPOSAL "plan" -> PLAN - "directive" -> DIRECTIVE "order" -> ORDER - "original-order" -> ORIGINALORDER - "reflex-order" -> REFLEXORDER - "filler-order" -> FILLERORDER - "instance-order" -> INSTANCEORDER - "option" -> OPTION - else -> NULL + else -> OTHER(code) } } } diff --git a/workflow/src/test/java/com/google/android/fhir/workflow/activity/ActivityFlowTest.kt b/workflow/src/test/java/com/google/android/fhir/workflow/activity/ActivityFlowTest.kt index 7fb912111a..13b545d2ec 100644 --- a/workflow/src/test/java/com/google/android/fhir/workflow/activity/ActivityFlowTest.kt +++ b/workflow/src/test/java/com/google/android/fhir/workflow/activity/ActivityFlowTest.kt @@ -206,16 +206,18 @@ class ActivityFlowTest { fun `communication request flow new api`(): Unit = runBlockingOnWorkerThread { val cpgCommunicationRequest = CPGRequestResource.of( - CommunicationRequest().apply { - id = "com-req-01" - status = CommunicationRequest.CommunicationRequestStatus.ACTIVE - subject = Reference("Patient/pat-01") - meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest") - - addPayload().apply { content = StringType("Proposal") } - }, - ) - + CommunicationRequest().apply { + id = "com-req-01" + status = CommunicationRequest.CommunicationRequestStatus.ACTIVE + subject = Reference("Patient/pat-01") + meta.addProfile( + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest", + ) + + addPayload().apply { content = StringType("Proposal") } + }, + ) + .apply { setIntent(Intent.PROPOSAL) } val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine) repository.create(cpgCommunicationRequest.resource) @@ -422,15 +424,18 @@ class ActivityFlowTest { fun `draftPlan is success when flow is in proposal phase`(): Unit = runBlockingOnWorkerThread { val cpgCommunicationRequest = CPGRequestResource.of( - CommunicationRequest().apply { - id = "com-req-01" - status = CommunicationRequest.CommunicationRequestStatus.ACTIVE - subject = Reference("Patient/pat-01") - meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest") - - addPayload().apply { content = StringType("Proposal") } - }, - ) + CommunicationRequest().apply { + id = "com-req-01" + status = CommunicationRequest.CommunicationRequestStatus.ACTIVE + subject = Reference("Patient/pat-01") + meta.addProfile( + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest", + ) + + addPayload().apply { content = StringType("Proposal") } + }, + ) + .apply { setIntent(Intent.PROPOSAL) } val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine) repository.create(cpgCommunicationRequest.resource) @@ -523,15 +528,18 @@ class ActivityFlowTest { fun `draftOrder is success when flow is in proposal phase`(): Unit = runBlockingOnWorkerThread { val cpgCommunicationRequest = CPGRequestResource.of( - CommunicationRequest().apply { - id = "com-req-01" - status = CommunicationRequest.CommunicationRequestStatus.ACTIVE - subject = Reference("Patient/pat-01") - meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest") - - addPayload().apply { content = StringType("Proposal") } - }, - ) + CommunicationRequest().apply { + id = "com-req-01" + status = CommunicationRequest.CommunicationRequestStatus.ACTIVE + subject = Reference("Patient/pat-01") + meta.addProfile( + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest", + ) + + addPayload().apply { content = StringType("Proposal") } + }, + ) + .apply { setIntent(Intent.PROPOSAL) } val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine) repository.create(cpgCommunicationRequest.resource) @@ -624,15 +632,18 @@ class ActivityFlowTest { fun `draftPerform is success when flow is in proposal phase`(): Unit = runBlockingOnWorkerThread { val cpgCommunicationRequest = CPGRequestResource.of( - CommunicationRequest().apply { - id = "com-req-01" - status = CommunicationRequest.CommunicationRequestStatus.ACTIVE - subject = Reference("Patient/pat-01") - meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest") - - addPayload().apply { content = StringType("Proposal") } - }, - ) + CommunicationRequest().apply { + id = "com-req-01" + status = CommunicationRequest.CommunicationRequestStatus.ACTIVE + subject = Reference("Patient/pat-01") + meta.addProfile( + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest", + ) + + addPayload().apply { content = StringType("Proposal") } + }, + ) + .apply { setIntent(Intent.PROPOSAL) } val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine) repository.create(cpgCommunicationRequest.resource) @@ -725,15 +736,18 @@ class ActivityFlowTest { fun `getCurrentPhase should return the current phase of the flow`() = runBlockingOnWorkerThread { val cpgCommunicationRequest = CPGRequestResource.of( - CommunicationRequest().apply { - id = "com-req-01" - status = CommunicationRequest.CommunicationRequestStatus.ACTIVE - subject = Reference("Patient/pat-01") - meta.addProfile("http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest") - - addPayload().apply { content = StringType("Proposal") } - }, - ) + CommunicationRequest().apply { + id = "com-req-01" + status = CommunicationRequest.CommunicationRequestStatus.ACTIVE + subject = Reference("Patient/pat-01") + meta.addProfile( + "http://hl7.org/fhir/uv/cpg/StructureDefinition/cpg-communicationrequest", + ) + + addPayload().apply { content = StringType("Proposal") } + }, + ) + .apply { setIntent(Intent.PROPOSAL) } val repository = FhirEngineRepository(FhirContext.forR4Cached(), fhirEngine) repository.create(cpgCommunicationRequest.resource)