// propagate encounterDatetime to Obs where necessary
if (submissionActions.getObsToCreate() != null) {
List<Obs> toCheck = new ArrayList<Obs>();
toCheck.addAll(submissionActions.getObsToCreate());
while (toCheck.size() > 0) {
Obs o = toCheck.remove(toCheck.size() - 1);
if (o.getObsDatetime() == null && o.getEncounter() != null) {
o.setObsDatetime(o.getEncounter().getEncounterDatetime());
if (log.isDebugEnabled())
log.debug("Set obsDatetime to " + o.getObsDatetime() + " for "
+ o.getConcept().getBestName(Context.getLocale()));
}
if (o.getLocation() == null && o.getEncounter() != null) {
o.setLocation(o.getEncounter().getLocation());
}
if (o.hasGroupMembers())
toCheck.addAll(o.getGroupMembers());
}
}
// propagate encounterDatetime to PatientPrograms where necessary
if (submissionActions.getPatientProgramsToCreate() != null) {
for (PatientProgram pp : submissionActions.getPatientProgramsToCreate()) {
if (pp.getDateEnrolled() == null)
pp.setDateEnrolled(encounter.getEncounterDatetime());
}
}
if (submissionActions.getPatientProgramsToComplete() != null) {
for (PatientProgram pp : submissionActions.getPatientProgramsToComplete()) {
if (pp.getDateCompleted() == null)
pp.setDateCompleted(encounter.getEncounterDatetime());
}
}
// 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.)