Loading a Form Resource from json file into the form_resource table

Continuing the discussion from How can custom forms be added to the Android Client from an OpenMRS Backend:

I am looking for a way to load an android form (.json) into the form resource table without having to run a manual script to database for the Android App. All HTML Form Entry methods expect XML, yet the Android app uses JSON

@dkayiwa @mogoodrich @mksd

Which exact html form entry methods were you looking at?

@dkayiwa this class only handles XML not JSON https://github.com/openmrs/openmrs-module-htmlformentry/blob/6285025297ac7423f6cfba697a2b7bd6961545d3/api/src/main/java/org/openmrs/module/htmlformentry/HtmlFormEntryUtil.java

I am trying to replicate what is needed to setup a form resource for this JSON

for this JSON

{ “name”: “My Form”, “uuid”: “7ce5eb4d-722c-46e4-a6ef-05fa34feaa6e”, “processor”: “EncounterFormProcessor”, “pages”: [ { “label”: “”, “sections”: [ { “label”: “Label 1”, “questions”: [ { “type”: “obsGroup”, “label”: “Details of the past Screening”, “questionOptions”: { “rendering”: “group”, “concept”: “138405AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA” }, “questions”: [ { “label”: “Question 1”, “questionOptions”: { “rendering”: “radio”, “concept”: “138405AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “answers”: [ { “concept”: “1065AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “Yes” }, { “concept”: “1066AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “No” }, { “concept”: “1067AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “Unknown” } ] }, “type”: “obs”, “validators”: [] }, { “label”: “Does the patient have any symptoms?”, “questionOptions”: { “rendering”: “radio”, “concept”: “1729AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “answers”: [ { “concept”: “1065AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “Yes” }, { “concept”: “1066AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “No” }, { “concept”: “1067AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “Unknown” } ] }, “type”: “obs”, “validators”: [] } ] } ] } ] } ] }

to extract the details which are used to build the following equivalent SQL

insert into form_resource (form_id, name, value_reference, datatype, datatype_config, preferred_handler, handler_config, uuid, date_changed, changed_by) values (6, ‘json’, ‘{ “name”: “My Form”, “uuid”: “7ce5eb4d-722c-46e4-a6ef-05fa34feaa6e”, “processor”: “EncounterFormProcessor”, “pages”: [ { “label”: “”, “sections”: [ { “label”: “Label 1”, “questions”: [ { “type”: “obsGroup”, “label”: “Details of the past Screening”, “questionOptions”: { “rendering”: “group”, “concept”: “138405AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA” }, “questions”: [ { “label”: “Question 1”, “questionOptions”: { “rendering”: “radio”, “concept”: “138405AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “answers”: [ { “concept”: “1065AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “Yes” }, { “concept”: “1066AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “No” }, { “concept”: “1067AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “Unknown” } ] }, “type”: “obs”, “validators”: [] }, { “label”: “Does the patient have any symptoms?”, “questionOptions”: { “rendering”: “radio”, “concept”: “1729AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “answers”: [ { “concept”: “1065AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “Yes” }, { “concept”: “1066AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “No” }, { “concept”: “1067AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA”, “label”: “Unknown” } ] }, “type”: “obs”, “validators”: [] } ] } ] } ] } ] }’, ‘org.openmrs.customdatatype.datatype.FreeTextDatatype’, null, null, null, ‘d0b8ab1c-196e-4bcc-9cae-b6477bcc0153’, null, null);

Which exact method in that class?

@dkayiwa I do not understand your question, what I am saying is that I do not see a method that I can use for JSON yet methods like stringToDocument, findChild, findNode, getNodeAttribute, getNodeContentsAsString all leverage the Sax Parser.

How can I leverage them for JSON is what i am looking for - not clearly apparent to me

Oh i see!

How about using REST? Here are example tests that deal with form resources: https://github.com/openmrs/openmrs-module-webservices.rest/blob/master/omod-1.9/src/test/java/org/openmrs/module/webservices/rest/web/v1_0/controller/openmrs1_9/FormResourceController1_9Test.java

This is an example resource being loaded in the tests: https://github.com/openmrs/openmrs-module-webservices.rest/blob/master/omod-1.9/src/test/resources/formResourcefile.txt

You can get more details from the ticket: https://issues.openmrs.org/browse/RESTWS-523

@dkayiwa I want to load this during module activation, won’t this cause an issue since one is not sure if all the services have been started

Which is why I wanted to use the Service methods to load the data

That makes it even simpler. Just directly use the openmrs-core api which the rest webservices module delegates to.