[ignore] update of main controller to use the new switch() expression. This expression needs some exposure :-)

svn path=/trunk/eXist/; revision=14656

(: 	This is the main controller for the web application. It is called from the
	XQueryURLRewrite filter configured in web.xml. :)
xquery version "3.0";

(:~ -------------------------------------------------------
    Main controller: handles all requests not matched by
    sub-controllers.
    ------------------------------------------------------- :)

declare namespace c="http://exist-db.org/xquery/controller";

import module namespace request="http://exist-db.org/xquery/request";
import module namespace xdb = "http://exist-db.org/xquery/xmldb";

let $query := request:get-parameter("q", ())
return
	(: redirect webapp root to index.xml :)
    if ($exist:path eq '/') then
		<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
			<redirect url="index.xml"/>
		</dispatch>

	(: ignore Cocoon :)
	else if (matches($exist:path, "/cocoon")) then
		<ignore xmlns="http://exist.sourceforge.net/NS/exist">
            <cache-control cache="yes"/>
		</ignore>

	else if ($exist:resource eq 'applications.xml') then
		<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
			<!-- query results are passed to XSLT servlet via request attribute -->
			<set-attribute name="xquery.attribute" value="model"/>
			<view>
				<forward servlet="XSLTServlet">
					<set-attribute name="xslt.input" value="model"/>
					<set-attribute name="xslt.stylesheet" value="apps/applications.xsl"/>
				</forward>
				<forward servlet="XSLTServlet">
					<set-attribute name="xslt.input" value=""/>
					<set-attribute name="xslt.stylesheet" value="stylesheets/db2html.xsl"/>
				</forward>
			</view>
		</dispatch>

	else if ($exist:resource eq 'articles') then
		<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
			<forward url="xquery/exist-articles.xql">
				<!-- query results are passed to XSLT servlet via request attribute -->
				<set-attribute name="xquery.attribute" value="model"/>
			</forward>
			<view>
				<forward servlet="XSLTServlet">
					<set-attribute name="xslt.input" value="model"/>
					<set-attribute name="xslt.stylesheet" value="stylesheets/db2xhtml.xsl"/>
				</forward>
			</view>
		</dispatch>

    else if ($exist:path = "/xforms/idxconf.xml") then
        let $log := util:log("DEBUG", "in xforms rule") return
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
            <forward url="idxconf.xql"/>
			<!--view>
				<forward servlet="XSLTServlet">
					<set-attribute name="xslt.stylesheet" value="xforms/xsltforms/xsltforms.xsl"/>
				</forward>
			</view-->
            <cache-control cache="no"/>
		</dispatch>

	(: the following xml files use different stylesheets :)
	else if ($exist:resource = ('index.xml', 'roadmap.xml', 'facts.xml') or $exist:path = '/examples.xml') then
		let $stylesheet := switch ($exist:resource)
			case "roadmap.xml" return "stylesheets/roadmap.xsl"
			case "facts.xml"   return "stylesheets/facts.xsl"
			default            return "stylesheets/doc2html-2.xsl"
		return
			<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
				<view>
					<forward servlet="XSLTServlet">
						<set-attribute name="xslt.stylesheet" value="{$stylesheet}"/>
						<set-attribute name="xslt.exist.version" value="{system:get-version()}-rev{system:get-revision()}-{system:get-build()}"/>
					</forward>
				</view>
				<cache-control cache="no"/>
			</dispatch>

	else if (ends-with($exist:resource, '.xml')) then
		(: 	check if the requested document was selected from a query result.
			if yes, pass it to docs.xql to get the match highlighting. :)
		if ($query) then
				<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
					<forward url="/xquery/docs.xql">
						<add-parameter name="path" value="/db/xqdocs/{$exist:resource}"/>
					</forward>
					<view>
    					<forward servlet="XSLTServlet">
    						<set-attribute name="xslt.stylesheet" value="stylesheets/db2xhtml.xsl"/>
    					    <set-attribute name="xslt.output.media-type" value="text/html"/>
    						<set-attribute name="xslt.output.doctype-public" value="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
    						<set-attribute name="xslt.output.doctype-system" value="resources/xhtml1-transitional.dtd"/>
    						<set-attribute name="xslt.output.add-exist-id" value="all"/>
    					    <set-attribute name="xslt.highlight-matches" value="all"/>
    				        <set-attribute name="xslt.xinclude-path" value=".."/>
    				        <set-attribute name="xslt.root" value="."/>
    					</forward>
    				</view>
				</dispatch>

		else
			<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
				<view>
					<forward servlet="XSLTServlet">
						<set-attribute name="xslt.stylesheet" value="stylesheets/db2xhtml.xsl"/>
					    <set-attribute name="xslt.output.media-type" value="text/html"/>
						<set-attribute name="xslt.output.doctype-public" value="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
						<set-attribute name="xslt.output.doctype-system" value="resources/xhtml1-transitional.dtd"/>
						<set-attribute name="xslt.root" value="."/>
					    {
					        if ($exist:resource eq 'download.xml') then
					            <set-attribute name="xslt.table-of-contents" value="'no'"/>
			                else
			                    ()
	                    }
					</forward>
				</view>
            	<cache-control cache="no"/>
			</dispatch>

	else if (ends-with($exist:path, '/dump')) then
		let $newPath := substring-before($exist:path, '/dump')
		return
        	<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
				<forward url="{$exist:controller}{$newPath}"/>
				<view>
					<forward url="xquery/dump.xql">
					   <set-attribute name="controller" value="{$exist:controller}"/>
					   <set-attribute name="path" value="{$exist:path}"/>
					   <set-attribute name="resource" value="{$exist:resource}"/>
				    </forward>
				</view>
			</dispatch>

	else
		<ignore xmlns="http://exist.sourceforge.net/NS/exist">
            <cache-control cache="yes"/>
		</ignore>

(: 	This is the main controller for the web application. It is called from the
	XQueryURLRewrite filter configured in web.xml. :)
xquery version "1.0";

(:~ -------------------------------------------------------
    Main controller: handles all requests not matched by
    sub-controllers.
    ------------------------------------------------------- :)

declare namespace c="http://exist-db.org/xquery/controller";

import module namespace request="http://exist-db.org/xquery/request";
import module namespace xdb = "http://exist-db.org/xquery/xmldb";

