source: ndgCommon/trunk/ndg/common/src/clients/xmldb/abstractsearchxmldbclient.py @ 5184

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/ndgCommon/trunk/ndg/common/src/clients/xmldb/abstractsearchxmldbclient.py@5184
Revision 5184, 4.0 KB checked in by cbyrom, 11 years ago (diff)

Simplify backups structure in eXist - putting all backups under a
single toplevel structure.

Line 
1'''
2 Abstract implementation of xml search clients interface - adding some universal methods
3 
4 @author: C Byrom - Tessella, Feb 09
5'''
6from ndg.common.src.lib.ndgresources import ndgResources
7from ndg.common.src.clients.interfacesearchclient import InterfaceSearchClient
8from ndg.common.src.models.ndgObject import ndgObject as no
9from xml.etree import cElementTree as ET
10import logging
11
12class AbstractSearchXMLDBClient(InterfaceSearchClient):
13
14    resources = ndgResources()
15    NO_DOCS_FOUND_ERROR = "No document found for the specified inputs"
16   
17    def buildAndRunQuery(self, queryType, targetCollection, 
18                         providerID, localID, useChunked = False):
19        '''
20        Build an Xquery then run it and return result docs
21        @param queryType: type of xquery to run - must be one of ndgResource.xq keys
22        @param targetCollection: target collection in XML DB to run query against
23        @param providerID: ID of the provider of the metadata
24        @param localID: ID of the metadata to lookup
25        @keyword useChunked: if True, use the xmlrpc.query() command to run the query
26        (NB, this is possibly only relevant to eXist DBs).
27        @return array of result docs generated by query; if none found, an empty array is returned
28        '''
29        logging.debug("Building and running xquery")
30        xquery = self.resources.createXQuery(queryType, targetCollection, providerID, localID)
31        results = self.client.runQuery(xquery, useChunked = useChunked)
32        logging.debug("Returning results from xquery")
33        return results
34   
35   
36    def getNDGDoc(self, providerID, docType, localID, targetCollection = "/"):
37        '''
38        Retrieve the document represented by an ndgURI
39        @param providerID: ID of the provider of the metadata
40        @param docType: document type - see ndgObject for valid types
41        @param localID: local ID of the ndg document to retrieve
42        @keyword targetCollection: collection in XML db to look for doc in.  Default = '/'
43        - i.e. the base collection
44        @return document matching the ndgURI
45        '''
46        logging.debug("Retrieving doc with providerID:'%s', docType:'%s', localID:'%s', collection:'%s'" \
47                      %(providerID,docType,localID,targetCollection))
48       
49        if not self.resources.knownQueries.has_key(docType):
50            raise ValueError("Unrecognised document type - '%s'" %docType)
51       
52        xqtype=self.resources.knownQueries[docType]
53
54        if docType == 'NDG-B1':
55            # this is a general moles object - so need to further establish the type of moles doc
56            # it is to get the correct XQUery to use
57            logging.debug("- ambiguous document type requested (%s) - determine what is actually required" %xqtype)
58            results = self.buildAndRunQuery(xqtype, targetCollection, 
59                                            providerID, localID)
60            if len(results) == 0:
61                raise ValueError(self.NO_DOCS_FOUND_ERROR)
62           
63            xml=ET.fromstring(results[0])
64            otype=int(xml.text or 0)
65           
66            xqtype={4:'stubB_dataEntity',
67                    3:'stubB_observationStation',
68                    2:'stubB_DPT',
69                    1:'stubB_activity'}[otype]
70            logging.debug("- need to retrieve a %s document" %xqtype)
71
72        results = self.buildAndRunQuery(xqtype, targetCollection,
73                                        providerID, localID)
74        if len(results) == 0:
75            raise ValueError(self.NO_DOCS_FOUND_ERROR)
76        # NB, if retrieving backups, just get return first doc found - if we
77        # need more granularity here, we can implement the functionality when required
78        elif len(results) > 1 and docType != no.ATOM_BACKUP_DOC_TYPE:
79            raise ValueError("More than one document found for the inputs - this should not have occured")
80
81        logging.debug("- returning document matching the requested ndgURI")
82        return results[0]
Note: See TracBrowser for help on using the repository browser.