class SparqlTranslationProcessor {

  public static RecommendationQuery process(POFMetadata meta, ASTQueryContainer ast)
  throws MalformedQueryException, VisitorException {
    // replace any dataset URI (graph and FROM) by their second-level domain name
    final Dataset datasets = DatasetDeclProcessor.process(ast);
    final ASTIRI d;

    astQueryContainer = ast;
    if (datasets != null) {
      if (!datasets.getDefaultGraphs().isEmpty() || !datasets.getNamedGraphs().isEmpty()) {
        if (!datasets.getDefaultGraphs().isEmpty() && !datasets.getNamedGraphs().isEmpty()) {
          throw new DGSException("There can be only one default/named graph");
        }
        if (!datasets.getDefaultGraphs().isEmpty() && datasets.getDefaultGraphs().size() != 1) {
          throw new DGSException("There can be only one default/named graph");
        }
        if (!datasets.getNamedGraphs().isEmpty() && datasets.getNamedGraphs().size() != 1) {
          throw new DGSException("There can be only one default/named graph");
        }
      }
      if (!datasets.getDefaultGraphs().isEmpty() && datasets.getNamedGraphs().isEmpty()) {
        d = new ASTIRI(SyntaxTreeBuilderTreeConstants.JJTIRI);
        final String value = datasets.getDefaultGraphs().toArray(new URI[1])[0].stringValue();
        d.setValue(URIUtil.getSndDomainFromUrl(value));
      } else if (datasets.getDefaultGraphs().isEmpty() && !datasets.getNamedGraphs().isEmpty()) {
        d = new ASTIRI(SyntaxTreeBuilderTreeConstants.JJTIRI);
        final String value = datasets.getNamedGraphs().toArray(new URI[1])[0].stringValue();
        d.setValue(URIUtil.getSndDomainFromUrl(value));
      } else {
        d = null;
      }
    } else {
      d = null;
    }

    String filter = null;
    if (meta != null && meta.pofNode.getMetadata() != null) {
      final Object keyword = meta.pofNode.getMetadata(SyntaxTreeBuilder.Keyword);
      final Object prefix = meta.pofNode.getMetadata(SyntaxTreeBuilder.Prefix);
      final Object qname = meta.pofNode.getMetadata(SyntaxTreeBuilder.Qname);

      if (keyword != null) {
        filter = "regex(str(?" + SyntaxTreeBuilder.PointOfFocus + "), \"" + keyword + "\", \"i\")";
      } else if (prefix != null) {
        filter = "regex(str(?" + SyntaxTreeBuilder.PointOfFocus + "), \"^" + prefix + "\", \"i\")";
      } else if (qname != null) {
        filter = "regex(str(?" + SyntaxTreeBuilder.PointOfFocus + "), \"^" + qname + "\", \"i\")";
      }
    }

    final RecommendationQuery rq = new RecommendationQuery(DataGraphSummaryVocab.GRAPH_SUMMARY_GRAPH, filter);
    final SparqlTranslationVisitor v = new SparqlTranslationVisitor(rq, d);

    v.visit(ast, null);
    // Change variable name of the POF ressource
    changeToPofResource(rq, v.pofResourceName);
    return rq;
  }

  private  SparqlTranslationProcessor();

}