Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor out query execution logic #4

Merged
merged 1 commit into from
Oct 3, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading