}
else if ("edit".equalsIgnoreCase(modeParam)) {
mode = Mode.EDIT;
}
Patient patient = null;
Encounter encounter = null;
Form form = null;
HtmlForm htmlForm = null;
if (StringUtils.hasText(request.getParameter("encounterId"))) {
Integer encounterId = Integer.valueOf(request.getParameter("encounterId"));
encounter = Context.getEncounterService().getEncounter(encounterId);
if (encounter == null)
throw new IllegalArgumentException("No encounter with id=" + encounterId);
patient = encounter.getPatient();
patientId = patient.getPatientId();
personId = patient.getPersonId();
if (formId != null) { // I think formId is allowed to differ from encounter.form.id because of HtmlFormFlowsheet
form = Context.getFormService().getForm(formId);
htmlForm = HtmlFormEntryUtil.getService().getHtmlFormByForm(form);
if (htmlForm == null)
throw new IllegalArgumentException("No HtmlForm associated with formId " + formId);
} else {
form = encounter.getForm();
htmlForm = HtmlFormEntryUtil.getService().getHtmlFormByForm(encounter.getForm());
if (htmlForm == null)
throw new IllegalArgumentException("The form for the specified encounter (" + encounter.getForm() + ") does not have an HtmlForm associated with it");
}
} else { // no encounter specified
// get person from patientId/personId (register module uses patientId, htmlformentry uses personId)
if (patientId != null) {
personId = patientId;
}
if (personId != null) {
patient = Context.getPatientService().getPatient(personId);
}
// determine form
if (htmlFormId != null) {
htmlForm = HtmlFormEntryUtil.getService().getHtmlForm(htmlFormId);
} else if (formId != null) {
form = Context.getFormService().getForm(formId);
htmlForm = HtmlFormEntryUtil.getService().getHtmlFormByForm(form);
}
if (htmlForm == null) {
throw new IllegalArgumentException("You must specify either an htmlFormId or a formId for a valid html form");
}
String which = request.getParameter("which");
if (StringUtils.hasText(which)) {
if (patient == null)
throw new IllegalArgumentException("Cannot specify 'which' without specifying a person/patient");
List<Encounter> encs = Context.getEncounterService().getEncounters(patient, null, null, null, Collections.singleton(form), null, null, false);
if (which.equals("first")) {
encounter = encs.get(0);
} else if (which.equals("last")) {
encounter = encs.get(encs.size() - 1);
} else {
throw new IllegalArgumentException("which must be 'first' or 'last'");
}
}
}
if (mode != Mode.ENTER && patient == null)
throw new IllegalArgumentException("No patient with id of personId=" + personId + " or patientId=" + patientId);
FormEntrySession session = null;
if (mode == Mode.ENTER && patient == null) {
patient = new Patient();
}
if (encounter != null) {
session = new FormEntrySession(patient, encounter, mode, htmlForm, request.getSession());
}
else {