/**
   * Checks if any of the 'E' risk factors are present in the group obs.
   *
   * @param grp
   * @return
   */
  private boolean anyRiskFactorsTypeE(GroupObs grp) {
    final Concept yes = Functions.trueConcept();

    // check if any of the 'E' risk factors are present
    for (CachedConceptId rfEConceptId :
        new CachedConceptId[] {
          MCMedicalHistoryConcepts.TUBERCULOSIS, //
          MCMedicalHistoryConcepts.HEART_DISEASE, //
          MCMedicalHistoryConcepts.DIABETES, //
          MCMedicalHistoryConcepts.ASTHMA, //
          MCMedicalHistoryConcepts.THYROID
        }) {
      final Obs conditionObs = grp.getMember(rfEConceptId);
      if (conditionObs != null && yes.equals(conditionObs.getValueCoded())) {
        // risk factor 'E' is present
        return true;
      }
    }

    // risk factor 'E' not found in this group observation
    return false;
  }
  /**
   * Calculates the risk factors based on the maternal care observation in the form.
   *
   * @return A comma-delimited list of risk factors (from A to E)
   */
  public String getRiskFactors() {
    // store risk factors in a list
    final List<String> riskFactors = new ArrayList<String>();

    if (getPatient() != null && mcProgramObs != null) {
      // check for risk factor 'a' using age at LMP
      final Obs lmpObs =
          mcProgramObs
              .getObstetricHistory()
              .getMember(MCObstetricHistoryConcepts.LAST_MENSTRUAL_PERIOD);
      if (lmpObs != null
          && lmpObs.getValueDatetime() != null
          && getPatient().getBirthdate() != null) {
        final int age =
            DateUtil.yearsBetween(getPatient().getBirthdate(), lmpObs.getValueDatetime());
        if (age < 18 || age > 35) {
          riskFactors.add("A");
        }
      }

      // check for risk factor 'b' using patient's height
      final Obs heightObs = Functions.observation(getPatient(), VisitConcepts.HEIGHT_CM);
      if (heightObs != null && heightObs.getValueNumeric() != null) {
        if (heightObs.getValueNumeric() < 145) {
          riskFactors.add("B");
        }
      }

      // check for risk factor 'c' using patient obstetric history
      final ObstetricHistory obHistory = mcProgramObs.getObstetricHistory();
      final Obs gravidaObs =
          obHistory.getMember(MCObstetricHistoryConcepts.OBSTETRIC_SCORE_GRAVIDA);
      if (gravidaObs != null && gravidaObs.getValueNumeric() != null) {
        if (gravidaObs.getValueNumeric() >= 4) {
          riskFactors.add("C");
        }
      }

      // check for risk factors 'd' using registration data
      final Concept yes = Functions.trueConcept();
      if (yes.equals(
              obHistory.getMember(MCObstetricHistoryConcepts.HISTORY_PREV_CSECTION).getValueCoded())
          || yes.equals(
              obHistory
                  .getMember(MCObstetricHistoryConcepts.HISTORY_3OR_MORE_MISCARRIAGES)
                  .getValueCoded())
          || yes.equals(
              obHistory
                  .getMember(MCObstetricHistoryConcepts.HISTORY_OF_POSTPARTUM_HEMORRHAGE)
                  .getValueCoded())) {
        riskFactors.add("D");
      }

      if (anyRiskFactorsTypeE(mcProgramObs.getPatientMedicalHistory())) {
        // check 'past' medical history
        riskFactors.add("E");
      } else {
        // check medical history observed during this pregnancy
        for (PrenatalVisitRecord visit : mcProgramObs.getPrenatalVisits()) {
          if (anyRiskFactorsTypeE(visit.getNewMedicalConditions())) {
            riskFactors.add("E");
            break;
          }
        }
      }
    }

    if (riskFactors.isEmpty()) {
      return "none";
    } else {
      final String riskFactorsText = riskFactors.toString();
      return riskFactorsText.substring(1, riskFactorsText.length() - 1);
    }
  }