Last active
November 9, 2016 15:39
-
-
Save paxtonhare/81f1eeeabc50f71f8cd3628e906f0c2b to your computer and use it in GitHub Desktop.
Sample scaffolding output for ES driven content plugin
This file contains hidden or 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
xquery version "1.0-ml"; | |
module namespace plugin = "http://marklogic.com/data-hub/plugins"; | |
import module namespace es = "http://marklogic.com/entity-services" | |
at "/Marklogic/entity-services/entity-services.xqy"; | |
declare option xdmp:mapping "false"; | |
(:~ | |
: Create Content Plugin | |
: | |
: @param $id - the identifier returned by the collector | |
: @param $options - a map containing options. Options are sent from Java | |
: | |
: @return - your transformed content | |
:) | |
declare function plugin:create-content( | |
$id as xs:string, | |
$options as map:map) as map:map | |
{ | |
let $doc := fn:doc($id) | |
let $source := | |
if ($doc/es:envelope) then | |
$doc/es:envelope/es:instance/node() | |
else if ($doc/instance) then | |
$doc/instance | |
else | |
$doc | |
return | |
plugin:extract-instance-Run($source) | |
}; | |
(:~ | |
: Creates a map:map instance from some source document. | |
: @param $source-node A document or node that contains | |
: data for populating a Race | |
: @return A map:map instance with extracted data and | |
: metadata about the instance. | |
:) | |
declare function plugin:extract-instance-Race( | |
$source as node()? | |
) as map:map | |
{ | |
let $id as sem:iri? := () | |
let $name as xs:string := () | |
(: The following property assigment comes from an external reference. | |
Its generated value probably requires developer attention. :) | |
let $race-category := | |
plugin:make-reference-object('Running', (: put your value here :) '') | |
(: The following property assigment comes from an external reference. | |
Its generated value probably requires developer attention. :) | |
let $stuff-junk := json:to-array( | |
plugin:make-reference-object('Running', (: put your value here :) '') | |
) | |
(: The following property is a local reference. :) | |
let $comprised-of-runs := | |
let $runs := | |
(: create a sequence of Run instances from your data :) | |
for $source in () | |
return | |
plugin:extract-instance-Run($source) | |
return | |
if (fn:exists($runs)) then | |
json:to-array($runs) | |
else () | |
(: The following property is a local reference. :) | |
let $won-by-runner := | |
let $runners := | |
(: create a sequence of Runner instances from your data :) | |
for $source in () | |
return | |
plugin:extract-instance-Runner($source) | |
return | |
json:to-array($runners) | |
let $course-length as xs:decimal := () | |
(: return the in-memory instance :) | |
(: using the XQuery 3.0 syntax... :) | |
let $model := json:object() | |
let $_ := ( | |
map:put($model, '$type', 'Race'), | |
es:optional($model, 'id', $id), | |
map:put($model, 'name', $name), | |
es:optional($model, 'raceCategory', $race-category), | |
es:optional($model, 'stuff-junk', $stuff-junk), | |
es:optional($model, 'comprisedOfRuns', $comprised-of-runs), | |
map:put($model, 'wonByRunner', $won-by-runner), | |
map:put($model, 'courseLength', $course-length) | |
) | |
(: if you prefer the xquery 3.1 version with the => operator.... | |
: https://www.w3.org/TR/xquery-31/#id-arrow-operator | |
let $model := | |
json:object() | |
=>map:with('$type', 'Race') | |
=>es:optional('id', $id) | |
=>map:with('name', $name) | |
=>es:optional('raceCategory', $race-category) | |
=>es:optional('stuff-junk', $stuff-junk) | |
=>es:optional('comprisedOfRuns', $comprised-of-runs) | |
=>map:with('wonByRunner', $won-by-runner) | |
=>map:with('courseLength', $course-length) | |
:) | |
return | |
$model | |
}; | |
(:~ | |
: Creates a map:map instance from some source document. | |
: @param $source-node A document or node that contains | |
: data for populating a Run | |
: @return A map:map instance with extracted data and | |
: metadata about the instance. | |
:) | |
declare function plugin:extract-instance-Run( | |
$source as node()? | |
) as map:map | |
{ | |
(: the original source documents :) | |
let $attachments := $source | |
let $id as sem:iri := () | |
let $date as xs:date := () | |
let $distance as xs:decimal := () | |
let $distance-label as xs:string? := () | |
let $duration as xs:dayTimeDuration? := () | |
(: The following property is a local reference. :) | |
let $run-by-runner := | |
let $runners := | |
(: create a sequence of Runner instances from your data :) | |
for $source in () | |
return | |
plugin:extract-instance-Runner($source) | |
return | |
json:to-array($runners) | |
(: return the in-memory instance :) | |
(: using the XQuery 3.0 syntax... :) | |
let $model := json:object() | |
let $_ := ( | |
map:put($model, '$attachments', $attachments), | |
map:put($model, '$type', 'Run'), | |
map:put($model, 'id', $id), | |
map:put($model, 'date', $date), | |
map:put($model, 'distance', $distance), | |
es:optional($model, 'distanceLabel', $distance-label), | |
es:optional($model, 'duration', $duration), | |
map:put($model, 'runByRunner', $run-by-runner) | |
) | |
(: if you prefer the xquery 3.1 version with the => operator.... | |
: https://www.w3.org/TR/xquery-31/#id-arrow-operator | |
let $model := | |
json:object() | |
=>map:with('$attachments', $attachments) | |
=>map:with('$type', 'Run') | |
=>map:with('id', $id) | |
=>map:with('date', $date) | |
=>map:with('distance', $distance) | |
=>es:optional('distanceLabel', $distance-label) | |
=>es:optional('duration', $duration) | |
=>map:with('runByRunner', $run-by-runner) | |
:) | |
return | |
$model | |
}; | |
(:~ | |
: Creates a map:map instance from some source document. | |
: @param $source-node A document or node that contains | |
: data for populating a Runner | |
: @return A map:map instance with extracted data and | |
: metadata about the instance. | |
:) | |
declare function plugin:extract-instance-Runner( | |
$source as node()? | |
) as map:map | |
{ | |
let $id as sem:iri? := () | |
let $name as xs:string := () | |
let $age as xs:decimal := () | |
let $gender as xs:string? := () | |
(: return the in-memory instance :) | |
(: using the XQuery 3.0 syntax... :) | |
let $model := json:object() | |
let $_ := ( | |
map:put($model, '$type', 'Runner'), | |
es:optional($model, 'id', $id), | |
map:put($model, 'name', $name), | |
map:put($model, 'age', $age), | |
es:optional($model, 'gender', $gender) | |
) | |
(: if you prefer the xquery 3.1 version with the => operator.... | |
: https://www.w3.org/TR/xquery-31/#id-arrow-operator | |
let $model := | |
json:object() | |
=>map:with('$type', 'Runner') | |
=>es:optional('id', $id) | |
=>map:with('name', $name) | |
=>map:with('age', $age) | |
=>es:optional('gender', $gender) | |
:) | |
return | |
$model | |
}; | |
declare function plugin:make-reference-object( | |
$type as xs:string, | |
$ref as xs:string) | |
{ | |
let $o := json:object() | |
let $_ := ( | |
map:put($o, '$type', $type), | |
map:put($o, '$ref', $ref) | |
) | |
return | |
$o | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment