仅从嵌套列表中获取重要数据

时间:2012-11-18 18:35:04

标签: list grails groovy many-to-one

嗨!我想获得下载链接的重要信息(完整流程)。

我的域名设计如下:

Serie { hasMany[Season] }
Season { hasMany[Episode] and belongsTo[Serie] }
Episode { hasMany[DownloadInfo] and belongsTo[Season] }
DownloadInfo { link nullable:true //among other data }

我的数据库中有大约这么多记录:

serie:4500
season:500
episode:6500
download_info:10000

我们可以看到有很多系列没有其他信息。

我怎样才能只获得所有这些系列>季节>剧集> downloadInfo数据?

例如,拥有:

Serie "A"
  Season "sA1"
Serie "B"
  Season "sB1"
    Episode "eB1"
      Link "b1.1"
      Link "b1.2"
    Episode "eB2"
    Episode "eB3"
      Link "b3.1"
Serie "C"
  Season "sC1"
    Episode "eC1"
Serie "D"
Serie "E"
  Season "sE1"
    Episode "eE1"
      Link "e1.1"
  Season "sE2"
    Episode "eE2"
      Link "e2.1"

我想得到这个:

def model = [series:[
  name:'B',
  seasons:[
    name:'sB1',
    episodes:[
      name:'eB1',
      links:['b1.1','b1.2']
    ],[
      name:'eB3',
      links:['b3.1']
    ]
  ]
],[
  name:'E'
  seasons:[
    name:'sE1',
    episodes:[
      name:'eE1',
      links:['e1.1']
    ]
  ],[
    name:'sE2'
    episodes:[
      name:'eE2',
      links:['e2.1']
    ]
  ]
]

放弃所有没有链接的系列,季节剧集和DownloadInfo。

BTW,我只需要下载链接属性不为null的DownloadInfo。 我开始时:

DownloadInfo.where { link!= null }.list().each {
  // ...
}

但我无法找到一种优雅的方式来完成所有流程。任何线索?

提前致谢!

更新

Groovy等效于以下SQL,按每个实体的名称分组:

select * from serie s
inner join season ss on s.id = ss.serie_id
inner join episode e on ss.id = e.season_id
inner join download_info di on e.id = di.episode_id
where di.link is not null

0 个答案:

没有答案