Skip to content

Commit

Permalink
Initial commit of a utility to load a Structurizr workspace into a Ne…
Browse files Browse the repository at this point in the history
…o4j database.
  • Loading branch information
simonbrowndotje committed Aug 26, 2024
1 parent 8d57511 commit 2ed0cca
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 1 deletion.
3 changes: 2 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ include 'structurizr-core'
include 'structurizr-dsl'
include 'structurizr-export'
include 'structurizr-import'
include 'structurizr-inspection'
include 'structurizr-inspection'
include 'structurizr-neo4j'
5 changes: 5 additions & 0 deletions structurizr-neo4j/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# structurizr-neo4j

[![Maven Central](https://img.shields.io/maven-central/v/com.structurizr/structurizr-neo4j.svg?label=Maven%20Central)](https://search.maven.org/artifact/com.structurizr/structurizr-neo4j)

This library provides utilities to import a Structurizr workspace into Neo4j.
9 changes: 9 additions & 0 deletions structurizr-neo4j/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
dependencies {

api project(':structurizr-core')
implementation 'org.neo4j.driver:neo4j-java-driver:5.23.0'

testImplementation project(':structurizr-client')
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.2'

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.structurizr.neo4j;

import com.structurizr.Workspace;
import com.structurizr.model.Element;
import com.structurizr.model.Relationship;
import com.structurizr.util.StringUtils;
import org.neo4j.driver.Driver;
import org.neo4j.driver.SessionConfig;

public class SimpleLoader {

public void load(Workspace workspace, Driver driver, String database) {
try (var session = driver.session(SessionConfig.builder().withDatabase(database).build())) {
for (Element element : workspace.getModel().getElements()) {
session.run(String.format(
"CREATE ( :Element { id: '%s', name: \"%s\", type: \"%s\" })",
element.getId(), element.getName(), element.getClass().getSimpleName().toLowerCase()
));
}

session.run("CREATE INDEX element_index FOR (n:Element) ON (n.id)");

for (Relationship relationship : workspace.getModel().getRelationships()) {
session.run(String.format(
"""
MATCH ( from:Element { id: '%s' } ), ( to:Element { id: '%s' } )
CREATE (from)-[:HAS_RELATIONSHIP_WITH {role: '%s'}]->(to)""",
relationship.getSource().getId(),
relationship.getDestination().getId(),
!StringUtils.isNullOrEmpty(relationship.getDescription()) ? relationship.getDescription() : "uses"
));
}
}
}

}
28 changes: 28 additions & 0 deletions structurizr-neo4j/src/test/java/com/structurizr/Example.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package com.structurizr;

import com.structurizr.neo4j.SimpleLoader;
import com.structurizr.util.WorkspaceUtils;
import org.neo4j.driver.AuthTokens;
import org.neo4j.driver.Driver;
import org.neo4j.driver.GraphDatabase;
import org.neo4j.driver.Result;

import java.io.File;

public class Example {

public static void main(String[] args) throws Exception {
Workspace workspace = WorkspaceUtils.loadWorkspaceFromJson(new File("workspace.json"));

try (Driver driver = GraphDatabase.driver("bolt://localhost:7687", AuthTokens.basic("neo4j", "password"))) {
try (var session = driver.session()) {
session.run("DROP DATABASE structurizr IF EXISTS");
Result result = session.run("CREATE DATABASE structurizr");
System.out.println(result.consume());
}

new SimpleLoader().load(workspace, driver, "structurizr");
}
}

}

0 comments on commit 2ed0cca

Please sign in to comment.