private String getErrorDescription(TrackedEntityInstance trackedEntityInstance) {
    FailedItem failedItem =
        DataValueController.getFailedItem(
            FailedItem.TRACKEDENTITYINSTANCE, trackedEntityInstance.localId);
    // Select.byId(FailedItem.class, trackedEntityInstance.localId);

    if (failedItem != null) {
      if (failedItem.getHttpStatusCode() == 200) {
        if (failedItem.getImportSummary() != null)
          return failedItem.getImportSummary().getDescription();
      }
      if (failedItem.getHttpStatusCode() == 401) {
        return getString(R.string.error_401_description);
      }

      if (failedItem.getHttpStatusCode() == 408) {
        return getString(R.string.error_408_description);
      }

      if (failedItem.getHttpStatusCode() >= 400 && failedItem.getHttpStatusCode() < 500) {
        return getString(R.string.error_series_400_description);
      }

      if (failedItem.getHttpStatusCode() >= 500) {
        return failedItem.getErrorMessage();
      }
    }

    return getString(R.string.unknown_error);
  }
 public Map<Long, FailedItem> getFailedEvents() {
   Map<Long, FailedItem> failedItemMap = new HashMap<>();
   List<FailedItem> failedItems = DataValueController.getFailedItems();
   if (failedItems != null && failedItems.size() > 0) {
     for (FailedItem failedItem : failedItems) {
       if (failedItem.getItemType().equals(FailedItem.EVENT))
         failedItemMap.put(failedItem.getItemId(), failedItem);
     }
   }
   return failedItemMap;
 }
  public void showDataEntryFragment(Event event, String programStage) {
    Bundle args = getArguments();
    DataEntryFragment fragment;
    String progId = mForm.getProgram().getId(); // new line :AgeToday
    if (event == null) {
      fragment =
          DataEntryFragment.newInstanceWithEnrollment(
              args.getString(ORG_UNIT_ID),
              args.getString(PROGRAM_ID),
              programStage,
              mForm.getEnrollment().localId);
    } else if (progId.equals("u9FZUM7kn38") && event != null) {
      /*- The If-Else block below is used to add the AgeToday Feature, a requirement for the HPV Campaign Program -*/
      fragment =
          DataEntryFragment.newInstanceWithEnrollment(
              args.getString(ORG_UNIT_ID),
              args.getString(PROGRAM_ID),
              programStage,
              event.getLocalEnrollmentId(),
              event.getLocalId());

      Enrollment enrollment = DataValueController.getEnrollment(event.getLocalEnrollmentId());
      List<TrackedEntityAttributeValue> teav = enrollment.getAttributes();
      // getActionBar().setSubtitle("Learner's AgeToday is: " + displayAge);
      // getActionBar().setDisplayShowTitleEnabled(true);

      for (TrackedEntityAttributeValue val : teav) {
        System.out.println("The learner details are: ->" + val.getValue());
      }

      // If passport number is entered then :  String learnerDob   = teav.get(4).getValue();
      // String learnerDob   = teav.get(3).getValue(); Training Instance - Position for LearnerDOB
      // when IDNumber is entered
      String learnerDob = teav.get(2).getValue(); // Staging Instance
      // String learnerDob   = teav.get(3).getValue();
      if (!isValidDate(learnerDob)) learnerDob = teav.get(3).getValue();
      System.out.println("What is in learnerstring :" + learnerDob);
      // --Use Joda Time to compute the learner's age today
      LocalDate birthdate = new LocalDate(learnerDob);
      LocalDate now = new LocalDate();
      // Compute learners age today
      Years learnersAgeToday = Years.yearsBetween(birthdate, now);

      System.out.println("The learners age today is:====>" + learnersAgeToday);
      ageToday = String.valueOf(learnersAgeToday);
      StringBuilder sbAgeToday = new StringBuilder(ageToday);
      sbAgeToday.delete(0, 1);

      sbAgeToday.append("EARS");
      System.out.println("The OLD learners age is:" + ageToday);
      //  String myDisplay = printLearnersAge();
      displayAge = sbAgeToday.toString();
      System.out.println("THE NEW LEARNERS AGE FIXED: " + displayAge);
      //   List<ProgramStageDataElement> programStageDataElements =
      // MetaDataController.getProgramStageDataElements
      // (MetaDataController.getProgramStage(event.getProgramStageId()));
      //  for(ProgramStageDataElement de: programStageDataElements) {
      //       System.out.println("The data elements in the program are:" + de.getDataElement());

      AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

      builder
          .setMessage("The learner's Age Today is: " + displayAge)
          .setCancelable(false)
          .setTitle("Learner's Age Today Notification")
          .setPositiveButton(
              "OK",
              new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                  dialog.dismiss();
                }
              });

      AlertDialog dialog = builder.create();
      dialog.show();

    } else {
      fragment =
          DataEntryFragment.newInstanceWithEnrollment(
              args.getString(ORG_UNIT_ID),
              args.getString(PROGRAM_ID),
              programStage,
              event.getLocalEnrollmentId(),
              event.getLocalId());
    }

    mNavigationHandler.switchFragment(fragment, ProgramOverviewFragment.CLASS_TAG, true);
  }
  @Override
  public void onLoadFinished(
      Loader<ProgramOverviewFragmentForm> loader, ProgramOverviewFragmentForm data) {
    if (LOADER_ID == loader.getId()) {
      mForm = data;
      mProgressBar.setVisibility(View.GONE);
      if (mForm == null || mForm.getEnrollment() == null) {
        showNoActiveEnrollment();
        return;
      } else {
        enrollmentLayout.setVisibility(View.VISIBLE);
        missingEnrollmentLayout.setVisibility(View.GONE);
      }
      enrollmentDateLabel.setText(data.getDateOfEnrollmentLabel());
      enrollmentDateValue.setText(data.getDateOfEnrollmentValue());
      incidentDateLabel.setText(data.getIncidentDateLabel());
      incidentDateValue.setText(data.getIncidentDateValue());

      if (mForm.getEnrollment().getStatus().equals(Enrollment.COMPLETED)) {
        setCompleted();
      }

      if (mForm.getEnrollment().getStatus().equals(Enrollment.CANCELLED)) {
        setTerminated();
      }

      if (mForm.getEnrollment().getFollowup()) {
        setFollowupButton(true);
      }

      if (data.getAttribute1Label() == null || data.getAttribute1Value() == null) {
        attribute1Label.setVisibility(View.GONE);
        attribute1Value.setVisibility(View.GONE);
      } else {
        attribute1Label.setText(data.getAttribute1Label());
        attribute1Value.setText(data.getAttribute1Value());
      }

      if (data.getAttribute2Label() == null || data.getAttribute2Value() == null) {
        attribute2Label.setVisibility(View.GONE);
        attribute2Value.setVisibility(View.GONE);
      } else {
        attribute2Label.setText(data.getAttribute2Label());
        attribute2Value.setText(data.getAttribute2Value());
      }

      final Map<Long, FailedItem> failedEvents = getFailedEvents();
      List<Event> events = DataValueController.getEventsByEnrollment(data.getEnrollment().localId);
      Log.d(CLASS_TAG, "num failed events: " + failedEvents.size());
      boolean generateNextVisit = false;

      for (ProgramStageRow row : data.getProgramStageRows()) {
        if (row instanceof ProgramStageLabelRow) {
          ProgramStageLabelRow stageRow = (ProgramStageLabelRow) row;
          if (stageRow.getProgramStage().getRepeatable()) {
            stageRow.setButtonListener(this);
          }

          if (generateNextVisit) {
            int stageCount = 0;

            if (stageRow.getEventRows() != null) {

              for (ProgramStageEventRow eventRow : stageRow.getEventRows()) {
                stageCount++;
              }
            }
            if (stageCount < 1
                || stageRow
                    .getProgramStage()
                    .getRepeatable()) // should only be able to add more stages if stage is
                                      // repeatable
            stageRow.setButtonListener(this);

            generateNextVisit = false;
          }

          if (stageRow.getProgramStage().getAllowGenerateNextVisit()) {
            if (stageRow.getEventRows() != null) {
              for (ProgramStageEventRow eventRow : stageRow.getEventRows()) {
                if (eventRow.getEvent().getStatus().equals(Event.STATUS_COMPLETED))
                  generateNextVisit = true;
              }
            }
          }

        } else if (row instanceof ProgramStageEventRow) {
          final ProgramStageEventRow eventRow = (ProgramStageEventRow) row;

          if (failedEvents.containsKey(eventRow.getEvent().getLocalId())) {
            eventRow.setHasFailed(true);
            eventRow.setMessage(
                failedEvents.get(eventRow.getEvent().getLocalId()).getErrorMessage());
          } else if (eventRow.getEvent().getFromServer()) {
            eventRow.setSynchronized(true);
            eventRow.setMessage(getString(R.string.status_sent_description));
          } else if (!(eventRow.getEvent().getFromServer())) {
            eventRow.setSynchronized(false);
            eventRow.setMessage(getString(R.string.status_offline_description));
          }
        }
      }

      adapter.swapData(data.getProgramStageRows());
    }
  }