Search in sources :

Example 11 with Ball

use of ini.trakem2.display.Ball in project TrakEM2 by trakem2.

the class DBLoader method updateInDatabase.

private void updateInDatabase(Ball ball, String key) throws Exception {
    if (!connectToDatabase()) {
        Utils.log("Not connected and can't connect to database.");
        return;
    }
    try {
        boolean autocommit = connection.getAutoCommit();
        connection.setAutoCommit(false);
        Statement st = connection.createStatement();
        StringBuffer sb_query = new StringBuffer("UPDATE ab_zdisplayables SET ");
        boolean update_all_points = false;
        if (key.startsWith("INSERT INTO ab_ball_points ")) {
            connection.prepareStatement(key).executeUpdate();
            connection.commit();
            // restore
            connection.setAutoCommit(autocommit);
            return;
        } else if (key.equals("points")) {
            update_all_points = true;
        } else if (key.startsWith("UPDATE ab_ball_points")) {
            // used to update points individually
            connection.prepareStatement(key).executeUpdate();
            connection.commit();
            // restore
            connection.setAutoCommit(autocommit);
            return;
        } else if (key.equals("layer_set_id")) {
            sb_query.append("layer_set_id=").append(ball.getLayerSet().getId());
        } else {
            // Displayable level
            connection.setAutoCommit(autocommit);
            updateInDatabase((Displayable) ball, key);
            return;
        }
        if (sb_query.length() > 28) {
            // 28 is the length of the string 'UPDATE ab_zdisplayables SET '
            st.addBatch(sb_query.toString());
        }
        if (update_all_points) {
            // delete and re-add
            st.addBatch("DELETE FROM ab_ball_points WHERE ball_id=" + ball.getId());
            String[] s_points = ball.getPointsForSQL();
            for (int i = 0; i < s_points.length; i++) {
                st.addBatch(s_points[i]);
            }
        }
        sb_query.append(" WHERE pipe_id=").append(ball.getId());
        // commit
        st.executeBatch();
        connection.commit();
        // restore
        connection.setAutoCommit(autocommit);
    } catch (SQLException sqle) {
        IJError.print(sqle);
        Exception next;
        if (null != (next = sqle.getNextException())) {
            IJError.print(next);
        }
        try {
            connection.rollback();
            // default ..
            connection.setAutoCommit(true);
        } catch (SQLException sqle2) {
            IJError.print(sqle2);
        }
    }
}
Also used : Displayable(ini.trakem2.display.Displayable) ZDisplayable(ini.trakem2.display.ZDisplayable) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Point(java.awt.Point) PGpoint(org.postgresql.geometric.PGpoint) SQLException(java.sql.SQLException)

Example 12 with Ball

use of ini.trakem2.display.Ball in project TrakEM2 by trakem2.

the class DBLoader method fetchBall.

private Ball fetchBall(Project project, long id) throws Exception {
    // strange query, but can't distinguish between pipes and balls otherwise
    ResultSet r = connection.prepareStatement("SELECT ab_displayables.id, title, ab_displayables.width, height, alpha, visible, color_red, color_green, color_blue, ab_zdisplayables.id, ab_ball_points.ball_id, ab_displayables.locked, m00, m10, m01, m11, m02, m12 FROM ab_zdisplayables, ab_displayables, ab_ball_points WHERE ab_zdisplayables.id=ab_displayables.id AND ab_zdisplayables.id=ab_ball_points.ball_id AND ab_zdisplayables.id=" + id).executeQuery();
    Ball b = null;
    if (r.next()) {
        b = new Ball(project, id, r.getString("title"), (float) r.getDouble("width"), (float) r.getDouble("height"), r.getFloat("alpha"), r.getBoolean("visible"), new Color(r.getInt("color_red"), r.getInt("color_green"), r.getInt("color_blue")), r.getBoolean("locked"), new AffineTransform(r.getDouble("m00"), r.getDouble("m10"), r.getDouble("m01"), r.getDouble("m11"), r.getDouble("m02"), r.getDouble("m12")));
    }
    r.close();
    return b;
}
Also used : Ball(ini.trakem2.display.Ball) Color(java.awt.Color) ResultSet(java.sql.ResultSet) AffineTransform(java.awt.geom.AffineTransform)

Aggregations

Ball (ini.trakem2.display.Ball)5 Point (java.awt.Point)5 Color (java.awt.Color)4 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 GenericDialog (ij.gui.GenericDialog)3 Displayable (ini.trakem2.display.Displayable)3 Pipe (ini.trakem2.display.Pipe)3 ZDisplayable (ini.trakem2.display.ZDisplayable)3 ProjectThing (ini.trakem2.tree.ProjectThing)3 AreaList (ini.trakem2.display.AreaList)2 AreaTree (ini.trakem2.display.AreaTree)2 Connector (ini.trakem2.display.Connector)2 DLabel (ini.trakem2.display.DLabel)2 Patch (ini.trakem2.display.Patch)2 Polyline (ini.trakem2.display.Polyline)2 Profile (ini.trakem2.display.Profile)2 Treeline (ini.trakem2.display.Treeline)2 AffineTransform (java.awt.geom.AffineTransform)2 NoninvertibleTransformException (java.awt.geom.NoninvertibleTransformException)2