/*
   *get collaboration information based on organisation - returns a collaborationList.
   */
  public HashMap<Integer, CollaborationList> getCollaborations_org(
      String id, TreeMap<Integer, Collaborator> network) {
    HashMap<Integer, CollaborationList> collaborations =
        new java.util.HashMap<Integer, CollaborationList>();

    // reset the iterator
    Set<Integer> networkKeys = network.keySet();
    Iterator<Integer> networkKeyIterator = networkKeys.iterator();
    Integer networkKey = null;

    CollaborationList collaborationList;

    // loop through the list of keys
    while (networkKeyIterator.hasNext()) {

      // get the key
      networkKey = (Integer) networkKeyIterator.next();

      // get the list of collaborations for this user
      collaborationList = getCollaborationList_org(id, networkKey.toString());

      // add the collaborationList object to the list
      collaborations.put(networkKey, collaborationList);
    }

    return collaborations;
  }
  /**
   * Returns the string value of the first of the properties in the uriDescriptionList for the given
   * resource (as an URI). In case the resource does not have any of the properties mentioned, its
   * URI is returned. The value is obtained by querying the endpoint and the endpoint is queried
   * repeatedly until it gives a response (value or the lack of it)
   *
   * <p>It is highly recommended that the list contains properties like labels or titles, with test
   * values.
   *
   * @param uri - the URI for which a label is required
   * @return a String value, either a label for the parameter or its value if no label is obtained
   *     from the endpoint
   */
  private String getLabelForUri(String uri) {
    String result;

    if (uriLabelCache.containsKey(uri)) {
      return uriLabelCache.get(uri);
    }

    for (String prop : uriDescriptionList) {
      String innerQuery = "SELECT ?r WHERE {<" + uri + "> <" + prop + "> ?r } LIMIT 1";

      try {
        Query query = QueryFactory.create(innerQuery);
        QueryExecution qExec = QueryExecutionFactory.sparqlService(rdfEndpoint, query);
        boolean keepTrying = true;
        while (keepTrying) {
          keepTrying = false;
          try {
            ResultSet results = qExec.execSelect();

            if (results.hasNext()) {
              QuerySolution sol = results.nextSolution();
              result = EEASettings.parseForJson(sol.getLiteral("r").getLexicalForm());
              if (!result.isEmpty()) {
                uriLabelCache.put(uri, result);
                return result;
              }
            }
          } catch (Exception e) {
            keepTrying = true;
            logger.warn("Could not get label for uri {}. Retrying.", uri);
          } finally {
            qExec.close();
          }
        }
      } catch (QueryParseException qpe) {
        logger.error("Exception for query {}. The label cannot be obtained", innerQuery);
      }
    }
    return uri;
  }
  @SuppressWarnings("unchecked")
  public Document toGraphMLDOM_org(Document egoDom, String id, int radius, String graphType) {

    Collaborator collaborator = null;

    if (db.connect() == false) {
      throw new RuntimeException("Unable to connect to the database");
    }

    // add the graph element
    Element rootElement = egoDom.getDocumentElement();
    rootElement = createHeaderElements(egoDom, rootElement, graphType);

    Element graph = egoDom.createElement("graph");
    graph.setAttribute("id", "Contributor Network for organisation:" + " ( " + id + " )");
    graph.setAttribute("edgedefault", graphType);
    rootElement.appendChild(graph);

    network = getRawCollaboratorData_org(id, radius);

    if (network != null) {
      // add some additional required information for contributors
      collaborators = getCollaborators(network, "0");

      // get collaboration List
      collaborations = getCollaborations_org(id, network);
    } else return egoDom;

    // create node element in DOM
    for (int i = 0; i < collaborators.size(); i++) {
      Element node = createNodeElement(egoDom, i);
      graph.appendChild(node);
    }

    // we have a list of collaborations.
    // loop through the list, for each - create a collaboration
    //								  - create edge.

    // 1.iterate through the collaboration hashmap
    Iterator contributorIter = collaborations.values().iterator();
    int edgeIndex = 0;
    // collabCheck used to ensure no doubling up of collaborations. (pretty quick hack, ideally this
    // issue would be sorted before this point)
    Vector collabCheck = new Vector();

    while (contributorIter.hasNext()) {
      CollaborationList list = (CollaborationList) contributorIter.next();
      // get the actual collaborations
      if (list != null) {

        Iterator collaborationIter = list.getCollaborations().values().iterator();
        //	loop through the hashmap of collaborations
        while (collaborationIter.hasNext()) {

          Collaboration collaboration = (Collaboration) collaborationIter.next();

          if (!collabCheck.contains(
              collaboration.getPartner() + "-" + collaboration.getCollaborator())) {
            // create an edge for each of them
            Element edge =
                createEdgeElement(
                    egoDom,
                    collaboration,
                    Integer.parseInt(collaboration.getCollaborator()),
                    Integer.parseInt(collaboration.getPartner()),
                    edgeIndex);
            graph.appendChild(edge);
            //	add the collaboration to the list for checking.
            collabCheck.add(collaboration.getCollaborator() + "-" + collaboration.getPartner());
            edgeIndex++;
          }
        }
      }
    }

    return egoDom;
  }