Search in sources :

Example 1 with SQLiteDriver

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);
        }
    }
}
Also used : SQLException(java.sql.SQLException) Statement(java.sql.Statement) SQLiteDriver(org.gephi.io.database.drivers.SQLiteDriver) Node(org.gephi.graph.api.Node) Connection(java.sql.Connection) FileNotFoundException(java.io.FileNotFoundException) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) Graph(org.gephi.graph.api.Graph) GraphModel(org.gephi.graph.api.GraphModel) Edge(org.gephi.graph.api.Edge) GraphController(org.gephi.graph.api.GraphController)

Aggregations

FileNotFoundException (java.io.FileNotFoundException)1 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Edge (org.gephi.graph.api.Edge)1 Graph (org.gephi.graph.api.Graph)1 GraphController (org.gephi.graph.api.GraphController)1 GraphModel (org.gephi.graph.api.GraphModel)1 Node (org.gephi.graph.api.Node)1 SQLiteDriver (org.gephi.io.database.drivers.SQLiteDriver)1