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

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

Implementation of tickets #22390

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, extractTitle,\
9    createMD_Identifier, extractSummary, extractQuality, \
10    hasAtomDocumentSameHash, getAtomDocumentHashByMO, extractTitle,\
11        createMO_ResponsiblePartyInfoAsCI_Organization,\
12        createMO_ResponsiblePartyInfoAsCI_Individual
13from ea_model.moles3_4.utilities.mo_rolevalue import MO_RoleValue
14from ea_model.ceda_metadatamodel.ceda_observation.ceda_observation import CEDA_Observation
15from libs.migration.MigrationEPB import MigrationEPB
16from libs.epb import EPB
17from libs.migration.processor.deployment_data import DeploymentDataProcessor
18from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue
19from libs.migration.exception.exceptions import NoDataLineage,\
20    NoAssociatedAuthor
21from MolesManager.moles3epb import Moles3EPB
22from libs.migration.authors import authors
23from logging import StreamHandler
24import logging
25
26class DeploymentProcessor(object):
27    '''
28        Migrates a deployment element in a CEDA_Observation entity
29    '''
30    publisherName = 'CEDA'
31   
32    log = logging.getLogger('DeploymentProcessor')
33    log.addHandler(StreamHandler())
34    log.setLevel(logging.DEBUG)   
35    def __init__(self, dataEntityMigration, deploymentMigration, migrationSessions):
36        '''
37            Initializes the class
38            @param dataEntityMigration: a DataEntityMigration instance
39            @param deploymentMigration: the DeploymentMigration instance
40            @param migrationSessions: a MigrationSessions instance   
41        '''             
42        self._dataEntityMigration = dataEntityMigration
43        self._deploymentMigration = deploymentMigration
44        self._dataEntityHasSameHash = hasAtomDocumentSameHash(self._dataEntityMigration) and self._dataEntityMigration.doc_hash is not None
45        self._deploymentHasSameHash = hasAtomDocumentSameHash(self._deploymentMigration) and self._deploymentMigration.doc_hash is not None
46       
47        self._migrationSessions = migrationSessions
48
49    def _existsCEDAasPublisher(self):
50        for rp in self._ceda_observation.relatedParty:
51            if rp.role == MO_RoleValue.cl_publisher:
52                for party in rp.parties:
53                    if party.name == DeploymentProcessor.publisherName:
54                        return True
55        return False
56                   
57    def _extractResponsiblePartyInfo(self, authors = None, parsedParties = []):
58        '''
59            @param authors: the deployment's authors
60            @param parsedParties: a list to fill with the created MO_ResponsiblePartyInfos
61        '''
62        #First is assumend to be the author
63        parsedParties.append(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_author, authors[:1]))   
64        parsedParties.append(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_coinvestigator, authors[1:]))           
65
66    def _addResponsiblePartyInfo(self, oldResponsiblePartyInfos, newResponsiblePartyInfo):
67        opi = None
68        for oldPartyInfo in oldResponsiblePartyInfos:
69            if oldPartyInfo.role == newResponsiblePartyInfo.role:
70                opi = oldPartyInfo
71                break
72           
73        if len(oldResponsiblePartyInfos) == 0 or opi is None:
74            oldResponsiblePartyInfos.append(newResponsiblePartyInfo)
75       
76        if opi is not None: 
77            for np in newResponsiblePartyInfo.party:
78                opi.party.append(np)
79
80    def _assignName(self, observation):       
81        '''
82            @param relatedPartyInfos: a MO_ResponsiblePartyInfo list
83            @return True if the documents changed, False otherwise
84        '''
85        whereAreAuthors = self._deploymentMigration
86       
87        doc_authors = findAuthorsInResource(self._deploymentMigration)
88       
89        if (len(set(doc_authors['authors']) & set([DO_BADC, DO_NEODC])) > 0):
90            doc_authors = findAuthorsInResource(self._dataEntityMigration)
91            whereAreAuthors = self._dataEntityMigration
92        Moles3EPB.addRelatedPartyInfoToObservation(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_curator, [CEDA]), observation, self._migrationSessions.molesSession)
93        tmp_auth = None
94        if authors.has_key(doc_authors['authors']):
95            tmp_auth = authors[doc_authors['authors']]
96            ret = self._extractIndividualsAndOrganizations(tmp_auth, whereAreAuthors)
97            if len(ret['ind']) > 0:
98                createMO_ResponsiblePartyInfoAsCI_Individual(MO_RoleValue.cl_author, [ret['ind']])
99            if len(ret['org']) > 0:
100                createMO_ResponsiblePartyInfoAsCI_Organization(MO_RoleValue.cl_author, [ret['org']])
101            if len(ret['co_ind']) > 0:
102                createMO_ResponsiblePartyInfoAsCI_Individual(MO_RoleValue.cl_author, [ret['co_ind']])
103            if len(ret['co_org']) > 0:
104                createMO_ResponsiblePartyInfoAsCI_Organization(MO_RoleValue.cl_author, [ret['co_org']])                                                         
105        else:
106            raise NoAssociatedAuthor(doc_authors['authors'], migrationObject = whereAreAuthors)
107
108
109    def _extractIndividualsAndOrganizations(self, tmp_auth, whereAreAuthors):   
110        ret = {'ind': [], 'org': [], 'co_ind': [], 'co_org': []}
111        if tmp_auth['type'] == 'ind':
112            ret['ind'].append(tmp_auth['author'])
113        elif tmp_auth['type'] == 'org':
114            ret['org'].append(tmp_auth['author'])
115       
116        if tmp_auth['author'] == 'unknown':
117            DeploymentProcessor.log.info("%s %s %s has unknown author" % (whereAreAuthors.doc_status, whereAreAuthors.doc_owner, whereAreAuthors.doc_name))       
118 
119        for item in tmp_auth['co_author_type']:
120            if item == 'ind':
121                ret['ind'].append(tmp_auth['co_author'].index(item))
122            elif item == 'org':
123                ret['org'].append(tmp_auth['co_author'].index(item))
124       
125            if (tmp_auth['co_author'].index(item) == 'unknown'):
126                DeploymentProcessor.log.info("%s %s %s has unknown author" % (whereAreAuthors.doc_status, whereAreAuthors.doc_owner, whereAreAuthors.doc_name))
127        return ret
128
129    def _assignLineage(self, observation):
130        data_lineage = findMolesLineage(self._dataEntityMigration)
131        if data_lineage is None:
132            raise NoDataLineage(self._dataEntityMigration)
133        Moles3EPB.addDataLineageToObservation(data_lineage, observation, self._migrationSessions.molesSession)       
134       
135    def _assignTitle(self, observation):       
136        doc_title = extractTitle(self._deploymentMigration)
137       
138        if doc_title.startswith('deployment_'):
139            doc_title = extractTitle(self._dataEntityMigration)       
140        Moles3EPB.addDataLineageToObservation(createMD_Identifier(title = "Data from %s" % (doc_title)), observation, self._migrationSessions.molesSession)
141
142    def _assignDescription(self, observation):
143        summary = extractSummary(self._deploymentMigration, self._dataEntityMigration)
144        Moles3EPB.addDescriptionToObservation(summary, observation, self._migrationSessions.molesSession)
145       
146    def _assignQuality(self, observation):
147        doc_quality = extractQuality(self._dataEntityMigration)
148        Moles3EPB.addDocumentQuality(doc_quality, observation, self._migrationSessions.molesSession)
149       
150    def updateObservation(self):
151        ceda_observation = EPB.searchOrCreate(CEDA_Observation, self._migrationSessions.molesSession, self._deploymentMigration.ceda_observation_id)
152        return ceda_observation
153
154    def createNewObservation(self):
155        ceda_observation = CEDA_Observation()
156        ceda_observation.publicationState = MO_PublicationStateValue.cl_working
157        self._assignQuality(ceda_observation)
158        self._assignLineage(ceda_observation)
159        self._assignDescription(ceda_observation)
160        self._assignName(ceda_observation)
161        self._assignTitle(ceda_observation)
162        self._migrationSessions.molesSession.commit()
163        #self._commitMoles3()
164        #self._commitMigration()
165        Moles3EPB.addCedaObject(ceda_observation, self._migrationSessions.molesSession, True)
166        MigrationEPB.associateObservationToDeployment(self._deploymentMigration, ceda_observation.id, self._migrationSessions.molesSession, True)
167        return ceda_observation
168
169    def _getObservation(self):
170        return EPB.search(CEDA_Observation, self._deploymentMigration.ceda_observation_id, self._migrationSessions.molesSession)
171           
172    def process(self):
173        ceda_observation = None
174        #Moles3 object exists...
175        if self._deploymentMigration.ceda_observation_id: 
176            # ...and the data entity document has not changed
177            if hasAtomDocumentSameHash(self._deploymentMigration):
178                ceda_observation = self._getObservation()
179            else:
180                ceda_observation = self.updateObservation()
181        else:
182            #... does not exist so create it
183            ceda_observation = self.createNewObservation() 
184       
185        #process the CEDA_Observation.procedure
186        deploymentDataProcessor = DeploymentDataProcessor(self._deploymentMigration, self._migrationSessions)
187        procedure = deploymentDataProcessor.process()
188        if procedure:
189            ceda_observation.procedure = procedure
190            self._migrationSessions.molesSession.commit()
191           
192        if not self._deploymentHasSameHash:
193            self._deploymentMigration.doc_hash = getAtomDocumentHashByMO(self._deploymentMigration)
194            self._commitMigration()
195             
196       
197        return ceda_observation
Note: See TracBrowser for help on using the repository browser.