Skip to content

Commit

Permalink
Centralize 'label' field name
Browse files Browse the repository at this point in the history
  • Loading branch information
QubitPi committed Nov 15, 2024
1 parent 5863928 commit 1e41ade
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 15 deletions.
16 changes: 11 additions & 5 deletions src/main/java/org/qubitpi/wilhelm/Link.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@
@JsonIncludeProperties({ "label", "sourceNodeId", "targetNodeId", "attributes" })
public class Link {

/**
* The database node attribute name whose value is used for displaying the relationship caption on UI.
* <p>
* For example, for Neo4J database, this would correspond to a relationship property.
*/
public static final String LABEL_ATTRIBUTE = "label";

private static final Logger LOG = LoggerFactory.getLogger(Link.class);

private final String label;
Expand Down Expand Up @@ -93,18 +100,17 @@ private Link(
* @throws IllegalStateException if {@code relationship} is missing a "name" property
*/
public static Link valueOf(final Relationship relationship) {
final String labelKey = "name";
if (!Objects.requireNonNull(relationship).asMap().containsKey(labelKey)) {
LOG.error("Neo4J relationship does not contain '{}' attribute: {}", labelKey, relationship.asMap());
if (!Objects.requireNonNull(relationship).asMap().containsKey(LABEL_ATTRIBUTE)) {
LOG.error("Neo4J relationship does not contain '{}' attribute: {}", LABEL_ATTRIBUTE, relationship.asMap());
throw new IllegalStateException(
"There seems to be a data format mismatch between Wilhelm webservice and Neo4J database. " +
"Please file an issue at https://github.com/QubitPi/wilhelm-ws/issues for a fix"
);
}

final String label = relationship.asMap().get(labelKey).toString();
final String label = relationship.asMap().get(LABEL_ATTRIBUTE).toString();
final Map<String, Object> attributes = relationship.asMap().entrySet().stream()
.filter(entry -> !labelKey.equals(entry.getKey()))
.filter(entry -> !LABEL_ATTRIBUTE.equals(entry.getKey()))
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));

return new Link(label, relationship.startNodeElementId(), relationship.endNodeElementId(), attributes);
Expand Down
16 changes: 11 additions & 5 deletions src/main/java/org/qubitpi/wilhelm/Node.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@
@JsonIncludeProperties({ "id", "label", "attributes" })
public class Node {

/**
* The database node attribute name whose value is used for displaying the node caption on UI.
* <p>
* For example, for Neo4J database, this would correspond to a node property.
*/
public static final String LABEL_ATTRIBUTE = "label";

private static final Logger LOG = LoggerFactory.getLogger(Node.class);

private final String id;
Expand Down Expand Up @@ -82,18 +89,17 @@ private Node(@NotNull final String id, @NotNull final String label, @NotNull fin
* @throws IllegalStateException if {@code node} is missing a "name" property
*/
public static Node valueOf(@NotNull final org.neo4j.driver.types.Node node) {
final String labelKey = "name";
if (!Objects.requireNonNull(node).asMap().containsKey(labelKey)) {
LOG.error("Neo4J node does not contain '{}' attribute: {}", labelKey, node.asMap());
if (!Objects.requireNonNull(node).asMap().containsKey(LABEL_ATTRIBUTE)) {
LOG.error("Neo4J node does not contain '{}' attribute: {}", LABEL_ATTRIBUTE, node.asMap());
throw new IllegalStateException(
"There seems to be a data format mismatch between Wilhelm webservice and Neo4J database. " +
"Please file an issue at https://github.com/QubitPi/wilhelm-ws/issues for a fix"
);
}

final String label = node.asMap().get(labelKey).toString();
final String label = node.asMap().get(LABEL_ATTRIBUTE).toString();
final Map<String, Object> attributes = node.asMap().entrySet().stream()
.filter(entry -> !labelKey.equals(entry.getKey()))
.filter(entry -> !LABEL_ATTRIBUTE.equals(entry.getKey()))
.collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue));

return new Node(node.elementId(), label, attributes);
Expand Down
17 changes: 12 additions & 5 deletions src/main/java/org/qubitpi/wilhelm/web/endpoints/Neo4JServlet.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ public Response getVocabularyByLanguagePaged(

final String query = String.format(
"MATCH (t:Term WHERE t.language = '%s')-[r]->(d:Definition) " +
"RETURN t.label AS term, d.label AS definition " +
"RETURN t.%s AS term, d.%s AS definition " +
"SKIP %s LIMIT %s",
requestedLanguage.getDatabaseName(), (Integer.parseInt(page) - 1) * Integer.parseInt(perPage), perPage
Node.LABEL_ATTRIBUTE,
Node.LABEL_ATTRIBUTE,
requestedLanguage.getDatabaseName(),
(Integer.parseInt(page) - 1) * Integer.parseInt(perPage), perPage
);

return Response
Expand All @@ -142,14 +145,17 @@ public Response getVocabularyByLanguagePaged(
*
* @param keyword The provided keyword
*
* @return all nodes whose "label" attribute contains the search keyword
* @return all nodes whose {@link Node#LABEL_ATTRIBUTE "label"} attribute contains the search keyword
*/
@GET
@Path("/search/{keyword}")
@Produces(MediaType.APPLICATION_JSON)
@SuppressWarnings("MultipleStringLiterals")
public Response search(@NotNull @PathParam("keyword") final String keyword) {
final String query = String.format("MATCH (node) WHERE node.label =~ '.*%s.*' RETURN node", keyword);
final String query = String.format(
"MATCH (node) WHERE node.%s =~ '.*%s.*' RETURN node",
Node.LABEL_ATTRIBUTE, keyword
);

return Response
.status(Response.Status.OK)
Expand Down Expand Up @@ -313,12 +319,13 @@ public Response expandApoc(

final String query = String.format(
"""
MATCH (node{label:'%s'})
MATCH (node{%s:'%s'})
CALL apoc.path.expand(node, "LINK", null, 1, %s)
YIELD path
RETURN path, length(path) AS hops
ORDER BY hops;
""",
Node.LABEL_ATTRIBUTE,
word.replace("'", "\\'"), maxHops
);

Expand Down

0 comments on commit 1e41ade

Please sign in to comment.