xquery version "1.0"; declare namespace util="http://exist-db.org/xquery/util"; 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 local: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 local:ID-exists($entry as xs:string, $entrylang as xs:string) as xs:boolean { let $query := local:build-query($entry, $entrylang), $hits := util:eval( $query ), $count := count($hits) return if ( $count = 0 ) then false() else true() }; (: Main function - retrieves some request attributes, forks further processing to other defined functions depending on the retrieved values :) declare function local:main() as element()+ { let $record := request:request-parameter("id", ""), $rawentry := request:request-parameter("entry", ""), $pos := request:request-parameter("pos", ""), $entry := ristenutil:cleanEntry( $rawentry ), $entrylang := request:request-parameter("lang", ""), $inputtype := request:request-parameter("common", ""), $newid := concat(translate($entry,' ','_'),'\',$pos) let $exists := local: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 := local: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}

, :) ristenutil:display($entrylang, $hits) ) };
{ let $start := util:system-time(), $entrylang := request:request-parameter("lang", "") return ( ristenutil:pre-main($entrylang, local:main()),

SearchTime { seconds-from-duration(util:system-time()-$start) }

) }