let $query := request:get-parameter("q", ())
return
	(: redirect webapp root to index.xml :)
    if ($exist:path eq '/') then
		<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
			<redirect url="index.xml"/>
		</dispatch>

	(: ignore Cocoon :)
	else if (matches($exist:path, "/cocoon")) then
		<ignore xmlns="http://exist.sourceforge.net/NS/exist">
            <cache-control cache="yes"/>
		</ignore>

	else if ($exist:resource eq 'applications.xml') then
		<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
			<!-- query results are passed to XSLT servlet via request attribute -->
			<set-attribute name="xquery.attribute" value="model"/>
			<view>
				<forward servlet="XSLTServlet">
					<set-attribute name="xslt.input" value="model"/>
					<set-attribute name="xslt.stylesheet" value="apps/applications.xsl"/>
				</forward>
				<forward servlet="XSLTServlet">
					<set-attribute name="xslt.input" value=""/>
					<set-attribute name="xslt.stylesheet" value="stylesheets/db2html.xsl"/>
				</forward>
			</view>
		</dispatch>

	else if ($exist:resource eq 'articles') then
		<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
			<forward url="xquery/exist-articles.xql">
				<!-- query results are passed to XSLT servlet via request attribute -->
				<set-attribute name="xquery.attribute" value="model"/>
			</forward>
			<view>
				<forward servlet="XSLTServlet">
					<set-attribute name="xslt.input" value="model"/>
					<set-attribute name="xslt.stylesheet" value="stylesheets/db2xhtml.xsl"/>
				</forward>
			</view>
		</dispatch>

    else if ($exist:path = "/xforms/idxconf.xml") then
        let $log := util:log("DEBUG", "in xforms rule") return
        <dispatch xmlns="http://exist.sourceforge.net/NS/exist">
            <forward url="idxconf.xql"/>
			<!--view>
				<forward servlet="XSLTServlet">
					<set-attribute name="xslt.stylesheet" value="xforms/xsltforms/xsltforms.xsl"/>
				</forward>
			</view-->
            <cache-control cache="no"/>
		</dispatch>

	(: the following xml files use different stylesheets :)
	else if ($exist:resource = ('index.xml', 'roadmap.xml', 'facts.xml') or $exist:path = '/examples.xml') then
		let $stylesheet :=
			if ($exist:resource eq 'roadmap.xml') then
				"stylesheets/roadmap.xsl"
			else if ($exist:resource eq "facts.xml") then
				"stylesheets/facts.xsl"
			else
				"stylesheets/doc2html-2.xsl"
		return
			<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
				<view>
					<forward servlet="XSLTServlet">
						<set-attribute name="xslt.stylesheet" value="{$stylesheet}"/>
						<set-attribute name="xslt.exist.version" value="{system:get-version()}-rev{system:get-revision()}-{system:get-build()}"/>
					</forward>
				</view>
				<cache-control cache="no"/>
			</dispatch>

	else if (ends-with($exist:resource, '.xml')) then
		(: 	check if the requested document was selected from a query result.
			if yes, pass it to docs.xql to get the match highlighting. :)
		if ($query) then
				<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
					<forward url="/xquery/docs.xql">
						<add-parameter name="path" value="/db/xqdocs/{$exist:resource}"/>
					</forward>
					<view>
    					<forward servlet="XSLTServlet">
    						<set-attribute name="xslt.stylesheet" value="stylesheets/db2xhtml.xsl"/>
    					    <set-attribute name="xslt.output.media-type" value="text/html"/>
    						<set-attribute name="xslt.output.doctype-public" value="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
    						<set-attribute name="xslt.output.doctype-system" value="resources/xhtml1-transitional.dtd"/>
    						<set-attribute name="xslt.output.add-exist-id" value="all"/>
    					    <set-attribute name="xslt.highlight-matches" value="all"/>
    				        <set-attribute name="xslt.xinclude-path" value=".."/>
    				        <set-attribute name="xslt.root" value="."/>
    					</forward>
    				</view>
				</dispatch>

		else
			<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
				<view>
					<forward servlet="XSLTServlet">
						<set-attribute name="xslt.stylesheet" value="stylesheets/db2xhtml.xsl"/>
					    <set-attribute name="xslt.output.media-type" value="text/html"/>
						<set-attribute name="xslt.output.doctype-public" value="-//W3C//DTD XHTML 1.0 Transitional//EN"/>
						<set-attribute name="xslt.output.doctype-system" value="resources/xhtml1-transitional.dtd"/>
						<set-attribute name="xslt.root" value="."/>
					    {
					        if ($exist:resource eq 'download.xml') then
					            <set-attribute name="xslt.table-of-contents" value="'no'"/>
			                else
			                    ()
	                    }
					</forward>
				</view>
            	<cache-control cache="no"/>
			</dispatch>

	else if (ends-with($exist:path, '/dump')) then
		let $newPath := substring-before($exist:path, '/dump')
		return
        	<dispatch xmlns="http://exist.sourceforge.net/NS/exist">
				<forward url="{$exist:controller}{$newPath}"/>
				<view>
					<forward url="xquery/dump.xql">
					   <set-attribute name="controller" value="{$exist:controller}"/>
					   <set-attribute name="path" value="{$exist:path}"/>
					   <set-attribute name="resource" value="{$exist:resource}"/>
				    </forward>
				</view>
			</dispatch>

	else
		<ignore xmlns="http://exist.sourceforge.net/NS/exist">
            <cache-control cache="yes"/>
		</ignore>


remove references to csd_webconf it init

import module namespace csr_proc = "https://github.com/openhie/openinfoman/csr_proc";
import module namespace csd_psd = "https://github.com/openhie/openinfoman/csd_psd";
import module namespace csd_lsc = "https://github.com/openhie/openinfoman/csd_lsc";
import module namespace svs_lsvs = "https://github.com/openhie/openinfoman/svs_lsvs";

(csr_proc:init()
,csd_lsc:init_cache_meta()
,csd_psd:init()
,svs_lsvs:init_store()
)

import module namespace csd_webconf =  "https://github.com/openhie/openinfoman/csd_webconf";
import module namespace csr_proc = "https://github.com/openhie/openinfoman/csr_proc";
import module namespace csd_psd = "https://github.com/openhie/openinfoman/csd_psd";
import module namespace csd_lsc = "https://github.com/openhie/openinfoman/csd_lsc";
import module namespace svs_lsvs = "https://github.com/openhie/openinfoman/svs_lsvs";

(csr_proc:init($csd_webconf:db)
,csd_lsc:init_cache_meta($csd_webconf:db)
,csd_psd:init($csd_webconf:db)
,svs_lsvs:init_store($csd_webconf:db)
)


remove uneeded file

remove uneeded file

module namespace page = 'http://basex.org/modules/web-page';

import module namespace csd_psd = "https://github.com/openhie/openinfoman/csd_psd";
import module namespace csd_dm = "https://github.com/openhie/openinfoman/csd_dm";
import module namespace csd_lsc = "https://github.com/openhie/openinfoman/csd_lsc" ;
import module namespace csd_qus =  "https://github.com/openhie/openinfoman/csd_qus";
import module namespace csd_webconf =  "https://github.com/openhie/openinfoman/csd_webconf";

declare function page:redirect($redirect as xs:string) as element(restxq:redirect)
{
  <restxq:redirect>{ $redirect }</restxq:redirect>
};

declare function page:nocache($response) {
(<http:response status="200" message="OK">

  <http:header name="Cache-Control" value="must-revalidate,no-cache,no-store"/>
</http:response>,
$response)
};

declare updating
  %rest:path("/CSD/cacheService/init_cache_meta")
  %rest:GET
  function page:init_cache_meta()
{
  (csd_lsc:init_cache_meta($csd_webconf:db)
  ,
  db:output(page:redirect(concat($csd_webconf:baseurl,"CSD/cacheService")))
  )

};

declare
  %rest:path("/CSD/cacheService")
  %rest:GET
  %output:method("xhtml")
  function page:poll_service_list()
{
if ( not(csd_lsc:cache_meta_exists($csd_webconf:db))) then
  page:redirect(concat($csd_webconf:baseurl,"CSD/cacheService/init_cache_meta"))
else
  let $services := csd_psd:registered_directories($csd_webconf:db)
  let $response :=

Global Operations

 Get all cache Meta-Data

    {
    if ( csd_lsc:cache_meta_exists($csd_webconf:db)) then

Service Directory Operations

	    {for $name in $services
	    let $mtime := csd_lsc:get_service_directory_mtime($csd_webconf:db,$name)
	    order by $name
	    return

	      {$name} last cached on {$mtime}:

	      {page:services_menu($name)}

	    }

    else ()
    }

  return page:nocache(  csd_webconf:wrapper($response))

};

declare function page:services_menu($name) {
  let $url := csd_psd:get_service_directory_url($csd_webconf:db,$name)
  let $mtime := csd_lsc:get_service_directory_mtime($csd_webconf:db,$name)
  return

    {if (not(csd_lsc:directory_exists($csd_webconf:db,$name))) then

Create cache of {$name}

  else
    (

Empty local cache of {$name}
,

Get local cache  of {$name}
,

      Update local cache  of {$name}

	WARNING:An InfoMan trying to cache its own service directory will result in a deadlock.  see this issue

,

Get cache Meta Data  for {$name}
,

Drop cache Meta Data  of {$name}

  )
    }

};


