/**
   * @see
   *     org.springframework.web.servlet.mvc.AbstractFormController#formBackingObject(javax.servlet.http.HttpServletRequest)
   */
  protected Object formBackingObject(HttpServletRequest request) throws ServletException {

    // get list of formentry error queue items that have a name error

    FormEntryService fes = (FormEntryService) Context.getService(FormEntryService.class);
    // get all form entry errors
    Collection<FormEntryError> errors = fes.getFormEntryErrors();

    // list to return to the view
    List<RemoteFormEntryErrorModel> newPatientErrors = new Vector<RemoteFormEntryErrorModel>();

    String prefix = RemoteFormEntryException.class.getName();

    for (FormEntryError error : errors) {
      if (error.getError().startsWith(prefix))
        newPatientErrors.add(new RemoteFormEntryErrorModel(error));
    }

    return newPatientErrors;
  }
  /**
   * @see
   *     org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
   *     javax.servlet.http.HttpServletResponse, java.lang.Object,
   *     org.springframework.validation.BindException)
   */
  @Override
  protected ModelAndView onSubmit(
      HttpServletRequest request,
      HttpServletResponse response,
      Object object,
      BindException bindException)
      throws Exception {

    // user must be authenticated (avoids auth errors)
    if (Context.isAuthenticated()) {
      FormEntryService formEntryService = getFormEntryService();
      RemoteFormEntryService remoteFormEntryService = getRemoteFormEntryService();
      PatientService patientService = Context.getPatientService();
      XPath xp = getXPathFactory().newXPath();

      // there should be equal numbers of each of these parameters
      int[] errorIds = ServletRequestUtils.getIntParameters(request, "formEntryErrorId");

      // loop over all parameters and do the actions the user requested
      for (int x = 0; x < errorIds.length; x++) {
        Integer errorId = errorIds[x];
        String actionItem = request.getParameter("errorItemAction-" + errorId);

        if ("currentPatient".equals(actionItem)) {
          // must do the request items like this so that the javascript picker works
          // correctly...
          String patientIdString = request.getParameter("currentPatientId-" + errorId);
          Integer patientId = Integer.valueOf(patientIdString);

          // fetch the selected patient from the database
          Patient patient = patientService.getPatient(patientId);

          // fetch the error queue item from the database
          FormEntryError errorItem = getFormEntryService().getFormEntryError(errorId);
          Document doc = getDocumentForErrorQueueItem(errorItem.getFormData());

          // update the selected patient with metadata from the form
          remoteFormEntryService.updatePatientInDatabase(patient, doc, xp);

          // create the formentry queue item so it can be processed normally
          remoteFormEntryService.createFormEntryQueueForPatient(errorItem.getFormData(), patient);

          // delete the formentry error queue item
          formEntryService.deleteFormEntryError(errorItem);
        } else if ("newPatient".equals(actionItem)) {
          // fetch the FormEntryError item from the database
          FormEntryError errorItem = getFormEntryService().getFormEntryError(errorId);
          Document doc = getDocumentForErrorQueueItem(errorItem.getFormData());

          // create the patient from the form data
          Patient newPatient = remoteFormEntryService.createPatientInDatabase(doc, xp);

          // create the formentry queue item so it can be processed normally
          remoteFormEntryService.createFormEntryQueueForPatient(
              errorItem.getFormData(), newPatient);

          // delete the formentry error queue item
          formEntryService.deleteFormEntryError(errorItem);
        } else if ("deleteError".equals(actionItem)) {
          // fetch the FormEntryError item from the database
          FormEntryError errorItem = getFormEntryService().getFormEntryError(errorId);

          // delete the formentry error queue item
          formEntryService.deleteFormEntryError(errorItem);

        } else if ("noChange".equals(actionItem)) {
          // do nothing here
        } else throw new APIException("Invalid action selected for: " + errorId);
      }

      HttpSession httpSession = request.getSession();
      httpSession.setAttribute(
          WebConstants.OPENMRS_MSG_ATTR, "remoteformentry.resolveErrors.success");

      return new ModelAndView(new RedirectView(getSuccessView()));
    }

    return showForm(request, response, bindException);
  }