M
madiraluap
Hi Everyone,
I am trying to emulate a tree structure like this:
A
A.1
A.1.1
A.2
B
C
C.1
C.1.1
and so on.
I have the sql query figured out.
It returns the parent id, folder name, category code, the path, level
and the leaf indicator.
Now I am trying to put it through a recursive function but i am not
getting it correctly.
I am sure someone here has run into the same troubles and was wondering
if you could suggest anything. Any help would be appreciated.
This is my query and code:
QUERY
SELECT Folder.FOLDER_ID,
Folder.FOLDER_NAME,
Folder.CATEGORY_CD,
Folder.PARENT_FOLDER_ID,
CodeValue.CODE_VALUE AS CODE_VALUE1,
CodeValue.DISPLAY,
LPAD(' ', 2*level-1)|| decode (category_cd,-1,'
','\'||category_cd) || SYS_CONNECT_BY_PATH( folder_name, '\') "Path",
(level+1) L,
CONNECT_BY_ISLEAF "IsLeaf"
FROM FOLDER Folder, CODE_VALUE CodeValue
WHERE Folder.CATEGORY_CD = CodeValue.CODE_VALUE
connect by Folder.parent_folder_id = prior Folder.folder_id
start with parent_folder_ID is null
ORDER SIBLINGS BY folder_name
I am using oracle adf and view objects to do this:
public DefaultMutableTreeNode getCompleteTree()
{
DocumentTreeViewImpl dtv = getDocumentTreeView();
DefaultMutableTreeNode root = new
DefaultMutableTreeNode("ROOT");
//retrieving the categories and the level 1 folders first.
dtv.executeQuery();
dtv.reset();
TreeMap catfolders = new TreeMap();
while (dtv.hasNext())
{
DocumentTreeViewRowImpl dtvrow = (DocumentTreeViewRowImpl)
dtv.next();
if (dtvrow != null)
{
if ((dtvrow.getCategoryCd().intValue() != -1) &&
(dtvrow.getIsleaf().intValue() == 1))
{
String currentCategory = dtvrow.getDisplay();
DefaultMutableTreeNode currentCat = new
DefaultMutableTreeNode(currentCategory);
if (!catfolders.containsKey(currentCategory))
{
ArrayList folders = new ArrayList();
String firstFolder = dtvrow.getFolderName();
DefaultMutableTreeNode ff = new
DefaultMutableTreeNode(firstFolder);
System.out.println("Adding new category " +
currentCategory);
System.out.println("First folder: " + ff);
folders.add(ff);
catfolders.put(currentCategory, folders);
}
else
{
ArrayList fl = ((ArrayList)
(catfolders.get(currentCategory)));
System.out.println("Consequent folder: " +
dtvrow.getFolderName());
if (fl.contains(new
DefaultMutableTreeNode(dtvrow.getFolderName())) == false)
{
((ArrayList)
(catfolders.get(currentCategory))).add(new
DefaultMutableTreeNode(dtvrow.getFolderName()));
}
}
}
else if ((dtvrow.getCategoryCd().intValue() != -1) &&
(dtvrow.getIsleaf().intValue() == 0))
{
String currentCategory = dtvrow.getDisplay();
DefaultMutableTreeNode rnode = getChildren(new
DefaultMutableTreeNode(dtvrow.getFolderName()));
((ArrayList)
(catfolders.get(currentCategory))).add(rnode);
}
}
}
Set keys = catfolders.keySet();
for (Iterator it = keys.iterator(); it.hasNext()
{
String currentKey = it.next().toString();
ArrayList currentFolderList = ((ArrayList)
(catfolders.get(currentKey)));
DefaultMutableTreeNode currentCat = new
DefaultMutableTreeNode(currentKey);
for (int i = 0; i < currentFolderList.size(); i++)
{
currentCat.add((DefaultMutableTreeNode)
currentFolderList.get(i));
}
root.add(currentCat);
}
return root;
}
public DefaultMutableTreeNode getChildren(DefaultMutableTreeNode
parent)
{
DocumentTreeViewImpl dtv = getDocumentTreeView();
while (dtv.hasNext())
{
DocumentTreeViewRowImpl dtvrow = (DocumentTreeViewRowImpl)
dtv.next();
if (dtvrow.getCategoryCd().intValue() == -1) // as long as
its a child folder keep going
{
if (dtvrow.getIsleaf().intValue() == 0) //not leaf
{
DefaultMutableTreeNode childNode = new
DefaultMutableTreeNode(dtvrow.getFolderName());
getChildren(childNode);
}
else if (dtvrow.getIsleaf().intValue() == 1) //leaf
{
DefaultMutableTreeNode childNode = new
DefaultMutableTreeNode(dtvrow.getFolderName(), false);
parent.add(childNode);
}
}
else
{
//parent.add(child);
break; //return parent;
}
}
return parent;
}
It seems that at a particular point it just keeps on adding to the
child of a child instead of returning back to the last parent. I have
no clue how to "rewind" it back to the last parent.
I am trying to emulate a tree structure like this:
A
A.1
A.1.1
A.2
B
C
C.1
C.1.1
and so on.
I have the sql query figured out.
It returns the parent id, folder name, category code, the path, level
and the leaf indicator.
Now I am trying to put it through a recursive function but i am not
getting it correctly.
I am sure someone here has run into the same troubles and was wondering
if you could suggest anything. Any help would be appreciated.
This is my query and code:
QUERY
SELECT Folder.FOLDER_ID,
Folder.FOLDER_NAME,
Folder.CATEGORY_CD,
Folder.PARENT_FOLDER_ID,
CodeValue.CODE_VALUE AS CODE_VALUE1,
CodeValue.DISPLAY,
LPAD(' ', 2*level-1)|| decode (category_cd,-1,'
','\'||category_cd) || SYS_CONNECT_BY_PATH( folder_name, '\') "Path",
(level+1) L,
CONNECT_BY_ISLEAF "IsLeaf"
FROM FOLDER Folder, CODE_VALUE CodeValue
WHERE Folder.CATEGORY_CD = CodeValue.CODE_VALUE
connect by Folder.parent_folder_id = prior Folder.folder_id
start with parent_folder_ID is null
ORDER SIBLINGS BY folder_name
I am using oracle adf and view objects to do this:
public DefaultMutableTreeNode getCompleteTree()
{
DocumentTreeViewImpl dtv = getDocumentTreeView();
DefaultMutableTreeNode root = new
DefaultMutableTreeNode("ROOT");
//retrieving the categories and the level 1 folders first.
dtv.executeQuery();
dtv.reset();
TreeMap catfolders = new TreeMap();
while (dtv.hasNext())
{
DocumentTreeViewRowImpl dtvrow = (DocumentTreeViewRowImpl)
dtv.next();
if (dtvrow != null)
{
if ((dtvrow.getCategoryCd().intValue() != -1) &&
(dtvrow.getIsleaf().intValue() == 1))
{
String currentCategory = dtvrow.getDisplay();
DefaultMutableTreeNode currentCat = new
DefaultMutableTreeNode(currentCategory);
if (!catfolders.containsKey(currentCategory))
{
ArrayList folders = new ArrayList();
String firstFolder = dtvrow.getFolderName();
DefaultMutableTreeNode ff = new
DefaultMutableTreeNode(firstFolder);
System.out.println("Adding new category " +
currentCategory);
System.out.println("First folder: " + ff);
folders.add(ff);
catfolders.put(currentCategory, folders);
}
else
{
ArrayList fl = ((ArrayList)
(catfolders.get(currentCategory)));
System.out.println("Consequent folder: " +
dtvrow.getFolderName());
if (fl.contains(new
DefaultMutableTreeNode(dtvrow.getFolderName())) == false)
{
((ArrayList)
(catfolders.get(currentCategory))).add(new
DefaultMutableTreeNode(dtvrow.getFolderName()));
}
}
}
else if ((dtvrow.getCategoryCd().intValue() != -1) &&
(dtvrow.getIsleaf().intValue() == 0))
{
String currentCategory = dtvrow.getDisplay();
DefaultMutableTreeNode rnode = getChildren(new
DefaultMutableTreeNode(dtvrow.getFolderName()));
((ArrayList)
(catfolders.get(currentCategory))).add(rnode);
}
}
}
Set keys = catfolders.keySet();
for (Iterator it = keys.iterator(); it.hasNext()
{
String currentKey = it.next().toString();
ArrayList currentFolderList = ((ArrayList)
(catfolders.get(currentKey)));
DefaultMutableTreeNode currentCat = new
DefaultMutableTreeNode(currentKey);
for (int i = 0; i < currentFolderList.size(); i++)
{
currentCat.add((DefaultMutableTreeNode)
currentFolderList.get(i));
}
root.add(currentCat);
}
return root;
}
public DefaultMutableTreeNode getChildren(DefaultMutableTreeNode
parent)
{
DocumentTreeViewImpl dtv = getDocumentTreeView();
while (dtv.hasNext())
{
DocumentTreeViewRowImpl dtvrow = (DocumentTreeViewRowImpl)
dtv.next();
if (dtvrow.getCategoryCd().intValue() == -1) // as long as
its a child folder keep going
{
if (dtvrow.getIsleaf().intValue() == 0) //not leaf
{
DefaultMutableTreeNode childNode = new
DefaultMutableTreeNode(dtvrow.getFolderName());
getChildren(childNode);
}
else if (dtvrow.getIsleaf().intValue() == 1) //leaf
{
DefaultMutableTreeNode childNode = new
DefaultMutableTreeNode(dtvrow.getFolderName(), false);
parent.add(childNode);
}
}
else
{
//parent.add(child);
break; //return parent;
}
}
return parent;
}
It seems that at a particular point it just keeps on adding to the
child of a child instead of returning back to the last parent. I have
no clue how to "rewind" it back to the last parent.