source: TI07-MOLES/trunk/PythonCode/wsgi/DocumentRetrieve.py @ 1991

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI07-MOLES/trunk/PythonCode/wsgi/DocumentRetrieve.py@1991
Revision 1991, 4.2 KB checked in by lawrence, 13 years ago (diff)

Minor mods associated with wsgi discovery (fixed xquery for d.c.)

Line 
1from eXistInterface import ndg_eXist
2from ndg_xqueries import *
3from xml.dom import minidom
4
5# The MOLES document retrieval is a python port of :
6# TI07-MOLES/trunk/JavaCode/returnmolesxmldb/ndg/services/returnmoles/Main.java
7
8def queryReplace(xquery,repository,localID,targetCollection):
9    ''' Used to modify the vanilla xqueries to a query which gets the actual requested documents '''
10    xquery=xquery.replace('RepositoryID',repository,1)
11    xquery=xquery.replace('LocalID',localID,1)
12    xquery=xquery.replace('/db/ndg_B_metadata', targetCollection)
13   
14    return xquery
15   
16class DocumentRetrieve (ndg_eXist):
17    ''' This class provides a document retrieval service via the NDG exist interfaces '''
18
19    def __init__(self,repository):
20
21        ndg_eXist.__init__(self,db=repository)
22        self.repository=repository
23
24    def get(self,repository,schema,localID,format='NDG-B0',targetCollection='/db/discovery/moles'):
25
26        # we are making the assumption for now that everything is stored as
27        # MOLES documents and that we can retrieve from MOLES anything we like
28        # in another format.
29        if schema == 'CSML':
30           
31            # temporary expedient ...  should really use Kev's code ...
32            raise TypeError('DocumentRetrieve code does not yet know how to find specific CSML Documents')
33       
34        elif (schema[0:5] == 'NDG-B' or schema[0:3]=='DIF'):
35           
36            # find out what type of object actually exists of this sort
37            # 0 - None, 1 - Activity, 2 - DPT, 3 - ObsStn, 4 - DE
38            # if the schema is a DIF, we expect to find a DE from the DIF ingestion to MOLES ...
39           
40            # the following xquery can be used to generate a listing of the database
41            # contents ...
42            # xquery=listingQuery
43
44            #targetCollection='/db/ndg_B_metadata'
45            xquery=ObjectTypeQuery
46            xquery=queryReplace(xquery,repository,localID,targetCollection)
47         
48            id,summary=self.executeQuery(xquery)
49            hits=summary['hits']
50           
51            if hits!=1: raise ValueError,'%s documents returned for uri %s:%s:%s'%(hits,repository,'NDG-B0',localID)
52           
53            # check output formats
54            allowed = ['DIF','DC','ISO19115','NDG-B0','NDG-B1']
55            if format not in allowed: raise TypeError,'Invalid document output format [%s]'%format
56           
57            # get output type
58            r=self.retrieve(id,0,{})
59           
60            # typical response looks like this:
61            #        <objectType xmlns="http://ndg.nerc.ac.uk/moles">1</objectType>
62            xml=minidom.parseString(r)
63            objectType=int(xml.getElementsByTagNameNS(
64                            "http://ndg.nerc.ac.uk/moles", "objectType").item(0).firstChild.data)
65           
66            if format.find('NDG')==-1 and objectType!=4:
67                msg={1:'Activity',2:'Data Production Tool',3:'Observation Station'}[objectType]
68                raise TypeError,'Document URI type [%s] not valid for output format [%s]'%(msg,format)
69           
70            #select the correct xquery
71            xquery={'DIF':DIFQuery,
72                    'DC':DublinCoreDEQuery,
73                    'ISO19115':ISO19139Query,
74                    'NDG-B0':MOLESQuery,
75                    'NDG-B1':{
76                        4:StubBDEQuery,
77                        3:StubBObsStnQuery,
78                        2:StubBDPTQuery,
79                        1:StubBActQuery}[objectType]
80                     }[format]
81                     
82            xquery=queryReplace(xquery,repository,localID,targetCollection)
83            try:
84                id,summary=self.executeQuery(xquery)
85            except Exception,e:
86                raise Exception,e
87            hits=summary['hits']
88            # should only be the one document in the result set
89            if hits!=1: 
90                raise ValueError,'Actual Document Query returned [%s] hits - internal error!'%hits
91             
92            # now let's get it and return it
93            r=self.retrieve(id,0,{})
94
95        else:
96            raise TypeError('Unknown Schema "%s" in URI'%schema)
97        self.sessionRelease(id)
98        return r
99       
100
101       
Note: See TracBrowser for help on using the repository browser.