source: TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/DocumentRetrieve.py @ 2672

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/ows_framework/trunk/ows_server/ows_server/models/DocumentRetrieve.py@2760
Revision 2672, 5.1 KB checked in by lawrence, 13 years ago (diff)

Interim commit of discovery/browse in ows stack.

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=0
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,pwfile='passwords.txt'):
22
23        ndg_eXist.__init__(self,db=repository,passwordFile=pwfile)
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 in ['NDG-A0','NumSim']:
32            xquery={'NDG-A0':csmlQuery,'NumSim':numsimQuery}[schema]
33            if schema=='NumSim': xquery=xquery.replace('RepositoryID',repository)
34            xquery=xquery.replace('TargetCollection',targetCollection)
35            xquery=xquery.replace('LocalID',localID)
36            id,summary=self.executeQuery(xquery)
37            if summary['hits']==1:
38                r=self.retrieve(id,0,{})
39                self.sessionRelease(id)
40            else:
41                raise ValueError('Unable to obtain %s document [%s] (hits=%s)'%(schema,localID,summary['hits']))
42            return r
43       
44        elif (schema[0:5] == 'NDG-B' or schema[0:3]=='DIF' or schema[0:4] == 'MDIP'):
45           
46            # find out what type of object actually exists of this sort
47            # 0 - None, 1 - Activity, 2 - DPT, 3 - ObsStn, 4 - DE
48            # if the schema is a DIF, we expect to find a DE from the DIF ingestion to MOLES ...
49           
50            # the following xquery can be used to generate a listing of the database
51            # contents ...
52            # xquery=listingQuery
53
54            # default code has targetCollection='/db/ndg_B_metadata, this gets us the right one ...
55            xquery=ObjectTypeQuery
56            xquery=queryReplace(xquery,repository,localID,targetCollection)
57         
58            id,summary=self.executeQuery(xquery)
59            hits=summary['hits']
60           
61            if hits!=1: raise ValueError,'%s documents returned for uri %s:%s:%s (in %s)'%(hits,repository,'NDG-B0',localID,targetCollection)
62           
63            # check output formats
64            allowed = ['DIF','DC','ISO19139','NDG-B0','NDG-B1','MDIP']
65            if format not in allowed: raise TypeError,'Invalid document output format [%s]'%format
66           
67            # get output type
68            r=self.retrieve(id,0,{})
69           
70            # typical response looks like this:
71            #        <objectType xmlns="http://ndg.nerc.ac.uk/moles">1</objectType>
72            xml=minidom.parseString(r)
73            objectType=int(xml.getElementsByTagNameNS(
74                            "http://ndg.nerc.ac.uk/moles", "objectType").item(0).firstChild.data)
75            if debug: print 'objectType=%s'%objectType
76           
77            if format.find('NDG')==-1 and objectType!=4:
78                msg={1:'Activity',2:'Data Production Tool',3:'Observation Station'}[objectType]
79                raise TypeError,'Document URI type [%s] not valid for output format [%s]'%(msg,format)
80           
81            #select the correct xquery
82            xquery={'DIF':DIFQuery,
83                    'DC':DublinCoreDEQuery,
84                    'ISO19139':ISO19139Query,
85                    'NDG-B0':MOLESQuery,
86                    'MDIP':MDIPQuery,
87                    'NDG-B1':{
88                        4:StubBDEQuery,#strip_de_xquery,#
89                        3:StubBObsStnQuery,
90                        2:StubBDPTQuery,
91                        1:StubBActQuery}[objectType]
92                     }[format]
93                     
94            xquery=queryReplace(xquery,repository,localID,targetCollection)
95            try:
96                id,summary=self.executeQuery(xquery)
97            except Exception,e:
98                print xquery
99                raise Exception,e
100            hits=summary['hits']
101            # should only be the one document in the result set
102            if hits!=1: 
103                if debug:
104                    f=open('xquery.%s.fails.xq'%format,'w')
105                    f.write(xquery)
106                    f.close()
107                raise ValueError,'Actual Document Query returned [%s] hits - internal error!'%hits
108             
109            # now let's get it and return it
110            r=self.retrieve(id,0,{})
111
112        else:
113            raise TypeError('Unknown Schema "%s" in URI'%schema)
114        self.sessionRelease(id)
115        return r
Note: See TracBrowser for help on using the repository browser.