extバックグラウンド調査データベースデータ組立treeデータ


由来:Extjs交流群(164648099)
 
説明:
2つのファイルJAva treeVOオブジェクトデータベース内のツリーのデータベースをここに格納
jackytree.JAvaは再帰アルゴリズムを用いて一度に検出したデータをtreeJSON構造を生成する
2つのファイルをeclipseのrun asに直接コピーすると表示されます.
 
jackyNode.java
package com.codeasy.common;


public class jackyNode {
	private int id;
	private int parentId;
	private String text;
	jackyNode(){}
	jackyNode(int id,int parentId,String text){
		this.id=id;
		this.parentId = parentId;
		this.text = text;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public int getParentId() {
		return parentId;
	}
	public void setParentId(int parentId) {
		this.parentId = parentId;
	}
	public String getText() {
		return text;
	}
	public void setText(String text) {
		this.text = text;
	}
}


 
 
jackytree.java 
package com.codeasy.common;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/*
 *             
 * id  parentid  name 
 * 
 * 
 * 
 */

public class jackytree {
	List nodeList =new ArrayList();
	jackytree(){//          List
		jackyNode node1 = new jackyNode(1,0,"   ");  
        jackyNode node2 = new jackyNode(2,1,"    ");  
        jackyNode node3 = new jackyNode(3,1,"    ");  
        jackyNode node4 = new jackyNode(4,2,"    ");  
        jackyNode node5 = new jackyNode(5,2,"    ");  
        jackyNode node6 = new jackyNode(6,2,"    ");  
        jackyNode node7 = new jackyNode(7,6,"    ");  
        jackyNode node8 = new jackyNode(8,6,"    ");  
          
        nodeList.add(node1);  
        nodeList.add(node2);  
        nodeList.add(node3);  
        nodeList.add(node4);  
        nodeList.add(node5);  
        nodeList.add(node6);  
        nodeList.add(node7);  
        nodeList.add(node8);  
	}
	StringBuffer returnStr=new StringBuffer();  
    public void recursionFn(List list , jackyNode node){  
        if(hasChild(list,node)){  
            returnStr.append("{id:");
            returnStr.append(node.getId());
            returnStr.append(",text:'");
            returnStr.append(node.getText());
            returnStr.append("'");
            returnStr.append(",children:[");  
            List childList = getChildList(list,node);  
            Iterator it = childList.iterator();  
            while(it.hasNext()){  
                jackyNode n = (jackyNode)it.next();  
                recursionFn(list,n);  
            }  
            returnStr.append("]},");  
        }else{  
            returnStr.append("{id:");
            returnStr.append(node.getId());
            returnStr.append(",text:'");
            returnStr.append(node.getText());
            returnStr.append("'");
            returnStr.append(",leaf:true},");  
        }  
          
    }  
    public boolean hasChild(List list, jackyNode node){  //        
    	return getChildList(list,node).size()>0?true:false;
    }
    public List getChildList(List list , jackyNode node){  //       
        List li = new ArrayList();  
        Iterator it = list.iterator();  
        while(it.hasNext()){  
            jackyNode n = (jackyNode)it.next();  
            if(n.getParentId()==node.getId()){  
                li.add(n);  
            }  
        }  
        return li;  
    }
    public String modifyStr(String returnStr){//        Extjs Json  
    	return ("["+returnStr+"]").replaceAll(",]", "]");
    	
    }
    public static void main(String[] args) {  
        jackytree r = new jackytree();  
        r.recursionFn(r.nodeList, new jackyNode(1,0,"   "));  
        System.out.println(r.modifyStr(r.returnStr.toString()));  
    }  
}