[bugfix] admin html interface used deprecated function

svn path=/trunk/eXist/; revision=7879

xquery version "1.0";
(: $Id$ :)
(:
    Module: functions for formatting an xs:dateTime value.
:)

module namespace date="http://exist-db.org/xquery/admin-interface/date";

declare variable $date:months {
	("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
	"Nov", "Dec")
};

declare function date:format-date($date as xs:dateTime) as xs:string {
	string-join((
		$date:months[month-from-date($date)],
		xs:string( day-from-date($date)  ),
		xs:string( year-from-date($date) ) ), " ")
};

declare function date:format-int($component as xs:integer) as xs:string {
	if($component lt 10) then
		concat("0", $component)
	else
		xs:string($component)
};

declare function date:format-time($time as xs:dateTime) as xs:string {
	concat(
		date:format-int(hours-from-dateTime($time)), ":",
		date:format-int(minutes-from-dateTime($time)), ":",
		date:format-int(xs:integer(seconds-from-dateTime($time)))
	)
};

declare function date:format-dateTime($dt as xs:dateTime) as xs:string {
    concat(date:format-date($dt), " ", date:format-time($dt))
};

xquery version "1.0";
(: $Id$ :)
(:
    Module: functions for formatting an xs:dateTime value.
:)

module namespace date="http://exist-db.org/xquery/admin-interface/date";

declare variable $date:months {
	("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
	"Nov", "Dec")
};

declare function date:format-date($date as xs:dateTime) as xs:string {
	string-join((
		item-at($date:months, month-from-date($date)),
		xs:string( day-from-date($date)  ),
		xs:string( year-from-date($date) ) ), " ")
};

declare function date:format-int($component as xs:integer) as xs:string {
	if($component lt 10) then
		concat("0", $component)
	else
		xs:string($component)
};

declare function date:format-time($time as xs:dateTime) as xs:string {
	concat(
		date:format-int(hours-from-dateTime($time)), ":",
		date:format-int(minutes-from-dateTime($time)), ":",
		date:format-int(xs:integer(seconds-from-dateTime($time)))
	)
};

declare function date:format-dateTime($dt as xs:dateTime) as xs:string {
    concat(date:format-date($dt), " ", date:format-time($dt))
};


[testsuite] Fixed small issue in range index tests.

xquery version "3.0";

module namespace rt="http://exist-db.org/xquery/range/test";

import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";

declare variable $rt:COLLECTION_CONFIG :=
    <collection xmlns="http://exist-db.org/collection-config/1.0">
        <index xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:tei="http://www.tei-c.org/ns/1.0">
            <fulltext default="none" attributes="false"/>
            <lucene>
                <text field="lucene-name" qname="name"/>
            </lucene>
            <range>
                <create match="//address">
                    <field name="address-name" match="name" type="xs:string" whitespace="normalize"/>
                    <field name="address-city" match="city" type="xs:string"/>
                    <field name="address-code" match="city/@code" type="xs:integer"/>
                </create>
                <create match="//tei:placeName">
                    <field name="name" type="xs:string" nested="no"/>
                    <field name="type" match="@type" type="xs:string"/>
                    <field name="subtype" match="@subtype" type="xs:string"/>
                </create>
                <create match="/test/address/name" whitespace="normalize"/>
                <create match="/test/address/city" type="xs:string" collation="?lang=de-DE&strength=primary"/>
                <create match="/test/address/city/@code" type="xs:integer"/>
                <create qname="@id" type="xs:string"/>
            </range>
        </index>
    </collection>;

declare variable $rt:DATA_NESTED :=
    <place xmlns="http://www.tei-c.org/ns/1.0">
        <placeName xml:id="ODB_S00004004_NAM001" xml:lang="de-DE" type="ref" subtype="inofficial">Hofthiergarten<note type="source">
                <date ana="#notBefore">1750</date>
                <date ana="#notAfter">1820</date>
            </note>
        </placeName>
        <placeName xml:id="ODB_S00004004_NAM002" xml:lang="de-DE" type="main" subtype="official">Hofthiergarten<note type="source">
                <date ana="#when">2011-08-24</date>
            </note>
        </placeName>
        <placeName xml:id="ODB_A00000393_NAM001" xml:lang="de-DE" type="main" subtype="official">Dorfprozelten<note type="source">
                <date ana="#when">2001-04-07</date>
                <bibl>
                    <ptr target="#HAB/Laube"/>
                </bibl>
            </note>
        </placeName>
    </place>;

declare variable $rt:DATA :=
    <test>
        <address id="muh">
            <name>Berta  Muh
            </name>
            <street>Wiesenweg 14</street>
            <city code="65463">Almweide</city>
        </address>
        <address id="rüssel">
            <name>Rudi Rüssel</name>
            <street>Elefantenweg 67</street>
            <city code="65428">Rüsselsheim</city>
        </address>
        <address id="amsel">
            <name>Albert Amsel</name>
            <street>Birkenstraße 77</street>
            <city code="76878">Waldstadt</city>
        </address>
        <address id="reh">
            <name>Pü Reh</name>
            <street>Am Waldrand 4</street>
            <city code="89283">Wiesental</city>
        </address>
    </test>;

declare variable $rt:DATA2 :=
    <object>
        <parameter>
            <name>key1</name>
            <value>value1</value>
        </parameter>
    </object>;

declare
    %test:setUp
function rt:setup() {
    xmldb:create-collection("/db/system/config/db", "rangetest"),
    xmldb:store("/db/system/config/db/rangetest", "collection.xconf", $rt:COLLECTION_CONFIG),
    xmldb:create-collection("/db", "rangetest"),
    xmldb:store("/db/rangetest", "test.xml", $rt:DATA),
    xmldb:store("/db/rangetest", "nested.xml", $rt:DATA_NESTED)
};

declare
    %test:tearDown
function rt:cleanup() {
    xmldb:remove("/db/rangetest"),
    xmldb:remove("/db/system/config/db/rangetest")
};

declare
    %test:args("Rudi Rüssel")
    %test:assertEquals("Rüsselsheim")
    %test:args("Berta Muh")
    %test:assertEquals("Almweide")
function rt:equality-string($name as xs:string) {
    //address[range:eq(name, $name)]/city/text()
};

declare
    %test:args("Rudi Rüssel")
    %test:assertEquals("Rudi Rüssel")
    %test:args("Berta Muh")
    %test:assertEquals("Berta Muh")
