xquery version "1.0"; declare namespace util="http://exist-db.org/xquery/util"; declare namespace fn="http://exist-db.org/local-functions"; declare namespace i18n="http://apache.org/cocoon/i18n/2.1"; import module namespace save="http://www.risten.no/edit/save" at "saveroutines.xqm"; import module namespace ristenutil="http://www.risten.no/shared/util" at "../xquery/ristenutil.xqm"; (: Assemble the query string :) declare function fn:build-query($record as xs:string, $entrylang as xs:string) as xs:string { let $expr := ristenutil:id-filter($record), $doc := ristenutil:get-doc($entrylang) return concat("for $r in document('", $doc, "')//entry[", $expr, "] return $r") }; (: Check for the existence of the new entry: :) declare function fn:ID-exists($entry as xs:string, $entrylang as xs:string) as xs:boolean { let $query := fn:build-query($entry, $entrylang), $hits := util:eval( $query ), $count := count($hits) return if ( $count = 0 ) then false() else true() }; (: Modify the query results before display: :) declare function fn:display($entrylang as xs:string, $hits as node()+) as element() { let $entry := item-at($hits, 1), $common := $entry/common, $senses := $entry/senses, $id := $entry/@id return {$common} { for $s in $entry/senses/sense let $status := $s/@status order by $status return { if ( $s/def ) then $s/def else (), if ( $s/synonyms ) then $s/synonyms else () } } }; (: Main function - retrieves some request attributes, forks further processing to other defined functions depending on the retrieved values :) declare function fn:main() as element()+ { let $record := request:request-parameter("id", ""), $entry := request:request-parameter("entry", ""), $pos := request:request-parameter("pos", ""), $entrylang := request:request-parameter("lang", ""), $inputtype := request:request-parameter("common", ""), $newid := concat(translate($entry,' ','_'),'\',$pos) let $exists := fn:ID-exists($newid, $entrylang) return if(string-length($record) = 0) then

WARNING: There was an error with this request! Please check with the webmaster. Cause: No request record value.

else if ( ( $exists ) and ($record != $newid) ) then

WARNING: You can not change the entry to an existing entry. Please copy the information to the existing entry.

else let $updatedid := if ($record != $newid) then save:updateID($record, $newid) else $record let $saveresult := save:saveentry($updatedid), $query := fn:build-query($updatedid, $entrylang), $hits := util:eval( $query ) return if (empty($hits)) then (

ERROR! Record {$updatedid} not found!

,

Your language: {$entrylang}

,

Your request: {$updatedid}

,

Your entry: {$entry}

,

Your search query:

) else ( (: DEBUG:

Your search query:

,

The input type: {$inputtype}

,

The save result: {$saveresult}

, :) fn:display($entrylang, $hits) ) };
{ let $start := current-time() return ( fn:main(),

Søket tok { seconds-from-duration(current-time()-$start) } sekund.

) }