source: exist/trunk/xquery/stubB_activity.xq @ 3118

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/exist/trunk/xquery/stubB_activity.xq@3158
Revision 3118, 15.3 KB checked in by lawrence, 12 years ago (diff)

Splitting out the (authoratative?) python xqueries into named
files within the new egg for exist (and other ndg utilities)

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