View Javadoc
1   package nc.adriens.schemacrawler.plugin.neo4j;
2   /*
3    http://www.tutorialspoint.com/neo4j/neo4j_native_java_api_example.htm
4    http://www.sportsstandards.org/oc
5   
6    Search nodes :
7   
8    http://stackoverflow.com/questions/15368579/select-a-node-by-name-in-neo4j-in-java
9   
10   schemacrawler -host=localhost -port=5432 -database=sportsdb -user=sc -password=sc  -schemas=public -c=neo4j -infolevel=maximum -server=postgresql -loglevel=CONFIG -outputDir=c:/tmp
11  
12  schemacrawler -host=localhost -port=5432 -database=sportsdb -user=sports_adm -password=user_adm  -schemas=public -c=neo4j -infolevel=maximum -server=postgresql -loglevel=CONFIG -outputDir=./neo4j
13  
14  MATCH (n)
15  RETURN n;
16  
17  MATCH (a)-[r:CONTAINS_SCHEMA]->(b)
18   RETURN r
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   * @author adriens
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          // recursively delete directory contents
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                  // we have the schema node
77                  // for each table, attach the table to the schema without
78                  // having to find it for better performances (no index needed)
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                      // attach the table to its schema
91                      Relationship schemaRelationShip = tableNode.createRelationshipTo(schemaNode, SchemaRelationShips.BELONGS_TO_SCHEMA);
92                      
93                      //writer.println("o--> " + table);
94                      for (final Column column : table.getColumns()) {
95                          Node columnNode = dbService.createNode(DatabaseNodeType.TABLE_COLUMN);
96                          columnNode.setProperty("ordinalPosition", column.getOrdinalPosition());
97  
98                          //writer.println("     o--> " + column);
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 //        System.out.println(schema);
118 //        for (final Table table: catalog.getTables(schema))
119 //        {
120 //          writer.println("o--> " + table);
121 //          for (final Column column: table.getColumns())
122 //          {
123 //            writer.println("     o--> " + column);
124 //          }
125                 //}
126             }
127         }
128     }
129 
130     /**
131      * @return the outputDir
132      */
133     public String getOutputDir() {
134         return outputDir;
135     }
136 
137     /**
138      * @param outputDir the outputDir to set
139      */
140     public void setOutputDir(String outputDir) {
141         this.outputDir = outputDir;
142     }
143 
144     /**
145      * @return the dbFactory
146      */
147     public GraphDatabaseFactory getDbFactory() {
148         return dbFactory;
149     }
150 
151     /**
152      * @param dbFactory the dbFactory to set
153      */
154     public void setDbFactory(GraphDatabaseFactory dbFactory) {
155         this.dbFactory = dbFactory;
156     }
157 
158     /**
159      * @return the dbService
160      */
161     public GraphDatabaseService getDbService() {
162         return dbService;
163     }
164 
165     /**
166      * @param dbService the dbService to set
167      */
168     public void setDbService(GraphDatabaseService dbService) {
169         this.dbService = dbService;
170     }
171 
172 }