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);
}
}
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;
}
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;
}
}
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;
}
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();
}
Aggregations