How to write a recursive method for finding parent and child

Viewed 1769

This is to save the details with recursive.

Here I want to get the details from the Database and set into my bean with a recursive method. So I can dispaly in angularUi tree formate. How can I write the recursive method to set into my beans.

My DB Stricture:- enter image description here

I am separating the parents and childs with rowId.you can access my sample screen.

Eg:- Rowid is 1 for parent the child for this 1 is 1.1 and child for 1.1 is 1.1.1 like this it will prolong,,.

I am saving all the parents and child in a single table which is above image.

there will be items[] for each and every object(Row). If there is any child for parent then the child will be added in that items[] array, if that child have any child then the child will be added in the parent of that row's items[]... like this it will prolong.

Eg:- JSON object is:-

{
    "id": 1,
    "rowId": "1",
    "items": [
      {
        "id": 10,
        "rowId": "1.1",
        "items": [
          {
            "id": 100,
            "rowId": "1.1.1",
            "items": [
              {
                "id": 1000,
                "rowId": "1.1.1.1",
                "items": []
              }
            ]
          }
        ]
      },
      {
        "id": 11,
        "rowId": "1.2",
        "items": []
      }
    ]
  }

I have saved this data using the this answer.

But while retriving I am facing problems. The problem is while retrieving there will not be any parent and childs as the data will be saved in same table.The relationship is only rowid. For This I need to write a recursive method like saving, and need to add the childs to parent items[] array.

public class AdminComponentBean{

    List<MultiAdminComponent> componentListbean;
}

MultiAdminComponent.java:-

public class MultiAdminComponent {

    private String componentName;
    private String componentIdentification;
    private String componentType;
    private String componentState;
    private String componentUrl;
    private String rowId;
    private List<MultiAdminComponent> items;
}

Here I try to retrive all the details and trying to add the childs to parent.But it should make a recursive method

List<MultiAdminComponent> componentList=BaseDAO.getAdminComponentDAOObject().getComponentDetails();
   if(null != componentList) {
       for(MultiAdminComponent itemsList : componentList){
           if(itemsList.getRowId().length().equals() "1"){//here parent row will come
               //by considering rowid I need to find the child of the rowId
               //child of 1 is 1.1
               //if 1.1 is child of 1 then I need to add that 1.1 object to `items[]` array of 1
               //like this it should work recursve
            }
        }
    }
1 Answers
Related