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

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

oops, still had some test code in it ...

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