/**
  * This method creates map of latest version of ontology with contexts as key. It uses virtual
  * ontology ids associated with contexts.
  *
  * @param structure {@code Structure} for given resource
  * @return {@code HashMap} of latest local ontology id with context as key.
  */
 private HashMap<String, String> createLocalOntologyIDMap(Structure structure) {
   HashMap<String, String> localOntologyIDMap = new HashMap<String, String>();
   String virtualOntologyID;
   for (String contextName : structure.getOntoIds().keySet()) {
     virtualOntologyID = structure.getOntoIds().get(contextName);
     if (!virtualOntologyID.equals(Structure.FOR_CONCEPT_RECOGNITION)
         && !virtualOntologyID.equals(Structure.NOT_FOR_ANNOTATION)) {
       localOntologyIDMap.put(
           contextName, ontlogyService.getLatestLocalOntologyID(virtualOntologyID));
     }
   }
   return localOntologyIDMap;
 }
  /**
   * This method is used to get all elements from resource site.
   *
   * @return HashSet<Element>
   */
  private HashSet<Element> getAllElements() {
    logger.info("* Get All Elements for NIF CTD ChemDisease ... ");
    HashSet<Element> elementSet = new HashSet<Element>();
    int nbAdded;
    int offset = 0;
    int totalCount = 0;

    try {
      // get all elements from _ET table
      HashSet<String> allElementsInET = this.resourceUpdateService.getAllLocalElementIDs();

      Map<String, Map<String, String>> allRowsData = new HashMap<String, Map<String, String>>();
      // int rowcnt = 1;

      // parsing data
      do {
        Document dom = queryFederation(nifId, query, offset);
        if (dom != null) {
          Node tableData = dom.getFirstChild().getChildNodes().item(1);
          // get total records
          totalCount =
              Integer.parseInt(tableData.getAttributes().getNamedItem(resultCount).getNodeValue());
          offset += rowCount;

          Node results = tableData.getChildNodes().item(1);

          // Iterate over the returned structure
          NodeList rows = results.getChildNodes();
          for (int i = 0; i < rows.getLength(); i++) {
            String localElementId = EMPTY_STRING;
            Map<String, String> elementAttributes = new HashMap<String, String>();

            Node row = rows.item(i);
            for (int j = 0; j < row.getChildNodes().getLength(); j++) {
              NodeList vals = row.getChildNodes().item(j).getChildNodes();
              String name = null;
              String value = null;
              for (int k = 0; k < vals.getLength(); k++) {
                if (nodeName.equals(vals.item(k).getNodeName())) {
                  name = vals.item(k).getTextContent();
                } else if (nodeValue.equals(vals.item(k).getNodeName())) {
                  value = vals.item(k).getTextContent();
                }
              }
              if (name.equalsIgnoreCase(Disease)) {
                localElementId =
                    value.substring(
                        value.indexOf(ELT_URL) + ELT_URL.length(), value.indexOf(endTag));
                elementAttributes.put(
                    Structure.generateContextName(RESOURCEID, ITEMKEYS[0]),
                    Jsoup.parse(value).text());
              } else if (name.equalsIgnoreCase(ChemicalName)) {
                elementAttributes.put(
                    Structure.generateContextName(RESOURCEID, ITEMKEYS[1]),
                    Jsoup.parse(value).text());
              } else if (name.equalsIgnoreCase(Inference_Gene_Symbol)) {
                elementAttributes.put(
                    Structure.generateContextName(RESOURCEID, ITEMKEYS[2]), value);
              } else if (name.equalsIgnoreCase(Inference_Score)) {
                elementAttributes.put(
                    Structure.generateContextName(RESOURCEID, ITEMKEYS[3]), value);
              } else if (name.equalsIgnoreCase(Direct_Evidence)) {
                elementAttributes.put(
                    Structure.generateContextName(RESOURCEID, ITEMKEYS[4]), value);
              } else if (name.equalsIgnoreCase(OMIM_IDs)) {
                elementAttributes.put(
                    Structure.generateContextName(RESOURCEID, ITEMKEYS[5]),
                    Jsoup.parse(value).text());
              } else if (name.equalsIgnoreCase(References)) {
                elementAttributes.put(
                    Structure.generateContextName(RESOURCEID, ITEMKEYS[6]),
                    Jsoup.parse(value).text());
              }
            }

            // Check if elementId is present in database.
            if (allElementsInET.contains(localElementId)) {
            } else {
              //                            localElementId += SLASH_STRING + rowcnt;
              //                            rowcnt++;
              allRowsData.put(localElementId, elementAttributes);
            }
          }
        } else {
          offset += rowCount;
        }
      } while (offset < totalCount);

      // parsing ends

      // Second phase: creation of elements
      for (String localElementID : allRowsData.keySet()) {
        Map<String, String> elementAttributes;
        elementAttributes = allRowsData.get(localElementID);

        // PUT DATA INTO A STRUCTURE++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        Structure elementStructure = new Structure(STRUCTURE.getContextNames());
        for (String contextName : STRUCTURE.getContextNames()) {
          boolean attributeHasValue = false;

          for (String att : elementAttributes.keySet()) {
            if (contextName.equals(att)) {
              // not an existing annotation
              if (STRUCTURE.getOntoID(contextName).equals(Structure.FOR_CONCEPT_RECOGNITION)
                  || STRUCTURE.getOntoID(contextName).equals(Structure.NOT_FOR_ANNOTATION)) {
                elementStructure.putContext(contextName, elementAttributes.get(att));
                attributeHasValue = true;
              }
            }
          }

          // to avoid null value in the structure
          if (!attributeHasValue) {
            elementStructure.putContext(contextName, EMPTY_STRING);
          }
        }
        // put the element structure in a new element
        try {
          Element exp = new Element(localElementID, elementStructure);
          elementSet.add(exp);
        } catch (Element.BadElementStructureException e) {
          logger.error(EMPTY_STRING, e);
        }
      }

    } catch (Exception e) {
      logger.error("** PROBLEM ** Problem in getting rows.", e);
    }
    nbAdded = elementSet.size();
    logger.info((nbAdded) + " rows found.");
    return elementSet;
  }