using database table want create collection type output in groovy.
def categorylist =new hashmap();//to hold final result def parents=new hashmap(); def childs1= new arraylist(); def childs2= new hashmap(); def childs3= new arraylist(); list catlist =testing.executequery("from testing orgid in(:test)",['test':oid]) catlist.each{ if(it.parent==0){ def m=new hashmap(); m.put('id',it.id) m.put('title',it.name) def al =new arraylist() m.put('children',al) categorylist.put(it.id,m); parents.put(it.id,it) }else{ childs1.add(it) } } childs1.each{ def par = parents.get(it.parent) if(par){ childs2.put(it.id,it) }else{ childs3.add(it) } } def reg = new hashmap();/*to hold sub trees merging childs3 main childs2*/ childs3.each{ def par = childs2.get(it.parent) if(par){ if(!reg.get(par.id)){ map m = new hashmap() m.put('id',par.id) m.put('title',par.name) def al =new arraylist() al.add([id:it.id,title:it.name,parent:it.parent]) m.put('children',al) reg.put(par.id,m); }else{ reg.get(par.id).get('children').add([id:it.id,title:it.name,parent:it.parent]) } } } /*merging sub trees main tree */ childs2.values().each{ def par = categorylist.get(it.parent) if(par){ if(reg.get(it.id)){ par.get('children').add(reg.get(it.id)) }else{ par.get('children').add([id:it.id,title:it.name,parent:it.parent]) } } } def finallist = new arraylist(categorylist.values()); } } i can able create tree upto 3 level.but want add 1 more subtree level.database having id,tilte , parent fields. can me solve problem..?
Comments
Post a Comment