Search in sources :

Example 1 with TemplateThing

use of ini.trakem2.tree.TemplateThing in project TrakEM2 by trakem2.

the class DBLoader method getChildrenTemplateThings.

/**
 * Recursive into children.
 */
private ArrayList getChildrenTemplateThings(Project project, long parent_id) throws Exception {
    ArrayList al = new ArrayList();
    ResultSet r = connection.prepareStatement("SELECT * FROM ab_things WHERE parent_id=" + parent_id).executeQuery();
    while (r.next()) {
        long id = r.getLong("id");
        String type = r.getString("type");
        TemplateThing tt = new TemplateThing(type, project, id);
        tt.setup(getChildrenTemplateThings(project, id));
        al.add(tt);
    }
    r.close();
    return al;
}
Also used : TemplateThing(ini.trakem2.tree.TemplateThing) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet)

Example 2 with TemplateThing

use of ini.trakem2.tree.TemplateThing in project TrakEM2 by trakem2.

the class DBLoader method getProjectThing.

private ProjectThing getProjectThing(ResultSet r, Project project, HashMap<String, TemplateThing> hs_tt, HashMap<Long, Displayable> hs_d) throws Exception {
    long id = r.getLong("id");
    String type = r.getString("type");
    TemplateThing tt = (TemplateThing) hs_tt.get(type);
    if (null == tt) {
        Utils.log("Loader.getProjectThing: can not find a proper TemplateThing of type " + type + " for the ProjectThing of id=" + id);
        return null;
    }
    long object_id = r.getLong("object_id");
    // may be null
    Object ob = r.getString("title");
    if (-1 != object_id) {
        ob = getProjectObject(project, object_id);
        if (ob instanceof Displayable)
            hs_d.put(new Long(((Displayable) ob).getId()), (Displayable) ob);
        else
            Utils.log("Loader.getProjectThing: not adding to hs_d: " + ob);
    }
    return new ProjectThing(tt, project, id, ob, getChildrenProjectThings(project, id, type, hs_tt, hs_d));
}
Also used : Displayable(ini.trakem2.display.Displayable) ZDisplayable(ini.trakem2.display.ZDisplayable) TemplateThing(ini.trakem2.tree.TemplateThing) ProjectThing(ini.trakem2.tree.ProjectThing)

Example 3 with TemplateThing

use of ini.trakem2.tree.TemplateThing in project TrakEM2 by trakem2.

the class DBLoader method getLayerThing.

private LayerThing getLayerThing(ResultSet r, Project project, HashMap hs_pt, TemplateThing layer_set_tt, TemplateThing layer_tt) throws Exception {
    long id = r.getLong("id");
    String type = r.getString("type");
    // if not a "Layer", then it's a "Layer Set"
    TemplateThing template = type.equals("layer_set") ? layer_set_tt : layer_tt;
    // HERE the order of the arguments layer_set_tt and layer_tt was inverted, and it worked??? There was a compensating bug, incredibly enough, in the type.equals(.. above.
    return new LayerThing(template, project, id, r.getString("title"), getLayerThingObject(project, r.getLong("object_id"), template, hs_pt), getChildrenLayerThing(project, id, hs_pt, layer_set_tt, layer_tt));
}
Also used : LayerThing(ini.trakem2.tree.LayerThing) TemplateThing(ini.trakem2.tree.TemplateThing)

Example 4 with TemplateThing

use of ini.trakem2.tree.TemplateThing in project TrakEM2 by trakem2.

the class DBLoader method upgradeProjectsTable.

/**
 * Used to upgrade old databases.
 */
