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

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

More improvements to wsgi discovery, some unit tests, improved
namespace handling in DIF.py ...

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