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

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

Updated XQueries for BADC 'worst cases so far'

  • 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),
34element dgActivity {
35if (exists($SRAct/moles:dgActivity/moles:dgActivityDataCollection)) then
36($SRAct/*/moles:dgActivityDataCollection)
37else if (exists($SRAct/moles:dgActivity/moles:dgActivityDataProject)) then
38($SRAct/*/moles:dgActivityDataProject)
39else if (exists($SRAct/moles:dgActivity/moles:dgActivityDataCampaign)) then
40($SRAct/moles:dgActivity/moles:dgActivityDataCampaign)
41else
42($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),
43($SRAct/moles:dgActivity/moles:dgActivityCoverage),
44($SRAct/moles:dgActivity/moles:dgActivityDuration)
45} (: </dgActivity> :)
46} (: </activity> :)
47} ;
48
49declare function f:return-stub-dataentity($out-element-name as xs:string, $DE as element()) as element()
50(: Returns a 'stubB' style observation station record to expand an data entity record :)
51(: Security not implemented yet :)
52{
53let $SRDE := $DE
54return 
55element {$out-element-name} {
56($SRDE/moles:dgMetadataID),
57($SRDE/moles:dgMetadataDescription),
58($SRDE/moles:name),
59($SRDE/moles:abbreviation),
60($SRDE/moles:abbreviation),
61element dgDataEntity {
62($SRDE/moles:dgDataEntity/moles:dgDataSetType),
63($SRDE/moles:dgDataEntity/moles:dgDataGranule),
64($SRDE/moles:dgDataEntity/moles:dgDataSummary)
65} (: </dgDataEntity> :)       
66}
67} ;
68
69declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
70(: Returns a 'stubB' style dpt record to expand a dpt record :)
71(: Security not implemented yet :)
72{
73let $SRDPT := collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
74moles:dgMetadataID/moles:localIdentifier=$locid
75and
76moles:dgMetadataID/moles:repositoryIdentifier=$repid
77]
78return 
79element {$out-element-name} {
80$SRDPT/moles:dgMetadataID,
81$SRDPT/moles:dgMetadataDescription,
82$SRDPT/moles:name,
83$SRDPT/moles:abbreviation,
84$SRDPT/moles:logos,
85element dgDataProductionTool {
86$SRDPT/moles:dgDataProductionTool/moles:contactDetails,
87if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then
88$SRDPT/moles:dgDataProductionTool/moles:dgModel
89else (),
90if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then
91$SRDPT/moles:dgDataProductionTool/moles:dgInstrument
92else ()
93} (: </dgDataProductionTool> :)
94} (: </dataproductiontool> :)
95} ;
96
97declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
98(: Returns a 'stubB' style observation station record to expand an observation station record :)
99(: Security not implemented here yet;  :)
100{
101let $SRObsStn := collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
102moles:dgMetadataID/moles:localIdentifier=$locid
103and
104moles:dgMetadataID/moles:repositoryIdentifier=$repid
105]
106return
107element {$out-element-name} {
108($SRObsStn/moles:dgMetadataID),
109($SRObsStn/moles:dgMetadataDescription),
110($SRObsStn/moles:name),
111($SRObsStn/moles:abbreviation),
112($SRObsStn/moles:logos),
113element dgObservationStation {
114($SRObsStn/moles:dgObservationStation/moles:contactDetails),
115if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then
116element dgStationaryPlatform {
117$SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,
118if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgLandStation)) then element dgLandStation {''}
119else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgMooring)) then element dgMooring {''}
120else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:dgStationGroup)) then element dgStationGroup {''}
121else ()
122}
123else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then
124$SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform
125else ()
126} (: </dgObservationStation> :)
127}
128} ;
129
130declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
131(: Returns a 'stubB' style observation station record to expand a person record :)
132(: Security not implemented yet :)
133{
134let $person := collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
135moles:dgMetadataID/moles:localIdentifier=$locid
136and
137moles:dgMetadataID/moles:repositoryIdentifier=$repid
138]
139return
140element  {$out-element-name}
141{
142($person/moles:name),
143($person/moles:contactDetails),
144($person/moles:dgMetadataID)
145}
146} ;
147
148declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
149(: Returns a 'stubB' style observation station record to expand an organisation record :)
150(: Security not implemented yet :)
151{
152let $org := collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
153moles:dgMetadataID/moles:localIdentifier=$locid
154and
155moles:dgMetadataID/moles:repositoryIdentifier=$repid
156]
157return
158element  {$out-element-name} {
159$org/moles:dgMetadataID,
160$org/moles:name,
161$org/moles:abbreviation,
162$org/moles:contactDetails,
163$org/moles:logos
164}
165} ;
166
167for $Act in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
168    moles:dgActivity!=''
169    and moles:dgMetadataID[
170    moles:schemeIdentifier='NDG-B0'
171    and moles:repositoryIdentifier=$repositoryIdentifier
172    and moles:localIdentifier=$localIdentifier]]
173    return
174utillib:strip-namespace(element dgMetadataRecord {
175    element dgMetadataID {
176        element schemeIdentifier {'NDG-B1'},
177        (element repositoryIdentifier {data($Act/moles:dgMetadataID/moles:repositoryIdentifier)}),
178        element localIdentifier {data($Act/moles:dgMetadataID/moles:localIdentifier)}
179    },
180    $Act/moles:dgMetadataDescription,
181    $Act/moles:name,
182    $Act/moles:abbreviation,
183    element dgActivity {
184        for $RelatedActivities in $Act/dgActivity/moles:relatedActivity
185            return
186                element relatedActivity {
187                    $RelatedActivities/moles:activityRelation,
188                    f:return-stub-activity('activity', data($RelatedActivities/moles:relatedActivityID/moles:repositoryIdentifier), data($RelatedActivities/moles:relatedActivityID/moles:localIdentifier))
189            },    (: </relatedActivity> :)
190        if (exists($Act/moles:dgActivity/moles:dgActivityDataCollection)) then ($Act/moles:dgActivity/moles:dgActivityDataCollection)
191        else if (exists($Act/moles:dgActivity/moles:dgActivityDataProject)) then ($Act/moles:dgActivity/moles:dgActivityDataProject)
192        else if (exists($Act/moles:dgActivity/moles:dgActivityDataInvestigation)) then ($Act/moles:dgActivity/moles:dgActivityDataInvestigation)
193        else if (exists($Act/moles:dgActivity/moles:dgActivityDataCampaign)) then ($Act/moles:dgActivity/moles:dgActivityDataCampaign)
194        else (),
195        element dgActivityRole {
196            element dgInvestigator {
197                element dgPrincipalInvestigator {
198                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgMetadataID),
199                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:roleName),
200                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:abbreviation),
201                    ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:contactDetails),
202                    for $PIRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgPrincipalInvestigator/moles:dgRoleHolder
203                        order by $PIRole/moles:startDate empty least, $PIRole/moles:endDate empty least
204                        return
205                        element dgRoleHolder {
206                            if (exists($PIRole/moles:dgOrganisationID)) then
207                                f:return-stub-organisation('organisation', data($PIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PIRole/moles:dgOrganisationID/moles:localIdentifier))
208                             else if (exists($PIRole/moles:dgPersonID)) then
209                                f:return-stub-person('person', string($PIRole/moles:dgPersonID/moles:repositoryIdentifier), string($PIRole/moles:dgPersonID/moles:localIdentifier))
210                             else (),
211                            ($PIRole/moles:startDate),
212                            if (exists($PIRole/moles:endDate)) then
213                                ($PIRole/moles:endDate)
214                            else (),
215                            if (exists($PIRole/moles:localName)) then
216                                ($PIRole/moles:localName)
217                            else ()
218                        } (: </dgRoleHolder> :)
219                }, (: </dgPrincipalInvestigator> :)
220                for $CI in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgInvestigator/moles:dgCoInvestigator
221                return 
222                    element dgCoInvestigator {
223                        ($CI/moles:dgMetadataID),
224                        ($CI/moles:roleName),
225                        ($CI/moles:abbreviation),
226                        ($CI/moles:contactDetails),
227                        for $CIRole in $CI/moles:dgRoleHolder
228                            order by $CIRole/moles:startDate empty least, $CIRole/moles:endDate empty least
229                        return
230                        element dgRoleHolder {
231                            if (exists(CIRole/moles:dgOrganisationID)) then
232                                f:return-stub-organisation('organisation', data($CIRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($CIRole/moles:dgOrganisationID/moles:localIdentifier))
233                             else if (exists($CIRole/moles:dgPersonID)) then
234                                f:return-stub-person('person', string($CIRole/moles:dgPersonID/moles:repositoryIdentifier), string($CIRole/moles:dgPersonID/moles:localIdentifier))
235                             else (),
236                            ($CIRole/moles:startDate),
237                            if (exists($CIRole/moles:endDate)) then
238                                ($CIRole/moles:endDate)
239                            else (),
240                            if (exists($CIRole/moles:localName)) then
241                                ($CIRole/moles:localName)
242                            else ()
243                        } (: </dgRoleHolder> :)
244                } (: </dgCoInvestigator> :)
245        }, (: </dgInvestigator> :)
246        if (exists($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact)) then (
247            element dgTechnicalContact {
248                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgMetadataID),
249                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:roleName),
250                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:abbreviation),
251                ($Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:contactDetails),
252                for $TechConRole in $Act/moles:dgActivity/moles:dgActivityRole/moles:dgTechnicalContact/moles:dgRoleHolder
253                    order by $TechConRole/moles:startDate empty least, $TechConRole/moles:endDate empty least
254                    return
255                    element dgRoleHolder {
256                        if (exists($TechConRole/moles:dgOrganisationID)) then
257                            f:return-stub-organisation('organisation', data($TechConRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($TechConRole/moles:dgOrganisationID/moles:localIdentifier))
258                         else if (exists($TechConRole/moles:dgPersonID)) then
259                            f:return-stub-person('person', string($TechConRole/moles:dgPersonID/moles:repositoryIdentifier), string($TechConRole/moles:dgPersonID/moles:localIdentifier))
260                         else (),
261                        ($TechConRole/moles:startDate),
262                        if (exists($TechConRole/moles:endDate)) then
263                            ($TechConRole/moles:endDate)
264                        else (),
265                        if (exists($TechConRole/moles:localName)) then
266                                ($TechConRole/moles:localName)
267                           else ()
268                    } (: </dgRoleHolder> :)
269            } (: </dgTechnicalContact> :)
270        )
271        else (),
272        if (exists($Act/dgActivity/dgActivityRole/dgProjectManager)) then (
273            element dgProjectManager {
274                ($Act/dgActivity/dgActivityRole/dgProjectManager/dgMetadataID),
275                ($Act/dgActivity/dgActivityRole/dgProjectManager/roleName),
276                ($Act/dgActivity/dgActivityRole/dgProjectManager/abbreviation),
277                ($Act/dgActivity/dgActivityRole/dgProjectManager/contactDetails),
278                for $PMRole in $Act/dgActivity/dgActivityRole/dgProjectManager/dgRoleHolder
279                    order by $PMRole/startDate empty least, $PMRole/endDate empty least
280                    return
281                    element dgRoleHolder {
282                        if (exists($PMRole/moles:dgOrganisationID)) then
283                            f:return-stub-organisation('organisation', data($PMRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($PMRole/moles:dgOrganisationID/moles:localIdentifier))
284                         else if (exists($PMRole/moles:dgPersonID)) then
285                            f:return-stub-person('person', string($PMRole/moles:dgPersonID/moles:repositoryIdentifier), string($PMRole/moles:dgPersonID/moles:localIdentifier))
286                         else (),
287                        ($PMRole/startDate),
288                        if (exists($PMRole/moles:endDate)) then
289                            ($PMRole/moles:endDate)
290                        else (),
291                        if (exists($PMRole/moles:localName)) then
292                            ($PMRole/moles:localName)
293                        else ()
294                    } (: </dgRoleHolder> :)
295            } (: </dgProjectManager> :)
296        )
297        else ()
298    }, (: </dgActivityRole> :)
299    for $RelDep in $Act/moles:dgActivity/moles:ActivityDeployment
300        return
301        element ActivityDeployment {
302            ($RelDep/moles:DateStart),
303            if (exists($RelDep/moles:DateEnd)) then
304                ($RelDep/moles:DateEnd)
305            else (),
306            ($RelDep/moles:dgMetadataID),
307            f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)),
308            f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)),
309                for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:repositoryIdentifier=$RelDep/moles:dgMetadataID/moles:repositoryIdentifier
310and moles:dgDataEntity/moles:RelatedDeployment/moles:dgMetadataID/moles:localIdentifier=$RelDep/moles:dgMetadataID/moles:localIdentifier]
311return
312f:return-stub-dataentity('dataentity', $DE)
313        }, (: </ActivityDeployment> :)
314        if (exists($Act/dgActivityCoverage)) then
315            ($Act/dgActivityCoverage)
316            else (),
317        if (exists($Act/dgActivityDuration)) then
318            ($Act/dgActivityDuration)
319            else ()
320    } , (: </dgActivity> :)
321    for $strkwrd in $Act/moles:dgStructuredKeyword return $strkwrd,
322    if (exists($Act/moles:dgMetadataProvenance)) then $Act/moles:dgMetadataProvenance else (),
323    if (exists($Act/moles:dgMetadataSecurity)) then $Act/moles:dgMetadataSecurity else ()
324} (:</dgMRecord>:)
325) (: End namespace strip :)
Note: See TracBrowser for help on using the repository browser.