public void controller(
      FragmentConfiguration config,
      @SpringBean("emrApiProperties") EmrApiProperties emrApiProperties,
      @SpringBean("coreAppsProperties") CoreAppsProperties coreAppsProperties,
      @SpringBean("baseIdentifierSourceService") IdentifierSourceService identifierSourceService,
      @FragmentParam(required = false, value = "appContextModel") AppContextModel appContextModel,
      @FragmentParam("patient") Object patient,
      @InjectBeans PatientDomainWrapper wrapper,
      @SpringBean("conceptService") ConceptService conceptService,
      @SpringBean("obsService") ObsService obsService,
      @SpringBean("locationService") LocationService locationService,
      @SpringBean("appFrameworkService") AppFrameworkService appFrameworkService,
      @SpringBean("adtService") AdtService adtService,
      UiSessionContext sessionContext,
      UiUtils uiUtils,
      FragmentModel model) {

    if (patient instanceof Patient) {
      wrapper.setPatient((Patient) patient);
    } else {
      wrapper = (PatientDomainWrapper) patient;
    }
    config.addAttribute("patient", wrapper);
    config.addAttribute("patientNames", getNames(wrapper.getPersonName()));

    if (appContextModel == null) {
      AppContextModel contextModel = sessionContext.generateAppContextModel();
      contextModel.put("patient", new PatientContextModel(wrapper.getPatient()));
      model.addAttribute("appContextModel", contextModel);
    }

    List<Extension> firstLineFragments =
        appFrameworkService.getExtensionsForCurrentUser("patientHeader.firstLineFragments");
    Collections.sort(firstLineFragments);
    model.addAttribute("firstLineFragments", firstLineFragments);

    List<Extension> secondLineFragments =
        appFrameworkService.getExtensionsForCurrentUser("patientHeader.secondLineFragments");
    Collections.sort(secondLineFragments);
    model.addAttribute("secondLineFragments", secondLineFragments);

    // Adapting the header's content based on actual/current registration app's sections.
    List<AppDescriptor> regAppDescriptors = getRegistrationAppConfig(appFrameworkService);
    List<RegistrationSectionData> regAppSections =
        getRegistrationData(
            regAppDescriptors,
            new DataContextWrapper(
                sessionContext.getLocale(), wrapper, conceptService, obsService, locationService));
    config.addAttribute("regAppSections", regAppSections);

    List<ExtraPatientIdentifierType> extraPatientIdentifierTypes =
        new ArrayList<ExtraPatientIdentifierType>();

    for (PatientIdentifierType type : emrApiProperties.getExtraPatientIdentifierTypes()) {
      List<AutoGenerationOption> options = identifierSourceService.getAutoGenerationOptions(type);
      // TODO note that this may allow use to edit a identifier that should not be editable, or vice
      // versa, in the rare case where there are multiple autogeneration
      // TODO options for a single identifier type (which is possible if you have multiple
      // locations) and the manual entry boolean is different between those two generators
      extraPatientIdentifierTypes.add(
          new ExtraPatientIdentifierType(
              type, options.size() > 0 ? options.get(0).isManualEntryEnabled() : true));
    }

    config.addAttribute("extraPatientIdentifierTypes", extraPatientIdentifierTypes);
    config.addAttribute(
        "extraPatientIdentifiersMappedByType",
        wrapper.getExtraIdentifiersMappedByType(sessionContext.getSessionLocation()));
    config.addAttribute("dashboardUrl", coreAppsProperties.getDashboardUrl());
  }
  public void controller(
      @RequestParam(value = "patientId", required = false) Patient patient,
      @RequestParam(value = "headerForm") String headerForm,
      @RequestParam(value = "flowsheets") String[] flowsheets,
      @RequestParam(value = "viewOnly", required = false) Boolean viewOnly,
      @RequestParam(value = "requireEncounter", required = false) Boolean requireEncounter,
      UiUtils ui,
      PageModel model,
      @SpringBean("htmlFormEntryService") HtmlFormEntryService htmlFormEntryService,
      @SpringBean("formService") FormService formService,
      @SpringBean("locationService") LocationService locationService,
      @SpringBean("coreResourceFactory") ResourceFactory resourceFactory,
      @InjectBeans PatientDomainWrapper patientDomainWrapper,
      PageRequest pageRequest) {

    patientDomainWrapper.setPatient(patient);
    model.addAttribute("patient", patientDomainWrapper);
    model.addAttribute("headerForm", headerForm);
    model.addAttribute("flowsheets", flowsheets);
    model.addAttribute("requireEncounter", (requireEncounter == null || requireEncounter));

    Location defaultLocation = null;
    Integer locationId =
        pageRequest
            .getSession()
            .getAttribute(PihMalawiWebConstants.SESSION_LOCATION_ID, Integer.TYPE);
    if (locationId != null) {
      defaultLocation = locationService.getLocation(locationId);
    }

    List<Encounter> allEncounters = new ArrayList<Encounter>();

    List<String> alerts = new ArrayList<String>();

    String headerFormResource = "pihmalawi:htmlforms/" + headerForm + ".xml";

    HtmlForm headerHtmlForm =
        getHtmlFormFromResource(
            headerFormResource, resourceFactory, formService, htmlFormEntryService);
    model.addAttribute("headerForm", headerForm);

    Encounter headerEncounter = null;
    List<Encounter> headerEncounters = getEncountersForForm(patient, headerHtmlForm);
    if (headerEncounters.size() > 0) {
      headerEncounter = headerEncounters.get(headerEncounters.size() - 1); // Most recent
      if (headerEncounters.size() > 1) {
        alerts.add(
            "WARNING:  More than one "
                + headerHtmlForm.getName()
                + " encounters exist for this patient.  Displaying the most recent only.");
      }
      allEncounters.add(headerEncounter);
    }
    model.addAttribute("headerEncounter", headerEncounter);

    Map<String, HtmlForm> flowsheetForms = new LinkedHashMap<String, HtmlForm>();
    Map<String, List<Integer>> flowsheetEncounters = new LinkedHashMap<String, List<Integer>>();
    if (flowsheets != null) {
      for (String flowsheet : flowsheets) {
        String flowsheetResource = "pihmalawi:htmlforms/" + flowsheet + ".xml";
        HtmlForm htmlForm =
            getHtmlFormFromResource(
                flowsheetResource, resourceFactory, formService, htmlFormEntryService);
        flowsheetForms.put(flowsheet, htmlForm);
        List<Integer> encIds = new ArrayList<Integer>();
        List<Encounter> encounters = getEncountersForForm(patient, htmlForm);
        for (Encounter e : encounters) {
          encIds.add(e.getEncounterId());
          allEncounters.add(e);
        }
        flowsheetEncounters.put(flowsheet, encIds);
      }
    }
    model.addAttribute("flowsheetForms", flowsheetForms);
    model.addAttribute("flowsheetEncounters", flowsheetEncounters);

    model.addAttribute("alerts", alerts);

    if (defaultLocation == null) {
      Date maxDate = null;
      if (allEncounters.size() > 0) {
        for (Encounter e : allEncounters) {
          if (maxDate == null || maxDate.compareTo(e.getEncounterDatetime()) < 0) {
            maxDate = e.getEncounterDatetime();
            defaultLocation = e.getLocation();
          }
        }
      }
    }
    model.addAttribute(
        "defaultLocationId", defaultLocation == null ? null : defaultLocation.getLocationId());
    model.addAttribute("viewOnly", viewOnly == Boolean.TRUE);

    model.addAttribute(
        "returnUrl",
        ui.pageLink(
            "pihmalawi",
            "mastercard",
            SimpleObject.create(
                "patientId",
                patient.getId(),
                "headerForm",
                headerForm,
                "flowsheets",
                flowsheets,
                "viewOnly",
                viewOnly)));
  }
 private boolean itBelongsToARealPatient(Visit candidate) {
   Patient patient = candidate.getPatient();
   PatientDomainWrapper domainWrapper =
       new PatientDomainWrapper(patient, emrApiProperties, null, null, null, null);
   return !domainWrapper.isTestPatient();
 }