diff --git a/insert-this-into-your-store-or-none-of-this-shit-will-work.txt b/insert-this-into-your-store-or-none-of-this-shit-will-work.txt index 3ec2d63..c17cb6b 100644 --- a/insert-this-into-your-store-or-none-of-this-shit-will-work.txt +++ b/insert-this-into-your-store-or-none-of-this-shit-will-work.txt @@ -1,5 +1,6 @@ prefix mu: prefix foaf: +prefix graphs: insert { @@ -16,6 +17,7 @@ a . "personal"; a . + "GRAPH1"; "Graph 1"; "hive"; diff --git a/src/main/java/scopes_service/query_pre_processor/Scopes/Scope.java b/src/main/java/scopes_service/query_pre_processor/Scopes/Scope.java index 76811a1..ec05021 100644 --- a/src/main/java/scopes_service/query_pre_processor/Scopes/Scope.java +++ b/src/main/java/scopes_service/query_pre_processor/Scopes/Scope.java @@ -94,9 +94,9 @@ public String calculateGraphToQuery() } } - String clearMetaInfoQuery = "WITH \n DELETE\n{\n"; - clearMetaInfoQuery += "?user ?uuid .\n"; - clearMetaInfoQuery += "?uuid ?gname .\n}"; + String clearMetaInfoQuery = "WITH \n DELETE\n{\n"; + clearMetaInfoQuery += "?user ?uuid .\n"; + clearMetaInfoQuery += "?uuid ?gname .\n}"; clearMetaInfoQuery += "WHERE\n{\n?user \"" + this.name + "\" .\n}"; try { SPARQLService.getInstance().postSPARQLResponse(SPARQLService.getLocalURL(), clearMetaInfoQuery); @@ -105,9 +105,9 @@ public String calculateGraphToQuery() } String UUID = java.util.UUID.randomUUID().toString(); - String setMetaInfoQuery = "WITH \n INSERT\n{\n"; - setMetaInfoQuery += "?user .\n"; - setMetaInfoQuery += " \"" + instanceGraph + "\".\n}"; + String setMetaInfoQuery = "WITH \n INSERT\n{\n"; + setMetaInfoQuery += "?user .\n"; + setMetaInfoQuery += " \"" + instanceGraph + "\".\n}"; setMetaInfoQuery += "WHERE\n{\n?user \"" + this.name + "\" .\n}"; try { SPARQLService.getInstance().postSPARQLResponse(SPARQLService.getLocalURL(), setMetaInfoQuery); diff --git a/src/main/java/scopes_service/query_pre_processor/Scopes/ScopeNode.java b/src/main/java/scopes_service/query_pre_processor/Scopes/ScopeNode.java index 49c02ca..c26efc2 100644 --- a/src/main/java/scopes_service/query_pre_processor/Scopes/ScopeNode.java +++ b/src/main/java/scopes_service/query_pre_processor/Scopes/ScopeNode.java @@ -1,5 +1,6 @@ package scopes_service.query_pre_processor.Scopes; +import com.tenforce.semtech.SPARQLParser.SPARQL.SPARQLQuery; import org.openrdf.model.vocabulary.SP; import scopes_service.query_pre_processor.query.SPARQLService; @@ -14,7 +15,7 @@ public class ScopeNode { private String name; private String UUID = java.util.UUID.randomUUID().toString(); - private String query = "select * from { ?s ?p ?o .}"; + private String query = ""; private ScopeNode parent; private int scopeNodeType; private List children = new ArrayList(); @@ -103,6 +104,19 @@ public String calculateScopes() } } + // performing the scope's query, if it exists + if(this.query != null && !this.query.isEmpty()) + { + try { + SPARQLQuery query = new SPARQLQuery(this.query); + query.setGraph(instanceGraph); + SPARQLService.getInstance().postSPARQLResponse(SPARQLService.getLocalURL(), query.constructQuery()); + } catch(Exception e) + { + e.printStackTrace(); + } + } + // adding my own inserts String pullInInsertsQuery = "INSERT\n{\n GRAPH <" + instanceGraph + ">\n {\n ?s ?p ?o .\n }\n}"; pullInInsertsQuery += "WHERE\n{\n GRAPH <" + getInsertName(this.getUUID()) + ">\n {\n ?s ?p ?o.\n }\n}"; diff --git a/src/main/java/scopes_service/query_pre_processor/query/SPARQLService.java b/src/main/java/scopes_service/query_pre_processor/query/SPARQLService.java index 81e32d0..f5f3cd2 100644 --- a/src/main/java/scopes_service/query_pre_processor/query/SPARQLService.java +++ b/src/main/java/scopes_service/query_pre_processor/query/SPARQLService.java @@ -353,6 +353,6 @@ public static String getLocalURL() if(System.getenv("SPARQLENDPOINT") != null && !System.getenv("SPARQLENDPOINT").isEmpty()) return System.getenv("SPARQLENDPOINT"); else - return "http://localhost/sparql"; + return "http://localhost:8890/sparql"; } } \ No newline at end of file diff --git a/src/main/java/scopes_service/query_pre_processor/web/RootController.java b/src/main/java/scopes_service/query_pre_processor/web/RootController.java index bdfa844..e468b5b 100644 --- a/src/main/java/scopes_service/query_pre_processor/web/RootController.java +++ b/src/main/java/scopes_service/query_pre_processor/web/RootController.java @@ -6,14 +6,11 @@ import scopes_service.query_pre_processor.Scopes.Scope; import scopes_service.query_pre_processor.Scopes.ScopeNode; import scopes_service.query_pre_processor.callback.CallBackService; -import scopes_service.query_pre_processor.callback.CallBackSetNotFoundException; -import scopes_service.query_pre_processor.query.DifferenceTriples; import scopes_service.query_pre_processor.query.QueryService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; -import scopes_service.query_pre_processor.query.Triple; - +import scopes_service.query_pre_processor.query.SPARQLService; import javax.annotation.PostConstruct; import javax.inject.Inject; import javax.servlet.http.HttpServletRequest; @@ -49,6 +46,208 @@ public void init() this.localSPARQLURL = System.getenv("SPARQLENDPOINT"); } + @RequestMapping(value="/users", method = RequestMethod.GET) + public ResponseEntity getGraphUserSettings() + { + Map graphUuids = new HashMap(); + List userUuids = new ArrayList(); + + try + { + String graphsJSON = SPARQLService.getInstance().getSPARQLResponse(SPARQLService.getLocalURL() + + "?query=" + URLEncoder.encode("SELECT ?uuid ?name FROM WHERE {" + + "?uri a . ?uri ?uuid ." + + "?uri ?name .}", "UTF-8")); + + + ObjectMapper mapper = new ObjectMapper(); + Map jsonMap = mapper.readValue(graphsJSON, Map.class); + Map nodeMap = new HashMap(); + + List l = ((List>) ((Map) jsonMap.get("results")).get("bindings")); + + for (Object tripleMap : l) { + Map cTripleMap = (Map) tripleMap; + + Map sMap = (Map) cTripleMap.get("uuid"); + Map nMap = (Map) cTripleMap.get("name"); + graphUuids.put((String) sMap.get("value"), (String) nMap.get("value")); + } + + }catch (Exception e) + { + e.printStackTrace(); + } + + try + { + String graphsJSON = SPARQLService.getInstance().getSPARQLResponse(SPARQLService.getLocalURL() + + "?query=" + URLEncoder.encode("SELECT ?uuid FROM WHERE {" + + "?uri a . ?uri ?uuid .}", "UTF-8")); + + + ObjectMapper mapper = new ObjectMapper(); + Map jsonMap = mapper.readValue(graphsJSON, Map.class); + Map nodeMap = new HashMap(); + + List l = ((List>) ((Map) jsonMap.get("results")).get("bindings")); + + for (Object tripleMap : l) { + Map cTripleMap = (Map) tripleMap; + + Map sMap = (Map) cTripleMap.get("uuid"); + userUuids.add((String) sMap.get("value")); + } + + }catch (Exception e) + { + e.printStackTrace(); + } + + String r = "{\"data\":["; + + for(String userUUID : userUuids) + { + r += "{\"user\":\"" + userUUID + "\", \"graphs\":["; + for(String graphUUID : graphUuids.keySet()) + { + + r += "{\"graph\":\"" + graphUUID + "\", \"name\":\"" + graphUuids.get(graphUUID) + "\", \"access\":\""; + boolean hasAccess = false; + String query = "prefix mu:\n" + + "prefix foaf:\n" + + "prefix graphs:\n" + + "\n" + + "ask\n" + + "{\n" + + "graph \n" + + "{\n" + + "?user graphs:hasAccessToGraph ?graph .\n" + + "?user mu:uuid \"" + userUUID + "\" .\n" + + "?graph mu:uuid \"" + graphUUID + "\" .\n" + + "}\n" + + "}"; + try { + String response = SPARQLService.getInstance().postSPARQLResponse(SPARQLService.getLocalURL(), query); + if(response.toLowerCase().equals("true")) + { + hasAccess = true; + } + }catch(Exception e) + { + e.printStackTrace(); + } + if(hasAccess) + { + r += "\"true\""; + } + else + { + r += "\"false\""; + } + r += "},"; + } + if(graphUuids.keySet().size() > 0) + r = r.substring(0, r.length() - 1); + r += "]},"; + } + if(userUuids.size() > 0) + r = r.substring(0, r.length() - 1); + + r += "]}"; + + return new ResponseEntity(r, HttpStatus.OK); + } + + @RequestMapping(value="/graphs", method = RequestMethod.POST) + public ResponseEntity addGraph(HttpServletRequest request, HttpServletResponse response, @RequestBody String body) + { + try { + ObjectMapper mapper = new ObjectMapper(); + Map jsonMap = mapper.readValue(body, Map.class); + Map nodeMap = new HashMap(); + + Map cTripleMap = (Map) ((Map) jsonMap.get("data")).get("attributes"); + + String name = (String) cTripleMap.get("name"); + String type = (String) cTripleMap.get("type"); + + String uuid = UUID.randomUUID().toString(); + String uri = "http://mu.semte.ch/vocabularies/graphs/Graph/" + uuid; + + String insertQuery = "prefix mu:\n" + + "prefix foaf:\n" + + "prefix graphs:\n" + + "with \n" + + "insert\n" + + "{\n" + + "<" + uri + "> mu:uuid \"" + uuid + "\".\n" + + "<" + uri + "> a graphs:Graph.\n" + + "<" + uri + "> graphs:graphType \"" + type + "\".\n" + + "<" + uri + "> foaf:name \"" + name + "\"\n" + + "}"; + + SPARQLService.getInstance().postSPARQLResponse(SPARQLService.getLocalURL(), insertQuery); + + }catch(Exception e) + { + e.printStackTrace(); + } + + return new ResponseEntity("", HttpStatus.ACCEPTED); + } + + @RequestMapping(value="/graphs", method = RequestMethod.GET) + public ResponseEntity getGraphs() + { + String getAllGraphsQuery = "prefix mu:\n" + + "prefix foaf:\n" + + "prefix graphs:\n" + + "select ?uuid ?name ?type\n" + + "from \n" + + "where\n" + + "{\n" + + "?uri mu:uuid ?uuid .\n" + + "?uri foaf:name ?name .\n" + + "?uri graphs:graphType ?type .\n" + + "?uri a graphs:Graph .\n" + + "}\n"; + + String r = "{\"data\":["; + + try { + String jsonString = this.queryService.sparqlService.getSPARQLResponse(this.localSPARQLURL + "?query=" + URLEncoder.encode(getAllGraphsQuery, "UTF-8")); + + ObjectMapper mapper = new ObjectMapper(); + Map jsonMap = mapper.readValue(jsonString, Map.class); + Map nodeMap = new HashMap(); + + List l = ((List>) ((Map) jsonMap.get("results")).get("bindings")); + + for (Object tripleMap : l) { + Map cTripleMap = (Map) tripleMap; + + Map sMap = (Map) cTripleMap.get("uuid"); + String uuid = (String) sMap.get("value"); + + Map nMap = (Map) cTripleMap.get("name"); + String name = (String) nMap.get("value"); + + Map tMap = (Map) cTripleMap.get("type"); + String type = (String) tMap.get("value"); + r += "{\"type\":\"graph\", \"id\":\"" + uuid + "\", \"attributes\":{"; + r += "\"name\":\"" + name + "\", \"type\":\"" + type + "\"}}"; + } + }catch(Exception e) + { + e.printStackTrace(); + } + + r += "]}"; + + return new ResponseEntity(r, HttpStatus.OK); + } + @RequestMapping(value="/ping") public ResponseEntity ping(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) String body) { @@ -98,9 +297,9 @@ public ResponseEntity preProcessQuery(HttpServletRequest request, HttpSe Enumeration hn = request.getHeaderNames(); while(hn.hasMoreElements()) { - if(hn.nextElement().equals("user-uuid")) + if(hn.nextElement().equals("MU-SESSION-ID")) { - userUUID = request.getHeader("user-uuid"); + userUUID = this.getUserUUIDFromSession(request.getHeader("MU-SESSION-ID")); } } @@ -132,10 +331,10 @@ public ResponseEntity preProcessQuery(HttpServletRequest request, HttpSe private String getGraphName(String userUUID) { - String askGraphInstanceName = "WITH SELECT ?instance WHERE { "; + String askGraphInstanceName = "WITH SELECT ?instance WHERE { "; askGraphInstanceName += "?user \"" + userUUID + "\" .\n"; - askGraphInstanceName += "?user ?instanceuri .\n"; - askGraphInstanceName += "?instanceuri ?instance .\n}"; + askGraphInstanceName += "?user ?instanceuri .\n"; + askGraphInstanceName += "?instanceuri ?instance .\n}"; try { String jsonString = this.queryService.sparqlService.getSPARQLResponse(this.localSPARQLURL + "?query=" + URLEncoder.encode(askGraphInstanceName, "UTF-8")); @@ -169,13 +368,46 @@ private String getGraphName(String userUUID) } } + private String getUserUUIDFromSession(String sessionID) + { + String getuseruuid = "\n" + + "select ?user-uuid\n" + + "from \n" + + "where\n" + + "{\n" + + "<" + sessionID + "> ?user-uri .\n" + + "?user-uri mu:uuid ?user-uuid .\n" + + "}"; + + try { + String jsonString = this.queryService.sparqlService.getSPARQLResponse(this.localSPARQLURL + "?query=" + URLEncoder.encode(getuseruuid, "UTF-8")); + ObjectMapper mapper = new ObjectMapper(); + Map jsonMap = mapper.readValue(jsonString, Map.class); + Map nodeMap = new HashMap(); + + List l = ((List>) ((Map) jsonMap.get("results")).get("bindings")); + + for (Object tripleMap : l) { + Map cTripleMap = (Map) tripleMap; + + Map sMap = (Map) cTripleMap.get("user-uuid"); + return (String) sMap.get("value"); + } + }catch (Exception e) + { + e.printStackTrace(); + } + return ""; + } + private Scope buildScopeForUser(String userUUID) { - String getNodeInfo = "WITH SELECT ?node ?graphName ?graphType WHERE { ?user \"" + userUUID; - getNodeInfo += "\" .\n?user ?node .\n"; - getNodeInfo += "?node ?graph .\n"; - getNodeInfo += "?graph ?graphName .\n"; - getNodeInfo += "?graph ?graphType .\n}"; + String getNodeInfo = "WITH SELECT ?node ?graphName ?graphType ?graphQuery WHERE { ?user \"" + userUUID; + getNodeInfo += "\" .\n?user ?node .\n"; + getNodeInfo += "?node ?graph .\n"; + getNodeInfo += "?graph ?graphName .\n"; + getNodeInfo += "?graph ?graphType .\n"; + getNodeInfo += "?graph ?graphQuery .\n}"; Scope scope = new Scope(userUUID); @@ -213,8 +445,8 @@ private Scope buildScopeForUser(String userUUID) } for(String node : nodeMap.keySet()) { - String getNodeParent = "WITH SELECT ?parent WHERE { "; - getNodeParent += "<" + node + "> ?parent .\n}"; + String getNodeParent = "WITH SELECT ?parent WHERE { "; + getNodeParent += "<" + node + "> ?parent .\n}"; String jsonStringP = this.queryService.sparqlService.getSPARQLResponse(this.localSPARQLURL + "?query=" + URLEncoder.encode(getNodeParent, "UTF-8")); ObjectMapper mapperP = new ObjectMapper(); @@ -241,30 +473,4 @@ private Scope buildScopeForUser(String userUUID) } -// @RequestMapping(value = "/wtf") -// public ResponseEntity preTESTWTF(HttpServletRequest request, HttpServletResponse response, @RequestBody(required = false) String body) throws InvalidSPARQLException { -// this.buildScopeForUser("JONATHANUUID"); -// this.getGraphName("JONATHANUUID"); -// -// -// Scope scope = new Scope("test"); -// ScopeNode scopeNode1 = new ScopeNode(); -// scopeNode1.setUUID("1"); -// scopeNode1.setScopeNodeType(1); -// ScopeNode scopeNode2 = new ScopeNode(); -// scopeNode2.setUUID("2"); -// scopeNode2.setParent(scopeNode1); -// scopeNode2.setScopeNodeType(4); -// ScopeNode scopeNode3 = new ScopeNode(); -// scopeNode3.setUUID("3"); -// scopeNode3.setScopeNodeType(4); -// scope.getNodes().add(scopeNode1); -// scope.getNodes().add(scopeNode2); -// scope.getNodes().add(scopeNode3); -// -// return new ResponseEntity(scope.calculateGraphToQuery(), HttpStatus.OK); -// } - - - } diff --git a/target/SCOPES-ENDPOINT-1.0-SNAPSHOT.war b/target/SCOPES-ENDPOINT-1.0-SNAPSHOT.war index 61b4787..3eac115 100644 Binary files a/target/SCOPES-ENDPOINT-1.0-SNAPSHOT.war and b/target/SCOPES-ENDPOINT-1.0-SNAPSHOT.war differ