source: mauRepo/MolesManager/trunk/src/libs/migration/processor/loadResources.py @ 8180

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/libs/migration/processor/loadResources.py@8209
Revision 8180, 6.0 KB checked in by mnagni, 8 years ago (diff)

Global commit. In future Mylyn should manage a better per-ticket commit policy

Line 
1'''
2Created on 15 Nov 2011
3
4@author: mnagni
5'''
6from libs.migration.processor.commons import docStatus, getCollectionRefs,\
7    findMolesCreationDate, findID, stringToTimestamp, buildExistTypePath,\
8    buildExistDocPath, DT_DATA_ENTITIES, DT_DEPLOYMENTS, DT_DEPLOYMENT_DATA,\
9    getOwnerRefs, getAtomDocumentAsElementtree, getAtomDocumentHash
10from libs.migration.exception.exceptions import NoCreationDate, NoDocumentID
11from libs.migration.db.classes import DeploymentDataMigration,\
12    DeploymentsMigration, DataEntityMigration
13from libs.migration.MigrationEPB import MigrationEPB
14
15class LoadResources(object):
16    """
17     Loads informations from the eXist database to populate reference table for the migrations
18    """
19    def __init__(self):
20        self._migrationSession = MigrationEPB.getNewMigrationSession()
21
22    def process(self):
23        """
24            Loads all the folders below the atoms ('published', 'working', 'Published') folders
25        """ 
26        ex = []       
27        for status in docStatus:       
28            ex.extend(self._loadCollections(status, DT_DATA_ENTITIES, DataEntityMigration))
29            ex.extend(self._loadCollections(status, DT_DEPLOYMENTS, DeploymentsMigration))
30            ex.extend(self._loadCollections(status, DT_DEPLOYMENT_DATA, DeploymentDataMigration))                                               
31           
32        self._migrationSession.close()
33        return ex
34
35    def updateMigrationDoc(self, migrationClass, doc_id, docHash, docCreation):
36        """
37            Updates an existing migration document
38            @param migrationClass: the migration class associated with the document
39            @param doc_id: the document id
40            @param docHash: the document hashCode calculated by libs.migration.processor.commons.calculateHash
41            @param docCreation:the creation date
42            @return: True if the document exists, False otherwise             
43        """
44        coll = MigrationEPB.search(migrationClass, doc_id, self._migrationSession)
45        if coll != None:
46            #... and has the same hash nothing has changed
47            if coll.doc_hash == docHash:               
48                return True
49            else:
50                coll.doc_creation = stringToTimestamp(docCreation)
51                coll.doc_hash = docHash
52                self._migrationSession.commit()
53                return True
54        return False
55
56
57    def createMigrationDoc(self, migrationClass, doc_id, docName, docOwner, docStatus, docHash, docCreation):
58        coll = None
59        if migrationClass == DeploymentsMigration:
60            coll = DeploymentsMigration()
61            coll.depl_id = doc_id
62        elif  migrationClass == DataEntityMigration:
63            coll = DataEntityMigration()
64            coll.data_ent_id = doc_id
65        elif migrationClass == DeploymentDataMigration:
66            coll = DeploymentDataMigration()
67            coll.deployment_data_id = doc_id
68           
69        self._processNotExistingDoc(coll, docName, docOwner, docStatus, docHash, docCreation)
70
71    def _loadCollections(self, docStatus, docType, migrationClass):
72        ex = []
73        refs = getCollectionRefs(buildExistTypePath(docStatus, docType))           
74        for ref in refs:
75            docOwner = ref.get('name') 
76            if docOwner.startswith('.'):
77                continue                   
78            ex.extend(self._loadMigrationDocs(docStatus, docOwner, docType, migrationClass))
79
80        return ex
81
82    def _loadMigrationDocs(self, docStatus, docOwner, docType, migrationClass):
83        ex = []
84        refs = getOwnerRefs(docStatus, docType, docOwner)                   
85        for ref in refs:
86            docName = ref.get('name')
87            try:
88                self._processMigrationDoc(docName, docStatus, docOwner, docType, migrationClass)
89            except Exception as e:
90                if hasattr(e, 'value'):
91                    e.value = buildExistDocPath(docStatus, docType, docOwner, docName)
92                ex.append(e)
93        return ex
94
95    def _processMigrationDoc(self, docName, docStatus, docOwner, docType, migrationClass):
96        """
97            Ingests documents like DataEntity, Deployment, Deployment_Data. The document is register in a DB table of the appropriate
98            type (DataEntityMigration, DeploymentsMigration, DeploymentDataMigration).
99            @param docName: the document name
100            @param docStatus: a common.docStatus item
101            @param docOwner: a common.docOwner item   
102        """
103        docHash = getAtomDocumentHash(docStatus, docType, docOwner, docName)
104        xmlDocument = getAtomDocumentAsElementtree(docStatus, docType, docOwner, docName)
105                                   
106        docCreation = self._extractCreationDate(xmlDocument)
107        doc_id = findID(xmlDocument) 
108       
109        if self.updateMigrationDoc(migrationClass, doc_id, docHash, docCreation):
110            return
111
112        self.createMigrationDoc(migrationClass, doc_id, docName, docOwner, docStatus, docHash, docCreation)
113           
114    def _processNotExistingDoc(self, migrationObject, docName, docOwner, docStatus, docHash, docCreation):
115        if migrationObject is None:
116            raise Exception("migrationObject is None")
117        migrationObject.doc_name = docName
118        migrationObject.doc_owner = docOwner
119        migrationObject.doc_status = docStatus
120        migrationObject.doc_hash = docHash                     
121        migrationObject.doc_creation = stringToTimestamp(docCreation)
122        MigrationEPB.addMigration(migrationObject, self._migrationSession)
123        self._migrationSession.commit()   
124
125
126    def _extractID(self, xmlDocument):
127        doc_id = findID(xmlDocument) 
128        if doc_id is None:
129            raise NoDocumentID(doc_id)
130        return doc_id
131
132    def _extractCreationDate(self, xmlDocument):
133        creationDate = findMolesCreationDate(xmlDocument)           
134        if creationDate is None:
135            raise NoCreationDate(creationDate)
136        return creationDate
137           
138
139       
Note: See TracBrowser for help on using the repository browser.