Implementing communication client step by step
Since module version:
1.2.0
If one needs to implement a new communication client like FHIR or REST this tutorial will show you how it can be achieved.
Note: Currently, we only support HTTP based communication clients.
In order to create a new communication client you will have to follow these 9 steps:
STEP 1. Add dependency to FHIR module:
- Add this line to your properties in pom.xml:
<fhirVersion>1.X.Y</fhirVersion>
Note: Specify version you wish to have dependency on by setting 'X' and 'Y' values. Minimum version is 1.14.0.
- Add this block to your dependencies in your API and OMOD pom.xml files:
<dependency> <groupId>org.openmrs.module</groupId> <artifactId>fhir-api</artifactId> <scope>provided</scope> <version>${fhirVersion}</version> </dependency>
STEP 2. Implement org.openmrs.module.fhir.api.helper.ClientHelper interface:
for example:
public class TestClientHelper implements ClientHelper { ... }
Note: We will be using TestClientHelper example until the end of this tutorial.
STEP 3. Modify Sync2 module. Add a new constant to org.openmrs.module.sync2.SyncConstants:
As a name take the first part of your ClientHelper interface implementation class name, in our case it will be:
... public static final String TEST_CLIENT = "test"; ...
STEP 4. In Sync2 module add a new case to org.openmrs.module.sync2.client.ClientHelperFactory switch:
public class ClientHelperFactory { private static final Logger LOGGER = LoggerFactory.getLogger(ClientHelperFactory.class); public static ClientHelper createClient(final String clientType) { switch (clientType) { case REST_CLIENT: return new RESTClientHelper(); ... case TEST_CLIENT: return new TestClientHelper(); ... default: LOGGER.warn(String.format("Unrecognized clientType: %s. The REST Client will be used.", clientType)); return new RESTClientHelper(); } } }
Remember: You have to import TEST_CLIENT constant first.
STEP 5. In Sync2 module, in org.openmrs.module.sync2.api.utils.SyncUtils class, implement your own method for extracting UUID from your resource link:
private static String extractUUIDFromTestResource(String link) { String uuid; ... // extracting UUID from link ... return uuid; }
Note: Name of this method follows this pattern: extractUUIDFrom<name of your client>Resource.
STEP 6. In Sync2 module add a new case to org.openmrs.module.sync2.api.utils.extractUUIDFromResourceLinks switch:
public static String extractUUIDFromResourceLinks(Map<String, String> resourceLinks) { for (String client : resourceLinks.keySet()) { switch (client) { case REST_CLIENT: return extractUUIDFromRestResource(resourceLinks.get(client)); ... case TEST_CLIENT: return extractUUIDFromTestResource(resourceLinks.get(client)); ... default: } } LOGGER.error("Couldn't find any supported client to extract uuid from."); return null; }
Note: As an argument of your extract method put "resourceLinks.get(client)".
Remember: You have to import TEST_CLIENT constant first.
STEP 7. When running your OpenMRS instance, go to Atomfeed > Load Configuration page
Add your API endpoints to linkTemplates object in JSON file, for example:
"feedConfigurations": [ ... { "openMrsClass": "org.openmrs.Location", "enabled": "true", "title": "Location", "category": "location", "linkTemplates": { ... "test":"/test/Location/{uuid}" } }, ... ]
Note: Key (in our case "test") must match TEST_CLIENT constant that was set in STEP 2.
STEP 8. Set your communication client as preferred client for pulling resources.
From Home page go to System Administration > Advanced Administration > Maintenence > Advanced Settings.
Find sync2.resource.preferred.client property and set its value to the value of TEST_CLIENT constant, for example:
STEP 9. That's the end. Once you have finished this tutorial, you should have a working communication client.