use of org.gephi.io.database.drivers.SQLiteDriver in project gephi-plugins-bootcamp by gephi.
the class SQLiteDatabaseExporter method execute.
@Override
public boolean execute() {
Connection connection = null;
try {
if (path.getParentFile().exists()) {
//Create connection
SQLiteDriver sQLiteDriver = Lookup.getDefault().lookup(SQLiteDriver.class);
String connectionUrl = SQLUtils.getUrl(sQLiteDriver, path.getAbsolutePath(), 0, "");
connection = sQLiteDriver.getConnection(connectionUrl, "", "");
//Create statement and create nodes and egdes table
Statement statement = connection.createStatement();
// set timeout to 30 sec.
statement.setQueryTimeout(30);
statement.executeUpdate("drop table if exists nodes");
statement.executeUpdate("drop table if exists edges");
statement.executeUpdate("create table nodes (id string, label string)");
statement.executeUpdate("create table edges (source string, target string, weight real)");
//Get the current graph in the defined workspace
GraphController graphController = Lookup.getDefault().lookup(GraphController.class);
GraphModel graphModel = graphController.getGraphModel(workspace);
Graph graph = graphModel.getGraphVisible();
//Count the number of tasks (nodes + edges) and start the progress
int tasks = graph.getNodeCount() + graph.getEdgeCount();
Progress.start(progress, tasks);
//Export nodes. Progress is incremented at each step.
for (Node n : graph.getNodes().toArray()) {
String id = n.getId().toString();
String label = n.getLabel();
statement.executeUpdate("insert into nodes values('" + id + "', '" + label + "')");
if (cancel) {
return false;
}
Progress.progress(progress);
}
//Export edges. Progress is incremented at each step.
for (Edge e : graph.getEdges().toArray()) {
String sourceId = e.getSource().getId().toString();
String targetId = e.getTarget().getId().toString();
String weight = String.valueOf(e.getWeight());
statement.executeUpdate("insert into edges values('" + sourceId + "', '" + targetId + "', '" + weight + "')");
if (cancel) {
return false;
}
Progress.progress(progress);
}
//Finish progress
Progress.finish(progress);
return true;
} else {
throw new FileNotFoundException(path.getAbsolutePath() + " does not exist");
}
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
if (connection != null) {
connection.close();
}
} catch (SQLException e) {
// connection close failed.
System.err.println(e);
}
}
}
Aggregations