source: TI07-MOLES/trunk/StubB/XQuery/NDG-DEStubB.xquery @ 2236

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

Refactoring - working versions. Still slow though.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
Line 
1import module namespace voclib='http://ndg.nerc.ac.uk/xquery/lib/vocab' at 'xmldb:exist:///db/xqueryLib/Vocabs/vocab_xquery_lib.xquery';
2import module namespace utillib='http://ndg.nerc.ac.uk/xquery/lib/utilities' at 'xmldb:exist:///db/xqueryLib/Utilities/utility_xquery_lib.xquery';
3(:
4Will return when scope problem sussed... ;(
5import module namespace stubblib='http://ndg.nerc.ac.uk/xquery/lib/stubb' at 'xmldb:exist:///db/xqueryLib/StubB/stubb_xquery_lib.xquery';
6:)
7declare namespace f='http://ndg.nerc.ac.uk/moles/localfunctions';
8declare default element namespace 'http://ndg.nerc.ac.uk/moles/dataentity';
9declare namespace moles='http://ndg.nerc.ac.uk/moles';
10
11declare variable $targetCollection as xs:string {'TargetCollection'};
12declare variable $repositoryIdentifier as xs:string {'RepositoryID'};
13declare variable $localIdentifier as xs:string {'LocalID'};
14
15declare function f:return-stub-activity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
16(: Returns a 'stubB' style activity record to expand an activity record :)
17(: Security not implemented here yet :)
18{
19let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
20            moles:dgMetadataID/moles:repositoryIdentifier=$repid
21            and moles:dgMetadataID/moles:localIdentifier=$locid])
22return 
23    if ($found = 0) then
24        element {$out-element-name} {
25            element error {'not found'},
26            element repositoryIdentifier {$repid},
27            element localIdentifier {$locid}
28        }
29    else (
30        for $SRAct in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
31            moles:dgMetadataID/moles:repositoryIdentifier=$repid
32            and moles:dgMetadataID/moles:localIdentifier=$locid]
33        return
34            element  {$out-element-name} {
35                ($SRAct/moles:dgMetadataID),
36                ($SRAct/moles:dgMetadataDescription),
37                ($SRAct/moles:name),
38                ($SRAct/moles:abbreviation),
39                element dgActivity {
40                    if (exists($SRAct/*/moles:dgActivityDataCollection)) then
41                        ($SRAct/*/moles:dgActivityDataCollection)
42                    else if (exists($SRAct/*/moles:dgActivityDataProject)) then
43                        ($SRAct/*/moles:dgActivityDataProject)
44                    else if (exists($SRAct/*/dgActivityDataCampaign)) then
45                        ($SRAct/moles:dgActivity/moles:dgActivityDataCampaign)
46                    else
47                        ($SRAct/moles:dgActivity/moles:dgActivityDataInvestigation),
48                    ($SRAct/moles:dgActivity/moles:dgActivityCoverage),
49                    ($SRAct/moles:dgActivity/moles:dgActivityDuration)
50                } (: </dgActivity> :)
51            } (: </activity> :)
52    )
53} ;
54
55declare function f:return-stub-dataentity($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
56(: Returns a 'stubB' style observation station record to expand an data entity record :)
57(: Security not implemented yet :)
58{
59let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
60        moles:dgMetadataID/moles:repositoryIdentifier=$repid
61        and moles:dgMetadataID/moles:localIdentifier=$locid])
62return 
63    if ($found = 0) then
64        element {$out-element-name} {
65            element error {'not found'},
66            element repositoryIdentifier {$repid},
67            element localIdentifier {$locid}
68        }
69    else (
70        for $SRDE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
71            moles:dgMetadataID/moles:repositoryIdentifier=$repid
72            and moles:dgMetadataID/moles:localIdentifier=$locid]
73        return 
74            element {$out-element-name} {
75                ($SRDE/moles:dgMetadataID),
76                ($SRDE/moles:dgMetadataDescription),
77                ($SRDE/moles:name),
78                ($SRDE/moles:abbreviation),
79                element dgDataEntity {
80                    ($SRDE/moles:dgDataEntity/moles:dgDataSetType),
81                    ($SRDE/moles:dgDataEntity/moles:dgDataGranule),
82                    ($SRDE/moles:dgDataEntity/moles:dgDataSummary)
83                } (: </dgDataEntity> :)       
84            }
85    )
86} ;
87
88declare function f:return-stub-dpt($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
89(: Returns a 'stubB' style dpt record to expand a dpt record :)
90(: Security not implemented yet :)
91{
92let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
93        moles:dgMetadataID/moles:repositoryIdentifier=$repid
94        and moles:dgMetadataID/moles:localIdentifier=$locid])
95return 
96    if ($found = 0) then
97        element {$out-element-name} {
98            element error {'not found'},
99            element repositoryIdentifier {$repid},
100            element localIdentifier {$locid}
101        }
102    else (
103        for $SRDPT in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
104                moles:dgMetadataID/moles:repositoryIdentifier=$repid
105                and moles:dgMetadataID/moles:localIdentifier=$locid]
106        return 
107            element {$out-element-name} {
108                $SRDPT/moles:dgMetadataID,
109                $SRDPT/moles:dgMetadataDescription,
110                $SRDPT/moles:name,
111                $SRDPT/moles:abbreviation,
112                $SRDPT/moles:logos,
113                element dgDataProductionTool {
114                    $SRDPT/moles:dgDataProductionTool/moles:contactDetails,
115                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgModel)) then
116                        $SRDPT/moles:dgDataProductionTool/moles:dgModel
117                    else (),
118                    if (exists($SRDPT/moles:dgDataProductionTool/moles:dgInstrument)) then
119                        $SRDPT/moles:dgDataProductionTool/moles:dgInstrument
120                    else ()
121                    } (: </dgDataProductionTool> :)
122                } (: </dataproductiontool> :)
123    )
124} ;
125
126declare function f:return-stub-obsstn($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
127(: Returns a 'stubB' style observation station record to expand an observation station record :)
128(: Security not implemented here yet;  :)
129{
130let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
131            moles:dgMetadataID/moles:repositoryIdentifier=$repid
132            and moles:dgMetadataID/moles:localIdentifier=$locid])
133return 
134    if ($found = 0) then
135        element {$out-element-name} {
136            element error {'not found'},
137            element repositoryIdentifier {$repid},
138            element localIdentifier {$locid}
139        }
140    else (
141        for $SRObsStn in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
142            moles:dgMetadataID/moles:repositoryIdentifier=$repid
143            and moles:dgMetadataID/moles:localIdentifier=$locid]
144        return
145            element {$out-element-name} {
146                ($SRObsStn/moles:dgMetadataID),
147                ($SRObsStn/moles:dgMetadataDescription),
148                ($SRObsStn/moles:name),
149                ($SRObsStn/moles:abbreviation),
150                element dgObservationStation {
151                    ($SRObsStn/moles:dgObservationStation/moles:contactDetails),
152                    if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform)) then
153                        element dgStationaryPlatform {
154                            $SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/moles:position,
155                            if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgLandStation)) then element dgLandStation {''}
156                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgMooring)) then element dgMooring {''}
157                            else if (exists($SRObsStn/moles:dgObservationStation/moles:dgStationaryPlatform/dgStationGroup)) then element dgStationGroup {''}
158                            else ()
159                            }
160                    else if (exists($SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform)) then
161                        $SRObsStn/moles:dgObservationStation/moles:dgMovingPlatform
162                    else ()
163                } (: </dgObservationStation> :)
164            }
165    )
166} ;
167
168declare function f:return-stub-person($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
169(: Returns a 'stubB' style observation station record to expand a person record :)
170(: Security not implemented yet :)
171{
172let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
173            moles:dgMetadataID/moles:repositoryIdentifier=$repid
174            and moles:dgMetadataID/moles:localIdentifier=$locid])
175return 
176    if ($found = 0) then
177        element {$out-element-name} {
178            element error {'not found'},
179            element repositoryIdentifier {$repid},
180            element localIdentifier {$locid}
181        }
182    else (
183    for $person in collection($targetCollection)/moles:dgMetadata/moles:dgPerson[
184        moles:dgMetadataID/moles:repositoryIdentifier=$repid
185        and moles:dgMetadataID/moles:localIdentifier=$locid]
186    return
187        element  {$out-element-name}
188        {
189            ($person/moles:name),
190            ($person/moles:contactDetails),
191            ($person/moles:dgMetadataID)
192        }
193    )
194} ;
195
196declare function f:return-stub-organisation($out-element-name as xs:string, $repid as xs:string, $locid as xs:string) as element()
197(: Returns a 'stubB' style observation station record to expand an organisation record :)
198(: Security not implemented yet :)
199{
200    let $found := count(collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
201                moles:dgMetadataID/moles:repositoryIdentifier=$repid
202                and moles:dgMetadataID/moles:localIdentifier=$locid])
203    return 
204        if ($found = 0) then
205            element {$out-element-name} {
206                element error {'not found'},
207                element repositoryIdentifier {$repid},
208                element localIdentifier {$locid}
209            }
210        else (
211            for $org in collection($targetCollection)/moles:dgMetadata/moles:dgOrganisation[
212                moles:dgMetadataID/moles:repositoryIdentifier=$repid
213                and moles:dgMetadataID/moles:localIdentifier=$locid]
214            return
215                element  {$out-element-name} {
216                    $org/moles:dgMetadataID,
217                    $org/moles:name,
218                    $org/moles:abbreviation,
219                    $org/moles:contactDetails,
220                    $org/moles:logos
221                }
222        )
223} ;
224
225for $DE in collection($targetCollection)/moles:dgMetadata/moles:dgMetadataRecord[
226exists(moles:dgDataEntity)
227and moles:dgMetadataID[
228moles:schemeIdentifier='NDG-B0'
229and moles:repositoryIdentifier='badc.nerc.ac.uk'
230and moles:localIdentifier='dataent1']]
231return
232utillib:strip-namespace(element dgMetadataRecord {
233    element dgMetadataID {
234        element schemeIdentifier {'NDG-B1'},
235        $DE/moles:dgMetadataID/moles:repositoryIdentifier,
236        $DE/moles:dgMetadataID/moles:localIdentifier
237    }, (: </dgMetadataID> :)
238    $DE/moles:dgMetadataDescription,
239    $DE/moles:name,
240    $DE/moles:abbreviation,
241    element dgDataEntity {
242        $DE/moles:dgDataEntity/moles:dgDataSetType,
243        $DE/moles:dgDataEntity/moles:dgDataObjectType,
244        $DE/moles:dgDataEntity/moles:dgDataGranule,
245        $DE/moles:dgDataEntity/moles:dgDataSummary,
246        element dgDataRoles {
247            if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgMetadataID)) then
248                element dgDataCreator {
249                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgMetadataID,
250                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:roleName,
251                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:abbreviation,
252                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:contactDetails,
253                for $DataCreatorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCreator/moles:dgRoleHolder
254                    order by $DataCreatorRole/moles:startDate empty least, $DataCreatorRole/moles:endDate empty least
255                    return
256                        element dgRoleHolder {
257                            if (exists($DataCreatorRole/moles:dgOrganisationID)) then
258                                f:return-stub-organisation('organisation', data($DataCreatorRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($DataCreatorRole/moles:dgOrganisationID/moles:localIdentifier))
259                             else if (exists($DataCreatorRole/moles:dgPersonID)) then
260                                f:return-stub-person('person', string($DataCreatorRole/moles:dgPersonID/moles:repositoryIdentifier), string($DataCreatorRole/moles:dgPersonID/moles:localIdentifier))
261                             else (),
262                            $DataCreatorRole/moles:startDate,
263                            $DataCreatorRole/moles:endDate,
264                            $DataCreatorRole/moles:localName
265                        } (: </dgRoleHolder> :)
266                    } (: </dgDataCreator>) :)
267            else (),
268            if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgMetadataID)) then
269                element dgDataCurator {
270                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgMetadataID,
271                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:roleName,
272                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:abbreviation,
273                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:contactDetails,
274                for $DataCuratorRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataCurator/moles:dgRoleHolder
275                    order by $DataCuratorRole/moles:startDate empty least, $DataCuratorRole/moles:endDate empty least
276                    return
277                        element dgRoleHolder {
278                            if (exists($DataCuratorRole/moles:dgOrganisationID)) then
279                                f:return-stub-organisation('organisation', data($DataCuratorRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($DataCuratorRole/moles:dgOrganisationID/moles:localIdentifier))
280                             else if (exists($DataCuratorRole/moles:dgPersonID)) then
281                                f:return-stub-person('person', string($DataCuratorRole/moles:dgPersonID/moles:repositoryIdentifier), string($DataCuratorRole/moles:dgPersonID/moles:localIdentifier))
282                             else (),
283                            $DataCuratorRole/moles:startDate,
284                            $DataCuratorRole/moles:endDate,
285                            $DataCuratorRole/moles:localName
286                        } (: </dgRoleHolder> :)
287            } (: </dgDataCurator> :)
288            else (),
289            if (exists($DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:dgMetadataID)) then
290                element dgDataOtherRoles {
291                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:dgMetadataID,
292                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:roleName,
293                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:abbreviation,
294                $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:contactDetails,
295                for $DataOtherRolesRole in $DE/moles:dgDataEntity/moles:dgDataRoles/moles:dgDataOtherRoles/moles:dgRoleHolder
296                    order by $DataOtherRolesRole/moles:startDate empty least, $DataOtherRolesRole/moles:endDate empty least
297                    return
298                        element dgRoleHolder {
299                            if (exists($DataOtherRolesRole/moles:dgOrganisationID)) then
300                                f:return-stub-organisation('organisation', data($DataOtherRolesRole/moles:dgOrganisationID/moles:repositoryIdentifier), data($DataOtherRolesRole/moles:dgOrganisationID/moles:localIdentifier))
301                             else if (exists($DataOtherRolesRole/moles:dgPersonID)) then
302                                f:return-stub-person('person', string($DataOtherRolesRole/moles:dgPersonID/moles:repositoryIdentifier), string($DataOtherRolesRole/moles:dgPersonID/moles:localIdentifier))
303                             else (),
304                            $DataOtherRolesRole/moles:startDate,
305                            $DataOtherRolesRole/moles:endDate,
306                            $DataOtherRolesRole/moles:localName
307                        } (: </dgRoleHolder> :)
308            } (: </dgDataOtherRoles> :)
309            else ()
310        } , (: </dgDataRoles> :)
311        for $RelDep in $DE/moles:dgDataEntity/moles:RelatedDeployment
312            return
313            element RelatedDeployment {
314            $RelDep/moles:DateStart,
315            $RelDep/moles:DateEnd,
316            $RelDep/moles:dgMetadataID,
317            f:return-stub-activity('activity', data($RelDep/moles:ActivityID/moles:repositoryIdentifier), data($RelDep/moles:ActivityID/moles:localIdentifier)),
318            f:return-stub-dpt('dataproductiontool', data($RelDep/moles:DataProductionToolID/moles:repositoryIdentifier), data($RelDep/moles:DataProductionToolID/moles:localIdentifier)),
319            f:return-stub-obsstn('observationstation', data($RelDep/moles:ObservationStationID/moles:repositoryIdentifier), data($RelDep/moles:ObservationStationID/moles:localIdentifier)),
320            $RelDep/moles:Coverage
321        } (: </RelatedDeployment> :)
322        ,
323        for $RelatedDataEntity in $DE/moles:dgDataEntity/moles:dgRelatedDataEntity
324            return
325                element relateddataentity {
326                element relation {
327                    $RelatedDataEntity/moles:RelationID/moles:dgValidTerm,
328                    $RelatedDataEntity/moles:RelationID/moles:dgValidTermID
329                }, (: </relation> :)
330                f:return-stub-dataentity('dataentity', data($RelatedDataEntity/(moles:RelatedDataEntityID | moles:RelatedDataGranuleID)/moles:repositoryIdentifier), data($RelatedDataEntity/(moles:RelatedDataEntityID | moles:RelatedDataGranuleID)/moles:localIdentifier))
331                } (: </relateddataentity> :)
332    } (: </dgDataEntity> :),
333    for $strkwrd in $DE/moles:dgStructuredKeyword return $strkwrd,
334    if (exists($DE/moles:dgMetadataProvenance)) then $DE/moles:dgMetadataProvenance else (),
335    if (exists($DE/moles:dgMetadataSecurity)) then $DE/moles:dgMetadataSecurity else ()
336} (: </dgMetadataRecord> :)
337) (: end of namespace strip :)
Note: See TracBrowser for help on using the repository browser.