Search in sources :

Example 31 with TemplateThing

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

the class DBLoader method unpack.

/**
 * Recursive. Assumes all TemplateThing objects have a unique type.
 */
private void unpack(TemplateThing root, HashMap<String, TemplateThing> hs_tt) {
    String type = root.getType();
    // avoid replacing, the higher level one is the right one (for example for neurite_branch)
    if (null != hs_tt.get(type))
        return;
    hs_tt.put(type, root);
    if (null == root.getChildren())
        return;
    for (TemplateThing tt : root.getChildren()) {
        unpack(tt, hs_tt);
    }
}
Also used : TemplateThing(ini.trakem2.tree.TemplateThing)

Example 32 with TemplateThing

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

the class DBLoader method getChildrenProjectThings.

private ArrayList<ProjectThing> getChildrenProjectThings(Project project, long parent_id, String parent_type, HashMap<String, TemplateThing> hs_tt, HashMap<Long, Displayable> hs_d) throws Exception {
    final ArrayList<ProjectThing> al_children = new ArrayList<ProjectThing>();
    ResultSet r = null;
    if (-1 == parent_id)
        Utils.log("parent_id = -1 for parent_type=" + parent_type);
    if (parent_type.equals("profile_list")) {
        // the project_id field is redundant
        r = connection.prepareStatement("SELECT ab_things.* FROM ab_things,ab_displayables,ab_layers WHERE ab_things.parent_id=" + parent_id + " AND ab_things.object_id=ab_displayables.id AND ab_displayables.layer_id=ab_layers.id ORDER BY ab_layers.z,ab_things.id ASC").executeQuery();
    } else {
        r = connection.prepareStatement("SELECT * FROM ab_things WHERE parent_id=" + parent_id + " ORDER BY id").executeQuery();
    }
    while (r.next()) {
        ProjectThing thing = getProjectThing(r, project, hs_tt, hs_d);
        if (null != thing)
            al_children.add(thing);
    }
    r.close();
    return al_children;
}
Also used : ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) ProjectThing(ini.trakem2.tree.ProjectThing)

Example 33 with TemplateThing

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

the class DBLoader method getRootProjectThing.

/**
 * Get all the Thing objects, recursively, for the root, and their corresponding encapsulated objects. Also, fills in the given ArrayList with all loaded Displayable objects.
 */
public ProjectThing getRootProjectThing(Project project, TemplateThing root_tt, TemplateThing project_tt, HashMap<Long, Displayable> hs_d) {
    synchronized (db_lock) {
        // connect if disconnected
        if (!connectToDatabase()) {
            return null;
        }
        // unpack root_tt (assumes TemplateThing objects have unique types, skips any repeated type to avoid problems in recursive things such as neurite_branch)
        HashMap<String, TemplateThing> hs_tt = new HashMap<String, TemplateThing>();
        unpack(root_tt, hs_tt);
        ProjectThing root = null;
        try {
            // -1 signals root
            ResultSet r = connection.prepareStatement("SELECT * FROM ab_things WHERE project_id=" + project.getId() + " AND type='project' AND parent_id=-1").executeQuery();
            if (r.next()) {
                long id = r.getLong("id");
                root = new ProjectThing(project_tt, project, id, project, getChildrenProjectThings(project, id, project_tt.getType(), hs_tt, hs_d));
            }
            r.close();
            if (null == root) {
                Utils.log("Loader.getRootProjectThing: can't find it for project id=" + project.getId());
                return null;
            }
        } catch (Exception e) {
            IJError.print(e);
            return null;
        }
        return root;
    }
}
Also used : HashMap(java.util.HashMap) TemplateThing(ini.trakem2.tree.TemplateThing) ResultSet(java.sql.ResultSet) ProjectThing(ini.trakem2.tree.ProjectThing) SQLException(java.sql.SQLException)

Example 34 with TemplateThing

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

the class DBLoader method getTemplateRoot.

/**
 * Fetch the root of the TemplateThing tree from the database-stored hierarchy of TemplateThing objects defined in the original XML file .
 */
public TemplateThing getTemplateRoot(Project project) {
    // connect if disconnected
    if (!connectToDatabase()) {
        return null;
    }
    TemplateThing root = null;
    synchronized (db_lock) {
        // New way: TemplateThing instances are saved in the ab_things table
        try {
            // fetch TemplateThings, which have no stored object.
            // signature of the root TemplateThing is parent_id=-1 and object_id=-1
            ResultSet r = connection.prepareStatement("SELECT * FROM ab_things WHERE project_id=" + project.getId() + " AND parent_id=-1 AND object_id=-1").executeQuery();
            if (r.next()) {
                long id = r.getLong("id");
                String type = r.getString("type");
                root = new TemplateThing(type, project, id);
                root.setup(getChildrenTemplateThings(project, id));
            }
            r.close();
        } catch (Exception e) {
            IJError.print(e);
            return null;
        }
    }
    return root;
}
Also used : TemplateThing(ini.trakem2.tree.TemplateThing) ResultSet(java.sql.ResultSet) SQLException(java.sql.SQLException)

Example 35 with TemplateThing

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

the class TemplateThing method remove.

public boolean remove(boolean check) {
    if (check) {
        if (!Utils.check("Really delete " + this.toString() + (null == al_children || 0 == al_children.size() ? "" : " and all its children?")))
            return false;
    }
    // remove the children, recursively
    if (null != al_children) {
        Object[] children = new Object[al_children.size()];
        // can't delete directly from the al_children because the child will call removeChild on its parent
        al_children.toArray(children);
        for (int i = 0; i < children.length; i++) {
            Object ob = children[i];
            if (ob instanceof DBObject) {
                if (!((DBObject) ob).remove(false)) {
                    Utils.showMessage("Deletion incomplete, check database, for child: " + ob.toString());
                    return false;
                }
            }
        }
    }
    // remove the Thing itself
    if (null != parent && !parent.removeChild(this)) {
        Utils.showMessage("Deletion incomplete, check database, for parent of TemplateThing id=" + id);
        return false;
    }
    return removeFromDatabase();
}
Also used : DBObject(ini.trakem2.persistence.DBObject) DBObject(ini.trakem2.persistence.DBObject)

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