forked from marklogic-community/roxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
marklogic-community#109 added ability to add and update triggers
- Loading branch information
Showing
3 changed files
with
180 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
(: | ||
Copyright 2016 MarkLogic Corporation | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
:) | ||
xquery version "1.0-ml"; | ||
|
||
declare namespace trgr="http://marklogic.com/xdmp/triggers"; | ||
|
||
declare namespace triggers = "http://marklogic.com/roxy/triggers"; | ||
|
||
declare option xdmp:mapping "false"; | ||
|
||
(: | ||
: Loads a pipeline from a configuration xml | ||
: | ||
:@param $config - the configuration xml | ||
: | ||
:) | ||
declare function triggers:load-from-config($config as element(trgr:triggers)) | ||
{ | ||
let $triggers-db := xdmp:triggers-database() | ||
let $test := | ||
if ($triggers-db = 0) then | ||
fn:error(xs:QName("TRIGGERS-DB"), "You must have a triggers database configured to deploy triggers.") | ||
else () | ||
for $trgr in $config/trgr:trigger | ||
let $name := $trgr/trgr:name | ||
let $desc as xs:string := $trgr/trgr:description | ||
let $data-event := $trgr/trgr:data-event | ||
let $module := | ||
<trgr:module> | ||
<trgr:database>{xdmp:database($trgr/trgr:module/trgr:database/fn:string())}</trgr:database> | ||
{ | ||
$trgr/trgr:module/* except $trgr/trgr:module/trgr:database | ||
} | ||
</trgr:module> | ||
let $enabled as xs:boolean := $trgr/trgr:enabled | ||
let $permissions as element()* := | ||
( | ||
triggers:resolve-permissions($trgr/trgr:permissions/sec:permission), | ||
xdmp:default-permissions() | ||
)[1] | ||
let $recursive as xs:boolean := ($trgr/trgr:recursive, fn:true())[1] | ||
let $priority as xs:string? := $trgr/trgr:task-priority | ||
let $_ := | ||
if (fn:empty($priority) or $priority = ("normal", "higher")) then () | ||
else | ||
fn:error(xs:QName("PRIORITY-VALUE"), 'Task priority must be "normal" or "higher".') | ||
return ( | ||
xdmp:eval( | ||
'xquery version "1.0-ml"; | ||
import module namespace trgr="http://marklogic.com/xdmp/triggers" at "/MarkLogic/triggers.xqy"; | ||
declare variable $name external; | ||
declare variable $desc external; | ||
declare variable $data-event external; | ||
declare variable $module external; | ||
declare variable $enabled external; | ||
declare variable $permissions external; | ||
declare variable $recursive external; | ||
declare variable $priority external; | ||
if (fn:exists(/trgr:trigger/trgr:trigger-name[. = $name])) then | ||
(: trigger already exists. update it :) | ||
( | ||
trgr:trigger-set-description($name, $desc), | ||
trgr:trigger-set-event($name, $data-event), | ||
trgr:trigger-set-module($name, $module), | ||
if ($enabled) then trgr:trigger-enable($name) | ||
else trgr:trigger-disable($name), | ||
trgr:trigger-set-permissions($name, $permissions), | ||
trgr:trigger-set-recursive($name, $recursive), | ||
trgr:trigger-set-task-priority($name, $priority) | ||
) | ||
else | ||
(: new trigger. create it. :) | ||
trgr:create-trigger( | ||
$name, $desc, | ||
$data-event, | ||
$module, | ||
$enabled, | ||
$permissions, | ||
$recursive, | ||
($priority, "normal")[1] | ||
)', | ||
map:new(( | ||
map:entry("name", $name), | ||
map:entry("desc", $desc), | ||
map:entry("data-event", $data-event), | ||
map:entry("module", $module), | ||
map:entry("enabled", $enabled), | ||
map:entry("permissions", $permissions), | ||
map:entry("recursive", $recursive), | ||
map:entry("priority", ($priority, "normal")[1]) | ||
)), | ||
<options xmlns="xdmp:eval"> | ||
<database>{xdmp:triggers-database()}</database> | ||
</options> | ||
) | ||
) | ||
}; | ||
|
||
declare function triggers:resolve-permissions($perms as element(sec:permission)*) | ||
{ | ||
for $perm in $perms | ||
return | ||
<sec:permission> | ||
{$perm/sec:capability} | ||
<sec:role-id>{xdmp:role($perm/sec:role-name)}</sec:role-id> | ||
</sec:permission> | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<triggers xmlns:trgr="http://marklogic.com/xdmp/triggers"> | ||
<trgr:trigger> | ||
<trgr:name>CreateTrigger</trgr:name> | ||
<trgr:description>Trigger to enrich XML ingested via MLCP</trgr:description> | ||
<trgr:data-event> | ||
<trgr:directory-scope> | ||
<trgr:uri>/myDir/</trgr:uri> | ||
<trgr:depth>1</trgr:depth> | ||
</trgr:directory-scope> | ||
<trgr:document-content> | ||
<trgr:update-kind>modify</trgr:update-kind> | ||
</trgr:document-content> | ||
<trgr:when>pre-commit</trgr:when> | ||
</trgr:data-event> | ||
<trgr:module> | ||
<trgr:database name="roxy-modules"/> | ||
<trgr:root>/modules/</trgr:root> | ||
<trgr:path>log-create.xqy</trgr:path> | ||
</trgr:module> | ||
<trgr:enabled>true</trgr:enabled> | ||
<trgr:permissions> | ||
<sec:permission> | ||
<sec:capability>read</sec:capability> | ||
<sec:role-name>reader</sec:role-name> | ||
</sec:permission>, | ||
<sec:permission> | ||
<sec:capability>update</sec:capability> | ||
<sec:role-name>writer</sec:role-name> | ||
</sec:permission> | ||
</trgr:permissions> | ||
<trgr:recursive>false</trgr:recursive> | ||
<trgr:task-priority>normal</trgr:task-priority> | ||
</trgr:trigger> | ||
</triggers> |