Update EncounterProvider for an Encounter

Hi:

Is it possible via REST to update one or more Encounter providers for an Encounter using the REST WS? I am trying to do the following:

  1. Create a new Encounter with EncounterProviders: Person 1 with Role A, Person 2 with Role B, and Person 3 with Role C
  2. Update the Encounter so that the EncounterProviders are: Person1 with Role A, Person 2 with Role B, and Person 4 with Role C. The update is a POST with the Encounter UUID in the stem (e.g. /openmrs/ws/rest/v1/encounter/26805aee-88fe-4460-be5d-ec96b1ad1c3d)

I’ve tried posting the EncounterProviders with and without a UUID, but it fails with a 500 code at step 2 in both cases. Here’s the s snip of the EncounterProviders part of the JSON that is being posted for the update (the uuid values are the existing uuids for the EncounterProviders that were created at step #1):

{
  "encounterProviders": [
    {
      "provider": "06cf82e6-47a3-4f7a-b2ba-51eaf463294e",
      "encounterRole": "c458d78e-8374-4767-ad58-9f8fe276e01c"
    },
    {
      "uuid": "bd21207c-7764-4485-ab0f-ed0423068840",
      "provider": "13dbceea-8145-4ede-83a7-5f527c307b14",
      "encounterRole": "9b135b19-7ebe-4a51-aea2-69a53f9383af"
    },
    {
      "uuid": "7ab75ffe-8142-4a91-8920-47c034eb7f85",
      "provider": "13dbceea-8145-4ede-83a7-5f527c307b14",
      "encounterRole": "6e630e03-5182-4cb3-9a82-a5b1a85c09a7"
    }
  ]
} 

Below is a snip of the error that I am getting.

Any advice?

Thanks,

Joe

{"error":{"message":"[Could not execute JDBC batch update; SQL [update encounter_provider set uuid=?, date_created=?, voided=?, date_voided=?, void_reason=?, date_changed=?, changed_by=?, creator=?, voided_by=?, provider_id=?, encounter_id=?, encounter_role_id=? where encounter_provider_id=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update]","code":"org.springframework.orm.hibernate3.SessionFactoryUtils:637","detail":"org.springframework.dao.DataIntegrityViolationException: Could not execute JDBC batch update; SQL [update encounter_provider set uuid=?, date_created=?, voided=?, date_voided=?, void_reason=?, date_changed=?, changed_by=?, creator=?, voided_by=?, provider_id=?, encounter_id=?, encounter_role_id=? where encounter_provider_id=?]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: Could not execute JDBC batch update\n\tat org.springframework.orm.hibernate3.SessionFactoryUtils.convertHibernateAccessException(SessionFactoryUtils.java:637)\n\tat org.springframework.orm.hibernate3.HibernateTransactionManager.convertHibernateAccessException(HibernateTransactionManager.java:793)\n\tat org.springframework.orm.hibernate3.HibernateTransactionManager.doCommit(HibernateTransactionManager.java:664)\n\tat org.springframework.transaction.support.AbstractPlatformTransactionManager.processCommit(AbstractPlatformTransactionManager.java:754)\n\tat org.springframework.transaction.support.AbstractPlatformTransactionManager.commit(AbstractPlatformTransactionManager.java:723)\n\tat org.springframework.transaction.interceptor.TransactionAspectSupport.commitTransactionAfterReturning(TransactionAspectSupport.java:393)\n\tat org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:120)\n\tat org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172)\n\tat org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:202)\n\tat com.sun.proxy.$Proxy168.saveEncounter(Unknown Source)\n\tat org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9.EncounterResource1_9.save(EncounterResource1_9.java:81)\n\tat org.openmrs.module.webservices.rest.web.v1_0.resource.openmrs1_9.EncounterResource1_9.save(EncounterResource1_9.java:31)\n\tat org.openmrs.module.webservices.rest.web.resource.impl.DelegatingCrudResource.update(DelegatingCrudResource.java:142)\n\tat org.openmrs.module.webservices.rest.web.v1_0.controller.MainResourceController.update(MainResourceController.java:127)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n\tat sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)\n\tat sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)\n\tat java.lang.reflect.Method.invoke(Method.java:606)\n\tat org.springframework.web.bind.annotation.support.HandlerMethodInvoker.invokeHandlerMethod(HandlerMethodInvoker.java:176)\n\tat ....

@jcelano, encounterprovider is actually a subresource of encounter (see the Subresources section of https://wiki.openmrs.org/x/P4IaAQ) so you’ll need to work with the URL /openmrs/ws/rest/v1/encounter/26805aee-88fe-4460-be5d-ec96b1ad1c3d/encounterprovider

In this example you can DELETE one encounterprovider and POST another one. (It’s possible you could just POST to edit the one and change it from person 3 to person 4, I’m not sure.)

@darius, thanks for the info, this make sense…but when I do the POST to create the EnounterProvider, the web services returns OK that the EncounterProvider was created, but it doesn’t actually create it. Am I doing something wrong?

You can try this locally with an Encounter by running this CURL:

curl 'http://localhost:8080/openmrs/ws/rest/v1/encounter/26805aee-88fe-4460-be5d-ec96b1ad1c3d/encounterprovider' -H 'Origin: http://localhost:8080' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Referer: http://localhost:8080/openmrs/test.page' -H 'Cookie: JSESSIONID=1ai5tzj2ubem7; emr.lastSessionLocation=34; __openmrs_language=en_US' -H 'Connection: keep-alive' --data-binary '{"provider":"ce3679bd-be1a-4543-81a4-17b7928979bc","encounterRole":"9b135b19-7ebe-4a51-aea2-69a53f9383af"}' --compressed

You will just have to change the Encounter UUID, Provider UUID, and Encounter Role UUID. The server will respond with new encounterprovider, but if you look it up using REST it doesn’t exist. I’ve verified that the Provider and Encounter Role are both valid, but can’t figure out why they are not being added.

Thanks,

Joe

@jcelano, I was able to reproduce the behavior you describe.

Peeking at the code, I’m not sure if that subresource works at all. At a glance I don’t think that the save() or delete() method will work in this class because they don’t call EncounterService.saveEncounter().

I’m not sure of your skillset, but perhaps you can submit a fix for this to the webservices.rest module. Or else, create a ticket describing this bug.

@darius, Thanks for tracking this down! I will look into creating the ticket and fix.

@jcelano, actually I’m working on fixing this now. I’ll commit a fix shortly.

@jcelano I created a ticket RESTWS-637 and committed a fix.

To test this you would need to build or download the latest snapshot of the webservices.rest module and deploy it to the server in question.

@darius, Thanks for the quick update! I got the latest code and having a couple of weird issues.

My first issue is that sometimes the delete doesn’t work for an EncounterProvider. I haven’t been able to figure out how I get into this state, but eventually I have EncounterProviders that I cannot delete by their UUID using the web service. This might be related to the 2nd issue, but I am not sure. Could it have to do with their being multiple EncounterProviders with the same Provider UUID? I can’t figure this one out.

My second issue, which I can reproduce, is that when I create the EncounterProvider, it is returning some bogus UUID for the EncounterProvider. My steps are as follows:

  1. Create the Encounter
  2. Add a 1st EncounterProvider
  3. Add a 2nd EncounterProvider

When I add the EncounterProviders, the UUID returned by the web service is not right. This is a problem, b/c then I can’t delete them if I want to change them (and I can’t update them either). Am I doing something wrong?

Here’s the CURL for my calls:

Create encounter

curl 'http://localhost:8080/openmrs/ws/rest/v1/encounter' -H 'Origin: http://localhost:8080' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Referer: http://localhost:8080/openmrs/labtrackingapp/labtrackingOrderDetails.page?orderUuid=a02b082b-0ede-49ae-bcce-0d9dc1030628' -H 'Cookie: JSESSIONID=7klxqy6vefdm; emr.lastSessionLocation=34; __openmrs_language=en_US' -H 'Connection: keep-alive' --data-binary '{"patient":"6c7ffd68-8d0f-4868-bb90-1284a0cdbe8c","encounterType":"10db3139-07c0-4766-b4e5-a41b01363145","location":"199e7d87-92a0-4398-a0f8-11d012178164","encounterProviders":[{"provider":"06cf82e6-47a3-4f7a-b2ba-51eaf463294e","encounterRole":"c458d78e-8374-4767-ad58-9f8fe276e01c"}],"obs":[{"concept":"393dec41-2fb5-428f-acfa-36ea85da6666","value":"ORD-79","uuid":null},{"groupMembers":[{"concept":"226ed7ad-b776-4b99-966d-fd818d3302c2","value":"146825AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA","uuid":null},{"concept":"3cd9ef9a-26fe-102b-80cb-0017a47871b2","value":"3cd9bd04-26fe-102b-80cb-0017a47871b2","uuid":null}],"concept":"2da3ec67-62aa-4be8-a32c-cb32723742c8","uuid":null}]}' --compressed

Create first EncounterProvider

curl 'http://localhost:8080/openmrs/ws/rest/v1/encounter/41f42a18-6875-4c2e-b5a0-3a923260a0f2/encounterprovider' -H 'Origin: http://localhost:8080' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Referer: http://localhost:8080/openmrs/labtrackingapp/labtrackingOrderDetails.page?orderUuid=a02b082b-0ede-49ae-bcce-0d9dc1030628' -H 'Cookie: JSESSIONID=7klxqy6vefdm; emr.lastSessionLocation=34; __openmrs_language=en_US' -H 'Connection: keep-alive' --data-binary '{"provider":"ca105028-37ac-4685-94ab-523f4233cb94","encounterRole":"9b135b19-7ebe-4a51-aea2-69a53f9383af"}' --compressed

Create 2nd EncounterProvider

curl 'http://localhost:8080/openmrs/ws/rest/v1/encounter/41f42a18-6875-4c2e-b5a0-3a923260a0f2/encounterprovider' -H 'Origin: http://localhost:8080' -H 'Accept-Encoding: gzip, deflate, br' -H 'Accept-Language: en-US,en;q=0.8' -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.95 Safari/537.36' -H 'Content-Type: application/json;charset=UTF-8' -H 'Accept: application/json, text/plain, */*' -H 'Referer: http://localhost:8080/openmrs/labtrackingapp/labtrackingOrderDetails.page?orderUuid=a02b082b-0ede-49ae-bcce-0d9dc1030628' -H 'Cookie: JSESSIONID=7klxqy6vefdm; emr.lastSessionLocation=34; __openmrs_language=en_US' -H 'Connection: keep-alive' --data-binary '{"provider":"ce3679bd-be1a-4543-81a4-17b7928979bc","encounterRole":"6e630e03-5182-4cb3-9a82-a5b1a85c09a7"}' --compressed

@jcelano can you share the return values you are getting from those calls?

Mark

@mogoodrich, Here are the requests and responses.responses.txt (9.5 KB) . If there’s any other data that you need, please let me know.

Joe

Cool, got it… yep, looks as far as I can tell you are doing everything right but it is returning the wrong uuid when you attempt to add an encounter provider to an existing encounter.

I will see if myself or someone else can look into it in the next few days.

@jcelano @darius

I believe I have fixed the latest bug here:

Can you let me know if this works? (And if it helps with your delete issues at all?)

Take care, Mark

@mogoodrich, Thanks, this fixed the UUID issue. Unfortunately, the delete is still an issue, but at least now I am able to reproduce it. The steps to reproduce it are:

  1. Create an Encounter
  2. Add EncounterProvider #1
  3. Add EncounterProvider #2
  4. Update Encounter
  5. Delete EncounterProvider #1
  6. Delete EncounterProvider #2
  7. Add EncounterProvider #1
  8. Add EncounterProvider #2

After all these steps there are 5 Encounter Providers instead of 3. What’s really weird is if you skip steps #7-8, then the EncounterProviders are deleted. I’ve attached the requests and responses.responses_part_2.txt (17.8 KB) Once it is in this state, you can never delete the EncounterProviders.

Is it something that I am doing wrong? It looks like all the requests are ok and I am using the correct UUID’s.

Unfortunately I certainly wouldn’t be shocked if it was another bug, based on the other issues we’ve run into. I will take a look tomorrow.

Thanks for helping improve the REST module. :slight_smile:

Take care, Mark

Mark Goodrich