/**
   * Builds a String result for Elastic Search from an RDFNode
   *
   * @param node An RDFNode representing the value of a property for a given resource
   * @return If the RDFNode has a Literal value, among Boolean, Byte, Double, Float, Integer Long,
   *     Short, this value is returned, converted to String
   *     <p>If the RDFNode has a String Literal value, this value will be returned, surrounded by
   *     double quotes
   *     <p>If the RDFNode has a Resource value (URI) and toDescribeURIs is set to true, the value
   *     of @getLabelForUri for the resource is returned, surrounded by double quotes. Otherwise,
   *     the URI will be returned
   */
  private String getStringForResult(RDFNode node, boolean getNodeLabel) {
    String result = "";
    boolean quote = false;

    if (node.isLiteral()) {
      Object literalValue = node.asLiteral().getValue();
      try {
        Class<?> literalJavaClass = node.asLiteral().getDatatype().getJavaClass();

        if (literalJavaClass.equals(Boolean.class)
            || Number.class.isAssignableFrom(literalJavaClass)) {

          result += literalValue;
        } else {
          result = EEASettings.parseForJson(node.asLiteral().getLexicalForm());
          quote = true;
        }
      } catch (java.lang.NullPointerException npe) {
        result = EEASettings.parseForJson(node.asLiteral().getLexicalForm());
        quote = true;
      }

    } else if (node.isResource()) {
      result = node.asResource().getURI();
      if (getNodeLabel) {
        result = getLabelForUri(result);
      }
      quote = true;
    }
    if (quote) {
      result = "\"" + result + "\"";
    }
    return result;
  }
  /**
   * 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;
  }