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

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

Complete - # 22486: CEDA Observation Collection - Title
 http://team.ceda.ac.uk/trac/ceda/ticket/22486

Line 
1'''
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
29Created on 15 Nov 2011
30
31@author: Maurizio Nagni
32'''
33from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection import \
34    CEDA_ObservationCollection
35from libs.migration.exception.exceptions import MigrationObjectException, NoAssociatedAuthor,\
36    migrationObjectDescription, NoAssociatedDeployments
37from libs.migration.processor.commons import findDeploymentsInDE,\
38    createMD_Identifier, extractContent,\
39    hasMOSameHash, createCI_Citation, createCI_Date, findPublishedDate,\
40    isoDateTimeStringToTimeDate, findUpdatedDate, createDate,\
41    calculateHash, findDOIInMigrationDocument,\
42    hasMOBeenProcessed, getAtomDocumentHashByMO, extractTitle
43from libs.migration.processor.deployment import DeploymentProcessor
44from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue
45from logging import StreamHandler
46import logging
47from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_datetypecode import CI_DateTypeCode
48from MolesManager.ceda_guid import CedaGUID
49
50CEDA_TITLE = 'ceda_title'
51
52class DataEntityProcessor(object):
53    log = logging.getLogger('DataEntityProcessor')
54    log.addHandler(StreamHandler())
55    log.setLevel(logging.INFO) 
56    '''
57        Processes a DataEntityMigration item. Note that each DataEntity is associated to a "dataent_xxxx" file in Moles2
58    '''       
59    def __init__(self, dataEntityMigration, epbRepo):
60        '''
61            Initializes the class
62            @param _dataEntityMigration: the DataEntityMigration instance
63            @param epbRepo: an instance of EPBRepo
64        '''
65        #if dataEntityMigration is None:
66        #    raise MigrationObjectException("DataEntityProcessor cannot process an None item")
67        self._dataEntityMigration = dataEntityMigration       
68        self._dataEntityHasSameHash = hasMOSameHash(self._dataEntityMigration)       
69        self.epbRepo = epbRepo
70   
71    def _processTitle(self, ceda_observationCollection):
72        ititle = extractTitle(self._dataEntityMigration)
73        if ceda_observationCollection.identifier:
74            for ident in ceda_observationCollection.identifier:
75                if ident.authority.title == CEDA_TITLE:
76                    if ident.code != ititle:
77                        ident.code = ititle
78                    else:
79                        return           
80 
81        #Else create new
82        i_citation = createCI_Citation(title = CEDA_TITLE)
83        newIdentifier = createMD_Identifier(code = ititle, authority=i_citation)
84        ceda_observationCollection.identifier.append(newIdentifier)     
85   
86    def _processCitation(self, ceda_observationCollection):
87        contentDict = extractContent(self._dataEntityMigration)
88        if not contentDict.has_key('citation'):
89            DataEntityProcessor.log.info("The migration object "+ migrationObjectDescription(self._dataEntityMigration) \
90                                         + " has not associated cedacat:citation")
91        else:
92            ci_dates = []
93            doc_date = findPublishedDate(self._dataEntityMigration)           
94            if doc_date:
95                i_date = createDate(isoDateTimeStringToTimeDate(doc_date))           
96                ci_dates.append(createCI_Date(CI_DateTypeCode.cl_publication, date = i_date))
97
98            doc_date = findUpdatedDate(self._dataEntityMigration)           
99            if doc_date:
100                i_date = createDate(isoDateTimeStringToTimeDate(doc_date))               
101                ci_dates.append(createCI_Date(CI_DateTypeCode.cl_revision, date = i_date))
102               
103            i_citation = createCI_Citation(title = 'ceda_moles2_citation', date=ci_dates)
104            newIdentifier = createMD_Identifier(code = contentDict['citation'], authority=i_citation)
105            ceda_observationCollection.identifier.append(newIdentifier)
106
107    def _execute(self, ceda_observationCollection): 
108        """
109            Creates a new CEDA_ObservationCollection instance in the Moles3DB using the self._dataEntityMigration object.
110            If successful adds the new instance ID to the related DataEntityMigration object
111            @return: the persisted CEDA_ObservationCollection element
112        """
113       
114        self._processTitle(ceda_observationCollection)
115       
116        if not self._dataEntityHasSameHash:
117            self._processCitation(ceda_observationCollection)           
118            #self._processResult(ceda_observationCollection)
119       
120        #Is a first time process?
121        if not hasMOBeenProcessed(self._dataEntityMigration):
122            docHash = getAtomDocumentHashByMO(self._dataEntityMigration)
123            ceda_observationCollection.publicationState = MO_PublicationStateValue.cl_working
124            self.epbRepo.moles3EPB.persistInstance(ceda_observationCollection)
125            self.epbRepo.migrationEPB.updateMigrationObject(self._dataEntityMigration, \
126                {'ceda_observation_coll_id': ceda_observationCollection.id, \
127                 'doc_hash': docHash})
128
129        #Has to updated the hash?
130        if not self._dataEntityHasSameHash and hasMOBeenProcessed(self._dataEntityMigration):
131            docHash = getAtomDocumentHashByMO(self._dataEntityMigration)
132            self.epbRepo.migrationEPB.updateMigrationObject(self._dataEntityMigration, \
133                {'doc_hash': docHash})
134       
135        #Has a proper CEDAGUID?
136        if self.epbRepo.moles3EPB.retrieveGUIDFromInstance(ceda_observationCollection) is None:
137            #Adds the CedaGUID
138            ceda_guid = CedaGUID()
139            ceda_guid.id = calculateHash(self._dataEntityMigration.data_ent_id)
140            setattr(ceda_guid, 'ceda_observationcollection', ceda_observationCollection.id)
141            self.epbRepo.moles3EPB.persistInstance(ceda_guid)
142            DataEntityProcessor.log.info("GUID for this ObservationCollection: %s" % (ceda_guid.id))
143
144    def _processDOI(self, deploymentMigration, ceda_observation, deProcessor, single_deployment):       
145        doi = findDOIInMigrationDocument(deploymentMigration)                                                             
146        if single_deployment:
147            if doi is None:
148                doi = findDOIInMigrationDocument(self._dataEntityMigration)
149                    #collection_identifier = Moles3EPB.extractCollectionIdentifierByTitle(MD_CODE_MOLES2_CITATION, self.migrationSessions.molesSession)
150                    #if collection_identifier.count()==1:
151                    #    ceda_observation.identifier.append(collection_identifier.first())               
152        deProcessor.assignDOI(ceda_observation, doi)
153
154    def _processDeploymentMigration(self, deploymentMigration, single_deployment):                                     
155        deProcessor = DeploymentProcessor(self._dataEntityMigration, deploymentMigration, self.epbRepo)
156        try:
157            DataEntityProcessor.log.info("Processing deployment: %s" % (migrationObjectDescription(deploymentMigration)))
158            ceda_observation = deProcessor.process()   
159            try:                           
160                self._processDOI(deploymentMigration, ceda_observation, deProcessor, single_deployment)
161            except Exception as ex:
162                pass                               
163        except NoAssociatedAuthor as ex:
164            raise ex                 
165        except Exception as ex:
166            #self.migrationSessions.molesSession.rollback()
167            #self.migrationSessions.migrationSession.rollback()               
168            raise MigrationObjectException(ex)                     
169       
170        return ceda_observation
171   
172    def process(self):
173        obsColl = None
174        exs = []
175        DataEntityProcessor.log.info("Processing dataEntity: %s" % (migrationObjectDescription(self._dataEntityMigration)))
176        try :
177            if self._dataEntityMigration.ceda_observation_coll_id:
178                obsColl = self.epbRepo.moles3EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id) 
179            else:
180                obsColl = CEDA_ObservationCollection()
181            self._execute(obsColl)                 
182        except Exception as ex:
183            exs.append(ex)
184            return exs       
185       
186        #retrieves the associated deployment links from the data_entity
187        deploymentsLinks = findDeploymentsInDE(self._dataEntityMigration)       
188        #retrieves the DataEntityMigration sorted by creation date
189        deploymentMigrations = self.epbRepo.migrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate( \
190                                                self._dataEntityMigration, deploymentsLinks)
191   
192        if deploymentMigrations is None or len(deploymentMigrations) == 0:
193            return exs
194       
195        howManydm = 0
196        if deploymentMigrations:
197            howManydm = len(deploymentMigrations)
198        if howManydm == 0:
199            exs.append(NoAssociatedDeployments(self._dataEntityMigration))
200        for deploymentMigration in deploymentMigrations:
201            try:
202                ceda_observation = self._processDeploymentMigration(deploymentMigration, howManydm == 1)
203                #Is a first time process?
204                if not self.epbRepo.moles3EPB.observationCollectionHasObservation(getattr(obsColl, 'id'), getattr(ceda_observation, 'id')):
205                    self.epbRepo.moles3EPB.updateCedaObject(obsColl, {'member': ceda_observation})
206                                     
207            except Exception as ex:
208                exs.append(ex) 
209        return exs
Note: See TracBrowser for help on using the repository browser.