OpenMRS services usage
1- Add dependency to your pom file
<dependency> <groupId>org.opensrp</groupId> <artifactId>opensrp-connector</artifactId> <version>${release.version}</version> </dependency>
2- Use OpenmrsConnector to parse and extract the entities from form submission like below
//Check whether form is intended for pushing into OpenMRS if(openmrsConnector.isOpenmrsForm(formSubmission)){ //Find out if any entity exists with same ID to determine whether to create new person in OpenMRS //Note that OpenMRS would throw exception incase entity exists JSONObject p = patientService.getPatientByIdentifier(formSubmission.entityId()); //If entity exists we can not register a new patient. So just save the encounter part into OpenMRS if(p != null){ Event e = openmrsConnector.getEventFromFormSubmission(formSubmission);//Extract event from form submission //Donot forget to write code to save event into CouchDB as core Event and Obs //...... //Now push data to OpenMRS encounterService.createEncounter(e);//Create an encounter into OpenMRS } else {//If person does not already exist into OpenMRS we have two options // 1- Data is for a single person // 2- Data is to register Household and dependent entities // Extract dependent entities info from openmrsConnector like below Map<String, Map<String, Object>> dep = openmrsConnector.getDependentClientsFromFormSubmission(formSubmission); if(dep.size()>0){// If data is for Household Client hhhClient = openmrsConnector.getClientFromFormSubmission(formSubmission); Event hhhEvent = openmrsConnector.getEventFromFormSubmission(formSubmission); OpenmrsHouseHold hh = new OpenmrsHouseHold(hhhClient, hhhEvent); for (Map<String, Object> cm : dep.values()) { hh.addHHMember((Client)cm.get("client"), (Event)cm.get("event")); } householdService.saveHH(hh);//Save household into OpenMRS //Donot forget save info into core CouchDB as BaseEntity, Client, Event and Obs and as register entities if any //............ } else {// If data is for single entity Client c = openmrsConnector.getClientFromFormSubmission(formSubmission); //Donot forget to save data into core as Client and BaseEntity //........... patientService.createPatient(c);//Create patient into OpenMRS Event e = openmrsConnector.getEventFromFormSubmission(formSubmission); //Donot forget to save data into core as Event and Obs //........... encounterService.createEncounter(e);//Create encounter into OpenMRS } } }
A sample complete working class would be like below
@Component public class MyTestClass { private EncounterService encounterService; private OpenmrsConnector openmrsConnector; private PatientService patientService; private HouseholdService householdService; @Autowired public MyTestClass(EncounterService encounterService, OpenmrsConnector openmrsConnector, PatientService patientService, HouseholdService householdService) { this.encounterService = encounterService; this.openmrsConnector = openmrsConnector; this.patientService = patientService; this.householdService = householdService; } public void pushForms(List<FormSubmissionDTO> formSubmissionsDTO) { try { for (FormSubmission formSubmission : fsl) { if(openmrsConnector.isOpenmrsForm(formSubmission)){ JSONObject p = patientService.getPatientByIdentifier(formSubmission.entityId()); if(p != null){ Event e = openmrsConnector.getEventFromFormSubmission(formSubmission); encounterService.createEncounter(e); } else { Map<String, Map<String, Object>> dep = openmrsConnector.getDependentClientsFromFormSubmission(formSubmission); if(dep.size()>0){ Client hhhClient = openmrsConnector.getClientFromFormSubmission(formSubmission); Event hhhEvent = openmrsConnector.getEventFromFormSubmission(formSubmission); OpenmrsHouseHold hh = new OpenmrsHouseHold(hhhClient, hhhEvent); for (Map<String, Object> cm : dep.values()) { hh.addHHMember((Client)cm.get("client"), (Event)cm.get("event")); } householdService.saveHH(hh); } else { Client c = openmrsConnector.getClientFromFormSubmission(formSubmission); patientService.createPatient(c); Event e = openmrsConnector.getEventFromFormSubmission(formSubmission); encounterService.createEncounter(e); } } } } } catch (Exception e) { logger.error(format("Form submissions processing failed with exception {0}.\nSubmissions: {1}", e, formSubmissionsDTO)); } } }
Note that in class above there is no code to save data into core or CouchDB. Data saving into Couch DB is a requirement and application would not function properly without it.
This site is no longer maintained. Please visit docs.opensrp.io for current documentation.