Ignore:
Timestamp:
24/09/08 15:26:06 (11 years ago)
Author:
cbyrom
Message:

Add support for doing lists and summaries of atom docs via ndgDirectory and the existInterface class + add necessary xquery files for this + add new method to
allow retrieval of the collection to which an atom with a specified ID belongs - including the associated xquery file + extend tests to exercise these new features
+ add custom Atom error + improve preservation of key atom attributes when loading existing atoms into an Atom object + improve determining default atom collection

File:
1 edited

Legend:

Unmodified
Added
Removed
  • exist/trunk/python/ndgUtils/models/existdbclient.py

    r4220 r4229  
    88from ndgUtils.eXistInterface import ndg_eXist 
    99from ndgUtils.eXistConnector import eXistConnector as ec 
     10from ndgUtils.ndgXqueries import ndgXqueries 
     11 
     12try: 
     13    from xml.etree import ElementTree as ET 
     14except ImportError: 
     15    try: 
     16        import ElementTree as ET 
     17    except ImportError: 
     18        import elementtree.ElementTree as ET 
    1019 
    1120class eXistDBClient: 
    1221     
    1322     
    14     def __init__(self, configFile): 
     23    def __init__(self, configFile = None, eXistDBHostname = None): 
    1524        ''' 
    1625        Initialise a connection to the eXistDB 
    1726        ''' 
    1827        logging.info("Initialising connection to eXist DB") 
     28        self.eXistDBHostname = eXistDBHostname 
     29         
     30        inputs = {} 
    1931        # Firstly load in config data 
    20         self._loadDBDetails(configFile) 
    21          
     32        if configFile: 
     33            self._loadDBDetails(configFile) 
     34            inputs['passwordFile'] = configFile 
     35             
     36        if self.eXistDBHostname: 
     37            inputs['db'] = self.eXistDBHostname 
     38             
    2239        # Now set up the connection 
    23         self.xmldb = ndg_eXist(db = self.eXistDBHostname, passwordFile = configFile) 
     40        self.xmldb = ndg_eXist(**inputs) 
    2441         
    2542        # set up any collections required - NB, if these already exist they won't cause any files to be lost 
    26         self._setUpEXistCollections() 
     43        self._setUpEXistAtomCollections() 
    2744        logging.info("eXist DB connection initialised") 
    2845 
     
    4158 
    4259 
    43     def _setUpEXistCollections(self): 
     60    def _setUpEXistAtomCollections(self): 
    4461        ''' 
    4562        Set up the required eXist collections needed for running the granulator script 
     
    87104        ''' 
    88105        logging.info("Retrieving info for file, '%s'" %docPath) 
     106         
    89107        doc = self.xmldb.executeQuery('doc("' + docPath + '")') 
    90108         
     
    184202        @return: True, if file created successfully 
    185203        ''' 
    186         logging.info("Adding file, '%s' to eXist DB collection, '%s'" %(fileName, collection)) 
     204        logging.info("Adding file, '%s' to eXist DB collection, '%s'" \ 
     205                     %(fileName, collection)) 
    187206        logging.debug("data: %s" %xml) 
    188207 
     
    232251        self.createEXistFile(xml, collection, fileName) 
    233252 
     253 
     254    def getAtomFileCollectionPath(self, atomID): 
     255        ''' 
     256        Given an atom id, determine and return the collection path in eXist 
     257        of the associated atom file 
     258        @param atom: atom id to look up 
     259        @return: collection path, if it exists, None, otherwise  
     260        ''' 
     261        logging.info("Looking up collection path for atom ID, '%s'" %atomID) 
     262        xq = ndgXqueries()['atomFullPath'] 
     263        xq = xq.replace('TargetCollection', ec.BASE_COLLECTION_PATH) 
     264        xq = xq.replace('LocalID', atomID) 
     265 
     266        id, doc = self.xmldb.executeQuery(xq) 
     267        if doc['hits'] == 0: 
     268            logging.info("No document found with the specified ID") 
     269            return None 
     270 
     271        doc = self.xmldb.retrieve(id,0,{}) 
     272 
     273        docET = ET.fromstring(doc) 
     274        collPath = docET.text + '/' 
     275        logging.debug("Found collection path, '%s'" %collPath) 
     276        return collPath 
     277         
    234278             
    235279    def createAtomInExist(self, atom): 
     280        ''' 
     281        Create an atom in the eXist DB 
     282        @param atom: atom object to create in the DB 
     283        ''' 
    236284        logging.info("Creating atom in eXist") 
    237         eXistCollection = atom.getEXistCollectionPath() 
    238          
    239         # ensure this collection exists - otherwise an exception will be thrown 
    240         self.createCollections([eXistCollection]) 
    241         self.createOrUpdateEXistFile(atom.toPrettyXML(), \ 
    242                                      eXistCollection, atom.atomName) 
     285        eXistCollection = self.getAtomFileCollectionPath(atom.atomID) 
     286         
     287        # if collection not found, assume we're dealing with a new atom; get its 
     288        # default collection 
     289        if not eXistCollection: 
     290            eXistCollection = atom.getDefaultCollectionPath() 
     291        # create backup of atom if it already exists 
     292        else: 
     293            self.backupEXistFile(eXistCollection, atom.atomName) 
     294             
     295        self.createEXistFile(atom.toPrettyXML(), eXistCollection, atom.atomName) 
     296             
    243297        logging.info("Atom created in eXist") 
Note: See TracChangeset for help on using the changeset viewer.