11package org .gephi .plugins .neo4j .importer ;
22
3- import cern .colt .list .ObjectArrayList ;
43import io .reactivex .Flowable ;
54import io .reactivex .Observable ;
65import org .gephi .io .importer .api .*;
@@ -40,6 +39,7 @@ public class Neo4jDatabaseImporter implements WizardImporter, LongTask {
4039 public ContainerLoader container ;
4140 private Report report ;
4241 private boolean cancel = false ;
42+ private ProgressTicket progressTicket ;
4343
4444
4545 /**
@@ -95,17 +95,20 @@ public class Neo4jDatabaseImporter implements WizardImporter, LongTask {
9595 public boolean execute (ContainerLoader containerLoader ) {
9696 this .container = containerLoader ;
9797 this .report = new Report ();
98-
99-
100- // Create the neo4j driver
101- if (this .driver != null ) this .driver .close ();
102- this .driver = GraphDatabase .driver (
103- url != null ? url : "neo4j://localhost" ,
104- this .passwd != null ? AuthTokens .basic (this .username , this .passwd ) : AuthTokens .none ()
105- );
98+ this .progressTicket .setDisplayName ("Neo4j import" );
99+ this .progressTicket .switchToIndeterminate ();
100+ this .progressTicket .start ();
106101
107102 try {
103+ // Create the neo4j driver
104+ this .progressTicket .progress ("Connecting to neo4j..." );
105+ if (this .driver != null ) this .driver .close ();
106+ this .driver = GraphDatabase .driver (
107+ url != null ? url : "neo4j://localhost" ,
108+ this .passwd != null ? AuthTokens .basic (this .username , this .passwd ) : AuthTokens .none ()
109+ );
108110 this .driver .verifyConnectivity ();
111+
109112 // Creating default columns for nodes/edges
110113 this .getContainer ().addNodeColumn ("labels" , String [].class );
111114
@@ -122,6 +125,7 @@ public boolean execute(ContainerLoader containerLoader) {
122125 this .getReport ().logIssue (new Issue (e , Issue .Level .CRITICAL ));
123126 } finally {
124127 if (this .driver != null ) this .driver .close ();
128+ this .progressTicket .finish ();
125129 }
126130
127131 return !cancel ;
@@ -134,6 +138,7 @@ private void doImportByNodeAndEdgeTypes(List<String> labels, List<String> relati
134138 Value parameters = parameters ("labels" , labels , "relationshipTypes" , relationshipTypes );
135139
136140 // Import nodes
141+ this .progressTicket .progress ("Importing nodes..." );
137142 long nbNodesImported = Flowable .using (
138143 () -> this .driver .rxSession (this .DBName != null ? SessionConfig .forDatabase (this .DBName ) : SessionConfig .defaultConfig ()),
139144 session -> session .readTransaction (tx -> {
@@ -150,6 +155,7 @@ private void doImportByNodeAndEdgeTypes(List<String> labels, List<String> relati
150155 this .getReport ().log (String .format ("%s nodes imported" , nbNodesImported ));
151156
152157 // Import edges
158+ this .progressTicket .progress ("Importing edges..." );
153159 long nbEdgesImported = Flowable .using (
154160 () -> this .driver .rxSession (this .DBName != null ? SessionConfig .forDatabase (this .DBName ) : SessionConfig .defaultConfig ()),
155161 session -> session .readTransaction (tx -> {
@@ -172,6 +178,7 @@ private void doImportByNodeAndEdgeTypes(List<String> labels, List<String> relati
172178 */
173179 private void doImportByNodeAndEdgeQueries () {
174180 // Import nodes
181+ this .progressTicket .progress ("Importing nodes..." );
175182 long nbNodesImported = Flowable .using (
176183 () -> this .driver .rxSession (this .DBName != null ? SessionConfig .forDatabase (this .DBName ) : SessionConfig .defaultConfig ()),
177184 session -> session .readTransaction (tx -> {
@@ -197,6 +204,7 @@ private void doImportByNodeAndEdgeQueries() {
197204 this .getReport ().log (String .format ("%s nodes imported" , nbNodesImported ));
198205
199206 // Import edges
207+ this .progressTicket .progress ("Importing edges..." );
200208 long nbEdgesImported = Flowable .using (
201209 () -> this .driver .rxSession (this .DBName != null ? SessionConfig .forDatabase (this .DBName ) : SessionConfig .defaultConfig ()),
202210 session -> session .readTransaction (tx -> {
@@ -216,10 +224,10 @@ private void doImportByNodeAndEdgeQueries() {
216224 return false ;
217225 }
218226 return this .mergeEdgeInGephi (
219- record .get ("id" ).asString (),
227+ record .get ("id" ).toString (),
220228 record .containsKey ("type" ) ? record .get ("type" ).asString () : null ,
221- record .get ("sourceId" ).asString (),
222- record .get ("targetId" ).asString (),
229+ record .get ("sourceId" ).toString (),
230+ record .get ("targetId" ).toString (),
223231 record .fields ().stream ().filter (t -> !Arrays .asList ("id" , "type" , "sourceId" , "targetId" ).contains (t .key ())).collect (Collectors .toMap (Pair ::key , Pair ::value ))
224232 );
225233 })
@@ -290,11 +298,11 @@ private Boolean mergeNodeInGephi(String id, String[] labels, Map<String, Value>
290298
291299 // Setting gephi label
292300 if (attributes .containsKey ("name" )) {
293- draft .setLabel (attributes .get ("name" ).asString ());
301+ draft .setLabel (attributes .get ("name" ).toString ());
294302 } else if (attributes .containsKey ("id" )) {
295- draft .setLabel (attributes .get ("id" ).asString ());
303+ draft .setLabel (attributes .get ("id" ).toString ());
296304 } else if (attributes .containsKey ("title" )) {
297- draft .setLabel (attributes .get ("title" ).asString ());
305+ draft .setLabel (attributes .get ("title" ).toString ());
298306 } else {
299307 draft .setLabel (id );
300308 }
@@ -333,7 +341,7 @@ private Boolean mergeEdgeInGephi(String id, String type, String sourceId, String
333341 // Do some check to validate that the edge can be imported
334342 if (this .getContainer ().edgeExists (id )) return false ;
335343 if (!this .getContainer ().nodeExists (sourceId ) || !this .getContainer ().nodeExists (targetId )) {
336- this .report .log (String .format ("Edge %s has been skipped due to missing extrimity " , id ));
344+ this .report .log (String .format ("Edge %s has been skipped due to missing extremity " , id ));
337345 return false ;
338346 }
339347
@@ -342,6 +350,19 @@ private Boolean mergeEdgeInGephi(String id, String type, String sourceId, String
342350 draft .setType (type );
343351 draft .setSource (this .getContainer ().getNode (sourceId ));
344352 draft .setTarget (this .getContainer ().getNode (targetId ));
353+
354+ // Setting gephi Weight if possible
355+ try {
356+ if (attributes .get ("Weight" ) != null ) {
357+ draft .setWeight (attributes .get ("Weight" ).asDouble ());
358+ } else if (attributes .get ("weight" ) != null ) {
359+ draft .setWeight (attributes .get ("weight" ).asDouble ());
360+ } else if (attributes .get ("score" ) != null ) {
361+ draft .setWeight (attributes .get ("score" ).asDouble ());
362+ }
363+ } catch (Exception e ) {
364+ // nothing to do
365+ }
345366 this .addNeo4jAttributes (draft , attributes , type );
346367
347368 // Add edge to Gephi
@@ -356,19 +377,16 @@ private void addNeo4jAttributes(ElementDraft draft, Map<String, Value> attribute
356377 try {
357378 Object gephiValue = this .neo4jValueToGephi (value );
358379 if (gephiValue != null ) {
359- System .out .println (gephiValue .getClass ().toString ());
360380 draft .setValue (gephiColKey , gephiValue );
361381 }
362382 } catch (Exception e ) {
363- this .getReport ().log (String .format ("Property %s on %s %s has been skipped due to bad type " , key , draft instanceof NodeDraft ? "node" : "edge" , draft .getId ()));
383+ this .getReport ().log (String .format ("Property %s on %s %s has been skipped: %s " , key , draft instanceof NodeDraft ? "node" : "edge" , draft .getId (), e . getMessage ()));
364384 }
365385 });
366386 }
367387
368388 private Object neo4jValueToGephi (Value value ) {
369389 Object result = null ;
370- System .out .println (value );
371- System .out .println (value .type ().name ());
372390 switch (value .type ().name ()) {
373391 case "ANY" :
374392 throw new NotImplementedException ("Any value is not implemented" );
@@ -391,7 +409,7 @@ private Object neo4jValueToGephi(Value value) {
391409 break ;
392410 case "LIST" :
393411 case "LIST OF ANY?" :
394- if (value .asList ().size () > 0 ) {
412+ if (value .asList ().size () > 0 ) {
395413 result = toArray (value .asList (this ::neo4jValueToGephi ));
396414 }
397415
@@ -435,6 +453,7 @@ public <T> T[] toArray(List<T> list) {
435453
436454 @ Override
437455 public void setProgressTicket (ProgressTicket progressTicket ) {
456+ this .progressTicket = progressTicket ;
438457 }
439458
440459 @ Override
0 commit comments