function rt:equality-string-self($name as xs:string) {
    normalize-space(//address/name[range:eq(., $name)]/text())
};

declare
    %test:args(65428)
    %test:assertEquals("Rüsselsheim", "Rüsselsheim")
function rt:equality-int-attribute($code as xs:integer) {
    //address/city[range:eq(@code, $code)]/text(),
    //address[range:eq(city/@code, $code)]/city/text()
};

declare
    %test:args("muh")
    %test:assertEquals("Berta Muh")
function rt:equality-qname-string-attribute($id as xs:string) {
    normalize-space(//address[range:eq(@id, $id)]/name/text())
};

declare
    %test:args("russelsheim")
    %test:assertEquals("Rüsselsheim")
    %test:args("almweide")
    %test:assertEquals("Almweide")
function rt:equality-string-collation($name as xs:string) {
    //address[range:eq(city, $name)]/city/text()
};

declare
    %test:args("Berta Muh")
    %test:assertEquals("Almweide")
    %test:args("Pü Reh")
    %test:assertEquals("Wiesental")
function rt:equality-fields($name as xs:string) {
    //range:field-eq("address-name", $name)/city/text()
};

declare
    %test:args(65428)
    %test:assertEquals("Rudi Rüssel")
function rt:equality-field-integer($code as xs:integer) {
    //range:field-eq("address-code", $code)/name/text()
};

declare
    %test:args("Berta Muh")
    %test:assertEquals("Almweide")
    %test:args("Pü Reh")
    %test:assertEquals("Wiesental")
function rt:equality-fields-with-context($name as xs:string) {
    doc("/db/rangetest/test.xml")/range:field-eq("address-name", $name)/city/text()
};

declare
    %test:args("Berta Muh", "Almweide")
    %test:assertEquals("Almweide")
    %test:args("Pü Reh", "Wiesental")
    %test:assertEquals("Wiesental")
function rt:equality-fields-multi($name as xs:string, $city as xs:string) {
    //range:field-eq(("address-name", "address-city"), $name, $city)/city/text()
};

(:declare :)
(:    %test:args("Berta Muh", "Almweide"):)
(:    %test:assertEquals(1):)
(:function rt:equality-fields-multi($name as xs:string, $city as xs:string) {:)
(:    count(/test[range:field-eq(("address-name", "address-city"), $name, $city)]):)
(:};:)

declare
    %test:args("Berta Muh")
    %test:assertEquals(2)
    %test:args("Albert Amsel")
    %test:assertEquals(3)
    %test:args("Pü Reh")
    %test:assertEquals(1)
function rt:gt-string($name as xs:string) {
    count(//address[range:gt(name, $name)])
};

declare
    %test:args("Berta Muh")
    %test:assertEquals(3)
    %test:args("Albert Amsel")
    %test:assertEquals(4)
    %test:args("Pü Reh")
    %test:assertEquals(2)
function rt:ge-string($name as xs:string) {
    count(//address[range:ge(name, $name)])
};

declare
    %test:args("Berta Muh")
    %test:assertEquals(1)
    %test:args("Albert Amsel")
    %test:assertEquals(0)
    %test:args("Pü Reh")
    %test:assertEquals(2)
function rt:lt-string($name as xs:string) {
    count(//address[range:lt(name, $name)])
};

declare
    %test:args("Berta Muh")
    %test:assertEquals(2)
    %test:args("Albert Amsel")
    %test:assertEquals(1)
    %test:args("Pü Reh")
    %test:assertEquals(3)
function rt:le-string($name as xs:string) {
    count(//address[range:le(name, $name)])
};

declare
    %test:args(76878)
    %test:assertEquals(1)
    %test:args(89283)
    %test:assertEquals(0)
    %test:args(65463)
    %test:assertEquals(2)
function rt:gt-integer($code as xs:integer) {
    count(//address[range:gt(city/@code, $code)])
};

declare
    %test:args(76878)
    %test:assertEquals(2)
    %test:args(89283)
    %test:assertEquals(1)
    %test:args(65463)
    %test:assertEquals(3)
function rt:ge-integer($code as xs:integer) {
    count(//address[range:ge(city/@code, $code)])
};

declare
    %test:args(76878)
    %test:assertEquals(2)
    %test:args(65463)
    %test:assertEquals(1)
function rt:lt-integer($code as xs:integer) {
    count(//address[range:lt(city/@code, $code)])
};

declare
    %test:args(76878)
    %test:assertEquals(3)
    %test:args(65463)
    %test:assertEquals(2)
function rt:le-integer($code as xs:integer) {
    count(//address[range:le(city/@code, $code)])
};

declare
    %test:args("Rudi")
    %test:assertEquals("Rüsselsheim")
    %test:args("Berta")
    %test:assertEquals("Almweide")
function rt:starts-with-string($name as xs:string) {
    //address[range:starts-with(name, $name)]/city/text()
};

declare
    %test:args("Rüssel")
    %test:assertEquals("Rüsselsheim")
    %test:args("Muh")
    %test:assertEquals("Almweide")
function rt:ends-with-string($name as xs:string) {
    //address[range:ends-with(name, $name)]/city/text()
};

declare
    %test:args("üss")
    %test:assertEquals("Rüsselsheim")
    %test:args("ta M")
    %test:assertEquals("Almweide")
function rt:contains-string($name as xs:string) {
    //address[range:contains(name, $name)]/city/text()
};

declare
    %test:args(".*Rüssel")
    %test:assertEquals("Rüsselsheim")
function rt:matches-string($name as xs:string) {
    //address[range:matches(name, $name)]/city/text()
};

declare
    %test:args("Rudi")
    %test:assertEquals("Rüsselsheim")
    %test:args("Berta")
    %test:assertEquals("Almweide")
function rt:field-starts-with-string($name as xs:string) {
    //range:field-starts-with("address-name", $name)/city/text()
};

declare
    %test:args("Rüssel")
    %test:assertEquals("Rüsselsheim")
    %test:args("Muh")
    %test:assertEquals("Almweide")
function rt:field-ends-with-string($name as xs:string) {
    //range:field-ends-with("address-name", $name)/city/text()
};

declare
    %test:args("üss")
    %test:assertEquals("Rüsselsheim")
    %test:args("ta M")
    %test:assertEquals("Almweide")
function rt:field-contains-string($name as xs:string) {
    //range:field-contains("address-name", $name)/city/text()
};

declare
    %test:args(".*[rR]üss.*")
    %test:assertEquals("Rüsselsheim")
    %test:args(".*ta M.*")
    %test:assertEquals("Almweide")
function rt:field-matches-string($name as xs:string) {
    //range:field-matches("address-name", $name)/city/text()
};

declare
    %test:args("main", "official", "Hofthiergarten")
    %test:assertEquals("Hofthiergarten")
    %test:args("ref", "inofficial", "Hofthiergarten")
    %test:assertEquals("Hofthiergarten")
    %test:args("main", "official", "Dorfprozelten")
    %test:assertEquals("Dorfprozelten")
function rt:equality-field-nested($type as xs:string, $subtype as xs:string, $name as xs:string) {
    //range:field-eq(("type", "subtype", "name"), $type, $subtype, $name)/text()
};

declare
    %test:assertEquals("Almweide")
function rt:remove-document() {
    let $stored := xmldb:store("/db/rangetest", "test2.xml", $rt:DATA)
    return (
        doc("/db/rangetest/test2.xml")/range:field-eq("address-name", "Berta Muh")/city/string(),
        let $null := xmldb:remove("/db/rangetest", "test2.xml") return $null,
        doc("/db/rangetest/test2.xml")/range:field-eq("address-name", "Berta Muh")/city/string()
    )
};

declare
    %test:assertEquals("Uferweg 67", "Bach")
function rt:update-insert() {
    update insert
        <address>
            <name>Willi Wiesel</name>
            <street>Uferweg 67</street>
            <city code="77777">Bach</city>
        </address>
    into doc("/db/rangetest/test.xml")/test,
    range:field-eq("address-name", "Willi Wiesel")/street/text(),
    //address[range:eq(name, "Willi Wiesel")]/city/text()
};

declare
    %test:assertEmpty
function rt:update-delete() {
    update delete /test/address[range:eq(name, "Berta Muh")],
    //address[range:eq(name, "Berta Muh")],
    range:field-eq("address-name", "Berta Muh")
};

declare
    %test:assertEquals("Am Staudamm 3", "Bach")
function rt:update-replace() {
    update replace /test/address[range:eq(name, "Albert Amsel")]
    with
        <address>
            <name>Berta Bieber</name>
            <street>Am Staudamm 3</street>
            <city code="77777">Bach</city>
        </address>,
    //address[range:eq(name, "Albert Amsel")],
    range:field-eq("address-name", "Albert Amsel"),
    //address[range:eq(name, "Berta Bieber")]/street/text(),
    range:field-eq("address-name", "Berta Bieber")/city/text()
};

declare
    %test:assertEquals("Am Waldrand 4", "Wiesental")
function rt:update-value() {
    update value /test/address/name[range:eq(., "Pü Reh")] with "Rita Rebhuhn",
    //address[range:eq(name, "Pü Reh")],
    range:field-eq("address-name", "Pü Reh"),
    //address[range:eq(name, "Rita Rebhuhn")]/street/text(),
    range:field-eq("address-name", "Rita Rebhuhn")/city/text()
};

xquery version "3.0";

module namespace rt="http://exist-db.org/xquery/range/test";

import module namespace test="http://exist-db.org/xquery/xqsuite" at "resource:org/exist/xquery/lib/xqsuite/xqsuite.xql";

declare variable $rt:COLLECTION_CONFIG :=
    <collection xmlns="http://exist-db.org/collection-config/1.0">
        <index xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:tei="http://www.tei-c.org/ns/1.0">
            <fulltext default="none" attributes="false"/>
            <lucene>
                <text field="lucene-name" qname="name"/>
            </lucene>
            <range>
                <create match="//address">
                    <field name="address-name" match="name" type="xs:string" whitespace="normalize"/>
                    <field name="address-city" match="city" type="xs:string"/>
                    <field name="address-code" match="city/@code" type="xs:integer"/>
                </create>
                <create match="//tei:placeName">
                    <field name="name" type="xs:string" nested="no"/>
                    <field name="type" match="@type" type="xs:string"/>
                    <field name="subtype" match="@subtype" type="xs:string"/>
                </create>
                <create match="/test/address/name" whitespace="normalize"/>
                <create match="/test/address/city" type="xs:string" collation="?lang=de-DE&strength=primary"/>
                <create match="/test/address/city/@code" type="xs:integer"/>
                <create qname="@id" type="xs:string"/>
            </range>
        </index>
    </collection>;

declare variable $rt:DATA_NESTED :=
    <place xmlns="http://www.tei-c.org/ns/1.0">
        <placeName xml:id="ODB_S00004004_NAM001" xml:lang="de-DE" type="ref" subtype="inofficial">Hofthiergarten<note type="source">
                <date ana="#notBefore">1750</date>
                <date ana="#notAfter">1820</date>
            </note>
        </placeName>
        <placeName xml:id="ODB_S00004004_NAM002" xml:lang="de-DE" type="main" subtype="official">Hofthiergarten<note type="source">
                <date ana="#when">2011-08-24</date>
            </note>
        </placeName>
        <placeName xml:id="ODB_A00000393_NAM001" xml:lang="de-DE" type="main" subtype="official">Dorfprozelten<note type="source">
                <date ana="#when">2001-04-07</date>
                <bibl>
                    <ptr target="#HAB/Laube"/>
                </bibl>
            </note>
        </placeName>
    </place>;

declare variable $rt:DATA :=
    <test>
        <address id="muh">
            <name>Berta  Muh
            </name>
            <street>Wiesenweg 14</street>
            <city code="65463">Almweide</city>
        </address>
        <address id="rüssel">
            <name>Rudi Rüssel</name>
            <street>Elefantenweg 67</street>
            <city code="65428">Rüsselsheim</city>
        </address>
        <address id="amsel">
            <name>Albert Amsel</name>
            <street>Birkenstraße 77</street>
            <city code="76878">Waldstadt</city>
        </address>
        <address id="reh">
            <name>Pü Reh</name>
            <street>Am Waldrand 4</street>
            <city code="89283">Wiesental</city>
        </address>
    </test>;

declare variable $rt:DATA2 :=
    <object>
        <parameter>
            <name>key1</name>
            <value>value1</value>
        </parameter>
    </object>;

declare
    %test:setUp
function rt:setup() {
    xmldb:create-collection("/db/system/config/db", "rangetest"),
    xmldb:store("/db/system/config/db/rangetest", "collection.xconf", $rt:COLLECTION_CONFIG),
    xmldb:create-collection("/db", "rangetest"),
    xmldb:store("/db/rangetest", "test.xml", $rt:DATA),
    xmldb:store("/db/rangetest", "nested.xml", $rt:DATA_NESTED)
};

declare
    %test:tearDown
function rt:cleanup() {
    xmldb:remove("/db/rangetest"),
    xmldb:remove("/db/system/config/db/rangetest")
};

declare
    %test:args("Rudi Rüssel")
    %test:assertEquals("Rüsselsheim")
    %test:args("Berta Muh")
    %test:assertEquals("Almweide")
function rt:equality-string($name as xs:string) {
    //address[range:eq(name, $name)]/city/text()
};

declare
    %test:args("Rudi Rüssel")
    %test:assertEquals("Rudi Rüssel")
    %test:args("Berta Muh")
    %test:assertEquals("Berta Muh")
function rt:equality-string-self($name as xs:string) {
    normalize-space(//address/name[range:eq(., $name)]/text())
};

declare
    %test:args(65428)
    %test:assertEquals("Rüsselsheim", "Rüsselsheim")
function rt:equality-int-attribute($code as xs:integer) {
    //address/city[range:eq(@code, $code)]/text(),
    //address[range:eq(city/@code, $code)]/city/text()
};

declare
    %test:args("muh")
    %test:assertEquals("Berta Muh")
function rt:equality-qname-string-attribute($id as xs:string) {
    normalize-space(//address[range:eq(@id, $id)]/name/text())
};

declare
    %test:args("russelsheim")
    %test:assertEquals("Rüsselsheim")
    %test:args("almweide")
    %test:assertEquals("Almweide")
function rt:equality-string-collation($name as xs:string) {
    //address[range:eq(city, $name)]/city/text()
};

declare
    %test:args("Berta Muh")
    %test:assertEquals("Almweide")
    %test:args("Pü Reh")
    %test:assertEquals("Wiesental")
function rt:equality-fields($name as xs:string) {
    range:field-eq("address-name", $name)/city/text()
};

declare
    %test:args(65428)
    %test:assertEquals("Rudi Rüssel")
function rt:equality-field-integer($code as xs:integer) {
    range:field-eq("address-code", $code)/name/text()
};

declare
    %test:args("Berta Muh")
    %test:assertEquals("Almweide")
    %test:args("Pü Reh")
    %test:assertEquals("Wiesental")
function rt:equality-fields-with-context($name as xs:string) {
    doc("/db/rangetest/test.xml")/range:field-eq("address-name", $name)/city/text()
};

declare
    %test:args("Berta Muh", "Almweide")
    %test:assertEquals("Almweide")
    %test:args("Pü Reh", "Wiesental")
    %test:assertEquals("Wiesental")
function rt:equality-fields-multi($name as xs:string, $city as xs:string) {
    range:field-eq(("address-name", "address-city"), $name, $city)/city/text()
};

(:declare :)
(:    %test:args("Berta Muh", "Almweide"):)
(:    %test:assertEquals(1):)
(:function rt:equality-fields-multi($name as xs:string, $city as xs:string) {:)
(:    count(/test[range:field-eq(("address-name", "address-city"), $name, $city)]):)
(:};:)

declare
    %test:args("Berta Muh")
    %test:assertEquals(2)
    %test:args("Albert Amsel")
    %test:assertEquals(3)
    %test:args("Pü Reh")
    %test:assertEquals(1)
function rt:gt-string($name as xs:string) {
    count(//address[range:gt(name, $name)])
};

declare
    %test:args("Berta Muh")
    %test:assertEquals(3)
    %test:args("Albert Amsel")
    %test:assertEquals(4)
    %test:args("Pü Reh")
    %test:assertEquals(2)
function rt:ge-string($name as xs:string) {
    count(//address[range:ge(name, $name)])
};

declare
    %test:args("Berta Muh")
    %test:assertEquals(1)
    %test:args("Albert Amsel")
    %test:assertEquals(0)
    %test:args("Pü Reh")
    %test:assertEquals(2)
function rt:lt-string($name as xs:string) {
    count(//address[range:lt(name, $name)])
};

declare
    %test:args("Berta Muh")
    %test:assertEquals(2)
    %test:args("Albert Amsel")
    %test:assertEquals(1)
    %test:args("Pü Reh")
    %test:assertEquals(3)
function rt:le-string($name as xs:string) {
    count(//address[range:le(name, $name)])
};

declare
    %test:args(76878)
    %test:assertEquals(1)
    %test:args(89283)
    %test:assertEquals(0)
    %test:args(65463)
    %test:assertEquals(2)
function rt:gt-integer($code as xs:integer) {
    count(//address[range:gt(city/@code, $code)])
};

declare
    %test:args(76878)
    %test:assertEquals(2)
    %test:args(89283)
    %test:assertEquals(1)
    %test:args(65463)
    %test:assertEquals(3)
function rt:ge-integer($code as xs:integer) {
    count(//address[range:ge(city/@code, $code)])
};

declare
    %test:args(76878)
    %test:assertEquals(2)
    %test:args(65463)
    %test:assertEquals(1)
function rt:lt-integer($code as xs:integer) {
    count(//address[range:lt(city/@code, $code)])
};

declare
    %test:args(76878)
    %test:assertEquals(3)
    %test:args(65463)
    %test:assertEquals(2)
function rt:le-integer($code as xs:integer) {
    count(//address[range:le(city/@code, $code)])
};

declare
    %test:args("Rudi")
    %test:assertEquals("Rüsselsheim")
    %test:args("Berta")
    %test:assertEquals("Almweide")
function rt:starts-with-string($name as xs:string) {
    //address[range:starts-with(name, $name)]/city/text()
};

declare
    %test:args("Rüssel")
    %test:assertEquals("Rüsselsheim")
    %test:args("Muh")
    %test:assertEquals("Almweide")
function rt:ends-with-string($name as xs:string) {
    //address[range:ends-with(name, $name)]/city/text()
};

declare
    %test:args("üss")
    %test:assertEquals("Rüsselsheim")
    %test:args("ta M")
    %test:assertEquals("Almweide")
function rt:contains-string($name as xs:string) {
    //address[range:contains(name, $name)]/city/text()
};

declare
    %test:args(".*Rüssel")
    %test:assertEquals("Rüsselsheim")
function rt:matches-string($name as xs:string) {
    //address[range:matches(name, $name)]/city/text()
};

declare
    %test:args("Rudi")
    %test:assertEquals("Rüsselsheim")
    %test:args("Berta")
    %test:assertEquals("Almweide")
function rt:field-starts-with-string($name as xs:string) {
    range:field-starts-with("address-name", $name)/city/text()
};

declare
    %test:args("Rüssel")
    %test:assertEquals("Rüsselsheim")
    %test:args("Muh")
    %test:assertEquals("Almweide")
function rt:field-ends-with-string($name as xs:string) {
    range:field-ends-with("address-name", $name)/city/text()
};

declare
    %test:args("üss")
    %test:assertEquals("Rüsselsheim")
    %test:args("ta M")
    %test:assertEquals("Almweide")
function rt:field-contains-string($name as xs:string) {
    range:field-contains("address-name", $name)/city/text()
};

declare
    %test:args(".*[rR]üss.*")
    %test:assertEquals("Rüsselsheim")
    %test:args(".*ta M.*")
    %test:assertEquals("Almweide")
function rt:field-matches-string($name as xs:string) {
    range:field-matches("address-name", $name)/city/text()
};

declare
    %test:args("main", "official", "Hofthiergarten")
    %test:assertEquals("Hofthiergarten")
    %test:args("ref", "inofficial", "Hofthiergarten")
    %test:assertEquals("Hofthiergarten")
    %test:args("main", "official", "Dorfprozelten")
    %test:assertEquals("Dorfprozelten")
function rt:equality-field-nested($type as xs:string, $subtype as xs:string, $name as xs:string) {
    range:field-eq(("type", "subtype", "name"), $type, $subtype, $name)/text()
};

declare
    %test:assertEquals("Almweide")
function rt:remove-document() {
    let $stored := xmldb:store("/db/rangetest", "test2.xml", $rt:DATA)
    return (
        doc("/db/rangetest/test2.xml")/range:field-eq("address-name", "Berta Muh")/city/string(),
        let $null := xmldb:remove("/db/rangetest", "test2.xml") return $null,
        doc("/db/rangetest/test2.xml")/range:field-eq("address-name", "Berta Muh")/city/string()
    )
};

declare
    %test:assertEquals("Uferweg 67", "Bach")
function rt:update-insert() {
    update insert
        <address>
            <name>Willi Wiesel</name>
            <street>Uferweg 67</street>
            <city code="77777">Bach</city>
        </address>
    into doc("/db/rangetest/test.xml")/test,
    range:field-eq("address-name", "Willi Wiesel")/street/text(),
    //address[range:eq(name, "Willi Wiesel")]/city/text()
};

declare
    %test:assertEmpty
function rt:update-delete() {
    update delete /test/address[range:eq(name, "Berta Muh")],
    //address[range:eq(name, "Berta Muh")],
    range:field-eq("address-name", "Berta Muh")
};

declare
    %test:assertEquals("Am Staudamm 3", "Bach")
function rt:update-replace() {
    update replace /test/address[range:eq(name, "Albert Amsel")]
    with
        <address>
            <name>Berta Bieber</name>
            <street>Am Staudamm 3</street>
            <city code="77777">Bach</city>
        </address>,
    //address[range:eq(name, "Albert Amsel")],
    range:field-eq("address-name", "Albert Amsel"),
    //address[range:eq(name, "Berta Bieber")]/street/text(),
    range:field-eq("address-name", "Berta Bieber")/city/text()
};

declare
    %test:assertEquals("Am Waldrand 4", "Wiesental")
function rt:update-value() {
    update value /test/address/name[range:eq(., "Pü Reh")] with "Rita Rebhuhn",
    //address[range:eq(name, "Pü Reh")],
    range:field-eq("address-name", "Pü Reh"),
    //address[range:eq(name, "Rita Rebhuhn")]/street/text(),
    range:field-eq("address-name", "Rita Rebhuhn")/city/text()
};


unit test library

unit test library

svn path=/trunk/eXist/; revision=11531

xquery version "1.0";
(: $Id$ :)
(:
    Module: examples setup
:)

module namespace setup="http://exist-db.org/xquery/admin-interface/setup";

declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace system="http://exist-db.org/xquery/system";

declare function setup:main() as element()
{

Examples Setup

        {
let $action := request:get-parameter("action", ()) return
		if($action) then
		                (
		                    if($action eq "Import Example Data") then
		                    (
		                        setup:importLocal()
		                    )
		                    else if($action eq "Import Files") then
		                    (
		                        setup:importFromURLs()
		                    )
		                    else
		                    (
		                        setup:report()
		                    )
		                )
		                else
		                (
		                    setup:select()
		                )

        }

};

declare function setup:importLocal() as element()+
{
    let $home := system:get-exist-home(),
    $pathSep := util:system-property("file.separator"),
    $dir := if (doc-available(concat("file:///", $home, "/exist-samples/examples.xml"))) then
        (
            concat($home, $pathSep, "exist-samples")
        )
        else if(ends-with($home, "WEB-INF")) then
        (
            concat(substring-before($home, "WEB-INF"), "samples")
        )
        else
        (
            concat($home, $pathSep, "samples")
        ),
    $webapp := if (doc-available(concat("file:///", $home, "/webapp/index.xml"))) then
        (
            concat($home, $pathSep, "webapp")
        )
        else if(ends-with($home, "WEB-INF")) then
        (
            substring-before($home, "WEB-INF")
        )
        else
        (
            concat($home, $pathSep, "webapp")
        )
    return (
                setup:create-collection("/db/system/config", "db"),
                setup:create-collection("/db/system/config/db", "shakespeare"),
                setup:store-files("/db/system/config/db/shakespeare", $dir,
                    "shakespeare/collection.xconf", "text/xml"),
                setup:create-collection("/db", "shakespeare"),
                setup:create-collection("/db/shakespeare", "plays"),
                setup:store-files("/db/shakespeare/plays", $dir,  "shakespeare/*.xml", "text/xml"),
                setup:store-files("/db/shakespeare/plays", $dir, "shakespeare/*.xsl", "text/xml"),
                setup:store-files("/db/shakespeare/plays", $dir, "shakespeare/*.css", "text/css"),
                setup:create-collection("/db", "xinclude"),
                setup:create-collection("/db/xinclude", "styles"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xsl", "text/xml"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xml", "text/xml"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xq", "application/xquery"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.jpg", "image/jpeg"),
                setup:store-files("/db/xinclude/styles", $dir, "xinclude/styles/*", "text/css"),
                setup:create-collection("/db/xinclude", "scripts"),
                setup:create-collection("/db/xinclude/scripts", "syntax"),
                setup:store-files("/db/xinclude/scripts/syntax", $dir, "xinclude/scripts/syntax/*.js", "application/x-javascript"),
                setup:create-collection("/db", "library"),
                setup:store-files("/db/library", $dir, "*.rdf", "text/xml"),
                setup:create-collection("/db/system/config/db", "mods"),
                setup:store-files("/db/system/config/db/mods", $dir, "mods/collection.xconf", "text/xml"),
                setup:create-collection("/db", "mods"),
                setup:store-files("/db/mods", $dir, "mods/*.xml", "text/xml"),
                setup:store-files("/db", $dir, "*.xml", "text/xml"),
                setup:create-collection("/db/system/config/db", "mondial"),
                setup:store-files("/db/system/config/db/mondial", $dir, "mondial.xconf", "text/xml"),
                setup:create-collection("/db/system/config/db", "xmlad"),
                setup:store-files("/db/system/config/db/xmlad", $dir, "acronyms.xconf", "text/xml"),

                (: Storing Testing :)
                setup:create-collection("/db", "testing"),
                setup:store-files("/db/testing", $webapp, "testing/*.xml", "text/html"),
                setup:store-files("/db/testing", $webapp, "testing/*.xq*", "application/xquery"),
                setup:create-collection("/db/testing", "examples"),
                setup:store-files("/db/testing/examples", $webapp, "testing/examples/*.xml", "text/html"),
                setup:store-files("/db/testing/examples", $webapp, "testing/examples/*.xq*", "application/xquery"),

                (: Storing XProc :)
                setup:create-collection("/db", "xproc"),
                setup:create-collection("/db/xproc", "unit-test"),
                setup:create-collection("/db/xproc", "xml"),
                setup:create-collection("/db/xproc", "xslt"),
                setup:create-collection("/db/xproc", "result"),
                setup:create-collection("/db/xproc", "examples"),
                setup:create-collection("/db/xproc", "pipelines"),
                setup:store-files("/db/xproc/unit-test", $webapp, "xproc/unit-test/*.xml", "text/xml"),
                setup:store-files("/db/xproc/unit-test", $webapp, "xproc/unit-test/*.xql", "application/xquery"),
                setup:store-files("/db/xproc/unit-test", $webapp, "xproc/unit-test/*.xsl", "text/xml"),

                setup:store-files("/db/xproc/pipelines", $webapp, "xproc/pipelines/*.xml", "text/xml"),
                setup:store-files("/db/xproc/xml", $webapp, "xproc/xml/*.xml", "text/xml"),
                setup:store-files("/db/xproc/xslt", $webapp, "xproc/xml/*.xsl", "text/xml"),

                setup:store-files("/db/xproc/examples", $webapp, "xproc/examples/*.xproc", "application/xml+xproc"),
                setup:store-files("/db/xproc/examples", $webapp, "xproc/examples/*.xml", "text/xml"),

                setup:store-files("/db/xproc/examples", $webapp, "xproc/examples/*.xql", "application/xquery"),
                setup:store-files("/db/xproc", $webapp, "xproc/*.xml", "text/xml"),
                setup:store-files("/db/xproc", $webapp, "xproc/*.xql", "application/xquery") (:,

                (: Storing XForms in the db won't work right now :)

                setup:create-collection("/db", "xforms"),
                setup:store-files("/db/xforms", $webapp, "xforms/*.xml", "text/html"),
                setup:store-files("/db/xforms", $webapp, "xforms/*.xq*", "application/xquery"),
                setup:create-collection("/db/xforms", "xsltforms"),
                setup:create-collection("/db/xforms", "data"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.properties", "text/plain"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.png", "image/png"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.gif", "image/gif"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.xsl","text/xml"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.js","application/x-javascript"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.css","text/css")
                :)

    )
};

declare function setup:importFromURLs() as element()+
{
    (
        setup:report(),

Actions:

            {
                let $includeXmlad := request:get-parameter("xmlad", ()),
                $includeMondial := request:get-parameter("mondial", ()),
 				$includeLocal := request:get-parameter("local", ()) return
                (
					if($includeLocal) then
					( setup:importLocal()
					)
					else (),
                    if($includeXmlad) then
                    (
                        setup:create-collection("/db", "xmlad"),
                        setup:load-URL("/db/xmlad", xs:anyURI("http://surfnet.dl.sourceforge.net/sourceforge/xmlad/xmlad.xml"), "xmlad.xml")
                    )else (),
                    if($includeMondial) then
                    (
                        setup:create-collection("/db", "mondial"),
                        setup:load-URL("/db/mondial", xs:anyURI("http://www.dbis.informatik.uni-goettingen.de/Mondial/mondial-europe.xml"), "mondial.xml")
                    )else ()
                )
            }

    )
};

declare function setup:load-URL($collection as xs:string, $url as xs:anyURI, $docName as xs:string) as element()
{
    let $x := xdb:store($collection, $docName, $url) return

File {$docName} imported from url: {$url}

};

declare function setup:store-files($collection as xs:string, $home as xs:string, $patterns as xs:string, $mimeType as xs:string) as element()*
{
    let $stored := xdb:store-files-from-pattern($collection, $home, $patterns, $mimeType)
    for $doc in $stored return

Uploaded: {$doc}

};

declare function setup:create-collection($parent as xs:string, $name as xs:string) as element()
{
    let $col := xdb:create-collection($parent, $name) return

Created collection: {$col}

};

declare function setup:select() as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="post">

eXist ships with a number of XQuery examples. Some of these
        require certain documents to be stored in the database. The XQuery examples also use some
		XML data not included with the distribution which can be downloaded by selecting
		the checkboxes below.

		<input type="checkbox" name="local" checked="true"/>
		eXist-db shipped files

        <input type="checkbox" name="xmlad"/>
        The XML Acronym Demystifier
        (approx. 384K)

        <input type="checkbox" name="mondial"/>
        The Mondial Database (Europe)
        (approx. 410K)

<input type="submit" name="action" value="Import Files"/>
        <input type="submit" name="action" value="Skip"/>

        <input type="hidden" name="panel" value="setup"/>
    </form>
};

declare function setup:report() as element()
{

Files have been loaded. You can now go to the
    examples page.

};

xquery version "1.0";
(: $Id$ :)
(:
    Module: examples setup
:)

module namespace setup="http://exist-db.org/xquery/admin-interface/setup";

declare namespace xdb="http://exist-db.org/xquery/xmldb";
declare namespace request="http://exist-db.org/xquery/request";
declare namespace session="http://exist-db.org/xquery/session";
declare namespace util="http://exist-db.org/xquery/util";
declare namespace system="http://exist-db.org/xquery/system";

declare function setup:main() as element()
{

Examples Setup

        {
let $action := request:get-parameter("action", ()) return
		if($action) then
		                (
		                    if($action eq "Import Example Data") then
		                    (
		                        setup:importLocal()
		                    )
		                    else if($action eq "Import Files") then
		                    (
		                        setup:importFromURLs()
		                    )
		                    else
		                    (
		                        setup:report()
		                    )
		                )
		                else
		                (
		                    setup:select()
		                )

        }

};

declare function setup:importLocal() as element()+
{
    let $home := system:get-exist-home(),
    $pathSep := util:system-property("file.separator"),
    $dir := if (doc-available(concat("file:///", $home, "/exist-samples/examples.xml"))) then
        (
            concat($home, $pathSep, "exist-samples")
        )
        else if(ends-with($home, "WEB-INF")) then
        (
            concat(substring-before($home, "WEB-INF"), "samples")
        )
        else
        (
            concat($home, $pathSep, "samples")
        ),
    $webapp := if (doc-available(concat("file:///", $home, "/webapp/index.xml"))) then
        (
            concat($home, $pathSep, "webapp")
        )
        else if(ends-with($home, "WEB-INF")) then
        (
            substring-before($home, "WEB-INF")
        )
        else
        (
            concat($home, $pathSep, "webapp")
        )
    return (
                setup:create-collection("/db/system/config", "db"),
                setup:create-collection("/db/system/config/db", "shakespeare"),
                setup:store-files("/db/system/config/db/shakespeare", $dir,
                    "shakespeare/collection.xconf", "text/xml"),
                setup:create-collection("/db", "shakespeare"),
                setup:create-collection("/db/shakespeare", "plays"),
                setup:store-files("/db/shakespeare/plays", $dir,  "shakespeare/*.xml", "text/xml"),
                setup:store-files("/db/shakespeare/plays", $dir, "shakespeare/*.xsl", "text/xml"),
                setup:store-files("/db/shakespeare/plays", $dir, "shakespeare/*.css", "text/css"),
                setup:create-collection("/db", "xinclude"),
                setup:create-collection("/db/xinclude", "styles"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xsl", "text/xml"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xml", "text/xml"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.xq", "application/xquery"),
                setup:store-files("/db/xinclude", $dir, "xinclude/*.jpg", "image/jpeg"),
                setup:store-files("/db/xinclude/styles", $dir, "xinclude/styles/*", "text/css"),
                setup:create-collection("/db/xinclude", "scripts"),
                setup:create-collection("/db/xinclude/scripts", "syntax"),
                setup:store-files("/db/xinclude/scripts/syntax", $dir, "xinclude/scripts/syntax/*.js", "application/x-javascript"),
                setup:create-collection("/db", "library"),
                setup:store-files("/db/library", $dir, "*.rdf", "text/xml"),
                setup:create-collection("/db/system/config/db", "mods"),
                setup:store-files("/db/system/config/db/mods", $dir, "mods/collection.xconf", "text/xml"),
                setup:create-collection("/db", "mods"),
                setup:store-files("/db/mods", $dir, "mods/*.xml", "text/xml"),
                setup:store-files("/db", $dir, "*.xml", "text/xml"),
                setup:create-collection("/db/system/config/db", "mondial"),
                setup:store-files("/db/system/config/db/mondial", $dir, "mondial.xconf", "text/xml"),
                setup:create-collection("/db/system/config/db", "xmlad"),
                setup:store-files("/db/system/config/db/xmlad", $dir, "acronyms.xconf", "text/xml"),
                setup:create-collection("/db", "xproc"),
                setup:create-collection("/db/xproc", "unit-test"),
                setup:create-collection("/db/xproc", "xml"),
                setup:create-collection("/db/xproc", "xslt"),
                setup:create-collection("/db/xproc", "result"),
                setup:create-collection("/db/xproc", "examples"),
                setup:create-collection("/db/xproc", "pipelines"),
                setup:store-files("/db/xproc/unit-test", $webapp, "xproc/unit-test/*.xml", "text/xml"),
                setup:store-files("/db/xproc/unit-test", $webapp, "xproc/unit-test/*.xql", "application/xquery"),
                setup:store-files("/db/xproc/unit-test", $webapp, "xproc/unit-test/*.xsl", "text/xml"),

                setup:store-files("/db/xproc/pipelines", $webapp, "xproc/pipelines/*.xml", "text/xml"),
                setup:store-files("/db/xproc/xml", $webapp, "xproc/xml/*.xml", "text/xml"),
                setup:store-files("/db/xproc/xslt", $webapp, "xproc/xml/*.xsl", "text/xml"),

                setup:store-files("/db/xproc/examples", $webapp, "xproc/examples/*.xproc", "application/xml+xproc"),
                setup:store-files("/db/xproc/examples", $webapp, "xproc/examples/*.xml", "text/xml"),

                setup:store-files("/db/xproc/examples", $webapp, "xproc/examples/*.xql", "application/xquery"),
                setup:store-files("/db/xproc", $webapp, "xproc/*.xml", "text/xml"),
                setup:store-files("/db/xproc", $webapp, "xproc/*.xql", "application/xquery") (:,

                (: Storing XForms in the db won't work right now :)

                setup:create-collection("/db", "xforms"),
                setup:store-files("/db/xforms", $webapp, "xforms/*.xml", "text/html"),
                setup:store-files("/db/xforms", $webapp, "xforms/*.xq*", "application/xquery"),
                setup:create-collection("/db/xforms", "xsltforms"),
                setup:create-collection("/db/xforms", "data"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.properties", "text/plain"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.png", "image/png"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.gif", "image/gif"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.xsl","text/xml"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.js","application/x-javascript"),
                setup:store-files("/db/xforms/xsltforms", $webapp, "xforms/xsltforms/*.css","text/css")
                :)
    )
};

declare function setup:importFromURLs() as element()+
{
    (
        setup:report(),

Actions:

            {
                let $includeXmlad := request:get-parameter("xmlad", ()),
                $includeMondial := request:get-parameter("mondial", ()),
 				$includeLocal := request:get-parameter("local", ()) return
                (
					if($includeLocal) then
					( setup:importLocal()
					)
					else (),
                    if($includeXmlad) then
                    (
                        setup:create-collection("/db", "xmlad"),
                        setup:load-URL("/db/xmlad", xs:anyURI("http://surfnet.dl.sourceforge.net/sourceforge/xmlad/xmlad.xml"), "xmlad.xml")
                    )else (),
                    if($includeMondial) then
                    (
                        setup:create-collection("/db", "mondial"),
                        setup:load-URL("/db/mondial", xs:anyURI("http://www.dbis.informatik.uni-goettingen.de/Mondial/mondial-europe.xml"), "mondial.xml")
                    )else ()
                )
            }

    )
};

declare function setup:load-URL($collection as xs:string, $url as xs:anyURI, $docName as xs:string) as element()
{
    let $x := xdb:store($collection, $docName, $url) return

File {$docName} imported from url: {$url}

};

declare function setup:store-files($collection as xs:string, $home as xs:string, $patterns as xs:string, $mimeType as xs:string) as element()*
{
    let $stored := xdb:store-files-from-pattern($collection, $home, $patterns, $mimeType)
    for $doc in $stored return

Uploaded: {$doc}

};

declare function setup:create-collection($parent as xs:string, $name as xs:string) as element()
{
    let $col := xdb:create-collection($parent, $name) return

Created collection: {$col}

};

declare function setup:select() as element()
{
    <form action="{session:encode-url(request:get-uri())}" method="post">

eXist ships with a number of XQuery examples. Some of these
        require certain documents to be stored in the database. The XQuery examples also use some
		XML data not included with the distribution which can be downloaded by selecting
		the checkboxes below.

		<input type="checkbox" name="local" checked="true"/>
		eXist-db shipped files

        <input type="checkbox" name="xmlad"/>
        The XML Acronym Demystifier
        (approx. 384K)

        <input type="checkbox" name="mondial"/>
        The Mondial Database (Europe)
        (approx. 410K)

<input type="submit" name="action" value="Import Files"/>
        <input type="submit" name="action" value="Skip"/>

        <input type="hidden" name="panel" value="setup"/>
    </form>
};

declare function setup:report() as element()
{

Files have been loaded. You can now go to the
    examples page.

};