Index: MDRepository/trunk/xquery/cmd-model.xqm
===================================================================
--- MDRepository/trunk/xquery/cmd-model.xqm	(revision 681)
+++ MDRepository/trunk/xquery/cmd-model.xqm	(revision 691)
@@ -32,5 +32,13 @@
 declare variable $cmd-model:xmlExt as xs:string := ".xml";
 
+declare variable $cmd-model:valuesLimit as xs:integer := 100;
+
+
+
 declare function cmd-model:elem($collection as xs:string, $path as xs:string, $depth as xs:integer) as element() {
+cmd-model:elem-r($collection, $path, $depth, $depth)
+};
+
+declare function cmd-model:elem-r($collection as xs:string, $path as xs:string, $max-depth as xs:integer, $depth as xs:integer) as element() {
   let $path-nodes := util:eval(fn:concat("collection('", $collection, "')//", $path))
   let $path-count := count($path-nodes)
@@ -43,9 +51,18 @@
 	<Term path="{fn:concat("//", $path)}" name="{text:groups($path, "/([^/]+)$")[last()]}" count="{$path-count}" count_text="{$text-count}"  count_distinct_text="{$text-count-distinct}">{
 	  if ($depth > 0) then
-	    for $elname in $subs[. != '']
+	    (for $elname in $subs[. != '']
 	    return
-	      cmd-model:elem($collection, concat($path, '/', $elname), $depth - 1)
+	      cmd-model:elem-r($collection, concat($path, '/', $elname), $max-depth, $depth - 1),
+	      if ($max-depth eq 1) then cmd-model:values($path-nodes) else ())
 	  else 'maxdepth'
 	}</Term>
+};
+
+declare function cmd-model:values($nodes as node()*) as node()* {
+let $keys := distinct-values($nodes/text())
+for $key at $pos in $keys[if ($cmd-model:valuesLimit eq 0) then () else (1 to $cmd-model:valuesLimit)]
+  let $kcount := count($nodes[. eq $key])
+    order by lower-case($key) ascending
+    return <v key="{$key}" cnt="{$kcount}" />
 };
 
@@ -78,5 +95,5 @@
   Recurse for collections
 :)
-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()* {
+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()* {
     let $children := if ($depth eq 0) then () else $collection//ResourceProxy[ResourceType = "Metadata"]
     return
@@ -86,13 +103,26 @@
 	      let $child-doc := if (empty($child/unresolvable-uri)) then
 		cmd-model:get-resource-by-handle-or-collection-path(util:collection-name($child/root()), $child/ResourceRef) else (),
-		$child-name := ($child-doc//Corpus/Name, $child-doc//Session/Name, $child-doc//Collection/GeneralInfo/Name, "UNKNOWN")[1]
+		$child-name := if (empty($child-doc)) then concat(util:collection-name($child/root()), ":", $child/ResourceRef) else cmd-model:get-md-collection-name($child-doc)
             return
-	      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> :) 
+	      if (empty($child-doc)) then ()
 	     else
               cmd-model:recurse-collections($child-doc, $child-name, $child-doc//Header/MdSelfLink, $child/@id, $depth - 1)
 	  return
-	  <c n="{$parent}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="-1" sub-colls="{count($child-results)}" >{$child-results}</c>
+	  <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="-1" sub-colls="{count($child-results)}" >{$child-results}</c>
       else
-      <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>
+      <c n="{$name}" handle="{$handle}" proxy-id="{$proxy-id}" cnt="{if ($handle eq "") then "-1" else cmd-model:get-resource-count($handle)}"></c>
+
+};
+
+(:
+:)
+declare function cmd-model:get-resource-count($handle as xs:string) as xs:string {
+count(collection($cmd-model:cmdiMirrorPath)//IsPartOf[. eq $handle]/ancestor::CMD[descendant::ResourceType[. = "Resource"]])
+};
+
+(:
+:)
+declare function cmd-model:get-md-collection-name($collection-doc as node()) as xs:string {
+($collection-doc//Corpus/Name, $collection-doc//Session/Name, $collection-doc//Collection/GeneralInfo/Name, $collection-doc//Collection/GeneralInfo/Title, "UNKNOWN")[1]
 };
 
@@ -171,5 +201,5 @@
   $coll-count := update value $stored-doc/Collections/@count with sum($stored-doc//c[not(c)]/@cnt)
   return 
-  for $i in $stored-doc//c[c] 
+  for $i in $stored-doc//c[c][@cnt eq "-1"]
   return update value $i/@cnt with sum($i//c[not(c)]/@cnt)
 
@@ -237,6 +267,8 @@
 :)
 declare function cmd-model:get-resource-by-handle-or-collection-path($id as xs:string, $doc-name as xs:string?) as node()* {
-  if (starts-with($id, "test-") or starts-with($id, "clarin.at") or starts-with($doc-name, "clarin.at")) then
+  if (starts-with($id, "test-") or starts-with($id, "clarin-") or starts-with($doc-name, "clarin-")) then
     collection($cmd-model:cmdiMirrorPath)//MdSelfLink[. = xdb:decode(($doc-name, $id)[1])]/ancestor::CMD
+    (: let $key := xdb:decode(($doc-name, $id)[1])
+    return ft:query(//MdSelfLink, <term>{$key}</term>)[matches(., concat("^", $key, "$"))]/ancestor::CMD :)
   else if (empty($doc-name)) then
     collection($id)//IsPartOf[. = "root"]/ancestor::CMD
@@ -278,5 +310,5 @@
         return
 	for $collection-doc in cmd-model:get-resource-by-handle-or-collection-path($collection-item, ()) return
-	  cmd-model:recurse-collections($collection-doc, ($collection-doc//Corpus/Name, $collection-doc//Session/Name, "UNKNOWN")[1], $collection-doc//MdSelfLink, "", $max-depth)
+	  cmd-model:recurse-collections($collection-doc, cmd-model:get-md-collection-name($collection-doc), $collection-doc//MdSelfLink, "", $max-depth)
 	  return
 	    cmd-model:store-collection-data(<Collections count="-1" sub-colls="{count($children)}" root="{$collections}">{$children}</Collections>, $names[1], $names[2])
