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

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

remove the SQL echo

RevLine 
[8014]1'''
2Created on 15 Nov 2011
3
4@author: mnagni
5'''
[8089]6from MolesManager.moles3epb import Moles3EPB
7from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection import \
8    CEDA_ObservationCollection
9from libs.migration.MigrationEPB import MigrationEPB
[8158]10from libs.migration.exception.exceptions import MigrationObjectException, NoAssociatedAuthor,\
[8258]11    migrationObjectDescription, NoAssociatedDeployments
[8144]12from libs.migration.processor.commons import findDeploymentsInDE,\
[8323]13    createMD_Identifier, extractContent,\
[8202]14    hasAtomDocumentSameHash, createCI_Citation, createCI_Date, findPublishedDate,\
[8236]15    isoDateTimeStringToTimeDate, findUpdatedDate, createDate,\
[8323]16    calculateHash, extractUpdateFrequency, findDOIInMigrationDocument,\
17    getAtomDocumentHash
[8014]18from libs.migration.processor.deployment import DeploymentProcessor
[8144]19from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue
[8158]20from logging import StreamHandler
21import logging
[8202]22from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_datetypecode import CI_DateTypeCode
[8236]23from MolesManager.ceda_guid import CedaGUID
[8247]24from ea_model.iso_19115_2006_metadata_corrigendum.maintenance_information.md_maintenancefrequencycode import MD_MaintenanceFrequencyCode
[8325]25from libs.migration.processor.EPBRepo import EPBRepo
[8014]26
27class DataEntityProcessor(object):
[8158]28    log = logging.getLogger('DataEntityProcessor')
29    log.addHandler(StreamHandler())
[8269]30    log.setLevel(logging.INFO) 
[8014]31    '''
[8089]32        Processes a DataEntityMigration item. Note that each DataEntity is associated to a "dataent_xxxx" file in Moles2
33    '''       
[8323]34    def __init__(self, dataEntityMigration):
[8014]35        '''
36            Initializes the class
[8089]37            @param _dataEntityMigration: the DataEntityMigration instance
[8147]38        '''
39        if dataEntityMigration is None:
40            raise MigrationObjectException("DataEntityProcessor cannot process an None item")
[8089]41        self._dataEntityMigration = dataEntityMigration       
[8082]42   
[8144]43    def _processCitation(self, ceda_observationCollection):
[8147]44        # TDB - Check that if is an update or not!
[8158]45        contentDict = extractContent(self._dataEntityMigration)
46        if not contentDict.has_key('citation'):
[8258]47            DataEntityProcessor.log.info("The migration object "+ migrationObjectDescription(self._dataEntityMigration) \
[8198]48                                         + " has not associated cedacat:citation")
[8188]49        else:
[8202]50            ci_dates = []
51            doc_date = findPublishedDate(self._dataEntityMigration)           
52            if doc_date:
53                i_date = createDate(isoDateTimeStringToTimeDate(doc_date))           
54                ci_dates.append(createCI_Date(CI_DateTypeCode.cl_publication, date = i_date))
55
56            doc_date = findUpdatedDate(self._dataEntityMigration)           
57            if doc_date:
58                i_date = createDate(isoDateTimeStringToTimeDate(doc_date))               
59                ci_dates.append(createCI_Date(CI_DateTypeCode.cl_revision, date = i_date))
60               
61            i_citation = createCI_Citation(title = 'ceda_moles2_citation', date=ci_dates)
62            newIdentifier = createMD_Identifier(code = contentDict['citation'], authority=i_citation)
[8236]63            ceda_observationCollection.identifier.append(newIdentifier)
[8144]64   
[8147]65    def _getObservationCollection(self):
[8325]66        return EPBRepo.moles3EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id)
[8147]67   
68    def _processObservationCollection(self):                                   
69        #Moles3 object exists...
70        if self._dataEntityMigration.ceda_observation_coll_id: 
71            # ...and the data entity document has not changed
72            if hasAtomDocumentSameHash(self._dataEntityMigration):
73                return self._getObservationCollection()
74            else:
75                self.updateObservationCollection()
[8082]76
[8147]77        #... does not exist so create it
78        return self.createNewObservationCollection()
[8082]79
[8147]80    def updateObservationCollection(self):
81        """
82            Updated the existing CEDA_ObservationCollection instance binded to the self._dataEntityMigration object.
83            @return: the persisted and updated CEDA_ObservationCollection element
84        """       
85        ceda_observationCollection = self._getObservationCollection()
86        # TBD
87        #self._processCitation(ceda_observationCollection)
88        return ceda_observationCollection
89
[8323]90    def _update(self): 
91        # ...and the data entity document has not changed
92        if hasAtomDocumentSameHash(self._dataEntityMigration):
93                return self._getObservationCollection()
94        else:
95                return self.updateObservationCollection() 
96
[8245]97    def _create(self): 
[8147]98        """
99            Creates a new CEDA_ObservationCollection instance in the Moles3DB using the self._dataEntityMigration object.
100            If successful adds the new instance ID to the related DataEntityMigration object
101            @return: the persisted CEDA_ObservationCollection element
102        """
103        ceda_observationCollection = CEDA_ObservationCollection()
[8144]104        self._processCitation(ceda_observationCollection)
[8162]105        #self._processResult(ceda_observationCollection)
[8144]106        ceda_observationCollection.publicationState = MO_PublicationStateValue.cl_working
[8147]107                 
[8351]108        EPBRepo.moles3EPB.persistInstance(ceda_observationCollection)       
[8325]109        EPBRepo.migrationEPB.updateMigrationObject(self._dataEntityMigration, {'ceda_observation_coll_id': ceda_observationCollection.id})
[8323]110
[8245]111       
[8236]112        #Adds the CedaGUID
113        ceda_guid = CedaGUID()
114        ceda_guid.id = calculateHash(self._dataEntityMigration.data_ent_id)
115        ceda_guid.ceda_observationcollection = ceda_observationCollection.id
[8325]116        EPBRepo.moles3EPB.persistInstance(ceda_guid)
[8274]117        DataEntityProcessor.log.info("GUID for this ObservationCollection: %s" % (ceda_guid.id))
[8236]118       
[8089]119        return ceda_observationCollection
[8082]120
[8323]121    def _processResultAccumulation(self, ceda_observation):       
[8325]122        ceda_observation = EPBRepo.moles3EPB.loadAttributes(ceda_observation, "resultAccumulation")       
[8323]123        if ceda_observation.resultAccumulation is None:
124            updateFrequency = extractUpdateFrequency(self._dataEntityMigration)
125            if updateFrequency:
126                resultAccumulation = MD_MaintenanceFrequencyCode.from_string(updateFrequency)
[8325]127                EPBRepo.moles3EPB.updateCedaObject(ceda_observation, {'resultAccumulation': resultAccumulation})
[8323]128
129    def _processDOI(self, deploymentMigration, ceda_observation, deProcessor, single_deployment):       
130        doi = findDOIInMigrationDocument(deploymentMigration)                                                             
131        if single_deployment:
132            if doi is None:
133                doi = findDOIInMigrationDocument(self._dataEntityMigration)
134                    #collection_identifier = Moles3EPB.extractCollectionIdentifierByTitle(MD_CODE_MOLES2_CITATION, self.migrationSessions.molesSession)
135                    #if collection_identifier.count()==1:
136                    #    ceda_observation.identifier.append(collection_identifier.first())
137               
138        deProcessor.assignDOI(ceda_observation, doi)
139
[8254]140    def _processDeploymentMigration(self, deploymentMigration, single_deployment):                                     
[8323]141        deProcessor = DeploymentProcessor(self._dataEntityMigration, deploymentMigration)
[8089]142        try:
[8266]143            DataEntityProcessor.log.info("Processing deployment: %s" % (migrationObjectDescription(deploymentMigration)))
[8089]144            ceda_observation = deProcessor.process()
[8323]145            try:           
146                self._processResultAccumulation(ceda_observation)
147            except Exception as ex:
148                pass   
149            try:                           
150                self._processDOI(deploymentMigration, ceda_observation, deProcessor, single_deployment)
151            except Exception as ex:
152                pass               
[8266]153
[8323]154                if not hasAtomDocumentSameHash(deploymentMigration):
155                    doc_hash = getAtomDocumentHash(deploymentMigration.docStatus, deploymentMigration.docType, deploymentMigration.docOwner, deploymentMigration.docName)
[8325]156                    EPBRepo.migrationEPB.updateMigrationObject(deploymentMigration, {'doc_hash': doc_hash})               
[8147]157        except NoAssociatedAuthor as ex:
158            raise ex                 
159        except Exception as ex:
[8323]160            #self.migrationSessions.molesSession.rollback()
161            #self.migrationSessions.migrationSession.rollback()               
[8144]162            raise MigrationObjectException(ex)                     
[8082]163       
[8144]164        return ceda_observation
[8089]165   
166    def process(self):
[8245]167        cedaObservationCollection = None
[8089]168        exs = []
[8266]169        DataEntityProcessor.log.info("Processing dataEntity: %s" % (migrationObjectDescription(self._dataEntityMigration)))
[8245]170        try :
171            if self._dataEntityMigration.ceda_observation_coll_id:
172                cedaObservationCollection = self._update()
173            else:
174                cedaObservationCollection = self._create()
[8237]175        except Exception as ex:
176            exs.append(ex)
[8245]177            return exs       
178
[8089]179       
[8237]180        #retrieves the associated deployment links from the data_entity
[8089]181        deploymentsLinks = findDeploymentsInDE(self._dataEntityMigration)
182       
[8237]183        #retrieves the DataEntityMigration sorted by creation date
[8325]184        deploymentMigrations = EPBRepo.migrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate( \
[8323]185                                                self._dataEntityMigration, deploymentsLinks)
186        howManydm = 0
187        if deploymentMigrations:
188            howManydm = len(deploymentMigrations)
[8254]189        if howManydm == 0:
190            exs.append(NoAssociatedDeployments(self._dataEntityMigration))
[8089]191        for deploymentMigration in deploymentMigrations:
[8144]192            try:
[8254]193                ceda_observation = self._processDeploymentMigration(deploymentMigration, howManydm == 1)
[8350]194                EPBRepo.moles3EPB.updateCedaObject(cedaObservationCollection, {'member': ceda_observation})
[8323]195                                     
[8144]196            except Exception as ex:
197                exs.append(ex)
[8180]198            except RuntimeError as er:
199                print er               
[8323]200        #self.migrationSessions.molesSession.commit()                       
[8254]201               
[8082]202        return exs
Note: See TracBrowser for help on using the repository browser.