Generate Dynamic RichTree in Richfaces

TreeManager.java

public class TreeManager implements Serializable{
@ManagedProperty(value="#{BuyerServiceBean}")
BuyerServiceBean buyerServiceBean;

public BuyerServiceBean getBuyerServiceBean() {
return buyerServiceBean;
}

public void setBuyerServiceBean(BuyerServiceBean buyerServiceBean) {
this.buyerServiceBean = buyerServiceBean;
}
private TreeNode<String> root = null;
private String nodeTitle;
private String selectedCategory;

public TreeManager() {}

public TreeNode getRoot() {
if (root == null) {
List<DepartmentMasterDataBean> c = new ArrayList<DepartmentMasterDataBean>();
c = buyerServiceBean.treeList(0);
TreeModel tm = null;
tm = new TreeModel(c);
root = tm.getRoot();
}
return root;
}

public void processSelection(NodeSelectedEvent event) {
HtmlTree tree = (HtmlTree) event.getComponent();
selectedCategory = (String)tree.getRowData();
String test = tree.getRowKey().toString();
Integer id = Integer.parseInt(test.substring(test.lastIndexOf(":")+1, test.length()));
buyerServiceBean.getDepartmentMasterBindBean().setParentdeptid(id);
setNodeTitle(selectedCategory);
}

public String getNodeTitle() {
return nodeTitle;
}

public void setNodeTitle(String nodeTitle) {
this.nodeTitle = nodeTitle;
}

public void setRoot(TreeNode<String> root) {
this.root = root;
}

public String getSelectedCategory() {
return selectedCategory;
}

public void setSelectedCategory(String selectedCategory) {
this.selectedCategory = selectedCategory;
}
}

TreeModel.java

public class TreeModel {

private TreeNode<String> root;

public TreeModel(List<DepartmentMasterDataBean> c) {
root = new TreeNodeImpl<String>();
root.setParent(null);
root.setData("Kategorie");
createChildren(root, c);
}

private void createChildren(TreeNode<String> r, List<DepartmentMasterDataBean> ch) {
for (DepartmentMasterDataBean c : ch) {
TreeNode<String> nodeImpl = new TreeNodeImpl<String>();
nodeImpl.setData(c.getDeptname());
r.addChild(c.getDeptid(), nodeImpl);
if (c.getChildren() != null) {
createChildren(nodeImpl, c.getChildren());
}
}
}

public TreeNode getRoot() {
return root;
}
}

Important Method 4 Iteration

public List<DepartmentMasterDataBean> treeList(Integer deptid) {
List<DepartmentMasterDataBean> dataBeans = new ArrayList<DepartmentMasterDataBean>();
List<DepartmentMaster> dms = departmentMasterService.getDepartmentList("parentdeptid", Operation_enum.EQ, deptid);
for (DepartmentMaster departmentMaster : dms) {
dataBeans.add(_toDepartmentMasterDataBean(departmentMaster));
}
return dataBeans;
}

public DepartmentMasterDataBean _toDepartmentMasterDataBean(DepartmentMaster departmentMaster) {
DepartmentMasterDataBean dmdb = new DepartmentMasterDataBean();
List<DepartmentMasterDataBean> list1 = new ArrayList<DepartmentMasterDataBean>();
dmdb.setDeptid(departmentMaster.getDeptid());
dmdb.setDeptname(departmentMaster.getDeptname());
dmdb.setParentdeptid(departmentMaster.getParentdeptid());
dmdb.setAddress(departmentMaster.getAddress());
dmdb.setDatentime(departmentMaster.getDatentime());
dmdb.setPhone(departmentMaster.getPhone());
List<DepartmentMaster> dms = new ArrayList<DepartmentMaster>();
dms = departmentMasterService.getDepartmentList("parentdeptid", Operation_enum.EQ, departmentMaster.getDeptid());
for (DepartmentMaster departmentMaster1 : dms) {
list1.add(_toDepartmentMasterDataBean(departmentMaster1));
}
dmdb.setChildren(list1);
return dmdb;
}
Advertisements
This entry was posted in Richfaces. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s