Skip to content

Commit 73ffbea

Browse files
committed
Fixing issues and add more test cases
1 parent ccb5296 commit 73ffbea

4 files changed

Lines changed: 229 additions & 87 deletions

File tree

modules/Neo4jPlugin/pom.xml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
33
<modelVersion>4.0.0</modelVersion>
4+
45
<parent>
56
<artifactId>gephi-plugin-parent</artifactId>
67
<groupId>org.gephi</groupId>
@@ -9,14 +10,13 @@
910

1011
<groupId>ouestware</groupId>
1112
<artifactId>neo4j-plugin</artifactId>
12-
<name>Neo4J plugin</name>
13+
<name>Neo4j plugin</name>
1314
<version>1.0.0</version>
1415
<packaging>nbm</packaging>
1516

1617
<properties>
1718
<neo4j.driver.version>4.3.4</neo4j.driver.version>
1819
<testcontainers.version>1.17.3</testcontainers.version>
19-
<gephi.version>0.9.7</gephi.version>
2020
</properties>
2121

2222
<dependencies>
@@ -28,19 +28,16 @@
2828
<dependency>
2929
<groupId>org.gephi</groupId>
3030
<artifactId>io-importer-api</artifactId>
31-
<version>${gephi.version}</version>
3231
<scope>provided</scope>
3332
</dependency>
3433
<dependency>
3534
<groupId>org.gephi</groupId>
3635
<artifactId>desktop-import</artifactId>
37-
<version>${gephi.version}</version>
3836
<scope>provided</scope>
3937
</dependency>
4038
<dependency>
4139
<groupId>org.gephi</groupId>
4240
<artifactId>utils-longtask</artifactId>
43-
<version>${gephi.version}</version>
4441
<scope>provided</scope>
4542
</dependency>
4643
<dependency>

modules/Neo4jPlugin/src/main/java/org/gephi/plugins/neo4j/Neo4jDatabaseImporter.java

Lines changed: 89 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.neo4j.driver.types.Node;
99
import org.neo4j.driver.types.Relationship;
1010
import org.neo4j.driver.util.Pair;
11+
import org.openide.util.NotImplementedException;
1112

