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

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

Complete - # 22390: AMEND MOLES2 -> CEDA Moles Author and Co-author migration (plus PI assignment) to CEDA Observation record
 http://team.ceda.ac.uk/trac/ceda/ticket/22390
Incomplete - # 22392: citation migration from MOLES2
 http://team.ceda.ac.uk/trac/ceda/ticket/22392

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 MigrationObjectException, NoAssociatedAuthor,\
12    _migrationObjectDescription
13from libs.migration.processor.commons import findDeploymentsInDE,\
14    createMD_Identifier, extractContent, MD_CODE_MOLES2_CITATION,\
15    hasAtomDocumentSameHash, createCI_Citation, createCI_Date, findPublishedDate,\
16    isoDateTimeStringToTimeDate, findUpdatedDate, createDate, createDateTime
17from libs.migration.processor.deployment import DeploymentProcessor
18from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue
19from logging import StreamHandler
20import logging
21from libs.migration.processor.check.check import moles3Append
22from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_datetypecode import CI_DateTypeCode
23
24class DataEntityProcessor(object):
25    log = logging.getLogger('DataEntityProcessor')
26    log.addHandler(StreamHandler())
27    log.setLevel(logging.DEBUG) 
28    '''
29        Processes a DataEntityMigration item. Note that each DataEntity is associated to a "dataent_xxxx" file in Moles2
30    '''       
31    def __init__(self, dataEntityMigration, migrationSessions):
32        '''
33            Initializes the class
34            @param _dataEntityMigration: the DataEntityMigration instance
35            @param migrationSessions: a MigrationSessions instance
36        '''
37        if dataEntityMigration is None:
38            raise MigrationObjectException("DataEntityProcessor cannot process an None item")
39        self._dataEntityMigration = dataEntityMigration       
40        self.migrationSessions = migrationSessions
41   
42    def _processCitation(self, ceda_observationCollection):
43        # TDB - Check that if is an update or not!
44        contentDict = extractContent(self._dataEntityMigration)
45        if not contentDict.has_key('citation'):
46            DataEntityProcessor.log.info("The migration object "+ _migrationObjectDescription(self._dataEntityMigration) \
47                                         + " has not associated cedacat:citation")
48        else:
49            ci_dates = []
50            doc_date = findPublishedDate(self._dataEntityMigration)           
51            if doc_date:
52                i_date = createDate(isoDateTimeStringToTimeDate(doc_date))           
53                ci_dates.append(createCI_Date(CI_DateTypeCode.cl_publication, date = i_date))
54
55            doc_date = findUpdatedDate(self._dataEntityMigration)           
56            if doc_date:
57                i_date = createDate(isoDateTimeStringToTimeDate(doc_date))               
58                ci_dates.append(createCI_Date(CI_DateTypeCode.cl_revision, date = i_date))
59               
60            i_citation = createCI_Citation(title = 'ceda_moles2_citation', date=ci_dates)
61            newIdentifier = createMD_Identifier(code = contentDict['citation'], authority=i_citation)
62            moles3Append(ceda_observationCollection, 'identifier', newIdentifier)
63   
64    def _getObservationCollection(self):
65        return EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id, self.migrationSessions.molesSession)
66   
67    def _processObservationCollection(self):                                   
68        #Moles3 object exists...
69        if self._dataEntityMigration.ceda_observation_coll_id: 
70            # ...and the data entity document has not changed
71            if hasAtomDocumentSameHash(self._dataEntityMigration):
72                return self._getObservationCollection()
73            else:
74                self.updateObservationCollection()
75
76        #... does not exist so create it
77        return self.createNewObservationCollection()
78
79    def updateObservationCollection(self):
80        """
81            Updated the existing CEDA_ObservationCollection instance binded to the self._dataEntityMigration object.
82            @return: the persisted and updated CEDA_ObservationCollection element
83        """       
84        ceda_observationCollection = self._getObservationCollection()
85        # TBD
86        #self._processCitation(ceda_observationCollection)
87        return ceda_observationCollection
88
89    def createNewObservationCollection(self): 
90        """
91            Creates a new CEDA_ObservationCollection instance in the Moles3DB using the self._dataEntityMigration object.
92            If successful adds the new instance ID to the related DataEntityMigration object
93            @return: the persisted CEDA_ObservationCollection element
94        """
95        ceda_observationCollection = CEDA_ObservationCollection()
96        self._processCitation(ceda_observationCollection)
97        #self._processResult(ceda_observationCollection)
98        ceda_observationCollection.publicationState = MO_PublicationStateValue.cl_working
99                 
100        Moles3EPB.addCedaObject(ceda_observationCollection, self.migrationSessions.molesSession, True)
101        MigrationEPB.associateObservationCollectionToDataEntity(self._dataEntityMigration, ceda_observationCollection.id, True)
102        return ceda_observationCollection
103
104    def _processDeploymentMigration(self, deploymentMigration, cedaObservationCollection):                                     
105        deProcessor = DeploymentProcessor(self._dataEntityMigration, deploymentMigration, self.migrationSessions)
106        try:
107            ceda_observation = deProcessor.process()
108            Moles3EPB.addObservationToObservationCollection(cedaObservationCollection, ceda_observation, self.migrationSessions.molesSession, True)           
109            MigrationEPB.associateObservationCollectionToDataEntity(deploymentMigration, \
110                         cedaObservationCollection.id, self.migrationSessions.migrationSession, True)
111        except NoAssociatedAuthor as ex:
112            raise ex                 
113        except Exception as ex:
114            self.migrationSessions.molesSession.rollback()
115            self.migrationSessions.migrationSession.rollback()               
116            raise MigrationObjectException(ex)                     
117       
118        return ceda_observation
119   
120    def process(self):
121        exs = []
122        #creates/retrieves CEDA_ObservationCollection   
123        cedaObservationCollection = self._processObservationCollection()
124       
125        #retrieves the associated deployment links from the dataentity
126        deploymentsLinks = findDeploymentsInDE(self._dataEntityMigration)
127       
128        #retreives the DataEntityMigration sorted by creation date
129        deploymentMigrations = MigrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate( \
130                                                self._dataEntityMigration, deploymentsLinks, self.migrationSessions.migrationSession)
131        howManydm = deploymentMigrations.count()
132        for deploymentMigration in deploymentMigrations:
133            try:
134                ceda_observation = self._processDeploymentMigration(deploymentMigration, cedaObservationCollection)
135                if howManydm == 1:
136                    collection_identifier = Moles3EPB.extractCollectionIdentifierByTitle(MD_CODE_MOLES2_CITATION, self.migrationSessions.molesSession)
137                    if collection_identifier.count()==1:
138                        ceda_observation.identifier.append(collection_identifier.firt())
139                        self.migrationSessions.molesSession.commit()               
140            except Exception as ex:
141                exs.append(ex)
142            except RuntimeError as er:
143                print er               
144        return exs
Note: See TracBrowser for help on using the repository browser.