/**
  * @param appFrameworkService
  * @return The currently enabled Registration Apps AppDescriptors.
  */
 protected List<AppDescriptor> getRegistrationAppConfig(
     final AppFrameworkService appFrameworkService) {
   final List<AppDescriptor> regAppDescriptors = new ArrayList<AppDescriptor>();
   for (AppDescriptor appDesc : appFrameworkService.getAllEnabledApps()) {
     if (StringUtils.equals(appDesc.getInstanceOf(), "registrationapp.registerPatient")) {
       regAppDescriptors.add(appDesc);
     }
   }
   return regAppDescriptors;
 }
  @Test
  public void testGetAllApps() throws Exception {
    mockStatic(Context.class);
    when(Context.getService(AppFrameworkService.class)).thenReturn(mockAppFrameworkService);
    when(mockAppFrameworkService.getAllApps()).thenReturn(getTestAppList());

    List<AppDescriptor> appList = new AppFrameworkController().getApps();

    assertNotNull(appList);
    assertEquals(3, appList.size());
    assertEquals("app1", appList.get(0).getId());
  }
  public Redirect controller(
      HttpServletRequest request,
      @SpringBean AppFrameworkService appFrameworkService,
      @RequestParam(value = "patientId", required = false) Patient patient) {

    String appId = getCurrentApp(request);

    // TODO right now we only handle check-in app, and assume it is cyclical
    if (StringUtils.isNotBlank(appId) && appId.equals("mirebalais.liveCheckin")) {
      // TODO should we include patient here
      return new Redirect(appFrameworkService.getApp(appId).getUrl());
    }

    // TODO HACK--since we are only using this for the check-in use case righ now, by default we
    // redirect back to registration summary page
    return new Redirect("registrationapp", "registrationSummary", "patientId=" + patient.getId());
  }
  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());
  }