var http = require('http');

var host = "sandbox.fusepool.info";

exports.allEvents = function () {
    return encodeURIComponent("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
    "PREFIX schema: <http://schema.org/> " +
    "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
    "PREFIX dbo: <http://it.dbpedia.org/ontology> " +
    "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> " +
    "CONSTRUCT {?subject rdfs:label ?headline} " +
    "FROM <http://sandbox.fusepool.info:8181/ldp/wr-ldpc/Trentino-Events-1/eventi-xml-xml-transformed> " +
    "WHERE { " +
    "?subject a schema:Event ; " +
    "schema:description ?title ; " +
    "rdfs:label ?headline;" +
    "schema:startDate ?dateStart ; " +
    "schema:endDate ?dateEnd . " +
    "FILTER( lang(?title)='it' ) " +
    "}");
}

exports.singleEventProperties = function (eventURI) {
    return encodeURIComponent("PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
        "PREFIX schema: <http://schema.org/> " +
        "PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> " +
        "PREFIX dbo: <http://it.dbpedia.org/ontology> " +
        "PREFIX skos: <http://www.w3.org/2004/02/skos/core#> " +
        "CONSTRUCT {<" + eventURI + "> ?property ?hasValue } " +
        "WHERE { <" + eventURI + "> ?property ?hasValue } ");
}

exports.closerPOI = function (lowLat, highLat, lowLong, highLong) { //For testing (46.05, 46.07, 11.12, 11.14)  
    return encodeURIComponent("PREFIX geo: <http://www.w3.org/2003/01/geo/wgs84_pos#> " +
        "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " +
        "PREFIX schema: <http://schema.org/> " +
        "PREFIX dct: <http://purl.org/dc/terms/> " +
        "CONSTRUCT { ?subject ?property ?object .} " +
        "FROM <http://sandbox.fusepool.info:8181/ldp/trentino-point-of-interest-ttl> " +
        "WHERE { " +
        "?subject ?property ?object ; " +
        "geo:lat ?lat ; " +
        "geo:long ?long . " +
        "FILTER (?lat >= '" + lowLat + "'^^xsd:double && ?lat <= '" + highLat + "'^^xsd:double && ?long >= '" + lowLong + "'^^xsd:double && ?long <= '" + highLong + "'^^xsd:double) " +
        "}");
}

exports.launchSparqlQuery = function (request, response, query) {

    var result = "";

    var options = {
        host: host,
        path: "/sparql/select?query=" + query() + "&output=json",
        port: "8181",
        method: "GET",
    };

    var req = http.request(options, function(res) {
        console.log('STATUS: ' + res.statusCode);
        console.log('HEADERS: ' + JSON.stringify(res.headers));
        res.setEncoding('utf8');
        res.on('data', function(chunk) {
            result += chunk;
        });

        res.on('end', function() {
            response.send(result);
        });
    });

    req.on('error', function(e) {
        console.log('problem with request: ' + e.message);
    });

    req.end();
}

@@ -173,32 +173,36 @@
 df-schema#%3E %22 +%0A
+    
     %22PREFIX sche
@@ -226,32 +226,36 @@
 a.org/%3E %22 +%0A    
+   
+ 
 %22PREFIX xsd: %3Cht
@@ -287,24 +287,28 @@
 chema#%3E %22 +%0A
+    
     %22PREFIX 
@@ -349,24 +349,28 @@
 gy%3E %22 +%0A    
+  
+  
 %22PREFIX skos
@@ -410,24 +410,28 @@
 /core#%3E %22 +%0A
+    
     %22CONSTRU
@@ -469,24 +469,28 @@
 ne%7D %22 +%0A    
+ 
+   
 %22FROM %3Chttp:
@@ -576,24 +576,28 @@
 formed%3E %22 +%0A
+    
     %22WHERE %7B
@@ -597,24 +597,28 @@
 WHERE %7B %22 +%0A
+    
     %22?subjec
@@ -636,32 +636,36 @@
 Event ; %22 +%0A    
+   
+ 
 %22schema:descript
@@ -677,24 +677,28 @@
 title ; %22 +%0A
+    
     %22rdfs:la
@@ -711,32 +711,36 @@
 eadline;%22 +%0A    
+  
+  
 %22schema:startDat
@@ -754,24 +754,28 @@
 Start ; %22 +%0A
+    
     %22schema:
@@ -797,24 +797,28 @@
 d . %22 +%0A    
+ 
+   
 %22FILTER( lan
@@ -834,24 +834,28 @@
 ='it' ) %22 +%0A
+    
     %22%7D%22);%0A%7D%0A
@@ -2378,25 +2378,8 @@
 ry()
- + %22&output=json%22
 ,%0A  
@@ -2421,16 +2421,80 @@
  %22GET%22,%0A
+        headers: %7B%0A            accept: %22text/turtle%22%0A        %7D %0A
     %7D;%0A%0A

Change header request to support construct queries