1 package nc.adriens.schemacrawler.plugin.neo4j;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 import java.io.File;
24 import java.io.IOException;
25 import java.io.PrintWriter;
26 import java.sql.Connection;
27 import org.apache.commons.io.FileUtils;
28 import org.neo4j.graphdb.GraphDatabaseService;
29 import org.neo4j.graphdb.Transaction;
30 import org.neo4j.graphdb.factory.GraphDatabaseFactory;
31 import org.neo4j.graphdb.Node;
32 import org.neo4j.graphdb.Relationship;
33
34 import schemacrawler.schema.Catalog;
35 import schemacrawler.schema.Column;
36 import schemacrawler.schema.Schema;
37 import schemacrawler.schema.Table;
38 import schemacrawler.tools.executable.BaseStagedExecutable;
39
40
41
42
43
44 public class AdditionalExecutable extends BaseStagedExecutable {
45
46 static final String COMMAND = "neo4j";
47
48 private String outputDir;
49 private GraphDatabaseFactory dbFactory;
50 private GraphDatabaseService dbService;
51
52 protected AdditionalExecutable() {
53 super(COMMAND);
54 }
55
56 public void init() throws IOException {
57
58 System.out.println("Deleting directory <" + getOutputDir() + "> ...");
59 FileUtils.deleteDirectory(new File(getOutputDir()));
60 System.out.println("Output directory cleaned");
61 setDbFactory(new GraphDatabaseFactory());
62 setDbService(dbFactory.newEmbeddedDatabase(getOutputDir()));
63 }
64
65
66
67 public void feedTables(final Catalog catalog) {
68 try (Transaction tx = getDbService().beginTx()) {
69 for (final Schema schema : catalog.getSchemas()) {
70 Node schemaNode = dbService.createNode(DatabaseNodeType.SCHEMA);
71 schemaNode.setProperty("Name", schema.getName());
72 schemaNode.setProperty("FullName", schema.getFullName());
73 schemaNode.setProperty("LookupKey", schema.getLookupKey());
74 schemaNode.setProperty("Remarks", schema.getRemarks());
75
76
77
78
79 for (final Table table : catalog.getTables(schema)) {
80 Node tableNode = dbService.createNode(DatabaseNodeType.TABLE);
81 tableNode.setProperty("nbColumns", table.getColumns().size());
82 tableNode.setProperty("definition", table.getDefinition());
83 tableNode.setProperty("lookupKey", table.getLookupKey());
84 tableNode.setProperty("name", table.getName());
85 tableNode.setProperty("remarks", table.getRemarks());
86 tableNode.setProperty("schemaName", table.getSchema().getName());
87 tableNode.setProperty("schemaFullname", table.getSchema().getFullName());
88 tableNode.setProperty("tableType", table.getTableType().toString());
89
90
91 Relationship schemaRelationShip = tableNode.createRelationshipTo(schemaNode, SchemaRelationShips.BELONGS_TO_SCHEMA);
92
93
94 for (final Column column : table.getColumns()) {
95 Node columnNode = dbService.createNode(DatabaseNodeType.TABLE_COLUMN);
96 columnNode.setProperty("ordinalPosition", column.getOrdinalPosition());
97
98
99 Relationship relationship = columnNode.createRelationshipTo(tableNode, SchemaRelationShips.IS_COLUMN_OF_TABLE);
100 }
101
102 }
103 tx.success();
104 }
105 }
106 }
107
108 @Override
109 public void executeOn(final Catalog catalog, final Connection connection)
110 throws Exception {
111 try (final PrintWriter writer = new PrintWriter(outputOptions.openNewOutputWriter());) {
112 setOutputDir(additionalConfiguration.getStringValue("outputDir", "neo4j"));
113 init();
114 feedTables(catalog);
115
116 for (final Schema schema : catalog.getSchemas()) {
117
118
119
120
121
122
123
124
125
126 }
127 }
128 }
129
130
131
132
133 public String getOutputDir() {
134 return outputDir;
135 }
136
137
138
139
140 public void setOutputDir(String outputDir) {
141 this.outputDir = outputDir;
142 }
143
144
145
146
147 public GraphDatabaseFactory getDbFactory() {
148 return dbFactory;
149 }
150
151
152
153
154 public void setDbFactory(GraphDatabaseFactory dbFactory) {
155 this.dbFactory = dbFactory;
156 }
157
158
159
160
161 public GraphDatabaseService getDbService() {
162 return dbService;
163 }
164
165
166
167
168 public void setDbService(GraphDatabaseService dbService) {
169 this.dbService = dbService;
170 }
171
172 }