Changeset 802 for MDRepository
- Timestamp:
- 10/18/10 14:59:43 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MDRepository/trunk/xquery/cmd-model.xqm
r800 r802 51 51 $dummy := 52 52 if (cmd-model:is-doc-available($names[1], $names[2])) then 53 53 () 54 54 else 55 let $children := 56 for $collection-item in $collections 57 return 58 for $collection-doc in cmd-model:get-resource-by-handle($collection-item) 59 return cmd-model:recurse-collections($collection-doc, cmd-model:get-md-collection-name($collection-doc), $collection-doc//MdSelfLink, "", $max-depth) 60 (: let $res-count := $children//c[not(c)]/@cnt) 61 actually it should be enough to sum the first level: :) 62 let $res-count := sum($children/@cnt) 63 let $coll-count := sum($children/@cnt_subcolls) + count($children) 64 return cmd-model:store-collection-data(<Collections cnt="{$res-count}" cnt_subcolls="{$coll-count}" cnt_children="{count($children)}" root="{$collections}">{$children}</Collections>, $names[1], $names[2]) 55 let $children := 56 for $collection-item in $collections 57 return 58 for $collection-doc in cmd-model:get-resource-by-handle($collection-item) 59 return cmd-model:recurse-collections($collection-doc, cmd-model:get-md-collection-name($collection-doc), $collection-doc//MdSelfLink, "", $max-depth) 60 let $res-count := sum($children/@cnt) 61 let $coll-count := sum($children/@cnt_subcolls) + count($children) 62 return cmd-model:store-collection-data(<Collections cnt="{$res-count}" cnt_subcolls="{$coll-count}" cnt_children="{count($children)}" root="{$collections}">{$children}</Collections>, $names[1], $names[2]) 63 65 64 return 66 65 cmd-model:serialise-as(cmd-model:get-doc($names[1], $names[2]), $format) … … 177 176 178 177 (: 179 Get the resource ONLY by handle (previously: by handle or by path. )178 Get the MD resource by handle. 180 179 :) 181 180 declare function cmd-model:get-resource-by-handle($id as xs:string) as node()* { 182 183 if ($id="" or $id=$cmd-model:collectionRoot ) then 184 collection($cmd-model:cmdiMirrorPath)//IsPartOf[. = $cmd-model:collectionRoot]/ancestor::CMD 181 let $collection := collection($cmd-model:cmdiMirrorPath) 182 return 183 if ($id eq "" or $id eq $cmd-model:collectionRoot) then 184 $collection//IsPartOf[. = $cmd-model:collectionRoot]/ancestor::CMD 185 185 else 186 collection($cmd-model:cmdiMirrorPath)//MdSelfLink[. = xdb:decode($id)]/ancestor::CMD 187 (: let $key := xdb:decode(($doc-name, $id)[1]) 188 return ft:query(//MdSelfLink, <term>{$key}</term>)[matches(., concat("^", $key, "$"))]/ancestor::CMD :) 189 }; 190 191 (: 192 Recurse for collections 193 :) 194 declare function cmd-model:recurse-collections($collection as node()+, $name as xs:string, $handle as xs:string, $proxy-id as xs:string, $depth as xs:integer) as item()* { 195 (:let $children := if ($depth eq 0) then () else $collection//ResourceProxy[ResourceType = "Metadata"] 196 let $children_colls := $children $collection//ResourceProxy[ResourceType = "Metadata"] :) 197 let $children := if ($depth eq 0) then () else cmd-model:get-children-colls($collection) 186 util:eval(concat("$collection/ft:query(descendant::MdSelfLink, <term>", xdb:decode($id), "</term>)/ancestor::CMD")) 187 (: $collection/descendant::MdSelfLink[. = xdb:decode($id)]/ancestor::CMD :) 188 189 }; 190 191 (: 192 Recurse down in collections. 193 :) 194 declare function cmd-model:recurse-collections($collection as node(), $name as xs:string, $handle as xs:string, $proxy-id as xs:string, $depth as xs:integer) as item()* { 195 let $children := if ($depth eq 0) then () else cmd-model:get-children-colls($collection) 196 let $dummy := util:log('debug', fn:concat(cmd-model:get-md-collection-name($collection), " ", $collection//MdSelfLink, " ", xs:string($depth), " CHILDREN = ", string-join(for $child in $children return $child//MdSelfLink, "#"))) 198 197 return 199 198 if (fn:exists($children)) then 200 let $child-results := 201 for $child in $children 202 (: let $child-doc := if (empty($child/unresolvable-uri)) then 203 cmd-model:get-resource-by-handle($child/ResourceRef) else (), :) 204 let $child-name := cmd-model:get-md-collection-name($child) 205 let $proxyid := $collection//ResourceProxy[ResourceRef = $child//MdSelfLink]/@id 206 return 207 if (empty($child)) then () 208 else 209 cmd-model:recurse-collections($child, $child-name, $child//Header/MdSelfLink, $proxyid, $depth - 1) 210 return 211 <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="{sum($child-results/@cnt)}" cnt_subcolls="{if ($handle eq '') then '-1' else cmd-model:get-collection-count($handle)}" cnt_children="{count($child-results)}" >{$child-results}</c> 199 let $child-results := 200 for $child in $children 201 (: let $child-doc := if (empty($child/unresolvable-uri)) then 202 cmd-model:get-resource-by-handle($child/ResourceRef) else (), :) 203 let $child-name := cmd-model:get-md-collection-name($child) 204 let $proxyid := $collection//ResourceProxy[ResourceRef = $child//MdSelfLink]/@id 205 return 206 if (empty($child)) then () 207 else 208 cmd-model:recurse-collections($child, $child-name, $child//Header/MdSelfLink, $proxyid, $depth - 1) 209 210 return 211 <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="{sum($child-results/@cnt)}" cnt_subcolls="{if ($handle eq '') then '-1' else cmd-model:get-collection-count($handle)}" cnt_children="{count($child-results)}" >{$child-results}</c> 212 212 else 213 213 <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt_subcolls="{if ($handle eq '') then '-1' else cmd-model:get-collection-count($handle)}" cnt="{if ($handle eq '') then '-1' else cmd-model:get-resource-count($handle)}"></c> … … 216 216 217 217 (: 218 Get the next level collection-records (only ResourceType='Metadata') 219 rely just on the ResourceProxy of the parent (param), 220 :) 221 declare function cmd-model:get-children-colls($collection as node()+) as node()* { 222 (: let $proxies := $collection//ResourceProxy[ResourceType='Metadata']/ResourceRef :) 223 let $handle := $collection//MdSelfLink 224 return collection($cmd-model:cmdiMirrorPath)//IsPartOf[@level=1][matches(.,$handle)]/ancestor::CMD[descendant::ResourceType[. = "Metadata"]] 225 (: this turned out to be very inefficient!!: 226 return collection($cmd-model:cmdiMirrorPath)//CMD[descendant::MdSelfLink = $proxies/.][descendant::ResourceType = "Metadata"] 227 :) 228 }; 218 Get the next level collection-records (ResourceType='Metadata') 219 rely on the ResourceProxy of the parent (param) 220 :) 221 declare function cmd-model:get-children-colls($collection as node()) as node()* { 222 let $handle := $collection//MdSelfLink/text(), 223 $cmdi-collection := collection($cmd-model:cmdiMirrorPath) 224 return util:eval(concat("$cmdi-collection/ft:query(descendant::IsPartOf, <term>", $handle, "</term>)/ancestor::CMD[descendant::ResourceType[. = 'Metadata']]")) 225 (: collection($cmd-model:cmdiMirrorPath)/descendant::IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Metadata"]] :) 226 }; 229 227 230 228 (: count ALL (independent of maxDepth) resource-records (ie actually ResourceType=Resource, but 231 229 there are records without ResourceProxy[ResourceType=Resource] - so care for that (not(exists((ResourceType)))) 232 Still we dont want collections here!233 230 :) 234 231 declare function cmd-model:get-resource-count($handle as xs:string) as xs:string { 235 (: xs:string(count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Resource"]])) :)236 232 xs:string(count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Resource"] or not(exists(descendant::ResourceType)) ])) 237 233 }; … … 245 241 246 242 (: 247 try to derive a name from the collection-record (more-or-less agnostic about the actual schema 243 Try to derive a name from the collection-record (more-or-less agnostic about the actual schema. 248 244 :) 249 245 declare function cmd-model:get-md-collection-name($collection-doc as node()) as xs:string { 250 ($collection-doc//Corpus/Name, $collection-doc//Session/Name, $collection-doc//Collection/GeneralInfo/Name, $collection-doc//Collection/GeneralInfo/Title, 251 $collection-doc//Name, $collection-doc//name, $collection-doc//Title, $collection-doc//title, "UNKNOWN")[1] 246 ($collection-doc//Corpus/Name, $collection-doc//Session/Name, $collection-doc//Collection/GeneralInfo/Name, $collection-doc//Collection/GeneralInfo/Title, $collection-doc//Name, $collection-doc//name, $collection-doc//Title, $collection-doc//title, "UNKNOWN")[1] 252 247 }; 253 248 … … 380 375 381 376 (: 382 Skapa elementnamn för dokumentet av typ.377 Get element name for document type. 383 378 :) 384 379 declare function cmd-model:get-doc-type-element-name($type-name as xs:string) as xs:string {
Note: See TracChangeset
for help on using the changeset viewer.