Changeset 691 for MDRepository


Ignore:
Timestamp:
09/14/10 09:22:55 (14 years ago)
Author:
ljo
Message:

cmd-model.xqm - intermediate commit.

File:
1 edited

Legend:

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

    r681 r691  
    3232declare variable $cmd-model:xmlExt as xs:string := ".xml";
    3333
     34declare variable $cmd-model:valuesLimit as xs:integer := 100;
     35
     36
     37
    3438declare function cmd-model:elem($collection as xs:string, $path as xs:string, $depth as xs:integer) as element() {
     39cmd-model:elem-r($collection, $path, $depth, $depth)
     40};
     41
     42declare function cmd-model:elem-r($collection as xs:string, $path as xs:string, $max-depth as xs:integer, $depth as xs:integer) as element() {
    3543  let $path-nodes := util:eval(fn:concat("collection('", $collection, "')//", $path))
    3644  let $path-count := count($path-nodes)
     
    4351        <Term path="{fn:concat("//", $path)}" name="{text:groups($path, "/([^/]+)$")[last()]}" count="{$path-count}" count_text="{$text-count}"  count_distinct_text="{$text-count-distinct}">{
    4452          if ($depth > 0) then
    45             for $elname in $subs[. != '']
     53            (for $elname in $subs[. != '']
    4654            return
    47               cmd-model:elem($collection, concat($path, '/', $elname), $depth - 1)
     55              cmd-model:elem-r($collection, concat($path, '/', $elname), $max-depth, $depth - 1),
     56              if ($max-depth eq 1) then cmd-model:values($path-nodes) else ())
    4857          else 'maxdepth'
    4958        }</Term>
     59};
     60
     61declare function cmd-model:values($nodes as node()*) as node()* {
     62let $keys := distinct-values($nodes/text())
     63for $key at $pos in $keys[if ($cmd-model:valuesLimit eq 0) then () else (1 to $cmd-model:valuesLimit)]
     64  let $kcount := count($nodes[. eq $key])
     65    order by lower-case($key) ascending
     66    return <v key="{$key}" cnt="{$kcount}" />
    5067};
    5168
     
    7895  Recurse for collections
    7996:)
    80 declare function cmd-model:recurse-collections($collection as node()+, $parent as xs:string, $handle as xs:string, $proxy-id as xs:string, $depth as xs:integer) as item()* {
     97declare 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()* {
    8198    let $children := if ($depth eq 0) then () else $collection//ResourceProxy[ResourceType = "Metadata"]
    8299    return
     
    86103              let $child-doc := if (empty($child/unresolvable-uri)) then
    87104                cmd-model:get-resource-by-handle-or-collection-path(util:collection-name($child/root()), $child/ResourceRef) else (),
    88                 $child-name := ($child-doc//Corpus/Name, $child-doc//Session/Name, $child-doc//Collection/GeneralInfo/Name, "UNKNOWN")[1]
     105                $child-name := if (empty($child-doc)) then concat(util:collection-name($child/root()), ":", $child/ResourceRef) else cmd-model:get-md-collection-name($child-doc)
    89106            return
    90               if (empty($child-doc)) then () (:<no-target proxy-id="{$child/@id}">{<proxy>{$child}</proxy>, <in-document>{concat(util:collection-name($child/root()), "/", util:document-name($child/root()))}</in-document>}</no-target> :)
     107              if (empty($child-doc)) then ()
    91108             else
    92109              cmd-model:recurse-collections($child-doc, $child-name, $child-doc//Header/MdSelfLink, $child/@id, $depth - 1)
    93110          return
    94           <c n="{$parent}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="-1" sub-colls="{count($child-results)}" >{$child-results}</c>
     111          <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="-1" sub-colls="{count($child-results)}" >{$child-results}</c>
    95112      else
    96       <c n="{$parent}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="{count(collection(concat(util:collection-name($collection/CMD[1]/root()), "/../"))//CMD[not(//ResourceType[. = "Metadata"])])}"></c>
     113      <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="{if ($handle eq "") then "-1" else cmd-model:get-resource-count($handle)}"></c>
     114
     115};
     116
     117(:
     118:)
     119declare function cmd-model:get-resource-count($handle as xs:string) as xs:string {
     120count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Resource"]])
     121};
     122
     123(:
     124:)
     125declare function cmd-model:get-md-collection-name($collection-doc as node()) as xs:string {
     126($collection-doc//Corpus/Name, $collection-doc//Session/Name, $collection-doc//Collection/GeneralInfo/Name, $collection-doc//Collection/GeneralInfo/Title, "UNKNOWN")[1]
    97127};
    98128
     
    171201  $coll-count := update value $stored-doc/Collections/@count with sum($stored-doc//c[not(c)]/@cnt)
    172202  return
    173   for $i in $stored-doc//c[c] 
     203  for $i in $stored-doc//c[c][@cnt eq "-1"]
    174204  return update value $i/@cnt with sum($i//c[not(c)]/@cnt)
    175205
     
    237267:)
    238268declare function cmd-model:get-resource-by-handle-or-collection-path($id as xs:string, $doc-name as xs:string?) as node()* {
    239   if (starts-with($id, "test-") or starts-with($id, "clarin.at") or starts-with($doc-name, "clarin.at")) then
     269  if (starts-with($id, "test-") or starts-with($id, "clarin-") or starts-with($doc-name, "clarin-")) then
    240270    collection($cmd-model:cmdiMirrorPath)//MdSelfLink[. = xdb:decode(($doc-name, $id)[1])]/ancestor::CMD
     271    (: let $key := xdb:decode(($doc-name, $id)[1])
     272    return ft:query(//MdSelfLink, <term>{$key}</term>)[matches(., concat("^", $key, "$"))]/ancestor::CMD :)
    241273  else if (empty($doc-name)) then
    242274    collection($id)//IsPartOf[. = "root"]/ancestor::CMD
     
    278310        return
    279311        for $collection-doc in cmd-model:get-resource-by-handle-or-collection-path($collection-item, ()) return
    280           cmd-model:recurse-collections($collection-doc, ($collection-doc//Corpus/Name, $collection-doc//Session/Name, "UNKNOWN")[1], $collection-doc//MdSelfLink, "", $max-depth)
     312          cmd-model:recurse-collections($collection-doc, cmd-model:get-md-collection-name($collection-doc), $collection-doc//MdSelfLink, "", $max-depth)
    281313          return
    282314            cmd-model:store-collection-data(<Collections count="-1" sub-colls="{count($children)}" root="{$collections}">{$children}</Collections>, $names[1], $names[2])
Note: See TracChangeset for help on using the changeset viewer.