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

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

Great improvement on how SA alchemy session is managed

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.processor.EPBRepo import EPBRepo
14
15class LoadResources(object):
16
17    def process(self):
18        """
19            Loads all the folders below the atoms ('published', 'working', 'Published') folders
20        """ 
21        ex = []       
22        for status in docStatus:       
23            ex.extend(self._loadCollections(status, DT_DATA_ENTITIES, DataEntityMigration))
24            ex.extend(self._loadCollections(status, DT_DEPLOYMENTS, DeploymentsMigration))
25            ex.extend(self._loadCollections(status, DT_DEPLOYMENT_DATA, DeploymentDataMigration))                                               
26        return ex
27
28    def _createMigrationObject(self, migrationClass, doc_id, docName, docOwner, docStatus, docHash, docCreation):
29        migrationObject = None
30        if migrationClass == DeploymentsMigration:
31            migrationObject = DeploymentsMigration()
32            migrationObject.depl_id = doc_id
33        elif  migrationClass == DataEntityMigration:
34            migrationObject = DataEntityMigration()
35            migrationObject.data_ent_id = doc_id
36        elif migrationClass == DeploymentDataMigration:
37            migrationObject = DeploymentDataMigration()
38            migrationObject.deployment_data_id = doc_id
39         
40        if migrationObject is None:
41            raise Exception("migrationObject is None")
42        migrationObject.doc_name = docName
43        migrationObject.doc_owner = docOwner
44        migrationObject.doc_status = docStatus
45        migrationObject.doc_hash = docHash                     
46        migrationObject.doc_creation = stringToTimestamp(docCreation)
47        try:
48            EPBRepo.migrationEPB.persistInstance(migrationObject)
49        except Exception as e:
50            print e
51
52    def _loadCollections(self, docStatus, docType, migrationClass):
53        ex = []
54        refs = getCollectionRefs(buildExistTypePath(docStatus, docType))           
55        for ref in refs:
56            docOwner = ref.get('name') 
57            if docOwner.startswith('.'):
58                continue                   
59            ex.extend(self._loadMigrationDocs(docStatus, docOwner, docType, migrationClass))
60
61        return ex
62
63    def _loadMigrationDocs(self, docStatus, docOwner, docType, migrationClass):
64        ex = []
65        refs = getOwnerRefs(docStatus, docType, docOwner)                   
66        for ref in refs:
67            docName = ref.get('name')
68            try:
69                self._processMigrationDoc(docName, docStatus, docOwner, docType, migrationClass)
70            except Exception as e:
71                if hasattr(e, 'value'):
72                    e.value = buildExistDocPath(docStatus, docType, docOwner, docName)
73                ex.append(e)
74        return ex
75
76    def _processMigrationDoc(self, docName, docStatus, docOwner, docType, migrationClass):
77        """
78            Ingests documents like DataEntity, Deployment, Deployment_Data. The document is register in a DB table of the appropriate
79            type (DataEntityMigration, DeploymentsMigration, DeploymentDataMigration).
80            @param docName: the document name
81            @param docStatus: a common.docStatus item
82            @param docOwner: a common.docOwner item   
83        """
84         
85        docHash = getAtomDocumentHash(docStatus, docType, docOwner, docName)
86        xmlDocument = getAtomDocumentAsElementtree(docStatus, docType, docOwner, docName)
87        doc_id = findID(xmlDocument)
88         
89        #The document has been already classified
90        if doc_id is None or EPBRepo.migrationEPB.search(migrationClass, doc_id) is not None:
91            return
92       
93                         
94        #The document shall be classified                               
95        docCreation = self._extractCreationDate(xmlDocument)       
96        self._createMigrationObject(migrationClass, doc_id, docName, docOwner, docStatus, docHash, docCreation)
97
98
99    def _extractID(self, xmlDocument):
100        doc_id = findID(xmlDocument) 
101        if doc_id is None:
102            raise NoDocumentID(doc_id)
103        return doc_id
104
105    def _extractCreationDate(self, xmlDocument):
106        creationDate = findMolesCreationDate(xmlDocument)           
107        if creationDate is None:
108            raise NoCreationDate(creationDate)
109        return creationDate
110           
111
112       
Note: See TracBrowser for help on using the repository browser.