xquery version "1.0"; declare namespace request="http://exist-db.org/xquery/request"; 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 "xquery/saveroutines.xqm"; import module namespace ristenutil="http://www.risten.no/shared/util" at "../xquery/ristenutil.xqm"; (: This function updates the ID of an entry, and all references to it :) declare function local:updateID($oldID as xs:string, $newID as xs:string) as xs:string { let $lang := request:get-parameter("lang", ""), $user := session:get-attribute("user"), $cntr := "termcenter.xml", $doc := ristenutil:get-doc($lang), $cntdoc := concat($coll,'/',$cntr) (: Need to use ' since $oldcenterref is used as part of an attribute already containing '' :) (: Also, it has to be ' and not ", since it should match a string containing '' :) let $oldcenterref := concat("terms-", $lang, ".xml", "#xpointer(//entry[@id='", $oldID, "'])" ), $newcenterref := concat("terms-", $lang, ".xml", "#xpointer(//entry[@id='", $newID, "'])" ) (: Update the ID of the entry itself: :) let $mainupdate := {$newID} (: Update all references to the term in the central term records: :) (: Note that since $oldcenterref contains escaped '-s ('), it has to be surrounded by double quotes, which in turn has to be within single quotes. This alternation is important - without it the selection will crash due to unexpected end of string!!! :) let $centerupdate := { $newcenterref } (: Update all references to the term from synonyms, orthographical variants and main terms: :) (: The references to be updated are in attributes: - mainref - synref - variantref and have to be targeted independently. :) let $refupdate := ( {$newID}, {$newID}, {$newID} ) let $timestamp := save:languageTimeStamp($doc) let $cntrtimestamp := save:centerTimeStamp($cntdoc) (: Wrap all update statements in the outer XUpdate element: :) let $xupdate := { $mainupdate } { $centerupdate } { $refupdate } { $timestamp } { $cntrtimestamp } (: Execute the complete XUpdate: :) let $dummy := xmldb:update($collection, $xupdate) return $newID }; (: 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:get-parameter("id", ""), $rawentry := request:get-parameter("entry", ""), $pos := request:get-parameter("pos", ""), $entry := ristenutil:cleanEntry( $rawentry ), $entrylang := request:get-parameter("lang", ""), $inputtype := request:get-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 manually to the existing entry.

else let $updatedid := if ($record != $newid) then local: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:get-parameter("lang", "") return ( ristenutil:pre-main($entrylang, local:main()),

SearchTime { round-half-to-even( seconds-from-duration( util:system-time()-$start) , 3)}

) }