Changeset 792 for MDRepository
- Timestamp:
- 10/16/10 11:32:25 (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
MDRepository/trunk/xquery/cmd-model.xqm
r749 r792 36 36 declare variable $cmd-model:valuesLimit as xs:integer := 100; 37 37 38 39 (:~ 40 API function queryModel. 41 :) 42 declare function cmd-model:query-model($cmd-index-path as xs:string, $collection as xs:string+, $format as xs:string, $max-depth as xs:integer) as item()? { 43 cmd-model:serialise-as(cmd-model:get-result-doc($collection, $cmd-index-path, $max-depth), $format) 44 }; 45 46 (:~ 47 API function getCollections. 48 :) 49 declare function cmd-model:get-collections($collections as xs:string+, $format as xs:string, $max-depth as xs:integer) as item() { 50 let $names := ($cmd-model:commonFreqsPath, cmd-model:make-compound-doc-name($collections, "collection", xs:string($max-depth))), 51 $dummy := 52 if (cmd-model:is-doc-available($names[1], $names[2])) then 53 () 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]) 65 return 66 cmd-model:serialise-as(cmd-model:get-doc($names[1], $names[2]), $format) 67 }; 68 69 (:~ 70 API function searchRetrieve. 71 :) 72 declare function cmd-model:search-retrieve($cql-query as xs:string, $collections as xs:string+, $format as xs:string, $start-item as xs:integer, $end-item as xs:integer) as item()* { 73 let $collection := collection($cmd-model:cmdiMirrorPath), 74 $results := 75 if ($collections[1] eq $cmd-model:collectionRoot) then 76 util:eval(fn:concat("$collection", xdb:decode($cql-query), "/ancestor::CMD")) 77 else 78 for $coll in $collections return util:eval(fn:concat("$collection", xdb:decode($cql-query), "/ancestor::CMD[descendant::IsPartOf = '", xdb:decode($coll) ,"']")) 79 80 let $result-count := fn:count($results), 81 $result-seq := fn:subsequence($results, $start-item, $end-item), 82 $seq-count := fn:count($result-seq), 83 $result-fragment := 84 <searchRetrieveResponse> 85 <numberOfRecords>{$result-count}</numberOfRecords> 86 <echoedSearchRetrieveRequest>{$cql-query, $collections, $start-item, $end-item}</echoedSearchRetrieveRequest> 87 <diagnostics>{$seq-count}</diagnostics> 88 <records> 89 {$result-seq} 90 </records> 91 </searchRetrieveResponse> 92 93 return 94 cmd-model:serialise-as($result-fragment, $format) 95 96 }; 97 98 (: ********************** 99 queryModel - subfunctions 100 :) 38 101 39 102 … … 91 154 92 155 (: 93 156 OBSOLETE?? 94 157 :) 95 158 declare function cmd-model:recurse-collections-model($collection as xs:string, $type-name as xs:string, $depth as xs:integer, $name as xs:string) as item()* { … … 107 170 }; 108 171 172 173 (: ********************** 174 getCollections - subfunctions 175 :) 176 177 (: 178 Get the resource ONLY by handle (previously: by handle or by path. ) 179 :) 180 declare function cmd-model:get-resource-by-handle($id as xs:string) as node()* { 181 182 if ($id="" or $id=$cmd-model:collectionRoot ) then 183 collection($cmd-model:cmdiMirrorPath)//IsPartOf[. = $cmd-model:collectionRoot]/ancestor::CMD 184 else 185 collection($cmd-model:cmdiMirrorPath)//MdSelfLink[. = xdb:decode($id)]/ancestor::CMD 186 (: let $key := xdb:decode(($doc-name, $id)[1]) 187 return ft:query(//MdSelfLink, <term>{$key}</term>)[matches(., concat("^", $key, "$"))]/ancestor::CMD :) 188 }; 189 109 190 (: 110 191 Recurse for collections 111 192 :) 112 193 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()* { 113 let $children := if ($depth eq 0) then () else $collection//ResourceProxy[ResourceType = "Metadata"] 194 (:let $children := if ($depth eq 0) then () else $collection//ResourceProxy[ResourceType = "Metadata"] 195 let $children_colls := $children $collection//ResourceProxy[ResourceType = "Metadata"] :) 196 let $children := if ($depth eq 0) then () else cmd-model:get-children-colls($collection) 114 197 return 115 198 if (fn:exists($children)) then 116 let $child-results := 117 for $child in $children 118 let $child-doc := if (empty($child/unresolvable-uri)) then 119 cmd-model:get-resource-by-handle-or-collection-path(util:collection-name($child/root()), $child/ResourceRef) else (), 120 $child-name := if (empty($child-doc)) then concat(util:collection-name($child/root()), ":", $child/ResourceRef) else cmd-model:get-md-collection-name($child-doc) 121 return 122 if (empty($child-doc)) then () 123 else 124 cmd-model:recurse-collections($child-doc, $child-name, $child-doc//Header/MdSelfLink, $child/@id, $depth - 1) 125 return 126 <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="-1" sub-colls="{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 return 210 <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> 127 211 else 128 <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="{if ($handle eq "") then "-1" else cmd-model:get-resource-count($handle)}"></c> 129 130 }; 131 132 (: 212 <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> 213 214 }; 215 216 (: 217 Get the next level collection-records (only ResourceType='Metadata') 218 rely just on the ResourceProxy of the parent (param), 219 :) 220 declare function cmd-model:get-children-colls($collection as node()+) as node()* { 221 let $proxies := $collection//ResourceProxy[ResourceType='Metadata']/ResourceRef 222 let $handle := $collection//MdSelfLink 223 return collection($cmd-model:cmdiMirrorPath)//CMD[descendant::MdSelfLink = $proxies/.][descendant::ResourceType = "Metadata"] 224 225 (: alternatively we could rely on IsPartOf + IsPartOf/@level 226 return collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Metadata"]] :) 227 }; 228 229 (: count ALL (independent of maxDepth) resource-records (ie actually ResourceType=Resource, but 230 there are records without ResourceProxy[ResourceType=Resource] - so care for that (not(exists((ResourceType)))) 231 Still we dont want collections here! 133 232 :) 134 233 declare function cmd-model:get-resource-count($handle as xs:string) as xs:string { 135 xs:string(count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Resource"]])) 136 }; 137 138 (: 234 (: xs:string(count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Resource"]])) :) 235 xs:string(count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Resource"] or not(exists(descendant::ResourceType)) ])) 236 }; 237 238 (: this is complement to cmd-model:get-resource-count() 239 count ALL (independent of maxDepth) collection-records (ie ResourceType=Metadata 240 :) 241 declare function cmd-model:get-collection-count($handle as xs:string) as xs:string { 242 xs:string(count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Metadata"]])) 243 }; 244 245 (: 246 try to derive a name from the collection-record (more-or-less agnostic about the actual schema 139 247 :) 140 248 declare function cmd-model:get-md-collection-name($collection-doc as node()) as xs:string { 141 ($collection-doc//Corpus/Name, $collection-doc//Session/Name, $collection-doc//Collection/GeneralInfo/Name, $collection-doc//Collection/GeneralInfo/Title, "UNKNOWN")[1] 142 }; 249 ($collection-doc//Corpus/Name, $collection-doc//Session/Name, $collection-doc//Collection/GeneralInfo/Name, $collection-doc//Collection/GeneralInfo/Title, 250 $collection-doc//Name, $collection-doc//name, $collection-doc//Title, $collection-doc//title, "UNKNOWN")[1] 251 }; 252 253 (: *********************** 254 HELPER function - dealing with caching the results 255 :) 143 256 144 257 (: … … 205 318 $dummy := xdb:login($cmd-model:cmdiDatabaseURI, $clarin-writer//write-user/text(), $clarin-writer//write-user-cred/text()), 206 319 $store := (: util:catch("org.exist.xquery.XPathException", :) xdb:store($collection-path, $doc-name, $data),(: , ()) :) 207 $stored-doc := doc(concat($collection-path, "/", $doc-name)), 208 $coll-count := update value $stored-doc/Collections/@count with sum($stored-doc//c[not(c)]/@cnt) 320 $stored-doc := doc(concat($collection-path, "/", $doc-name)) 321 return $stored-doc 322 (: moved to get-collection 323 $coll-count := update value $stored-doc/Collections/@count with sum($stored-doc//c[not(c)]/@cnt) 209 324 return 210 325 for $i in $stored-doc//c[c][@cnt eq "-1"] 211 326 return update value $i/@cnt with sum($i//c[not(c)]/@cnt) 212 327 :) 213 328 }; 214 329 … … 270 385 }; 271 386 272 273 (:274 Get the resource by handle or by path.275 :)276 declare function cmd-model:get-resource-by-handle-or-collection-path($id as xs:string, $doc-name as xs:string?) as node()* {277 if (starts-with($id, "test-") or starts-with($id, "clarin-") or starts-with($doc-name, "clarin-")) then278 collection($cmd-model:cmdiMirrorPath)//MdSelfLink[. = xdb:decode(($doc-name, $id)[1])]/ancestor::CMD279 (: let $key := xdb:decode(($doc-name, $id)[1])280 return ft:query(//MdSelfLink, <term>{$key}</term>)[matches(., concat("^", $key, "$"))]/ancestor::CMD :)281 else if (empty($doc-name) or $id=$cmd-model:collectionRoot ) then282 collection($cmd-model:cmdiMirrorPath)//IsPartOf[. = $cmd-model:collectionRoot]/ancestor::CMD283 else284 doc(concat($id, "/", $doc-name))285 };286 287 387 (: 288 388 Seraliseringsformat. … … 298 398 299 399 300 (:~301 API function queryModel.302 :)303 declare function cmd-model:query-model($cmd-index-path as xs:string, $collection as xs:string+, $format as xs:string, $max-depth as xs:integer) as item()? {304 cmd-model:serialise-as(cmd-model:get-result-doc($collection, $cmd-index-path, $max-depth), $format)305 };306 307 (:~308 API function getCollections.309 :)310 declare function cmd-model:get-collections($collections as xs:string+, $format as xs:string, $max-depth as xs:integer) as item() {311 let $names := ($cmd-model:commonFreqsPath, cmd-model:make-compound-doc-name($collections, "collection", xs:string($max-depth))),312 $dummy :=313 if (cmd-model:is-doc-available($names[1], $names[2])) then314 ()315 else316 let $children :=317 for $collection-item in $collections318 return319 for $collection-doc in cmd-model:get-resource-by-handle-or-collection-path($collection-item, ()) return320 cmd-model:recurse-collections($collection-doc, cmd-model:get-md-collection-name($collection-doc), $collection-doc//MdSelfLink, "", $max-depth)321 return322 cmd-model:store-collection-data(<Collections count="-1" sub-colls="{count($children)}" root="{$collections}">{$children}</Collections>, $names[1], $names[2])323 return324 cmd-model:serialise-as(cmd-model:get-doc($names[1], $names[2]), $format)325 };326 327 (:~328 API function searchRetrieve.329 :)330 declare function cmd-model:search-retrieve($cql-query as xs:string, $collections as xs:string+, $format as xs:string, $start-item as xs:integer, $end-item as xs:integer) as item()* {331 let $collection := collection($cmd-model:cmdiMirrorPath),332 $results :=333 if ($collections[1] eq $cmd-model:collectionRoot) then334 util:eval(fn:concat("$collection", xdb:decode($cql-query), "/ancestor::CMD"))335 else336 for $coll in $collections return util:eval(fn:concat("$collection", xdb:decode($cql-query), "/ancestor::CMD[descendant::IsPartOf = '", xdb:decode($coll) ,"']"))337 338 let $result-count := fn:count($results),339 $result-seq := fn:subsequence($results, $start-item, $end-item),340 $seq-count := fn:count($result-seq),341 $result-fragment :=342 <searchRetrieveResponse>343 <numberOfRecords>{$result-count}</numberOfRecords>344 <echoedSearchRetrieveRequest>{$cql-query, $collections, $start-item, $end-item}</echoedSearchRetrieveRequest>345 <diagnostics>{$seq-count}</diagnostics>346 <records>347 {$result-seq}348 </records>349 </searchRetrieveResponse>350 351 return352 cmd-model:serialise-as($result-fragment, $format)353 354 };355 400 (: 356 401 {cmdComponent} //{cmdComponent} Actor //Actor
Note: See TracChangeset
for help on using the changeset viewer.