source: mauRepo/MolesManager/trunk/src/libs/migration/processor/deployment.py @ 8183

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

Complete - # 22404: CEDA Observation - dates for DOI entries
 http://team.ceda.ac.uk/trac/ceda/ticket/22404

Line 
1'''
2Created on 15 Nov 2011
3
4@author: mnagni
5'''
6from libs.migration.processor.commons import findMolesLineage,\
7    createMO_ResponsiblePartyInfo,\
8    DO_BADC, DO_NEODC, findAuthorsInResource, CEDA,\
9    createMD_Identifier, extractSummary, extractQuality, \
10    hasAtomDocumentSameHash, getAtomDocumentHashByMO, extractTitle,\
11        createMO_ResponsiblePartyInfoAsCI_Organization,\
12        createMO_ResponsiblePartyInfoAsCI_Individual, createCEDA_Result,\
13    createEX_GeographicBoundingBox, extractGeographicExtentInMigrationDocument, findDownloadLinksInMigrationDocument,\
14    findDOIInMigrationDocument, extractContent
15from ea_model.moles3_4.utilities.mo_rolevalue import MO_RoleValue
16from ea_model.ceda_metadatamodel.ceda_observation.ceda_observation import CEDA_Observation
17from libs.migration.MigrationEPB import MigrationEPB
18from libs.epb import EPB
19from libs.migration.processor.deployment_data import DeploymentDataProcessor
20from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue
21from libs.migration.exception.exceptions import NoDataLineage,\
22    NoAssociatedAuthor
23from MolesManager.moles3epb import Moles3EPB
24from libs.migration.authors import authors
25from logging import StreamHandler
26import logging
27from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_onlinefunctioncode import CI_OnLineFunctionCode
28from libs.migration.processor.check.check import moles3Append
29
30class DeploymentProcessor(object):
31    '''
32        Migrates a deployment element in a CEDA_Observation entity
33    '''
34    publisherName = 'CEDA'
35   
36    log = logging.getLogger('DeploymentProcessor')
37    log.addHandler(StreamHandler())
38    log.setLevel(logging.DEBUG)   
39    def __init__(self, dataEntityMigration, deploymentMigration, migrationSessions):
40        '''
41            Initializes the class
42            @param dataEntityMigration: a DataEntityMigration instance
43            @param deploymentMigration: the DeploymentMigration instance
44            @param migrationSessions: a MigrationSessions instance   
45        '''             
46        self._dataEntityMigration = dataEntityMigration
47        self._deploymentMigration = deploymentMigration
48        self._dataEntityHasSameHash = hasAtomDocumentSameHash(self._dataEntityMigration) and self._dataEntityMigration.doc_hash is not None
49        self._deploymentHasSameHash = hasAtomDocumentSameHash(self._deploymentMigration) and self._deploymentMigration.doc_hash is not None
50       
51        self._migrationSessions = migrationSessions
52
53    def _existsCEDAasPublisher(self):
54        for rp in self._ceda_observation.relatedParty:
55            if rp.role == MO_RoleValue.cl_publisher:
56                for party in rp.parties:
57                    if party.name == DeploymentProcessor.publisherName:
58                        return True
59        return False
60                   
61    def _extractResponsiblePartyInfo(self, authors = None, parsedParties = []):
62        '''
63            @param authors: the deployment's authors
64            @param parsedParties: a list to fill with the created MO_ResponsiblePartyInfos
65        '''
66        #First is assumend to be the author
67        parsedParties.append(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_author, authors[:1]))   
68        parsedParties.append(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_coinvestigator, authors[1:]))           
69
70    def _addResponsiblePartyInfo(self, oldResponsiblePartyInfos, newResponsiblePartyInfo):
71        opi = None
72        for oldPartyInfo in oldResponsiblePartyInfos:
73            if oldPartyInfo.role == newResponsiblePartyInfo.role:
74                opi = oldPartyInfo
75                break
76           
77        if len(oldResponsiblePartyInfos) == 0 or opi is None:
78            oldResponsiblePartyInfos.append(newResponsiblePartyInfo)
79       
80        if opi is not None: 
81            for np in newResponsiblePartyInfo.party:
82                opi.party.append(np)
83
84    def _assignName(self, observation):       
85        '''
86            @param relatedPartyInfos: a MO_ResponsiblePartyInfo list
87            @return True if the documents changed, False otherwise
88        '''
89        whereAreAuthors = self._deploymentMigration       
90        doc_authors = findAuthorsInResource(self._deploymentMigration)       
91        if doc_authors['authors'] in [DO_BADC, DO_NEODC]:
92            doc_authors = findAuthorsInResource(self._dataEntityMigration)
93            whereAreAuthors = self._dataEntityMigration
94                       
95        Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_curator, [CEDA]), observation, self._migrationSessions.molesSession)
96       
97        if authors.has_key(doc_authors['authors']):
98            tmp_auth = authors[doc_authors['authors']]           
99            ret = self._extractIndividualsAndOrganizations(tmp_auth, whereAreAuthors)
100            if len(ret['ind']) > 0:
101                Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfoAsCI_Individual(MO_RoleValue.cl_author, [ret['ind']]), observation, self._migrationSessions.molesSession)
102            if len(ret['org']) > 0:
103                Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfoAsCI_Organization(MO_RoleValue.cl_author, [ret['org']]), observation, self._migrationSessions.molesSession)               
104            if len(ret['co_ind']) > 0:
105                Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfoAsCI_Individual(MO_RoleValue.cl_coinvestigator, [ret['co_ind']]), observation, self._migrationSessions.molesSession)
106            if len(ret['co_org']) > 0:
107                Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfoAsCI_Organization(MO_RoleValue.cl_coinvestigator, [ret['co_org']]), observation, self._migrationSessions.molesSession)                                                         
108        else:
109            raise NoAssociatedAuthor(doc_authors['authors'], migrationObject = whereAreAuthors)
110       
111        if doc_authors['contributors'] is None:
112            return
113       
114        if authors.has_key(doc_authors['contributors']):
115            tmp_auth = authors[doc_authors['contributors']]           
116            ret = self._extractIndividualsAndOrganizations(tmp_auth, whereAreAuthors)
117            if len(ret['ind']) > 0:
118                Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfoAsCI_Individual(MO_RoleValue.cl_coinvestigator, [ret['ind']]), observation, self._migrationSessions.molesSession)
119            if len(ret['org']) > 0:
120                Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfoAsCI_Organization(MO_RoleValue.cl_coinvestigator, [ret['org']]), observation, self._migrationSessions.molesSession)               
121            if len(ret['co_ind']) > 0:
122                Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfoAsCI_Individual(MO_RoleValue.cl_coinvestigator, [ret['co_ind']]), observation, self._migrationSessions.molesSession)
123            if len(ret['co_org']) > 0:
124                Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfoAsCI_Organization(MO_RoleValue.cl_coinvestigator, [ret['co_org']]), observation, self._migrationSessions.molesSession)                                       
125        else:
126            raise NoAssociatedAuthor(doc_authors['contributors'], migrationObject = whereAreAuthors)
127
128
129    def _extractIndividualsAndOrganizations(self, tmp_auth, whereAreAuthors):
130        if whereAreAuthors is None:
131            raise Exception("migrationObject is None")
132        try:
133            ret = {'ind': [], 'org': [], 'co_ind': [], 'co_org': []}
134            if tmp_auth['type'] == 'ind':
135                ret['ind'].append(tmp_auth['author'])
136            elif tmp_auth['type'] == 'org':
137                ret['org'].append(tmp_auth['author'])
138           
139            if tmp_auth['author'] == 'unknown':
140                DeploymentProcessor.log.info("%s %s %s has unknown author" % (whereAreAuthors.doc_status, whereAreAuthors.doc_owner, whereAreAuthors.doc_name))       
141                 
142            for item in tmp_auth['co_author_type']:
143                if item == 'ind':
144                    ret['ind'].append(tmp_auth['co_author'][tmp_auth['co_author_type'].index(item)])
145                elif item == 'org':
146                    ret['org'].append(tmp_auth['co_author'][tmp_auth['co_author_type'].index(item)])
147           
148                if (tmp_auth['co_author'][tmp_auth['co_author_type'].index(item)] == 'unknown'):
149                    DeploymentProcessor.log.info("%s %s %s has unknown author" % (whereAreAuthors.doc_status, whereAreAuthors.doc_owner, whereAreAuthors.doc_name))
150        except Exception as e:
151            print e
152        return ret
153
154    def _assignLineage(self, observation):
155        data_lineage = findMolesLineage(self._dataEntityMigration)
156        if data_lineage is None:
157            raise NoDataLineage(self._dataEntityMigration)
158        Moles3EPB.addDataLineageToObservation(data_lineage, observation, self._migrationSessions.molesSession)       
159       
160    def _assignTitle(self, observation):       
161        doc_title = extractTitle(self._deploymentMigration)
162       
163        if doc_title.startswith('deployment_') or doc_title.startswith('Deployment_'):
164            doc_title = extractTitle(self._dataEntityMigration)       
165        Moles3EPB.addIdentifierToObservation(createMD_Identifier(code = doc_title, title = 'ceda_title'), observation, self._migrationSessions.molesSession)
166
167    def _assignDescription(self, observation):
168        summary = extractSummary(self._deploymentMigration, self._dataEntityMigration)
169        Moles3EPB.addDescriptionToObservation(summary, observation, self._migrationSessions.molesSession)
170       
171    def _assignQuality(self, observation):
172        doc_quality = extractQuality(self._dataEntityMigration)
173        Moles3EPB.addDocumentQuality(doc_quality, observation, self._migrationSessions.molesSession)
174       
175    def updateObservation(self):
176        ceda_observation = EPB.searchOrCreate(CEDA_Observation, self._migrationSessions.molesSession, self._deploymentMigration.ceda_observation_id)
177        return ceda_observation
178
179
180    def _assignResult(self, observation):
181        # TDB - Check that if is an update or not!       
182        download = findDownloadLinksInMigrationDocument(self._deploymentMigration)
183        content = None
184        if len(download) == 0:
185            download = findDownloadLinksInMigrationDocument(self._dataEntityMigration)
186            content = extractContent(self._dataEntityMigration)
187        else:
188            content = extractContent(self._deploymentMigration) 
189        for dwn in download:
190            int_description = None
191            int_applicationProfile = None
192            if content.has_key('formats'):
193                int_applicationProfile = content['formats']
194            if dwn['href'].startswith('http://badc.nerc.ac.uk/browse') or dwn['href'].startswith('http://neodc.nerc.ac.uk/browse'):
195                int_description = "download directly from archive"               
196            observation.result = createCEDA_Result(linkage = dwn['href'], name = dwn['title'], function = CI_OnLineFunctionCode.cl_download, description = int_description, applicationProfile = int_applicationProfile)                               
197
198
199    def _assignGeographicExtent(self, observation):
200        # TDB - Check that if is an update or not!
201        ge = extractGeographicExtentInMigrationDocument(self._deploymentMigration)
202        if not ge:
203            ge = extractGeographicExtentInMigrationDocument(self._dataEntityMigration)
204        if ge:
205            moles3Append(observation, 'geographicExtent', createEX_GeographicBoundingBox(ge['east'], ge['north'], ge['west'], ge['south']))
206
207    def _assignDOI(self, observation):
208        # TDB - Check that if is an update or not!
209        doi = findDOIInMigrationDocument(self._deploymentMigration) 
210        if not doi:
211            doi = findDOIInMigrationDocument(self._dataEntityMigration)
212        if doi:
213            Moles3EPB.addIdentifierToObservation(createMD_Identifier(code = doi['href']), observation, self._migrationSessions.molesSession)                   
214
215    def createNewObservation(self):
216        ceda_observation = CEDA_Observation()
217        ceda_observation.publicationState = MO_PublicationStateValue.cl_working
218        self._assignQuality(ceda_observation)
219        self._assignLineage(ceda_observation)
220        self._assignDescription(ceda_observation)
221        self._assignName(ceda_observation)
222        self._assignTitle(ceda_observation)
223        #self._assignResult(ceda_observation)   
224        self._assignGeographicExtent(ceda_observation)   
225        self._assignDOI(ceda_observation)       
226        #self._migrationSessions.molesSession.commit()
227        #self._commitMoles3()
228        #self._commitMigration()
229        Moles3EPB.addCedaObject(ceda_observation, self._migrationSessions.molesSession, True)
230        MigrationEPB.associateObservationToDeployment(self._deploymentMigration, ceda_observation.id, self._migrationSessions.molesSession, True)
231        return ceda_observation
232
233    def _getObservation(self):
234        return EPB.search(CEDA_Observation, self._deploymentMigration.ceda_observation_id, self._migrationSessions.molesSession)
235           
236    def process(self):
237        ceda_observation = None
238        #Moles3 object exists...
239        if self._deploymentMigration.ceda_observation_id: 
240            # ...and the data entity document has not changed
241            if hasAtomDocumentSameHash(self._deploymentMigration):
242                ceda_observation = self._getObservation()
243            else:
244                ceda_observation = self.updateObservation()
245        else:
246            #... does not exist so create it
247            ceda_observation = self.createNewObservation() 
248       
249        #process the CEDA_Observation.procedure
250        deploymentDataProcessor = DeploymentDataProcessor(self._deploymentMigration, self._migrationSessions)
251        procedure = deploymentDataProcessor.process()
252        if procedure:
253            ceda_observation.procedure = procedure
254            self._migrationSessions.molesSession.commit()
255           
256        if not self._deploymentHasSameHash:
257            self._deploymentMigration.doc_hash = getAtomDocumentHashByMO(self._deploymentMigration)
258            self._commitMigration()
259             
260       
261        return ceda_observation
Note: See TracBrowser for help on using the repository browser.