* Creates an encounter based on fields in the post object
* @param post
* @return
*/
private Encounter createEncounterFromPost(SimpleObject post) throws ResponseException {
Encounter encounter = new Encounter();
encounter.setPatient(Context.getPatientService().getPatientByUuid(post.get("patient").toString()));
for (int i = 0; i < DATE_FORMATS.length; i++) {
try {
Date date = new SimpleDateFormat(DATE_FORMATS[i]).parse(post.get("encounterDatetime").toString());
//Openmrs doesn't allow future encounters
if (date.after(new Date())) {
encounter.setEncounterDatetime(new Date());
break;
} else {
encounter.setEncounterDatetime(date);
break;
}
}
catch (Exception ex) {}
}
encounter.setEncounterType(service.getEncounterTypeByUuid(post.get("encounterType").toString()));
if (post.get("location") != null) {
encounter.setLocation(Context.getLocationService().getLocationByUuid(post.get("location").toString()));
}
if (post.get("provider") != null) {
encounter.setProvider(Context.getPersonService().getPersonByUuid(post.get("provider").toString()));
}
//if no provider is given in the post, set as the current user
else {
encounter.setProvider(Context.getAuthenticatedUser().getPerson());
}
Encounter newEncounter = service.saveEncounter(encounter);
if (post.get("obs") != null) {
createObsFromPost(post, newEncounter);
}
if (post.get("orders") != null) {
createOrdersFromPost(post, newEncounter);