* @should allow to submit a form if exit from care section is initially not filled
*/
public Collection<FormSubmissionError> validateSubmission(FormEntryContext context, HttpServletRequest submission) {
List<FormSubmissionError> ret = new ArrayList<FormSubmissionError>();
Date exitDate = null;
Concept exitReasonAnswerConcept = null;
Concept causeOfDeathAnswerConcept = null;
Patient patient = context.getExistingPatient();
String reasonConId = Context.getAdministrationService().getGlobalProperty("concept.reasonExitedCare");
Concept reasonExitConcept = Context.getConceptService().getConcept(reasonConId);
List<Obs> obsList = Context.getObsService().getObservationsByPersonAndConcept(patient, reasonExitConcept);
String causeOfDeathConId = Context.getAdministrationService().getGlobalProperty("concept.causeOfDeath");
Concept causeOfDeathConcept = Context.getConceptService().getConcept(causeOfDeathConId);
List<Obs> obsDeath = Context.getObsService().getObservationsByPersonAndConcept(patient, causeOfDeathConcept);
String patientDiedConId = Context.getAdministrationService().getGlobalProperty("concept.patientDied");
Concept patientDiedConcept = Context.getConceptService().getConcept(patientDiedConId);
String otherNonCodedConId = Context.getAdministrationService().getGlobalProperty("concept.otherNonCoded");
Concept otherNonCodedConcept = Context.getConceptService().getConcept(otherNonCodedConId);
if (dateWidget != null) {
exitDate = dateWidget.getValue(context, submission);
if (exitDate != null) {
if (OpenmrsUtil.compare(exitDate, new Date()) > 0) {
ret.add(new FormSubmissionError(context.getFieldName(dateErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.cannotBeInFuture")));
}
}
if (obsList != null && obsList.size() == 0) {
if (exitDate == null) {
if (reasonForExitWidget != null &&
HtmlFormEntryUtil.convertToType(reasonForExitWidget.getValue(context, submission).toString().trim(), Concept.class) != null) {
ret.add(new FormSubmissionError(context.getFieldName(dateErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.required")));
}
}
}
}
if (reasonForExitWidget != null) {
Object value = reasonForExitWidget.getValue(context, submission);
exitReasonAnswerConcept = (Concept) HtmlFormEntryUtil.convertToType(value.toString().trim(), Concept.class);
if (obsList != null && obsList.size() == 0) {
if (exitReasonAnswerConcept == null) {
if (dateWidget != null && dateWidget.getValue(context, submission) != null) {
ret.add(new FormSubmissionError(context.getFieldName(reasonForExitErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.required")));
}
} else if (exitReasonAnswerConcept != null && exitReasonAnswerConcept.getConceptId().equals(patientDiedConcept.getConceptId())) {
if (causeOfDeathWidget == null ||
HtmlFormEntryUtil.convertToType(causeOfDeathWidget.getValue(context, submission).toString().trim(), Concept.class) == null) {
ret.add(new FormSubmissionError(context.getFieldName(causeOfDeathErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.required")));
}
} else if (exitReasonAnswerConcept != null && !exitReasonAnswerConcept.getConceptId().equals(patientDiedConcept.getConceptId())) {
if (causeOfDeathWidget != null &&
HtmlFormEntryUtil.convertToType(causeOfDeathWidget.getValue(context, submission).toString().trim(), Concept.class) != null) {
ret.add(new FormSubmissionError(context.getFieldName(causeOfDeathErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.cannotEnterAValue")));
}
}
}
}
if (causeOfDeathWidget != null) {
Object value = causeOfDeathWidget.getValue(context, submission);
causeOfDeathAnswerConcept = (Concept) HtmlFormEntryUtil.convertToType(value.toString().trim(), Concept.class);
String valueString = otherReasonWidget.getValue(context, submission);
if (obsDeath != null && obsDeath.size() == 0) {
if (causeOfDeathAnswerConcept == null) {
if (otherReasonWidget != null && !valueString.equals("")) {
ret.add(new FormSubmissionError(context.getFieldName(otherReasonErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.cannotEnterAValue")));
}
} else if (causeOfDeathAnswerConcept != null && causeOfDeathAnswerConcept.getConceptId().equals(otherNonCodedConcept.getConceptId())) {
if (otherReasonWidget == null || valueString.equals("")) {
ret.add(new FormSubmissionError(context.getFieldName(otherReasonErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.required")));
}
} else if (causeOfDeathAnswerConcept != null && !causeOfDeathAnswerConcept.getConceptId().equals(otherNonCodedConcept.getConceptId())) {
if (otherReasonWidget != null && !valueString.equals("")) {
ret.add(new FormSubmissionError(context.getFieldName(otherReasonErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.cannotEnterAValue")));
}
}
}
}
// this validation is added to avoid user resetting the 'exit from care'
if (obsList != null && obsList.size() == 1) {
if (exitDate == null) {
ret.add(new FormSubmissionError(context.getFieldName(dateErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.required")));
} else if (exitReasonAnswerConcept == null) {
ret.add(new FormSubmissionError(context.getFieldName(reasonForExitErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.required")));
} else if (exitReasonAnswerConcept != null && exitReasonAnswerConcept.getConceptId().equals(patientDiedConcept.getConceptId())) {
if (causeOfDeathWidget == null ||
HtmlFormEntryUtil.convertToType(causeOfDeathWidget.getValue(context, submission).toString().trim(), Concept.class) == null) {
ret.add(new FormSubmissionError(context.getFieldName(causeOfDeathErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.required")));
}
} else if (exitReasonAnswerConcept != null && !exitReasonAnswerConcept.getConceptId().equals(patientDiedConcept.getConceptId())) {
if (causeOfDeathWidget != null &&
HtmlFormEntryUtil.convertToType(causeOfDeathWidget.getValue(context, submission).toString().trim(), Concept.class) != null) {
ret.add(new FormSubmissionError(context.getFieldName(causeOfDeathErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.cannotEnterAValue")));
}
}
}
// this validation is added to avoid user resetting the 'patient died' incident
String valueString = otherReasonWidget.getValue(context, submission);
if (obsDeath != null && obsDeath.size() == 1) {
if (causeOfDeathAnswerConcept != null && causeOfDeathAnswerConcept.getConceptId().equals(otherNonCodedConcept.getConceptId())) {
if (otherReasonWidget == null || valueString.equals("")) {
ret.add(new FormSubmissionError(context.getFieldName(otherReasonErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.required")));
}
} else if (causeOfDeathAnswerConcept != null && !causeOfDeathAnswerConcept.getConceptId().equals(otherNonCodedConcept.getConceptId())) {
if (otherReasonWidget != null && !valueString.equals("")) {
ret.add(new FormSubmissionError(context.getFieldName(otherReasonErrorWidget), Context.getMessageSourceService()
.getMessage("htmlformentry.error.cannotEnterAValue")));
}
}