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

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

Sundry modifications associated with the deployment on glue, and better
options for viewing and downloading the underlying xml records (not yet
complete)

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    return xquery
14   
15class DocumentRetrieve (ndg_eXist):
16    ''' This class provides a document retrieval service via the NDG exist interfaces '''
17
18    def __init__(self,repository):
19
20        ndg_eXist.__init__(self,db=repository)
21        self.repository=repository
22
23    def get(self,repository,schema,localID,format='NDG-B0',targetCollection='/db/discovery/moles'):
24
25        # we are making the assumption for now that everything is stored as
26        # MOLES documents and that we can retrieve from MOLES anything we like
27        # in another format.
28        if schema == 'CSML':
29           
30            # temporary expedient ...  should really use Kev's code ...
31            raise TypeError('DocumentRetrieve code does not yet know how to find specific CSML Documents')
32       
33        elif (schema[0:5] == 'NDG-B' or schema[0:3]=='DIF'):
34           
35            # find out what type of object actually exists of this sort
36            # 0 - None, 1 - Activity, 2 - DPT, 3 - ObsStn, 4 - DE
37            # if the schema is a DIF, we expect to find a DE from the DIF ingestion to MOLES ...
38           
39            # the following xquery can be used to generate a listing of the database
40            # contents ...
41            # xquery=listingQuery
42
43            #targetCollection='/db/ndg_B_metadata'
44            xquery=ObjectTypeQuery
45            xquery=queryReplace(xquery,repository,localID,targetCollection)
46         
47            id,summary=self.executeQuery(xquery)
48            hits=summary['hits']
49            #print 'bnl',targetCollection,xquery
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.