source: TI07-MOLES/trunk/StubB/XQuery/NDG-ActStubB.xquery @ 2699

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/StubB/XQuery/NDG-ActStubB.xquery@2699
Revision 2699, 15.4 KB checked in by ko23, 12 years ago (diff)

Fix for #799, and update examples

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1declare default element namespace 'http://ndg.nerc.ac.uk/moles/activity';
2declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';
3declare namespace moles='http://ndg.nerc.ac.uk/moles';
4
5import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
6import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
7(:
8Will return when scope problem sussed... ;(
9import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery';
10:)
11declare variable $targetCollection as xs:string {'/db/ndg_B_metadata'};
12declare variable $repositoryIdentifier as xs:string {'badc.nerc.ac.uk'};
13declare variable $localIdentifier as xs:string {'activity_11680365144712080'};
14(: replacable parameters
15declare variable $targetCollection as xs:string {'TargetCollection'};
16declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
17declare variable $localIdentifier as xs:string {'LocalID'};
18:)
19
20declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
21(: Returns a 'stubB' style activity record to expand an activity record :)
22(: Security not implemented here yet :)
23{
24let $SRAct := collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
25moles:dgMetadataID/moles:localIdentifier=$locid
26and moles:dgMetadataID/moles:repositoryIdentifier=$repid ]
27return
28element  {$out-element-name} {
29($SRAct/moles:dgMetadataID),
30($SRAct/moles:dgMetadataDescription),
31($SRAct/moles:name),
32($SRAct/moles:abbreviation),
33($SRAct/moles:logos)
34} (: </activity> :)
35} ;
36
37declare function f:return-stub-dataentity($out-element-name as xs:string, $DE as element()) as element()
38(: Returns a 'stubB' style observation station record to expand an data entity record :)
39(: Security not implemented yet :)
40{
41let $SRDE := $DE
42return 
43element {$out-element-name} {
44($SRDE/moles:dgMetadataID),
45($SRDE/moles:dgMetadataDescription),
46($SRDE/moles:name),
47($SRDE/moles:abbreviation)
48} (: </dgDataEntity> :)       
49} ;
50
51declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
52(: Returns a 'stubB' style dpt record to expand a dpt record :)
53(: Security not implemented yet :)
54{
55let $SRDPT := collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
56moles:dgMetadataID/moles:localIdentifier=$locid
57and
58moles:dgMetadataID/moles:repositoryIdentifier=$repid
59]
60return 
61element {$out-element-name} {
62$SRDPT/moles:dgMetadataID,
63$SRDPT/moles:dgMetadataDescription,
64$SRDPT/moles:name,
65$SRDPT/moles:abbreviation,
66$SRDPT/moles:logos
67} (: </dataproductiontool> :)
68} ;
69
70declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
71(: Returns a 'stubB' style observation station record to expand an observation station record :)
72(: Security not implemented here yet;  :)
73{
74let $SRObsStn := collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
75moles:dgMetadataID/moles:localIdentifier=$locid
76and
77moles:dgMetadataID/moles:repositoryIdentifier=$repid
78]
79return
80element {$out-element-name} {
81($SRObsStn/moles:dgMetadataID),
82($SRObsStn/moles:dgMetadataDescription),
83($SRObsStn/moles:name),
84($SRObsStn/moles:abbreviation),
85($SRObsStn/moles:logos)
86}
87} ;
88
89declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
90(: Returns a 'stubB' style observation station record to expand a person record :)
91(: Security not implemented yet :)
92{
93let $person := collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
94moles:dgMetadataID/moles:localIdentifier=$locid
95and
96moles:dgMetadataID/moles:repositoryIdentifier=$repid
97]
98return
99element  {$out-element-name}
100{
101($person/moles:name),
102($person/moles:contactDetails),
103($person/moles:dgMetadataID)
104}
105} ;
106
107declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
108(: Returns a 'stubB' style observation station record to expand an organisation record :)
109(: Security not implemented yet :)
110{
111let $org := collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
112moles:dgMetadataID/moles:localIdentifier=$locid
113and
114moles:dgMetadataID/moles:repositoryIdentifier=$repid
115]
116return
117element  {$out-element-name} {
118$org/moles:dgMetadataID,
119$org/moles:name,
120$org/moles:abbreviation,
121$org/moles:contactDetails,
122$org/moles:logos
123}
124} ;
125
126for $Act in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
127    moles:dgActivity!=''
128    and moles:dgMetadataID[
129    moles:schemeIdentifier='NDG-B0'
130    and moles:repositoryIdentifier=$repositoryIdentifier
131    and moles:localIdentifier=$localIdentifier]]
132    return
133utillib:strip-namespace(element dgMetadataRecord {
134    element dgMetadataID {
135        element schemeIdentifier {'NDG-B1'},
136        (element repositoryIdentifier {data($Act/moles:dgMetadataID/moles:repositoryIdentifier)}),
137        element localIdentifier {data($Act/moles:dgMetadataID/moles:localIdentifier)}
138    },
139    $Act/moles:dgMetadataDescription,
140    $Act/moles:name,
141    $Act/moles:abbreviation,
142    element dgActivity {
143        for $RelatedActivities in $Act/dgActivity/moles:relatedActivity
144            return
145                element relatedActivity {
146                    $RelatedActivities/moles:activityRelation,
147                    f:return-stub-activity('activity', data($RelatedActivities/moles:relatedActivityID/moles:repositoryIdentifier), data($RelatedActivities/moles:relatedActivityID/moles:localIdentifier))
148            },    (: </relatedActivity> :)
149        if (exists($Act/moles:dgActivity/moles:dgActivityDataCollection)) then ($Act/moles:dgActivity/moles:dgActivityDataCollection)
150        else if (exists($Act/moles:dgActivity/moles:dgActivityDataProject)) then ($Act/moles:dgActivity/moles:dgActivityDataProject)
151        else if (exists($Act/moles:dgActivity/moles:dgActivityDataInvestigation)) then ($Act/moles:dgActivity/moles:dgActivityDataInvestigation)
152        else if (exists($Act/moles:dgActivity/moles:dgActivityDataCampaign)) then ($Act/moles:dgActivity/moles:dgActivityDataCampaign)
153        else (),
154        element dgActivityRole {
155            element dgInvestigator {
156                element dgPrincipalInvestigator {
157                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgMetadataID),
158                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:roleName),
159                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:abbreviation),
160                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:contactDetails),
161                    for $PIRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgRoleHolder
162                        order by $PIRole/moles:startDate empty least, $PIRole/moles:endDate empty least
163                        return
164                        element dgRoleHolder {
165                            if (exists($PIRole/moles:dgOrganisationID)) then
166                                f:return-stub-organisation('organisation', data($PIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PIRole/moles:dgOrganisationID/moles:localIdentifier))
167                             else if (exists($PIRole/moles:dgPersonID)) then
168                                f:return-stub-person('person', string($PIRole/moles:dgPersonID/moles:repositoryIdentifier), string($PIRole/moles:dgPersonID/moles:localIdentifier))
169                             else (),
170                            ($PIRole/moles:startDate),
171                            if (exists($PIRole/moles:endDate)) then
172                                ($PIRole/moles:endDate)
173                            else (),
174                            if (exists($PIRole/moles:localName)) then
175                                ($PIRole/moles:localName)
176                            else ()
177                        } (: </dgRoleHolder> :)
178                }, (: </dgPrincipalInvestigator> :)
179                for $CI in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgCoInvestigator
180                return 
181                    element dgCoInvestigator {
182                        ($CI/moles:dgMetadataID),
183                        ($CI/moles:roleName),
184                        ($CI/moles:abbreviation),
185                        ($CI/moles:contactDetails),
186                        for $CIRole in $CI/moles:dgRoleHolder
187                            order by $CIRole/moles:startDate empty least, $CIRole/moles:endDate empty least
188                        return
189                        element dgRoleHolder {
190                            if (exists(CIRole/moles:dgOrganisationID)) then
191                                f:return-stub-organisation('organisation', data($CIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($CIRole/moles:dgOrganisationID/moles:localIdentifier))
192                             else if (exists($CIRole/moles:dgPersonID)) then
193                                f:return-stub-person('person', string($CIRole/moles:dgPersonID/moles:repositoryIdentifier), string($CIRole/moles:dgPersonID/moles:localIdentifier))
194                             else (),
195                            ($CIRole/moles:startDate),
196                            if (exists($CIRole/moles:endDate)) then
197                                ($CIRole/moles:endDate)
198                            else (),
199                            if (exists($CIRole/moles:localName)) then
200                                ($CIRole/moles:localName)
201                            else ()
202                        } (: </dgRoleHolder> :)
203                } (: </dgCoInvestigator> :)
204        }, (: </dgInvestigator> :)
205        if (exists($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact)) then (
206            element dgTechnicalContact {
207                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgMetadataID),
208                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:roleName),
209                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:abbreviation),
210                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:contactDetails),
211                for $TechConRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgRoleHolder
212                    order by $TechConRole/moles:startDate empty least, $TechConRole/moles:endDate empty least
213                    return
214                    element dgRoleHolder {
215                        if (exists($TechConRole/moles:dgOrganisationID)) then
216                            f:return-stub-organisation('organisation', data($TechConRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($TechConRole/moles:dgOrganisationID/moles:localIdentifier))
217                         else if (exists($TechConRole/moles:dgPersonID)) then
218                            f:return-stub-person('person', string($TechConRole/moles:dgPersonID/moles:repositoryIdentifier), string($TechConRole/moles:dgPersonID/moles:localIdentifier))
219                         else (),
220                        ($TechConRole/moles:startDate),
221                        if (exists($TechConRole/moles:endDate)) then
222                            ($TechConRole/moles:endDate)
223                        else (),
224                        if (exists($TechConRole/moles:localName)) then
225                                ($TechConRole/moles:localName)
226                           else ()
227                    } (: </dgRoleHolder> :)
228            } (: </dgTechnicalContact> :)
229        )
230        else (),
231        if (exists($Act/dgActivity/dgActivityRole/dgProjectManager)) then (
232            element dgProjectManager {
233                ($Act/dgActivity/dgActivityRole/dgProjectManager/dgMetadataID),
234                ($Act/dgActivity/dgActivityRole/dgProjectManager/roleName),
235                ($Act/dgActivity/dgActivityRole/dgProjectManager/abbreviation),
236                ($Act/dgActivity/dgActivityRole/dgProjectManager/contactDetails),
237                for $PMRole in $Act/dgActivity/dgActivityRole/dgProjectManager/dgRoleHolder
238                    order by $PMRole/startDate empty least, $PMRole/endDate empty least
239                    return
240                    element dgRoleHolder {
241                        if (exists($PMRole/moles:dgOrganisationID)) then
242                            f:return-stub-organisation('organisation', data($PMRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PMRole/moles:dgOrganisationID/moles:localIdentifier))
243                         else if (exists($PMRole/moles:dgPersonID)) then
244                            f:return-stub-person('person', string($PMRole/moles:dgPersonID/moles:repositoryIdentifier), string($PMRole/moles:dgPersonID/moles:localIdentifier))
245                         else (),
246                        ($PMRole/startDate),
247                        if (exists($PMRole/moles:endDate)) then
248                            ($PMRole/moles:endDate)
249                        else (),
250                        if (exists($PMRole/moles:localName)) then
251                            ($PMRole/moles:localName)
252                        else ()
253                    } (: </dgRoleHolder> :)
254            } (: </dgProjectManager> :)
255        )
256        else ()
257    }, (: </dgActivityRole> :)
258element DeploymentSummary {
259element dptList {
260for $DepDPTRepository in distinct-values($Act/moles:dgActivity/moles:ActivityDeployment/moles:DataProductionToolID/moles:repositoryIdentifier)
261for $DepDPTLocal in distinct-values($Act/moles:dgActivity/moles:ActivityDeployment/moles:DataProductionToolID/moles:localIdentifier)
262let $DepDPT :=  f:return-stub-dpt('dataproductiontool', data($DepDPTRepository), data($DepDPTLocal))
263return $DepDPT
264},
265element obsStnList {
266for $DepObsStnRepository in distinct-values($Act/moles:dgActivity/moles:ActivityDeployment/moles:ObservationStationID/moles:repositoryIdentifier)
267for $DepObsStnLocal in distinct-values($Act/moles:dgActivity/moles:ActivityDeployment/moles:ObservationStationID/moles:localIdentifier)
268let $DepObsStn :=  f:return-stub-obsstn('observationstation', data($DepObsStnRepository), data($DepObsStnLocal))
269return $DepObsStn
270},
271element dataEntityList {
272for $DE in
273collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgDataEntity/moles:RelatedDeployment
274/moles:DataProductionToolID
275[moles:localIdentifier=$Act/moles:dgMetadataID/moles:localIdentifier
276and moles:repositoryIdentifier=$Act/moles:dgMetadataID/moles:repositoryIdentifier]]
277order by $DE/moles:dgMetadataID
278return f:return-stub-dataentity('dataentity', $DE)
279}
280, element rawDeployments {for $rawDeployments in $Act/moles:dgActivity/moles:ActivityDeployment return $rawDeployments}
281}, (: </DeploymentSummary> :)
282        if (exists($Act/dgActivityCoverage)) then
283            ($Act/dgActivityCoverage)
284            else (),
285        if (exists($Act/dgActivityDuration)) then
286            ($Act/dgActivityDuration)
287            else ()
288    } , (: </dgActivity> :)
289    for $strkwrd in $Act/moles:dgStructuredKeyword return $strkwrd,
290    if (exists($Act/moles:dgMetadataProvenance)) then $Act/moles:dgMetadataProvenance else (),
291    if (exists($Act/moles:dgMetadataSecurity)) then $Act/moles:dgMetadataSecurity else ()
292} (:</dgMRecord>:)
293) (: End namespace strip :)
Note: See TracBrowser for help on using the repository browser.