private List<String> getRefTermAncestors(
      UiSessionContext context, String id, Locale locale, ConceptSource conceptSource)
      throws Exception {

    ConceptService conceptService = (ConceptService) Context.getService(ConceptService.class);

    ConceptManagementAppsService conceptManagementAppsService =
        (ConceptManagementAppsService) Context.getService(ConceptManagementAppsService.class);

    // there are no mappings so we need to send it through blank and let the user know
    if (StringUtils.isEmpty(id) || StringUtils.isBlank(id) || id.contains("empty")) {

      id = "0";
      Set<ConceptReferenceTerm> parentTerms = new HashSet<ConceptReferenceTerm>();
      Set<ConceptReferenceTerm> childTerms = new HashSet<ConceptReferenceTerm>();
      return formatByRefTermForUIWithGson(conceptService, id, childTerms, parentTerms, locale);
    }

    Set<ConceptReferenceTerm> parentTerms =
        conceptManagementAppsService.getRefTermParentReferenceTerms(
            conceptService.getConceptReferenceTerm(Integer.parseInt(id)), conceptSource);

    Set<ConceptReferenceTerm> childTerms =
        conceptManagementAppsService.getRefTermChildReferenceTerms(
            conceptService.getConceptReferenceTerm(Integer.parseInt(id)), conceptSource);

    return formatByRefTermForUIWithGson(conceptService, id, childTerms, parentTerms, locale);
  }
  private List<String> formatByRefTermForUIWithGson(
      ConceptService conceptService,
      String id,
      Set<ConceptReferenceTerm> childTerms,
      Set<ConceptReferenceTerm> parentTerms,
      Locale locale) {

    List<String> data = new ArrayList<String>();
    try {

      Gson gson = new Gson();

      ConceptReferenceTerm currentRefTerm =
          conceptService.getConceptReferenceTerm(Integer.parseInt(id));
      HashMap<ConceptReferenceTerm, List<Concept>> parentMappings =
          getAssociatedConceptsToRefTerms(parentTerms);
      HashMap<ConceptReferenceTerm, List<Concept>> childMappings =
          getAssociatedConceptsToRefTerms(childTerms);

      List<String> parents = new ArrayList<String>();
      for (ConceptReferenceTerm term : parentTerms) {

        List<Concept> mappedConcepts = parentMappings.get(term);
        List<DataObject> mappedConceptsDOList = new ArrayList<DataObject>();

        for (Concept concept : mappedConcepts) {

          DataObject mappedConceptDataObject = simplifyConcept(concept, locale);
          mappedConceptsDOList.add(mappedConceptDataObject);
        }
        DataObject refTermDataObject = simplifyReferenceTerm(term);
        parents.add(gson.toJson(simplifyMapping(mappedConceptsDOList, refTermDataObject)));
      }
      List<String> children = new ArrayList<String>();
      for (ConceptReferenceTerm term : childTerms) {

        List<Concept> mappedConcepts = childMappings.get(term);
        List<DataObject> mappedConceptsDOList = new ArrayList<DataObject>();

        for (Concept concept : mappedConcepts) {

          DataObject mappedConceptDataObject = simplifyConcept(concept, locale);
          mappedConceptsDOList.add(mappedConceptDataObject);
        }
        DataObject refTermDataObject = simplifyReferenceTerm(term);
        children.add(gson.toJson(simplifyMapping(mappedConceptsDOList, refTermDataObject)));
      }
      String currentTerm = gson.toJson(simplifyReferenceTerm(currentRefTerm));

      DataObject ancestorsDataObject = simplifyAncestors(parents, children, currentTerm);

      data.add(gson.toJson(ancestorsDataObject));

    } catch (Exception e) {
      log.error("Error generated", e);
    }
    return data;
  }