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

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

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

Line 
1#!/usr/bin/env python
2'''
3 Helper class to set up eXist DB for use with ndgCommon with moles atoms docs
4 - sets up (via the initialise method):
5 i) indexes
6 ii) schemas
7 iii) xqueries
8 iv) xquery libraries
9 v) xml resources to augment atom data when doing atom->DIF transforms
10 vi) required collections
11 vii) required feeds
12 
13 @author: C Byrom, Tessella Jan 2009
14'''
15import logging
16import ndg.common.src.clients.xmldb.eXist.dbconstants as dc
17import ndg.common.src.clients.xmldb.interfacexmldbatomclient
18
19class AtomDBInitialiser(object):
20    '''
21    Wrapper class to the various initialisation methods required to set up eXist
22    for use with atom docs
23    '''
24    def __init__(self, atomClient):
25        '''
26        Set up the initialiser with an eXist client
27        @param edc: instance of AtomClient
28        '''
29        logging.info("Setting up eXist initialiser")
30        if not isinstance(atomClient,
31                          ndg.common.src.clients.xmldb.interfacexmldbatomclient.InterfaceXMLDBAtomClient):
32            raise ValueError("Input object (type:%s) does not inherit from the InterfaceXMLDBAtomClient as required." %type(atomClient))
33                          #ndg.common.src.clients.xmldb.eXist.atomclient.AtomClient):
34       
35        self.ac = atomClient
36        logging.info("eXist initialiser set up")
37       
38    def initialise(self):
39        '''
40        Run the various methods to set the eXist DB up in a state ready for use
41        with ndgCommon
42        '''
43        logging.info("Setting up eXist for use with ndgCommon")
44        # set up any collections required - NB, if these already
45        # exist they won't cause any files to be lost
46        self.__setUpEXistAtomCollections()
47       
48        # set up the indexes on the DB
49        # NB, for some reason, if these indexes are set up, general searching stops
50        # working - so disable for the moment.
51        self.__setUpIndexes()
52       
53        # add the schema required for atom validation
54        self.__addAtomSchema()
55       
56        # add any xquery libs in the right places
57        # NB, this doesn't currently work since it appears you can't upload non-xml
58        # files - so these need to be added manually via the GUI client
59        #self.__addXQueryLibs()
60
61        # add common data missing from atoms - but used to create output DIF records
62        self.__uploadOrgData()
63       
64        if hasattr(self.ac, 'feedClient'):
65            # set up the required atom feeds
66            self.ac.feedClient.setupBasicFeeds()
67
68        logging.info("eXist set up completed")
69       
70           
71    def __setUpEXistAtomCollections(self):
72        '''
73        Set up the required eXist collections needed for the moles atom data
74        '''
75        logging.info("Ensuring required collections are available in eXist")
76        for col in [dc.ATOM_COLLECTION_PATH, dc.BACKUP_COLLECTION_PATH + dc.ATOM_COLLECTION_PATH]:
77            for type in [dc.OLD_COLLECTION_PATH, dc.PUBLISHED_COLLECTION_PATH, \
78                         dc.SMALL_P_PUBLISHED_COLLECTION_PATH, dc.WORKING_COLLECTION_PATH]:
79                self.ac.client.createCollection(col)
80                self.ac.client.createCollection(col + type)
81                self.ac.client.createCollection(col + type + dc.DE_COLLECTION_PATH)
82                self.ac.client.createCollection(col + type + dc.DEPLOYMENT_COLLECTION_PATH)
83                self.ac.client.createCollection(col + type + dc.DEPLOYMENTS_COLLECTION_PATH)
84                self.ac.client.createCollection(col + type + dc.GRANULE_COLLECTION_PATH)
85
86        self.ac.client.createCollection(dc.DIF_COLLECTION_PATH)
87        self.ac.client.createCollection(dc.PROVIDER_FEED_PATH)
88        self.ac.client.createCollection(dc.SCHEMAS_COLLECTION_PATH)
89        logging.info("Required collections available")
90
91   
92    def __setUpIndexes(self):
93        '''
94        Set up the required eXist indexes for the moles atom data
95        '''
96        logging.info("Setting up eXist indexes")
97        for fileName, indexData in self.ac.resources.indexes.items():
98            pathName = fileName.replace('_', '/')
99            pathName = dc.EXIST_CONFIG_PATH + pathName.replace('.xconf', '')
100            self.ac.createDoc(indexData, pathName, fileName)
101        logging.info("eXist indexes set up")
102       
103
104    def __addXQueryLibs(self):
105        '''
106        Add the required xquery libs to the db - to allow the various xqueries
107        to be ran correctly
108        '''
109        logging.info("Adding xquery libs to eXist")
110        for file, schemaData in self.ac.resources.xqlib.items():
111            bits = file.split('/')
112            path = dc.XQUERY_COLLECTION_PATH + '/'.join(bits[0:-1])
113            fileName = bits[-1]
114            self.ac.createDoc(schemaData, path, fileName)
115        logging.info("- xquery libs added")
116       
117
118    def __addAtomSchema(self):
119        '''
120        Add the required atom schema to the atoms collection - to allow validation
121        of input atoms
122        '''
123        logging.info("Adding atom schema to eXist")
124        for fileName, schemaData in self.ac.resources.xsd.items():
125            self.ac.createDoc(schemaData, dc.SCHEMAS_COLLECTION_PATH, 
126                                     fileName + '.xsd')
127        logging.info("- schema added")
128
129
130    def __uploadOrgData(self):
131        '''       
132        Upload the organisations DIF file to eXist - this is required for the atom->DIF output
133        '''
134        logging.info("Uploading organisation data to eXist")
135        for fileName, orgData in self.ac.resources.resources.items():
136            self.ac.createDoc(orgData, dc.RESOURCES_COLLECTION_PATH, fileName)
137        logging.info("Data uploaded")
Note: See TracBrowser for help on using the repository browser.