// TODO wrap this in a transaction
if (submissionActions.getPersonsToCreate() != null) {
for (Person p : submissionActions.getPersonsToCreate()) {
if (p instanceof Patient) {
Patient patient = (Patient) p;
PatientIdentifier patientIdentifier = patient.getPatientIdentifier();
if (!StringUtils.hasText(patient.getGivenName()) || !StringUtils.hasText(patient.getFamilyName())
|| !StringUtils.hasText(patient.getGender()) || patient.getBirthdate() == null
|| patientIdentifier == null || !StringUtils.hasText(patientIdentifier.getIdentifier())
|| patientIdentifier.getIdentifierType() == null || patientIdentifier.getLocation() == null) {
throw new BadFormDesignException(
"Please check the design of your form to make sure the following fields are mandatory to create a patient: <br/><b><personName/></b>, <b><birthDateOrAge/></b>, <b><gender/></b>, <b><identifierType/></b>, <b><identifier/></b>, and <b><identifierLocation/></b>");
}
}
Context.getPersonService().savePerson(p);
}
}
if (submissionActions.getEncountersToCreate() != null) {
for (Encounter e : submissionActions.getEncountersToCreate()) {
if (form != null) {
e.setForm(form);
if (form.getEncounterType() != null)
e.setEncounterType(form.getEncounterType());
}
Context.getEncounterService().saveEncounter(encounter);
}
}
//deal with relationships
if (submissionActions.getRelationshipsToCreate() != null) {
for (Relationship r : submissionActions.getRelationshipsToCreate()) {
if (log.isDebugEnabled()) {
log.debug("creating relationships" + r.getRelationshipType().getDescription());
}
Context.getPersonService().saveRelationship(r);
}
}
if (submissionActions.getRelationshipsToVoid() != null) {
for (Relationship r : submissionActions.getRelationshipsToVoid()) {
if (log.isDebugEnabled()) {
log.debug("voiding relationships" + r.getId());
}
Context.getPersonService().voidRelationship(r, "htmlformentry");
}
}
if (submissionActions.getRelationshipsToEdit() != null) {
for (Relationship r : submissionActions.getRelationshipsToCreate()) {
if (log.isDebugEnabled()) {
log.debug("editing relationships" + r.getId());
}
Context.getPersonService().saveRelationship(r);
}
}
// program enrollments are trickier since we need to make sure the patient isn't already enrolled
// 1. if the patient is already enrolled on the given date, just skip this
// 2. if the patient is enrolled *after* the given date, shift the existing enrollment to start earlier. (TODO decide if this is right)
// 3. otherwise just enroll them as requested
if (submissionActions.getPatientProgramsToCreate() != null) {
for (PatientProgram toCreate : submissionActions.getPatientProgramsToCreate()) {
boolean skip = false;
PatientProgram earliestAfter = null;
List<PatientProgram> already = Context.getProgramWorkflowService().getPatientPrograms(toCreate.getPatient(),
toCreate.getProgram(), null, null, null, null, false);
for (PatientProgram pp : already) {
if (pp.getActive(toCreate.getDateEnrolled())) {
skip = true;
break;
}
// if the existing one starts after toCreate
if (OpenmrsUtil.compare(pp.getDateEnrolled(), toCreate.getDateEnrolled()) > 0) {
if (earliestAfter == null
|| OpenmrsUtil.compare(pp.getDateEnrolled(), earliestAfter.getDateEnrolled()) < 0) {
earliestAfter = pp;
}
}
}
if (skip) {
continue;
}
if (earliestAfter != null) {
// edit this enrollment to move its start date earlier
earliestAfter.setDateEnrolled(toCreate.getDateEnrolled());
Context.getProgramWorkflowService().savePatientProgram(earliestAfter);
} else {
// just enroll as requested
Context.getProgramWorkflowService().savePatientProgram(toCreate);
}
}
}
//complete any necessary programs
if (submissionActions.getPatientProgramsToComplete() != null) {
for (PatientProgram toComplete : submissionActions.getPatientProgramsToComplete()) {
Context.getProgramWorkflowService().savePatientProgram(toComplete);
}
}
if (submissionActions.getPatientProgramsToUpdate() != null) {
for (PatientProgram patientProgram : submissionActions.getPatientProgramsToUpdate()) {
Context.getProgramWorkflowService().savePatientProgram(patientProgram);
}
}
ObsService obsService = Context.getObsService();
if (submissionActions.getObsToVoid() != null) {
for (Obs o : submissionActions.getObsToVoid()) {
if (log.isDebugEnabled())
log.debug("voiding obs: " + o.getObsId());
obsService.voidObs(o, "htmlformentry");
// if o was in a group and it has no obs left, void the group
voidObsGroupIfAllChildObsVoided(o.getObsGroup());
}
}
// If we're in EDIT mode, we have to save the encounter so that any new obs are created.
// This feels a bit like a hack, but actually it's a good thing to update the encounter's dateChanged in this case. (PS- turns out there's no dateChanged on encounter up to 1.5.)
// If there is no encounter (impossible at the time of writing this comment) we save the obs manually
if (context.getMode() == Mode.EDIT) {
if (encounter != null) {
if (voidEncounter) {
try {
HtmlFormEntryUtil.voidEncounter(encounter, htmlForm, "voided via htmlformentry form submission");
} catch (Exception ex) {
throw new RuntimeException("Unable to void encounter.", ex);
}
}
Context.getEncounterService().saveEncounter(encounter);
} else if (submissionActions.getObsToCreate() != null) {
// this may not work right due to savehandlers (similar error to HTML-135) but this branch is
// unreachable until html forms are allowed to edit data without an encounter
for (Obs o : submissionActions.getObsToCreate())
obsService.saveObs(o, null);
}
}
/*
ObsService obsService = Context.getObsService();
This should propagate from above
if (submissionActions.getObsToCreate() != null) {
for (Obs o : submissionActions.getObsToCreate())
Context.getObsService().saveObs(o, null);
}
*/
if (submissionActions.getIdentifiersToVoid() != null) {
for (PatientIdentifier patientIdentifier : submissionActions.getIdentifiersToVoid()) {
patientIdentifier.setVoided(true);
patientIdentifier.setVoidedBy(Context.getAuthenticatedUser());
patientIdentifier.setVoidReason(getForm().getName()); // Use form name as reason
patientIdentifier.setDateVoided(new Date());
}
}
// save the patient
// TODO: we are having some issues here when updating a Patient and an Encounter via an HTML form due recently discovered problems with the way