Changeset 792 for MDRepository


Ignore:
Timestamp:
10/16/10 11:32:25 (14 years ago)
Author:
vronk
Message:

rework get-collections

File:
1 edited

Legend:

Unmodified
Added
Removed
  • MDRepository/trunk/xquery/cmd-model.xqm

    r749 r792  
    3636declare variable $cmd-model:valuesLimit as xs:integer := 100;
    3737
     38
     39(:~
     40  API function queryModel.
     41:)
     42declare 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:)
     49declare 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:)
     72declare 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:)
    38101
    39102
     
    91154
    92155(:
    93 
     156OBSOLETE??
    94157:)
    95158declare 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()* {
     
    107170};
    108171
     172
     173(: **********************
     174        getCollections - subfunctions
     175:)
     176
     177(:
     178  Get the resource ONLY by handle (previously: by handle or by path. )
     179:)
     180declare 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
    109190(:
    110191  Recurse for collections
    111192:)
    112193declare 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)
    114197    return
    115198      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>
    127211      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:)
     220declare 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!   
    133232:)
    134233declare 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:)
     241declare 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(:
     246try to derive a name from the collection-record (more-or-less agnostic about the actual schema
    139247:)
    140248declare 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(: ***********************
     254HELPER function - dealing with caching the results
     255:)
    143256
    144257(:
     
    205318  $dummy := xdb:login($cmd-model:cmdiDatabaseURI, $clarin-writer//write-user/text(), $clarin-writer//write-user-cred/text()),
    206319  $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)
    209324  return
    210325  for $i in $stored-doc//c[c][@cnt eq "-1"]
    211326  return update value $i/@cnt with sum($i//c[not(c)]/@cnt)
    212 
     327                        :)
    213328};
    214329
     
    270385};
    271386
    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-")) then
    278     collection($cmd-model:cmdiMirrorPath)//MdSelfLink[. = xdb:decode(($doc-name, $id)[1])]/ancestor::CMD
    279     (: 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 ) then
    282     collection($cmd-model:cmdiMirrorPath)//IsPartOf[. = $cmd-model:collectionRoot]/ancestor::CMD
    283   else
    284     doc(concat($id, "/", $doc-name))
    285 };
    286 
    287387(:
    288388  Seraliseringsformat.
     
    298398
    299399
    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])) then
    314         ()
    315       else
    316         let $children :=
    317         for $collection-item in $collections
    318         return
    319         for $collection-doc in cmd-model:get-resource-by-handle-or-collection-path($collection-item, ()) return
    320           cmd-model:recurse-collections($collection-doc, cmd-model:get-md-collection-name($collection-doc), $collection-doc//MdSelfLink, "", $max-depth)
    321           return
    322             cmd-model:store-collection-data(<Collections count="-1" sub-colls="{count($children)}" root="{$collections}">{$children}</Collections>, $names[1], $names[2])
    323 return
    324   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) then
    334       util:eval(fn:concat("$collection", xdb:decode($cql-query), "/ancestor::CMD"))
    335     else
    336       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     return
    352         cmd-model:serialise-as($result-fragment, $format)
    353 
    354 };
    355400(:
    356401{cmdComponent}   //{cmdComponent}        Actor   //Actor
Note: See TracChangeset for help on using the changeset viewer.