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

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

Fixed some "None" errors

RevLine 
[8014]1'''
[8358]2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
[8014]29Created on 15 Nov 2011
30
[8358]31@author: Maurizio Nagni
[8014]32'''
[8089]33from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection import \
34    CEDA_ObservationCollection
[8158]35from libs.migration.exception.exceptions import MigrationObjectException, NoAssociatedAuthor,\
[8258]36    migrationObjectDescription, NoAssociatedDeployments
[8144]37from libs.migration.processor.commons import findDeploymentsInDE,\
[8323]38    createMD_Identifier, extractContent,\
[8358]39    hasMOSameHash, createCI_Citation, createCI_Date, findPublishedDate,\
[8236]40    isoDateTimeStringToTimeDate, findUpdatedDate, createDate,\
[8358]41    calculateHash, findDOIInMigrationDocument,\
42    hasMOBeenProcessed, getAtomDocumentHashByMO
[8014]43from libs.migration.processor.deployment import DeploymentProcessor
[8144]44from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue
[8158]45from logging import StreamHandler
46import logging
[8202]47from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_datetypecode import CI_DateTypeCode
[8236]48from MolesManager.ceda_guid import CedaGUID
[8014]49
50class DataEntityProcessor(object):
[8158]51    log = logging.getLogger('DataEntityProcessor')
52    log.addHandler(StreamHandler())
[8269]53    log.setLevel(logging.INFO) 
[8014]54    '''
[8089]55        Processes a DataEntityMigration item. Note that each DataEntity is associated to a "dataent_xxxx" file in Moles2
56    '''       
[8358]57    def __init__(self, dataEntityMigration, epbRepo):
[8014]58        '''
59            Initializes the class
[8089]60            @param _dataEntityMigration: the DataEntityMigration instance
[8358]61            @param epbRepo: an instance of EPBRepo
[8147]62        '''
[8358]63        #if dataEntityMigration is None:
64        #    raise MigrationObjectException("DataEntityProcessor cannot process an None item")
65        self._dataEntityMigration = dataEntityMigration       
66        self._dataEntityHasSameHash = hasMOSameHash(self._dataEntityMigration)       
67        self.epbRepo = epbRepo
[8082]68   
[8144]69    def _processCitation(self, ceda_observationCollection):
[8158]70        contentDict = extractContent(self._dataEntityMigration)
71        if not contentDict.has_key('citation'):
[8258]72            DataEntityProcessor.log.info("The migration object "+ migrationObjectDescription(self._dataEntityMigration) \
[8198]73                                         + " has not associated cedacat:citation")
[8188]74        else:
[8202]75            ci_dates = []
76            doc_date = findPublishedDate(self._dataEntityMigration)           
77            if doc_date:
78                i_date = createDate(isoDateTimeStringToTimeDate(doc_date))           
79                ci_dates.append(createCI_Date(CI_DateTypeCode.cl_publication, date = i_date))
80
81            doc_date = findUpdatedDate(self._dataEntityMigration)           
82            if doc_date:
83                i_date = createDate(isoDateTimeStringToTimeDate(doc_date))               
84                ci_dates.append(createCI_Date(CI_DateTypeCode.cl_revision, date = i_date))
85               
86            i_citation = createCI_Citation(title = 'ceda_moles2_citation', date=ci_dates)
87            newIdentifier = createMD_Identifier(code = contentDict['citation'], authority=i_citation)
[8236]88            ceda_observationCollection.identifier.append(newIdentifier)
[8082]89
[8358]90    def _execute(self, ceda_observationCollection): 
[8147]91        """
92            Creates a new CEDA_ObservationCollection instance in the Moles3DB using the self._dataEntityMigration object.
93            If successful adds the new instance ID to the related DataEntityMigration object
94            @return: the persisted CEDA_ObservationCollection element
95        """
[8358]96        if not self._dataEntityHasSameHash:
97            self._processCitation(ceda_observationCollection)
98            #self._processResult(ceda_observationCollection)
99       
100        #Is a first time process?
101        if not hasMOBeenProcessed(self._dataEntityMigration):
102            docHash = getAtomDocumentHashByMO(self._dataEntityMigration)
103            ceda_observationCollection.publicationState = MO_PublicationStateValue.cl_working
104            self.epbRepo.moles3EPB.persistInstance(ceda_observationCollection)
105            self.epbRepo.migrationEPB.updateMigrationObject(self._dataEntityMigration, \
106                {'ceda_observation_coll_id': ceda_observationCollection.id, \
107                 'doc_hash': docHash})
[8323]108
[8358]109        #Has to updated the hash?
110        if not self._dataEntityHasSameHash and hasMOBeenProcessed(self._dataEntityMigration):
111            docHash = getAtomDocumentHashByMO(self._dataEntityMigration)
112            self.epbRepo.migrationEPB.updateMigrationObject(self._dataEntityMigration, \
113                {'doc_hash': docHash})
[8245]114       
[8358]115        #Has a proper CEDAGUID?
116        if self.epbRepo.moles3EPB.retrieveGUIDFromInstance(ceda_observationCollection) is None:
117            #Adds the CedaGUID
118            ceda_guid = CedaGUID()
119            ceda_guid.id = calculateHash(self._dataEntityMigration.data_ent_id)
120            setattr(ceda_guid, 'ceda_observationcollection', ceda_observationCollection.id)
121            self.epbRepo.moles3EPB.persistInstance(ceda_guid)
122            DataEntityProcessor.log.info("GUID for this ObservationCollection: %s" % (ceda_guid.id))
[8082]123
[8323]124    def _processDOI(self, deploymentMigration, ceda_observation, deProcessor, single_deployment):       
125        doi = findDOIInMigrationDocument(deploymentMigration)                                                             
126        if single_deployment:
127            if doi is None:
128                doi = findDOIInMigrationDocument(self._dataEntityMigration)
129                    #collection_identifier = Moles3EPB.extractCollectionIdentifierByTitle(MD_CODE_MOLES2_CITATION, self.migrationSessions.molesSession)
130                    #if collection_identifier.count()==1:
[8358]131                    #    ceda_observation.identifier.append(collection_identifier.first())               
[8323]132        deProcessor.assignDOI(ceda_observation, doi)
133
[8254]134    def _processDeploymentMigration(self, deploymentMigration, single_deployment):                                     
[8358]135        deProcessor = DeploymentProcessor(self._dataEntityMigration, deploymentMigration, self.epbRepo)
[8089]136        try:
[8266]137            DataEntityProcessor.log.info("Processing deployment: %s" % (migrationObjectDescription(deploymentMigration)))
[8358]138            ceda_observation = deProcessor.process()   
[8323]139            try:                           
140                self._processDOI(deploymentMigration, ceda_observation, deProcessor, single_deployment)
141            except Exception as ex:
[8358]142                pass                               
[8147]143        except NoAssociatedAuthor as ex:
144            raise ex                 
145        except Exception as ex:
[8323]146            #self.migrationSessions.molesSession.rollback()
147            #self.migrationSessions.migrationSession.rollback()               
[8144]148            raise MigrationObjectException(ex)                     
[8082]149       
[8144]150        return ceda_observation
[8089]151   
152    def process(self):
[8358]153        obsColl = None
[8089]154        exs = []
[8266]155        DataEntityProcessor.log.info("Processing dataEntity: %s" % (migrationObjectDescription(self._dataEntityMigration)))
[8245]156        try :
157            if self._dataEntityMigration.ceda_observation_coll_id:
[8358]158                obsColl = self.epbRepo.moles3EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id) 
[8245]159            else:
[8358]160                obsColl = CEDA_ObservationCollection()
161            self._execute(obsColl)                 
[8237]162        except Exception as ex:
163            exs.append(ex)
[8245]164            return exs       
[8089]165       
[8237]166        #retrieves the associated deployment links from the data_entity
[8358]167        deploymentsLinks = findDeploymentsInDE(self._dataEntityMigration)       
[8237]168        #retrieves the DataEntityMigration sorted by creation date
[8358]169        deploymentMigrations = self.epbRepo.migrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate( \
[8323]170                                                self._dataEntityMigration, deploymentsLinks)
[8363]171   
172        if deploymentMigrations is None or len(deploymentMigrations) == 0:
173            return exs
174       
[8323]175        howManydm = 0
176        if deploymentMigrations:
177            howManydm = len(deploymentMigrations)
[8254]178        if howManydm == 0:
179            exs.append(NoAssociatedDeployments(self._dataEntityMigration))
[8089]180        for deploymentMigration in deploymentMigrations:
[8144]181            try:
[8254]182                ceda_observation = self._processDeploymentMigration(deploymentMigration, howManydm == 1)
[8358]183                #Is a first time process?
[8361]184                if not self.epbRepo.moles3EPB.observationCollectionHasObservation(getattr(obsColl, 'id'), getattr(ceda_observation, 'id')):
[8358]185                    self.epbRepo.moles3EPB.updateCedaObject(obsColl, {'member': ceda_observation})
[8323]186                                     
[8144]187            except Exception as ex:
[8358]188                exs.append(ex) 
[8082]189        return exs
Note: See TracBrowser for help on using the repository browser.