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

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

Implementation of tickets #22386, #22395

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
13from libs.migration.processor.check.check import checkObservationCollection
14from libs.migration.processor.commons import findDeploymentsInDE,\
15    createMD_Identifier, extractContent, MD_CODE_MOLES2_CITATION
16from libs.migration.processor.deployment import DeploymentProcessor
17from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue
18
19class DataEntityProcessor(object):
20    '''
21        Processes a DataEntityMigration item. Note that each DataEntity is associated to a "dataent_xxxx" file in Moles2
22    '''       
23    def __init__(self, dataEntityMigration, migrationSessions):
24        '''
25            Initializes the class
26            @param _dataEntityMigration: the DataEntityMigration instance
27            @param migrationSessions: a MigrationSessions instance
28        '''         
29        self._dataEntityMigration = dataEntityMigration       
30        self.migrationSessions = migrationSessions
31   
32    def _processCitation(self, ceda_observationCollection):
33        contentDict = extractContent(self._dataEntityMigration)       
34        ceda_observationCollection.identifier.append(createMD_Identifier(title = contentDict['citation'], code = MD_CODE_MOLES2_CITATION))
35   
36    def _processObservationCollection(self):
37        if self._dataEntityMigration is None:
38            raise Exception
39                           
40        #Does the migrationObject exists?
41        ceda_observationCollection = EPB.searchOrCreate(CEDA_ObservationCollection, self.migrationSessions.molesSession, self._dataEntityMigration.ceda_observation_coll_id)
42
43        if ceda_observationCollection.id:
44            return ceda_observationCollection
45
46        self._processCitation(ceda_observationCollection)
47        ceda_observationCollection.publicationState = MO_PublicationStateValue.cl_working
48        #if not adds the new CEDA_ObservationCollection         
49        Moles3EPB.addCedaObject(ceda_observationCollection, self.migrationSessions.molesSession)
50        self.migrationSessions.molesSession.commit()
51       
52        #associates the CEDA_ObservationCollection to the migrationObject
53        self._dataEntityMigration.ceda_observation_coll_id = ceda_observationCollection.id
54        self.migrationSessions.migrationSession.commit()
55        return ceda_observationCollection
56
57    def _processDeploymentMigration(self, deploymentMigration, cedaObservationCollection):                                     
58        deProcessor = DeploymentProcessor(self._dataEntityMigration, deploymentMigration, self.migrationSessions)
59        try:
60            ceda_observation = deProcessor.process()
61                       
62            checkObservationCollection(cedaObservationCollection, ceda_observation)
63            self.migrationSessions.molesSession.commit()
64                       
65            deploymentMigration.ceda_observation_coll_id = cedaObservationCollection.id
66            self.migrationSessions.migrationSession.commit()         
67        except NoDataLineage as ex:
68            self.migrationSessions.molesSession.rollback()
69            self.migrationSessions.migrationSession.rollback()                   
70            raise MigrationObjectException(ex)                     
71       
72        return ceda_observation
73   
74    def process(self):
75        exs = []
76        #creates/retrieves CEDA_ObservationCollection   
77        cedaObservationCollection = self._processObservationCollection()
78       
79        #retrieves the associated deployment links from the dataentity
80        deploymentsLinks = findDeploymentsInDE(self._dataEntityMigration)
81       
82        #retreives the DataEntityMigration sorted by creation date
83        deploymentMigrations = MigrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate(self._dataEntityMigration, deploymentsLinks, self.migrationSessions.migrationSession)
84        howManydm = deploymentMigrations.count()
85        for deploymentMigration in deploymentMigrations:
86            try:
87                ceda_observation = self._processDeploymentMigration(deploymentMigration, cedaObservationCollection)
88                if howManydm > 1:                   
89                    for identifier in cedaObservationCollection.identifier:
90                        if identifier.code == MD_CODE_MOLES2_CITATION:
91                            # TBD --- Here should compare if exists already
92                            ceda_observation.identifier.append(identifier)
93                            self.migrationSessions.molesSession.commit()
94            except Exception as ex:
95                exs.append(ex)
96        return exs
Note: See TracBrowser for help on using the repository browser.