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

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

Interim check in browse update ... (browse still not working properly)

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# Note that ndgRetreive essentially provides test cases for this code.
8
9debug=1
10
11def queryReplace(xquery,repository,localID,targetCollection):
12    ''' Used to modify the vanilla xqueries to a query which gets the actual requested documents '''
13    xquery=xquery.replace('RepositoryID',repository,1)
14    xquery=xquery.replace('LocalID',localID,1)
15    xquery=xquery.replace('TargetCollection', targetCollection)
16    return xquery
17   
18class DocumentRetrieve (ndg_eXist):
19    ''' This class provides a document retrieval service via the NDG exist interfaces '''
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',targetCollection='/db/discovery/moles'):
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' or schema[0:3]=='DIF' or schema[0:4] == 'MDIP'):
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            # if the schema is a DIF, we expect to find a DE from the DIF ingestion to MOLES ...
40           
41            # the following xquery can be used to generate a listing of the database
42            # contents ...
43            # xquery=listingQuery
44
45            # default code has targetCollection='/db/ndg_B_metadata, this gets us the right one ...
46            xquery=ObjectTypeQuery
47            xquery=queryReplace(xquery,repository,localID,targetCollection)
48         
49            id,summary=self.executeQuery(xquery)
50            hits=summary['hits']
51            print 'bnl',targetCollection,xquery
52           
53            if hits!=1: raise ValueError,'%s documents returned for uri %s:%s:%s (in %s)'%(hits,repository,'NDG-B0',localID,targetCollection)
54           
55            # check output formats
56            allowed = ['DIF','DC','ISO19139','NDG-B0','NDG-B1','MDIP']
57            if format not in allowed: raise TypeError,'Invalid document output format [%s]'%format
58           
59            # get output type
60            r=self.retrieve(id,0,{})
61           
62            # typical response looks like this:
63            #        <objectType xmlns="http://ndg.nerc.ac.uk/moles">1</objectType>
64            xml=minidom.parseString(r)
65            objectType=int(xml.getElementsByTagNameNS(
66                            "http://ndg.nerc.ac.uk/moles", "objectType").item(0).firstChild.data)
67            if debug: print 'objectType=%s'%objectType
68           
69            if format.find('NDG')==-1 and objectType!=4:
70                msg={1:'Activity',2:'Data Production Tool',3:'Observation Station'}[objectType]
71                raise TypeError,'Document URI type [%s] not valid for output format [%s]'%(msg,format)
72           
73            #select the correct xquery
74            xquery={'DIF':DIFQuery,
75                    'DC':DublinCoreDEQuery,
76                    'ISO19139':ISO19139Query,
77                    'NDG-B0':MOLESQuery,
78                    'MDIP':MDIPQuery,
79                    'NDG-B1':{
80                        4:StubBDEQuery,
81                        3:StubBObsStnQuery,
82                        2:StubBDPTQuery,
83                        1:StubBActQuery}[objectType]
84                     }[format]
85                     
86            xquery=queryReplace(xquery,repository,localID,targetCollection)
87            try:
88                id,summary=self.executeQuery(xquery)
89            except Exception,e:
90                print xquery
91                raise Exception,e
92            hits=summary['hits']
93            # should only be the one document in the result set
94            if hits!=1: 
95                if debug:
96                    f=open('xquery.%s.fails.xq'%format,'w')
97                    f.write(xquery)
98                    f.close()
99                raise ValueError,'Actual Document Query returned [%s] hits - internal error!'%hits
100             
101            # now let's get it and return it
102            r=self.retrieve(id,0,{})
103
104        else:
105            raise TypeError('Unknown Schema "%s" in URI'%schema)
106        self.sessionRelease(id)
107        return r
Note: See TracBrowser for help on using the repository browser.