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

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

Better error handling, better parameter showing. Shell example
javascript as an initial step to using the Vocabserver.

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'%(hits,repository,'NDG-B0',localID)
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                raise Exception,e
91            hits=summary['hits']
92            # should only be the one document in the result set
93            if hits!=1: 
94                if debug:
95                    f=open('xquery.%s.fails.xq'%format,'w')
96                    f.write(xquery)
97                    f.close()
98                raise ValueError,'Actual Document Query returned [%s] hits - internal error!'%hits
99             
100            # now let's get it and return it
101            r=self.retrieve(id,0,{})
102
103        else:
104            raise TypeError('Unknown Schema "%s" in URI'%schema)
105        self.sessionRelease(id)
106        return r
107           
108    def getOriginal(self,docName):
109        ''' Get a specific document from the originals collection collection '''
110
111        #xquery='''let $doc := collection('%s')//root()[util:document-name(.) = '%s' ]
112        #          for $i in $doc return <document>{document-uri($i)}</document>'''%(collection,docName)
113        #
114        #id,summary=self.executeQuery(xquery)
115        #hits=summary['hits']
116        #if hits!=1: raise ValueError,'%s original documents returned for uri %s'%(hits,docName)
117        #r=self.retrieve(id,0,{})
118        #self.sessionRelease(id)
119        #return r
120       
121        from ndgSearch import ndgSearch
122        ndgWS=ndgSearch()
123        result=ndgWS.getDoc(docName+'.xml')
124        return result
125       
Note: See TracBrowser for help on using the repository browser.