private boolean upgradeProjectsTable() throws Exception {
    // Upgrade database if necessary: set a version field, create the TemplateThing entries in the database for each project from its XML template file, and delete the xml_template column
    // Check columns: see if trakem2_version is there
    ResultSet r = connection.prepareStatement("SELECT column_name FROM information_schema.columns WHERE table_name='ab_projects' AND column_name='xml_template'").executeQuery();
    if (r.next()) {
        YesNoCancelDialog yn = new YesNoCancelDialog(IJ.getInstance(), "Upgrade", "Need to upgrade table projects.\nNo data will be lost, but reorganized.\nProceed?");
        if (!yn.yesPressed()) {
            return false;
        }
        // retrieve and parse XML template from each project
        ResultSet r1 = connection.prepareStatement("SELECT * FROM ab_projects").executeQuery();
        while (r1.next()) {
            long project_id = r1.getLong("id");
            // parse the XML file stored in the db and save the TemplateThing into the ab_things table
            InputStream xml_stream = null;
            try {
                String query = "SELECT xml_template FROM ab_projects WHERE id=" + project_id;
                ResultSet result = connection.prepareStatement(query).executeQuery();
                if (result.next()) {
                    xml_stream = result.getBinaryStream("xml_template");
                }
                result.close();
            } catch (Exception e) {
                IJError.print(e);
                return false;
            }
            if (null == xml_stream) {
                Utils.showMessage("Failed to upgrade the database schema: XML template stream is null.");
                return false;
            }
            TemplateThing template_root = new TrakEM2MLParser(xml_stream).getTemplateRoot();
            if (null == template_root) {
                Utils.showMessage("Failed to upgrade the database schema: root TemplateThing is null.");
                return false;
            }
            Project project = new Project(project_id, r1.getString("title"));
            project.setTempLoader(this);
            template_root.addToDatabase(project);
        }
        r1.close();
        // remove the XML column
        connection.prepareStatement("ALTER TABLE ab_projects DROP xml_template").execute();
        // org.postgresql.util.PSQLException: ERROR: adding columns with defaults is not implemented in 7.4.* (only in 8.1.4+)
        // connection.prepareStatement("ALTER TABLE ab_projects ADD version text default '" + Utils.version + "'").execute();
        // so: workaround
        connection.prepareStatement("ALTER TABLE ab_projects ADD version TEXT").execute();
        connection.prepareStatement("ALTER TABLE ab_projects ALTER COLUMN version SET DEFAULT '" + Utils.version + "'").execute();
    }
    r.close();
    // success!
    return true;
}
Also used : Project(ini.trakem2.Project) TrakEM2MLParser(ini.trakem2.tree.TrakEM2MLParser) LoggingInputStream(ini.trakem2.io.LoggingInputStream) InputStream(java.io.InputStream) TemplateThing(ini.trakem2.tree.TemplateThing) ResultSet(java.sql.ResultSet) YesNoCancelDialog(ij.gui.YesNoCancelDialog) SQLException(java.sql.SQLException)

Example 5 with TemplateThing

use of ini.trakem2.tree.TemplateThing in project TrakEM2 by trakem2.

the class DBLoader method getRootLayerThing.

/**
 * Fetches the root LayerSet, fills it with children (recursively) and uses the profiles, pipes, etc., from the project_thing. Will reconnect the links and open Displays for the layers that have one.
 */
public LayerThing getRootLayerThing(Project project, ProjectThing project_thing, TemplateThing layer_set_tt, TemplateThing layer_tt) {
    synchronized (db_lock) {
        // connect if disconnected
        if (!connectToDatabase()) {
            return null;
        }
        HashMap hs_pt = new HashMap();
        unpack(project_thing, hs_pt);
        LayerThing root = null;
        try {
            // -1 signals root
            ResultSet r = connection.prepareStatement("SELECT * FROM ab_things WHERE project_id=" + project.getId() + " AND type='layer_set' AND parent_id=-1").executeQuery();
            if (r.next()) {
                root = getLayerThing(r, project, hs_pt, layer_set_tt, layer_tt);
            }
            r.close();
            if (null == root) {
                Utils.log("Loader.getRootLayerThing: can't find it for project id=" + project.getId());
                return null;
            }
            // Redo the links! hs_pt contains now all Displayable objects.
            ResultSet rl = connection.prepareStatement("SELECT * FROM ab_links WHERE project_id=" + project.getId()).executeQuery();
            while (rl.next()) {
                Long id1 = new Long(rl.getLong("id1"));
                Long id2 = new Long(rl.getLong("id2"));
                Object ob1 = hs_pt.get(id1);
                Object ob2 = hs_pt.get(id2);
                if (null != ob1 && null != ob2) {
                    Displayable d = (Displayable) ob1;
                    d.link((Displayable) ob2, false);
                } else {
                    Utils.log("Loader: broken link between " + id1 + " and " + id2);
                }
            }
            rl.close();
        } catch (Exception e) {
            IJError.print(e);
            return null;
        }
        return root;
    }
}
Also used : Displayable(ini.trakem2.display.Displayable) ZDisplayable(ini.trakem2.display.ZDisplayable) HashMap(java.util.HashMap) LayerThing(ini.trakem2.tree.LayerThing) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException)

Aggregations

TemplateThing (ini.trakem2.tree.TemplateThing)17 HashMap (java.util.HashMap)11 ArrayList (java.util.ArrayList)10 Displayable (ini.trakem2.display.Displayable)9 ZDisplayable (ini.trakem2.display.ZDisplayable)9 ProjectThing (ini.trakem2.tree.ProjectThing)9 TreePath (javax.swing.tree.TreePath)9 LayerThing (ini.trakem2.tree.LayerThing)8 Project (ini.trakem2.Project)7 Layer (ini.trakem2.display.Layer)7 DefaultMutableTreeNode (javax.swing.tree.DefaultMutableTreeNode)7 LayerSet (ini.trakem2.display.LayerSet)6 DBObject (ini.trakem2.persistence.DBObject)6 ResultSet (java.sql.ResultSet)6 GenericDialog (ij.gui.GenericDialog)5 FSLoader (ini.trakem2.persistence.FSLoader)4 LayerTree (ini.trakem2.tree.LayerTree)4 ProjectTree (ini.trakem2.tree.ProjectTree)4 TemplateTree (ini.trakem2.tree.TemplateTree)4 SQLException (java.sql.SQLException)4