>From: "stephan opitz" <[EMAIL PROTECTED]> 
>
> problem is the structure. 
> 
> if i have an object containing an id and a parentId (which is null, or 
> linking to another object -> the parent one) 
> 
> if i get a list from db how is it possible to build up the structure 
> for the tree2 
> 
> i tried hashmap or recursive algorithm, but nothing with much success... 
> 
> any ideas 
> 

You might consider the following code snippet. 


public class TreeBacker {
    
    public TreeNode findNode(TreeNode root, Integer sarg) {
        Integer id = root.getDescription() == null ? null : new 
Integer(root.getDescription());    
        if (compare(id, sarg) == 0) {
            return root;
        }
        Iterator ci = root.getChildren().iterator();
        while (ci.hasNext()) {
            TreeNode found = findNode((TreeNode) ci.next(), sarg);
            if (found != null) 
                return found;
        }
        
        return null;
    }
    
    
    public TreeNode getTreeData() {
        
        TreeNode root = new TreeNodeBase("root", null, false);
        TreeNode current = root; // current root
        
        Collection dbRows = getDBRows();
        Iterator di = dbRows.iterator();
        while (di.hasNext()) {
            MyVO vo = (MyVO) di.next();
            
            Integer id = current.getDescription() == null ? null : new 
Integer(current.getDescription());
            int order = compare(id, vo.getParent());
            
            if (order != 0) {
                current = findNode(root, vo.getParent());
                // if current is null you have an orphaned node  
            }
            
            String type = vo.getParent() == null ? null : 
vo.getParent().toString();
            String desc = vo.getId() == null ? null : vo.getId().toString();
            
            current.getChildren().add(new TreeNodeBase(type, desc, false));
        }
        
        return root;
    }
    
    
    
    
    public Collection getDBRows() {    
        
        // select parent, id from table order by parent, id
        
        // null, 1
        // null, 2
        // null, 3
        // 1, 4
        // 1, 5
        // 2, 6
        // 2, 7
        // 3, 8
        // 3, 9
        // 9, 10
        // 9, 11
        
        Set sortedList = new TreeSet();
        sortedList.add(new MyVO(null, new Integer(1)));
        sortedList.add(new MyVO(new Integer(1), new Integer(4)));
        sortedList.add(new MyVO(new Integer(1), new Integer(5)));
        
        sortedList.add(new MyVO(null, new Integer(2)));
        sortedList.add(new MyVO(new Integer(2), new Integer(6)));
        sortedList.add(new MyVO(new Integer(2), new Integer(7)));
        
        sortedList.add(new MyVO(null, new Integer(3)));
        sortedList.add(new MyVO(new Integer(3), new Integer(8)));
        sortedList.add(new MyVO(new Integer(3), new Integer(9)));
        
        sortedList.add(new MyVO(new Integer(9), new Integer(10)));
        sortedList.add(new MyVO(new Integer(9), new Integer(11)));
        
        
        return sortedList; 
    }
    
    class MyVO implements Comparable {
        
        
        private Integer parent = null;
        private Integer id = null;
        
        public MyVO(Integer parent, Integer id) {
            this.parent = parent;
            this.id = id;
        }
        public Integer getId() {
            return id;
        }
        public void setId(Integer id) {
            this.id = id;
        }
        public Integer getParent() {
            return parent;
        }
        public void setParent(Integer parent) {
            this.parent = parent;
        }
        
        
        public int compareTo(Object obj) {
            
            MyVO vo = (MyVO) obj; 
            int order = compare(parent, vo.parent);
            if (order == 0)  
                order = compare(id, vo.id);
            
            return order;
        }
        
    }
    
    
    public static int compare(Integer col1, Integer col2) {
        
        if ((col1 == null) && (col2 == null))
            return 0;
        else if ((col1 == null) && (col2 != null))
            return -1;
        else if ((col2 == null) && (col1 != null))
            return 1;
        else 
            return ((Comparable) col1).compareTo(col2);    
    }
    
    
    
    public static void main(String[] args) {
        TreeBacker backer = new TreeBacker();
        TreeNode root = backer.getTreeData();
        
    }
    
    
}


> stephan 

Gary

Reply via email to