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

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

Modifications to support new exist database layout for discovery
(don't yet work on glue!)

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' or schema[0:3]=='DIF'):
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            #targetCollection='/db/ndg_B_metadata'
46            targetCollection='/db/discovery/moles'
47            xquery=ObjectTypeQuery
48            xquery=queryReplace(xquery,repository,localID,targetCollection)
49            print xquery
50            id,summary=self.executeQuery(xquery)
51            hits=summary['hits']
52           
53            if hits!=1: raise ValueError,'%s documents returned for uri %s:%s:%s'%(hits,repository,'NDG-B0',localID)
54           
55            # check output formats
56            allowed = ['DIF','DC','ISO19115','NDG-B0','NDG-B1']
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           
68            if format.find('NDG')==-1 and objectType!=4:
69                msg={1:'Activity',2:'Data Production Tool',3:'Observation Station'}[objectType]
70                raise TypeError(
71                    '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                    'ISO19115':ISO19139Query,
77                    'NDG-B0':MOLESQuery,
78                    'NDG-B1':{
79                        4:StubBDEQuery,
80                        3:StubBObsStnQuery,
81                        2:StubBDPTQuery,
82                        1:StubBActQuery}[objectType]
83                     }[format]
84                     
85            xquery=queryReplace(xquery,repository,localID,targetCollection)
86           
87            id,summary=self.executeQuery(xquery)
88            hits=summary['hits']
89             
90            # should only be the one document in the result set
91            if hits!=1: 
92                raise ValueError('Actual Document Query returned [%s] hits - internal error!'%hits)
93             
94            # now let's get it and return it
95            r=self.retrieve(id,0,{})
96
97        else:
98            raise TypeError('Unknown Schema "%s" in URI'%schema)
99        self.sessionRelease(id)
100        return r
101       
102
103       
Note: See TracBrowser for help on using the repository browser.