/**
  * Patients who were enrolled on the given programs (excluding transfers) on ${onOrBefore}
  *
  * @param programs the programs
  * @return the cohort definition
  */
 public CohortDefinition enrolledExcludingTransfersOnDate(Program... programs) {
   CompositionCohortDefinition cd = new CompositionCohortDefinition();
   cd.setName("enrolled excluding transfers in program on date in this facility");
   cd.addParameter(new Parameter("onOrBefore", "Before Date", Date.class));
   cd.addSearch(
       "enrolled", ReportUtils.map(enrolled(programs), "enrolledOnOrBefore=${onOrBefore}"));
   cd.addSearch("transferIn", ReportUtils.map(transferredIn(), "onOrBefore=${onOrBefore}"));
   cd.setCompositionString("enrolled AND NOT transferIn");
   return cd;
 }
 /**
  * Patients who are female and at least 18 years old on ${effectiveDate}
  *
  * @return the cohort definition
  */
 public CohortDefinition femalesAgedAtLeast18() {
   CompositionCohortDefinition cd = new CompositionCohortDefinition();
   cd.setName("females aged at least 18");
   cd.addParameter(new Parameter("effectiveDate", "Effective Date", Date.class));
   cd.addSearch("females", ReportUtils.map(females()));
   cd.addSearch(
       "agedAtLeast18", ReportUtils.map(agedAtLeast(18), "effectiveDate=${effectiveDate}"));
   cd.setCompositionString("females AND agedAtLeast18");
   return cd;
 }
 /**
  * Patients who were enrolled on the given programs (excluding transfers) between ${onOrAfter} and
  * ${onOrBefore}
  *
  * @param programs the programs
  * @return the cohort definition
  */
 public CohortDefinition enrolledExcludingTransfers(Program... programs) {
   CompositionCohortDefinition cd = new CompositionCohortDefinition();
   cd.setName("enrolled excluding transfers in program between dates");
   cd.addParameter(new Parameter("onOrAfter", "After Date", Date.class));
   cd.addParameter(new Parameter("onOrBefore", "Before Date", Date.class));
   cd.addSearch(
       "enrolled",
       ReportUtils.map(
           enrolled(programs), "enrolledOnOrAfter=${onOrAfter},enrolledOnOrBefore=${onOrBefore}"));
   cd.addSearch("transferIn", ReportUtils.map(transferredIn(), "onOrBefore=${onOrBefore}"));
   cd.addSearch(
       "completeProgram", ReportUtils.map(compltedProgram(), "completedOnOrBefore=${onOrBefore}"));
   cd.setCompositionString("enrolled AND NOT (transferIn OR completeProgram)");
   return cd;
 }
 @Override
 protected Mapped<CohortDefinition> buildCohort(
     HybridReportDescriptor descriptor, PatientDataSetDefinition dsd) {
   Integer period = Integer.parseInt(descriptor.getId().split("\\.")[7]);
   CohortDefinition cd = artCohortLibrary.netCohortMonthsBetweenDatesGivenMonths(period);
   return ReportUtils.map(cd, "startDate=${startDate},endDate=${endDate}");
 }
  /**
   * Creates the ART data set
   *
   * @return the data set
   */
  private DataSetDefinition createTbDataSet() {
    CohortIndicatorDataSetDefinition dsd = new CohortIndicatorDataSetDefinition();
    dsd.setName("G");
    dsd.setDescription("TB");
    dsd.addParameter(new Parameter("startDate", "Start Date", Date.class));
    dsd.addParameter(new Parameter("endDate", "End Date", Date.class));
    dsd.addDimension("age", map(commonDimensions.standardAgeGroups(), "onDate=${endDate}"));
    dsd.addDimension("gender", map(commonDimensions.gender()));

    List<ColumnParameters> columns = new ArrayList<ColumnParameters>();
    columns.add(new ColumnParameters("FP", "0-14 years, female", "gender=F|age=<15"));
    columns.add(new ColumnParameters("MP", "0-14 years, male", "gender=M|age=<15"));
    columns.add(new ColumnParameters("FA", ">14 years, female", "gender=F|age=15+"));
    columns.add(new ColumnParameters("MA", ">14 years, male", "gender=M|age=15+"));
    columns.add(new ColumnParameters("T", "total", ""));

    String indParams = "startDate=${startDate},endDate=${endDate}";

    EmrReportingUtils.addRow(
        dsd,
        "G1",
        "No. of detected cases (who have new Tb detected cases)",
        ReportUtils.map(tbIndicators.tbNewDetectedCases(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G2",
        "No. of Pulmonary smear positive (who have pulmonary TB and smear positive)",
        ReportUtils.map(tbIndicators.pulmonaryTbSmearPositive(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G3",
        "No. of Pulmonary smear negative (who have pulmonary TB and smear negative)",
        ReportUtils.map(tbIndicators.pulmonaryTbSmearNegative(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G4",
        "No. of Extra pulmonary TB (who have extra pulmonary TB)",
        ReportUtils.map(tbIndicators.extraPulmonaryTbPatients(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G5",
        "No. of TB Re-treatments (who are in Tb re-treatments)",
        ReportUtils.map(tbIndicators.tbRetreatmentsPatients(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G6",
        "No. of TB and Tested for HIV (who are in Tb program and tested for HIV)",
        ReportUtils.map(tbIndicators.inTbAndTestedForHiv(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G7",
        "No. of TB and Tested for HIV (whose HIV result is positive)",
        ReportUtils.map(tbIndicators.inTbAndTestedForHivPositive(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G8",
        "No. of TB and HIV (who are both in TB and HIV and are on CPT)",
        ReportUtils.map(tbIndicators.inTbAndHivProgramsAndOnCtxProphylaxis(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G9",
        "No. of TB defaulters (who defaulted or missed appointments)",
        ReportUtils.map(tbIndicators.defaulted(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G10",
        "No. of TB completes (who Completed Tb Treatment)",
        ReportUtils.map(tbIndicators.completedTbTreatment(), indParams),
        columns);
    EmrReportingUtils.addRow(
        dsd,
        "G11",
        "No. of TB deaths (who started tx this month last year)",
        ReportUtils.map(tbIndicators.diedAndStarted12MonthsAgo(), indParams),
        columns);

    return dsd;
  }
  /**
   * Creates the ART data set
   *
   * @return the data set
   */
  private DataSetDefinition createArtDataSet() {
    CohortIndicatorDataSetDefinition dsd = new CohortIndicatorDataSetDefinition();
    dsd.setName("K");
    dsd.setDescription("ART");
    dsd.addParameter(new Parameter("startDate", "Start Date", Date.class));
    dsd.addParameter(new Parameter("endDate", "End Date", Date.class));
    dsd.addDimension(
        "age", ReportUtils.map(commonDimensions.standardAgeGroups(), "onDate=${endDate}"));
    dsd.addDimension("gender", ReportUtils.map(commonDimensions.gender()));

    ColumnParameters colFPeds =
        new ColumnParameters("FP", "0-14 years, female", "gender=F|age=<15");
    ColumnParameters colMPeds = new ColumnParameters("MP", "0-14 years, male", "gender=M|age=<15");
    ColumnParameters colFAdults =
        new ColumnParameters("FA", ">14 years, female", "gender=F|age=15+");
    ColumnParameters colMAdults = new ColumnParameters("MA", ">14 years, male", "gender=M|age=15+");
    ColumnParameters colFTotal = new ColumnParameters("F", "totals, female", "gender=F");
    ColumnParameters colMTotal = new ColumnParameters("M", "totals, male", "gender=M");
    ColumnParameters colTotal = new ColumnParameters("T", "grand total", "");

    Concept pmtct = Dictionary.getConcept(Dictionary.PMTCT_PROGRAM);
    Concept vct = Dictionary.getConcept(Dictionary.VCT_PROGRAM);
    Concept tb = Dictionary.getConcept(Dictionary.TUBERCULOSIS_TREATMENT_PROGRAM);
    Concept[] inpatient = {
      Dictionary.getConcept(Dictionary.PEDIATRIC_INPATIENT_SERVICE),
      Dictionary.getConcept(Dictionary.ADULT_INPATIENT_SERVICE)
    };
    Concept cwc = Dictionary.getConcept(Dictionary.UNDER_FIVE_CLINIC);
    Concept[] all = {pmtct, vct, tb, inpatient[0], inpatient[1], cwc};

    List<ColumnParameters> allColumns =
        Arrays.asList(colFPeds, colMPeds, colFAdults, colMAdults, colFTotal, colMTotal, colTotal);
    List<ColumnParameters> femaleColumns = Arrays.asList(colFPeds, colFAdults, colFTotal, colTotal);
    List<ColumnParameters> pedsColumns =
        Arrays.asList(colFPeds, colMPeds, colFTotal, colMTotal, colTotal);

    String indParams = "startDate=${startDate},endDate=${endDate}";

    EmrReportingUtils.addRow(
        dsd,
        "K1-1",
        "New enrollments - PMTCT",
        ReportUtils.map(artIndicators.enrolledExcludingTransfersAndReferredFrom(pmtct), indParams),
        femaleColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K1-2",
        "New enrollments - VCT",
        ReportUtils.map(artIndicators.enrolledExcludingTransfersAndReferredFrom(vct), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K1-3",
        "New enrollments - TB",
        ReportUtils.map(artIndicators.enrolledExcludingTransfersAndReferredFrom(tb), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K1-4",
        "New enrollments - In Patient",
        ReportUtils.map(
            artIndicators.enrolledExcludingTransfersAndReferredFrom(inpatient), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K1-5",
        "New enrollments - CWC",
        ReportUtils.map(artIndicators.enrolledExcludingTransfersAndReferredFrom(cwc), indParams),
        pedsColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K1-6",
        "New enrollments - All others",
        ReportUtils.map(artIndicators.enrolledExcludingTransfersAndNotReferredFrom(all), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K1-7",
        "New enrollments - Sub-total",
        ReportUtils.map(artIndicators.enrolledExcludingTransfers(), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K2",
        "Cumulative enrolled",
        ReportUtils.map(artIndicators.enrolledCumulative(), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K3-1",
        "Starting ARVs - WHO stage 1",
        ReportUtils.map(artIndicators.startedArtWithWhoStage(1), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K3-2",
        "Starting ARVs - WHO stage 2",
        ReportUtils.map(artIndicators.startedArtWithWhoStage(2), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K3-3",
        "Starting ARVs - WHO stage 3",
        ReportUtils.map(artIndicators.startedArtWithWhoStage(3), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K3-4",
        "Starting ARVs - WHO stage 4",
        ReportUtils.map(artIndicators.startedArtWithWhoStage(4), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K3-5",
        "Starting ARVs - Sub-total",
        ReportUtils.map(artIndicators.startedArt(), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K4",
        "Cumulative started ARV",
        ReportUtils.map(artIndicators.startedArtCumulative(), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K5-1",
        "Currently on ARVs - Pregnant women",
        ReportUtils.map(artIndicators.onArtAndPregnant(), indParams),
        femaleColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K5-2",
        "Currently on ARVs - All others",
        ReportUtils.map(artIndicators.onArtAndNotPregnant(), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K5-3",
        "Currently on ARVs - Sub-total",
        ReportUtils.map(artIndicators.onArt(), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K6",
        "Eligible for ART",
        ReportUtils.map(artIndicators.eligibleForArt(), indParams),
        allColumns);
    // EmrReportingUtils.addRow(dsd, "K7-1", "Post-exposure prophylaxis..", map(???, indParams),
    // allColumns);
    // EmrReportingUtils.addRow(dsd, "K7-2", "Post-exposure prophylaxis..", map(???, indParams),
    // allColumns);
    // EmrReportingUtils.addRow(dsd, "K7-3", "Post-exposure prophylaxis..", map(???, indParams),
    // allColumns);
    // EmrReportingUtils.addRow(dsd, "K7-4", "Post-exposure prophylaxis..", map(???, indParams),
    // allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K8-1",
        "On prophylaxis - Cotrimoxazole",
        ReportUtils.map(artIndicators.onCotrimoxazoleProphylaxis(), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K8-2",
        "On prophylaxis - Fluconazole",
        ReportUtils.map(artIndicators.onFluconazoleProphylaxis(), indParams),
        allColumns);
    EmrReportingUtils.addRow(
        dsd,
        "K8-3",
        "On prophylaxis - Sub-total",
        ReportUtils.map(artIndicators.onProphylaxis(), indParams),
        allColumns);

    return dsd;
  }