* @return The number of matched Contexts.
*/
public static int get(WorkSpace sp, PTaskTemplate template, OutputStream out)
throws Exception
{
XMLOutput o = new XMLOutput();
SerializationHandler h = o.wrap(out);
org.syrup.Context c[] = sp.get(template);
Hashtable parents = new Hashtable();
// Make a parent Task table
for (int i = 0; i < c.length; i++)
{
org.syrup.Context pc = c[i];
if (pc.task().isParent())
{
Hashtable pn = new Hashtable();
pn.put("_parent", pc);
parents.put(pc.task().key(), pn);
}
}
// Put the child Tasks underneath the parent Tasks.
for (int i = 0; i < c.length; i++)
{
org.syrup.Context ct = c[i];
if (!ct.task().isParent())
{
Hashtable pn = (Hashtable) parents.get(ct.task().parentKey());
if (pn != null)
{
pn.put(ct.task().key(), ct);
}
else
{
parents.put(ct.task().key(), ct);
}
}
}
Hashtable cl = new Hashtable(parents);
Iterator keys = cl.keySet().iterator();
// Build hierarchy
while (keys.hasNext())
{
String k = (String) keys.next();
Object ph = (Object) cl.get(k);
if (ph instanceof Hashtable)
{
Hashtable phh = (Hashtable) ph;
org.syrup.Context ct = (org.syrup.Context) phh.get("_parent");
Hashtable oo = (Hashtable) cl.get(ct.task().parentKey());
if (oo != null)
{
parents.remove(k);
oo.put(k, phh);
}
}
}
// Outputs the fetched Contexts to the OutputStream in XML format.
o.startDocument("get", h);
output(parents, o, h);
o.endDocument("get", h);
return c.length;
}