Skip to content

Commit

Permalink
Refactor out query execution logic (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi authored Oct 3, 2024
1 parent 54a9c2a commit a646f01
Showing 1 changed file with 27 additions and 17 deletions.
44 changes: 27 additions & 17 deletions src/main/java/org/qubitpi/wilhelm/web/endpoints/DataServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -127,32 +127,42 @@ public Response getVocabularyByLanguagePaged(
LANGUAGES.get(language), (Integer.parseInt(page) - 1) * Integer.parseInt(perPage), perPage
);

return Response
.status(Response.Status.OK)
.entity(executeNativeQuery(query))
.build();
}

/**
* Runs a cypher query against Neo4J database and return the query result as a JSON-serializable object.
*
* @param query A standard cypher query string
*
* @return query result
*/
@NotNull
private Object executeNativeQuery(@NotNull final String query) {
try (Driver driver = GraphDatabase.driver(NEO4J_URL, AuthTokens.basic(NEO4J_USERNAME, NEO4J_PASSWORD))) {
driver.verifyConnectivity();

final EagerResult result = driver.executableQuery(query)
.withConfig(QueryConfig.builder().withDatabase(NEO4J_DATABASE).build())
.execute();

return Response
.status(Response.Status.OK)
.entity(
result
.records()
return result
.records()
.stream()
.map(
record -> record.keys()
.stream()
.map(
record -> record.keys()
.stream()
.map(key -> new AbstractMap.SimpleImmutableEntry<>(
key,
expand(record.get(key))
))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))

)
.collect(Collectors.toList())
.map(key -> new AbstractMap.SimpleImmutableEntry<>(
key,
expand(record.get(key))
))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue))

)
.build();
.collect(Collectors.toList());
}
}

Expand Down

0 comments on commit a646f01

Please sign in to comment.