// Create a lookup map for finding the parent to add each group to:
HashMap<String, GroupTreeNode> groups = new HashMap<String, GroupTreeNode>();
LinkedHashMap<GroupTreeNode, String> parentIds = new LinkedHashMap<GroupTreeNode, String>();
while ( rs.next()) {
AbstractGroup group = null;
String typeId = findGroupTypeName(rs.getString("group_types_id"), conn);
if (typeId.equals(AllEntriesGroup.ID)) {
// register the id of the root node:
groups.put(rs.getString("groups_id"), rootNode);
}
else if (typeId.equals(ExplicitGroup.ID)) {
group = new ExplicitGroup(rs.getString("label"),
rs.getInt("hierarchical_context"));
}
else if (typeId.equals(KeywordGroup.ID)) {
System.out.println("Keyw: "+ rs.getBoolean("case_sensitive"));
group = new KeywordGroup(rs.getString("label"),
Util.unquote(rs.getString("search_field"), '\\'),
Util.unquote(rs.getString("search_expression"), '\\'),
rs.getBoolean("case_sensitive"), rs.getBoolean("reg_exp"),
rs.getInt("hierarchical_context"));
}
else if (typeId.equals(SearchGroup.ID)) {
System.out.println("Search: "+ rs.getBoolean("case_sensitive"));
group = new SearchGroup(rs.getString("label"),
Util.unquote(rs.getString("search_expression"), '\\'),
rs.getBoolean("case_sensitive"), rs.getBoolean("reg_exp"),
rs.getInt("hierarchical_context"));
}
if (group != null) {
GroupTreeNode node = new GroupTreeNode(group);
parentIds.put(node, rs.getString("parent_id"));
groups.put(rs.getString("groups_id"), node);
}
}
statement.close();
// Ok, we have collected a map of all groups and their parent IDs,
// and another map of all group IDs and their group nodes.
// Now we need to build the groups tree:
for (Iterator<GroupTreeNode> i=parentIds.keySet().iterator(); i.hasNext();) {
GroupTreeNode node = i.next();
String parentId = parentIds.get(node);
// Look up the parent:
GroupTreeNode parent = groups.get(parentId);
if (parent == null) {
// TODO: missing parent
}
else {
parent.add(node);
}
}
// If we have explicit groups, set up group membership:
res = processDMLWithResults(conn, "SELECT * FROM entry_group;");
if (res instanceof Statement) {
statement = (Statement)res;
rs = statement.getResultSet();
while ( rs.next()) {
String entryId = rs.getString("entries_id"),
groupId = rs.getString("groups_id");
GroupTreeNode node = groups.get(groupId);
if ((node != null) && (node.getGroup() instanceof ExplicitGroup)) {
ExplicitGroup group = (ExplicitGroup)node.getGroup();
group.addEntry(entries.get(entryId));
} else {
// TODO: unable to find explicit group with the given id
}
}
statement.close();