source: mauRepo/MolesManager/trunk/src/libs/migration/processor/dataEntity.py @ 8147

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

Implementation of tickets #22390

Line 
1'''
2Created on 15 Nov 2011
3
4@author: mnagni
5'''
6from MolesManager.moles3epb import Moles3EPB
7from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection import \
8    CEDA_ObservationCollection
9from libs.epb import EPB
10from libs.migration.MigrationEPB import MigrationEPB
11from libs.migration.exception.exceptions import NoDataLineage,\
12    MigrationObjectException, NoAssociatedAuthor
13from libs.migration.processor.check.check import checkObservationCollection
14from libs.migration.processor.commons import findDeploymentsInDE,\
15    createMD_Identifier, extractContent, MD_CODE_MOLES2_CITATION,\
16    hasAtomDocumentSameHash
17from libs.migration.processor.deployment import DeploymentProcessor
18from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue
19
20class DataEntityProcessor(object):
21   
22    '''
23        Processes a DataEntityMigration item. Note that each DataEntity is associated to a "dataent_xxxx" file in Moles2
24    '''       
25    def __init__(self, dataEntityMigration, migrationSessions):
26        '''
27            Initializes the class
28            @param _dataEntityMigration: the DataEntityMigration instance
29            @param migrationSessions: a MigrationSessions instance
30        '''
31        if dataEntityMigration is None:
32            raise MigrationObjectException("DataEntityProcessor cannot process an None item")
33        self._dataEntityMigration = dataEntityMigration       
34        self.migrationSessions = migrationSessions
35   
36    def _processCitation(self, ceda_observationCollection):
37        # TDB - Check that if is an update or not!
38        contentDict = extractContent(self._dataEntityMigration)       
39        ceda_observationCollection.identifier.append(createMD_Identifier(title = contentDict['citation'], code = MD_CODE_MOLES2_CITATION))
40   
41    def _getObservationCollection(self):
42        return EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id, self.migrationSessions.molesSession)
43   
44    def _processObservationCollection(self):                                   
45        #Moles3 object exists...
46        if self._dataEntityMigration.ceda_observation_coll_id: 
47            # ...and the data entity document has not changed
48            if hasAtomDocumentSameHash(self._dataEntityMigration):
49                return self._getObservationCollection()
50            else:
51                self.updateObservationCollection()
52
53        #... does not exist so create it
54        return self.createNewObservationCollection()
55
56    def updateObservationCollection(self):
57        """
58            Updated the existing CEDA_ObservationCollection instance binded to the self._dataEntityMigration object.
59            @return: the persisted and updated CEDA_ObservationCollection element
60        """       
61        ceda_observationCollection = self._getObservationCollection()
62        # TBD
63        #self._processCitation(ceda_observationCollection)
64        return ceda_observationCollection
65
66    def createNewObservationCollection(self): 
67        """
68            Creates a new CEDA_ObservationCollection instance in the Moles3DB using the self._dataEntityMigration object.
69            If successful adds the new instance ID to the related DataEntityMigration object
70            @return: the persisted CEDA_ObservationCollection element
71        """
72        ceda_observationCollection = CEDA_ObservationCollection()
73        self._processCitation(ceda_observationCollection)
74        ceda_observationCollection.publicationState = MO_PublicationStateValue.cl_working
75                 
76        Moles3EPB.addCedaObject(ceda_observationCollection, self.migrationSessions.molesSession, True)
77        MigrationEPB.associateObservationCollectionToDataEntity(self._dataEntityMigration, ceda_observationCollection.id, True)
78        return ceda_observationCollection
79
80    def _processDeploymentMigration(self, deploymentMigration, cedaObservationCollection):                                     
81        deProcessor = DeploymentProcessor(self._dataEntityMigration, deploymentMigration, self.migrationSessions)
82        try:
83            ceda_observation = deProcessor.process()
84            Moles3EPB.addObservationToObservationCollection(cedaObservationCollection, ceda_observation, self.migrationSessions.molesSession, True)           
85            MigrationEPB.associateObservationCollectionToDataEntity(deploymentMigration, cedaObservationCollection.id, self.migrationSessions.migrationSession, True)
86        except NoAssociatedAuthor as ex:
87            raise ex                 
88        except Exception as ex:
89            self.migrationSessions.molesSession.rollback()
90            self.migrationSessions.migrationSession.rollback()               
91            raise MigrationObjectException(ex)                     
92       
93        return ceda_observation
94   
95    def process(self):
96        exs = []
97        #creates/retrieves CEDA_ObservationCollection   
98        cedaObservationCollection = self._processObservationCollection()
99       
100        #retrieves the associated deployment links from the dataentity
101        deploymentsLinks = findDeploymentsInDE(self._dataEntityMigration)
102       
103        #retreives the DataEntityMigration sorted by creation date
104        deploymentMigrations = MigrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate(self._dataEntityMigration, deploymentsLinks, self.migrationSessions.migrationSession)
105        howManydm = deploymentMigrations.count()
106        for deploymentMigration in deploymentMigrations:
107            try:
108                ceda_observation = self._processDeploymentMigration(deploymentMigration, cedaObservationCollection)
109                if howManydm > 1:                   
110                    for identifier in cedaObservationCollection.identifier:
111                        if identifier.code == MD_CODE_MOLES2_CITATION:
112                            # TBD --- Here should compare if exists already
113                            ceda_observation.identifier.append(identifier)
114                            self.migrationSessions.molesSession.commit()
115            except Exception as ex:
116                exs.append(ex)
117        return exs
Note: See TracBrowser for help on using the repository browser.