1213
import java.util.ArrayList;
1314
import java.util.Arrays;
@@ -32,54 +33,54 @@ public class Neo4jDatabaseImporter implements WizardImporter, LongTask {
3233
" any(label IN labels(m) WHERE label IN $labels) " +
3334
"RETURN r";
3435

35-
public static ContainerLoader container;
36-
private static Report report;
36+
public ContainerLoader container;
37+
private Report report;
3738
private boolean cancel = false;
3839

3940

4041
/**
4142
* Url of Neo4j database.
4243
* Default is `neo4j://localhost`
4344
*/
44-
private static String url = "neo4j://localhost";
45+
private String url = "neo4j://localhost";
4546

4647
/**
4748
* Username for the Neo4j database.
4849
* Default is `neo4j`
4950
*/
50-
private static String username = "neo4j";
51+
private String username = "neo4j";
5152

5253
/**
5354
* Password for the Neo4j database.
5455
* If not specified, we do not do auth
5556
*/
56-
private static String passwd;
57+
private String passwd;
5758

5859
/**
5960
* Database name
6061
* If not specified, we use the default neo4j database
6162
*/
62-
private static String DBName;
63+
private String DBName;
6364

6465
/**
6566
* List of labels that we want to import
6667
*/
67-
private static List<String> labels;
68+
private List<String> labels;
6869

6970
/**
7071
* List of relationship that we want to import
7172
*/
72-
private static List<String> relationshipTypes;
73+
private List<String> relationshipTypes;
7374

7475
/**
7576
* Cypher query to retrieve nodes
7677
*/
77-
private static String nodeQuery;
78+
private String nodeQuery;
7879

7980
/**
8081
* Cypher query to retrieve edges
8182
*/
82-
private static String edgeQuery;
83+
private String edgeQuery;
8384

8485
/**
8586
* Neo4J driver instance.
@@ -164,7 +165,7 @@ private void doImportByNodeAndEdgeQueries() {
164165
// Query nodes
165166
Result nodesResult = tx.run(this.nodeQuery);
166167
// Checking for mandatory keys
167-
if (nodesResult.keys().containsAll(Arrays.asList("id"))) {
168+
if (!nodesResult.keys().containsAll(Arrays.asList("id"))) {
168169
this.getReport().logIssue(new Issue(new Exception("Node query returns no `id` column"), Issue.Level.CRITICAL));
169170
return 1;
170171
}
@@ -173,7 +174,7 @@ private void doImportByNodeAndEdgeQueries() {
173174
while (nodesResult.hasNext()) {
174175
Record record = nodesResult.next();
175176
this.mergeNodeInGephi(
176-
record.get("id").asString(),
177+
record.get("id").toString(),
177178
record.containsKey("labels") ? record.get("labels").asList(t -> t.toString()).toArray(new String[0]) : null,
178179
record.fields().stream().filter(t -> !Arrays.asList("id", "labels").contains(t.key())).collect(Collectors.toMap(Pair::key, Pair::value))
179180
);
@@ -184,7 +185,7 @@ private void doImportByNodeAndEdgeQueries() {
184185
// Query edges
185186
Result edgesResult = tx.run(this.edgeQuery);
186187
// Checking for mandatory keys
187-
if (nodesResult.keys().containsAll(Arrays.asList("id", "sourceId", "targetId"))) {
188+
if (!edgesResult.keys().containsAll(Arrays.asList("id", "sourceId", "targetId"))) {
188189
this.getReport().logIssue(new Issue(new Exception("Edge query returns no `id` column"), Issue.Level.CRITICAL));
189190
return 1;
190191
}
@@ -214,7 +215,7 @@ private void doImportByNodeAndEdgeQueries() {
214215
private List<String> getDbLabels() {
215216
try (Session session = this.driver.session(this.DBName != null ? SessionConfig.forDatabase(this.DBName) : SessionConfig.defaultConfig())) {
216217
return session.readTransaction(tx -> {
217-
List<String> labels = new ArrayList();
218+
List<String> labels = new ArrayList<String>();
218219
Result rs = tx.run("CALL db.labels()");
219220
while (rs.hasNext()) {
220221
labels.add(rs.next().get(0).asString());
@@ -230,7 +231,7 @@ private List<String> getDbLabels() {
230231
private List<String> getDbRelationshipTypes() {
231232
try (Session session = this.driver.session(this.DBName != null ? SessionConfig.forDatabase(this.DBName) : SessionConfig.defaultConfig())) {
232233
return session.readTransaction(tx -> {
233-
List<String> labels = new ArrayList();
234+
List<String> labels = new ArrayList<String>();
234235
Result rs = tx.run("CALL db.relationshipTypes()");
235236
while (rs.hasNext()) {
236237
labels.add(rs.next().get(0).asString());
@@ -260,7 +261,7 @@ private void mergeNodeInGephi(Node neo4jNode) {
260261
*/
261262
private void mergeNodeInGephi(String id, String[] labels, Map<String, Value> attributes) {
262263
if (!this.getContainer().nodeExists(id)) {
263-
NodeDraft draft = this.getContainer().factory().newNodeDraft();
264+
NodeDraft draft = this.getContainer().factory().newNodeDraft(id);
264265
String mainLabel = (labels != null && labels.length > 0) ? labels[0] : "";
265266

266267
// Setting gephi label
@@ -271,12 +272,12 @@ private void mergeNodeInGephi(String id, String[] labels, Map<String, Value> att
271272
} else {
272273
draft.setLabel(id);
273274
}
274-
275275
// Setting node label
276-
draft.setValue("labels", labels);
277-
276+
if(labels!=null) draft.setValue("labels", labels);
277+
// Setting attributes
278278
this.addNeo4jAttributes(draft, attributes, mainLabel);
279279

280+
// Add node to gephi
280281
this.getContainer().addNode(draft);
281282
}
282283
}
@@ -301,19 +302,21 @@ private void mergeEdgeInGephi(Relationship neo4jRel) {
301302
* @param attributes Neo4j rel's attributes
302303
*/
303304
private void mergeEdgeInGephi(String id, String type, String sourceId, String targetId, Map<String, Value> attributes) {
304-
if (!this.getContainer().edgeExists(id) &&
305-
this.getContainer().nodeExists(sourceId) &&
306-
this.getContainer().nodeExists(targetId)
307-
) {
308-
EdgeDraft draft = this.getContainer().factory().newEdgeDraft();
309-
310-
draft.setLabel(type);
311-
draft.setType(type);
312-
draft.setSource(this.getContainer().getNode(sourceId));
313-
draft.setTarget(this.getContainer().getNode(targetId));
314-
this.addNeo4jAttributes(draft, attributes, type);
315-
316-
this.getContainer().addEdge(draft);
305+
if (!this.getContainer().edgeExists(id)) {
306+
if (this.getContainer().nodeExists(sourceId) && this.getContainer().nodeExists(targetId)) {
307+
308+
EdgeDraft draft = this.getContainer().factory().newEdgeDraft(id);
309+
draft.setLabel(type);
310+
draft.setType(type);
311+
draft.setSource(this.getContainer().getNode(sourceId));
312+
draft.setTarget(this.getContainer().getNode(targetId));
313+
this.addNeo4jAttributes(draft, attributes, type);
314+
315+
// Add edge to Gephi
316+
this.getContainer().addEdge(draft);
317+
} else {
318+
this.report.log(String.format("Edge %s has been skipped due to missing extrimity", id));
319+
}
317320
}
318321
}
319322

@@ -324,17 +327,51 @@ private void addNeo4jAttributes(ElementDraft draft, Map<String, Value> attribute
324327

325328
try {
326329
switch (value.getClass().getSimpleName()) {
327-
case "IntegerValue":
328-
draft.setValue(gephiColKey, value.asInt());
329-
break;
330330
case "BooleanValue":
331331
draft.setValue(gephiColKey, value.asBoolean());
332332
break;
333+
case "BytesValue":
334+
throw new NotImplementedException("Bytes value is not implemented");
335+
case "DateTimeValue":
336+
throw new NotImplementedException("DateTime value is not implemented");
337+
case "DateValue":
338+
throw new NotImplementedException("Date value is not implemented");
339+
case "DurationValue":
340+
throw new NotImplementedException("Duration value is not implemented");
333341
case "FloatValue":
334342
draft.setValue(gephiColKey, value.asFloat());
335343
break;
344+
case "IntegerValue":
345+
draft.setValue(gephiColKey, value.asInt());
346+
break;
347+
case "ListValue":
348+
draft.setValue(gephiColKey, value.asList());
349+
break;
350+
case "LocalDateTimeValue":
351+
throw new NotImplementedException("LocalDateTime value is not implemented");
352+
case "LocalTimeValue":
353+
throw new NotImplementedException("LocalTime value is not implemented");
354+
case "MapValue":
355+
throw new NotImplementedException("Map value is not implemented");
356+
case "NodeValue":
357+
throw new NotImplementedException("Node value is not implemented");
358+
case "NullValue":
359+
break;
360+
case "NumberValueAdapter":
361+
draft.setValue(gephiColKey, value.asNumber());
362+
break;
363+
case "ObjectValueAdapter":
364+
throw new NotImplementedException("Object value is not implemented");
365+
case "PathValue":
366+
throw new NotImplementedException("Path value is not implemented");
367+
case "PointValue":
368+
throw new NotImplementedException("Point value is not implemented");
369+
case "RelationshipValue":
370+
throw new NotImplementedException("Relationship value is not implemented");
371+
case "TimeValue":
372+
throw new NotImplementedException("Time value is not implemented");
336373
case "StringValue":
337-
draft.setValue(gephiColKey, value.toString());
374+
draft.setValue(gephiColKey, value.asString());
338375
break;
339376
}
340377
} catch (Exception e) {
@@ -367,36 +404,36 @@ public boolean cancel() {
367404
// Generated setter for import parameters
368405
//
369406

370-
public static void setUrl(String url) {
371-
Neo4jDatabaseImporter.url = url;
407+
public void setUrl(String url) {
408+
this.url = url;
372409
}
373410

374-
public static void setUsername(String username) {
375-
Neo4jDatabaseImporter.username = username;
411+
public void setUsername(String username) {
412+
this.username = username;
376413
}
377414

378-
public static void setPasswd(String passwd) {
379-
Neo4jDatabaseImporter.passwd = passwd;
415+
public void setPasswd(String passwd) {
416+
this.passwd = passwd;
380417
}
381418

382-
public static void setDBName(String DBName) {
383-
Neo4jDatabaseImporter.DBName = DBName;
419+
public void setDBName(String DBName) {
420+
this.DBName = DBName;
384421
}
385422

386-
public static void setLabels(List<String> labels) {
387-
Neo4jDatabaseImporter.labels = labels;
423+
public void setLabels(List<String> labels) {
424+
this.labels = labels;
388425
}
389426

390-
public static void setRelationshipTypes(List<String> relationshipTypes) {
391-
Neo4jDatabaseImporter.relationshipTypes = relationshipTypes;
427+
public void setRelationshipTypes(List<String> relationshipTypes) {
428+
this.relationshipTypes = relationshipTypes;
392429
}
393430

394-
public static void setNodeQuery(String nodeQuery) {
395-
Neo4jDatabaseImporter.nodeQuery = nodeQuery;
431+
public void setNodeQuery(String nodeQuery) {
432+
this.nodeQuery = nodeQuery;
396433
}
397434

398-
public static void setEdgeQuery(String edgeQuery) {
399-
Neo4jDatabaseImporter.edgeQuery = edgeQuery;
435+
public void setEdgeQuery(String edgeQuery) {
436+
this.edgeQuery = edgeQuery;
400437
}
401438

402439
}

0 commit comments

Comments
 (0)