1
votes

![enter image description here][1]I have class as it is defined bellow and I read the database fill the records with TbMenu List of TbMenu will be generated.

TbMenu is having its Children list pointing to Parent Id

case class TbMenu(Name:String,Url:String,Children:List[TbMenu]) - Squeryl Database case class

I want to write method to Read above structure recursively and create another list of of object according to following structure

case class MenuBO(Name:String,Url:String,Children:List[TbMenuBO])

It will be really great help to give me some sample implementation of above.

def GenerateMenuData( orgData: List[TbMenu]): List[MenuBO] = { val list: List[MenuBO] = Nil def helper(orgData:List[TbMenu], result:List[MenuBO]): List[MenuBO] = { orgData match{ case x :: tail => { val Menu = new MenuBO(x.id, x.Description, x.ParentId, x.Url, x.haschildren, null) x.children.toList match{ case x:: tail =>{ val Menu2 = new MenuBO(x.id, x.Description, x.ParentId, x.Url, x.haschildren, null) helper(tail, result ::: List(Menu2)) } case Nil => return result} Menu.Children=result helper(tail, result ::: List(Menu)) }case Nil => result }} helper(orgData,list) }

Thank you in advance

1
What is TbMenuBO? Was it intended to be MenuBO? - David Frank
TbMenuBO is a Business object that is used to draw menu in UI layer with play framework after filling it from TbMenu at the service layer - Bimal Kaluarachchi
What's the difference between TbMenuBO and MenuBO? - David Frank
TbMenu used to retrive data from Squeryl Database it has self relation defined to it self like parent child relation ship - Bimal Kaluarachchi
My question was about TbMenuBO, a list of which you are storing in class MenuBO. - David Frank

1 Answers

0
votes

Assuming that TbMenuBO is the same as MenuBO, the solution can be the following:

def Read(menu: TbMenu): MenuBO = {
  menu match {
    case TbMenu(name, url, children) => MenuBO(name, url, children.map(Read))
  }
}

I suggest you adhering to the official coding conventions (method names and field names should start with lower case characters).