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

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

Implementation of tickets #22386, #22395

Line 
1'''
2Created on 15 Nov 2011
3
4@author: mnagni
5'''
6from libs.migration.processor.commons import findMolesLineage,\
7    createMO_ResponsiblePartyInfo, getAtomDocumentByMO,\
8    DO_BADC, DO_NEODC, findAuthorsInResource, CEDA, extractAuthor,\
9    createMD_Identifier, extractSummary, extractQuality, createDQ_Element,\
10    hasAtomDocumentSameHash, getAtomDocumentHashByMO
11from ea_model.moles3_4.utilities.mo_rolevalue import MO_RoleValue
12from ea_model.ceda_metadatamodel.ceda_observation.ceda_observation import CEDA_Observation
13from MolesManager.moles3epb import Moles3EPB
14from libs.migration.MigrationEPB import MigrationEPB
15from libs.epb import EPB
16from libs.migration.processor.check.check import checkLineage, checkResponsiblePartyInfos,\
17    observationHasTitle, observationHasQuality
18from libs.migration.processor.deployment_data import DeploymentDataProcessor
19from ea_model.iso_19115_2006_metadata_corrigendum.identification_information.md_progresscode import MD_ProgressCode
20from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue
21
22class DeploymentProcessor(object):
23    '''
24        Migrates a deployment element in a CEDA_Observation entity
25    '''
26    publisherName = 'CEDA'
27    '''
28        Processes a Deployment item. 
29    '''   
30    def __init__(self, dataEntityMigration, deploymentMigration, migrationSessions):
31        '''
32            Initializes the class
33            @param dataEntityMigration: a DataEntityMigration instance
34            @param deploymentMigration: the DeploymentMigration instance
35            @param migrationSessions: a MigrationSessions instance   
36        '''             
37        self._dataEntityMigration = dataEntityMigration
38        self._deploymentMigration = deploymentMigration
39        self._dataEntityHasSameHash = hasAtomDocumentSameHash(self._dataEntityMigration) and self._dataEntityMigration.doc_hash is not None
40        self._deploymentHasSameHash = hasAtomDocumentSameHash(self._deploymentMigration) and self._deploymentMigration.doc_hash is not None
41       
42        self._migrationSessions = migrationSessions
43
44    def _existsCEDAasPublisher(self):
45        for rp in self._ceda_observation.relatedParty:
46            if rp.role == MO_RoleValue.cl_publisher:
47                for party in rp.parties:
48                    if party.name == DeploymentProcessor.publisherName:
49                        return True
50        return False
51                   
52    def _extractResponsiblePartyInfo(self, authors = None, parsedParties = []):
53        '''
54            @param authors: the deployment's authors
55            @param parsedParties: a list to fill with the created MO_ResponsiblePartyInfos
56        '''
57        #First is assumend to be the author
58        parsedParties.append(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_author, authors[:1]))   
59        parsedParties.append(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_coinvestigator, authors[1:]))           
60
61    def _addResponsiblePartyInfo(self, oldResponsiblePartyInfos, newResponsiblePartyInfo):
62        opi = None
63        for oldPartyInfo in oldResponsiblePartyInfos:
64            if oldPartyInfo.role == newResponsiblePartyInfo.role:
65                opi = oldPartyInfo
66                break
67           
68        if len(oldResponsiblePartyInfos) == 0 or opi is None:
69            oldResponsiblePartyInfos.append(newResponsiblePartyInfo)
70       
71        if opi is not None: 
72            for np in newResponsiblePartyInfo.party:
73                opi.party.append(np)
74
75    def _assignName(self):       
76        '''
77            @param relatedPartyInfos: a MO_ResponsiblePartyInfo list
78            @return True if the documents changed, False otherwise
79        '''       
80        newPartyInfos = []
81       
82        authors = findAuthorsInResource(self._deploymentMigration)
83       
84        if (len(set(authors['authors']) & set([DO_BADC, DO_NEODC])) > 0):
85            authors = findAuthorsInResource(self._dataEntityMigration)
86           
87        newPartyInfos.append(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_curator, [CEDA]))         
88        newPartyInfos.append(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_author, authors['authors']))
89        newPartyInfos.append(createMO_ResponsiblePartyInfo(MO_RoleValue.cl_coinvestigator, authors['contributors']))
90
91        checkResponsiblePartyInfos(self._ceda_observation.relatedParty, newPartyInfos)
92
93    def _assignLineage(self):
94        checkLineage(self._ceda_observation, findMolesLineage(self._dataEntityMigration))
95       
96    def _assignTitle(self):       
97        doc_title = extractAuthor(self._deploymentMigration)
98       
99        if doc_title.startswith('deployment_'):
100            doc_title = extractAuthor(self._dataEntityMigration)
101       
102        if not observationHasTitle(self._ceda_observation, doc_title): 
103            self._ceda_observation.identifier.append(createMD_Identifier(title = "Data from %s" % (doc_title)))
104
105    def _assignDescription(self):
106        summary = extractSummary(self._deploymentMigration, self._dataEntityMigration)       
107        self._ceda_observation.description = summary
108       
109    def _assignQuality(self):
110        doc_quality = extractQuality(self._dataEntityMigration)
111       
112        if not observationHasQuality(self._ceda_observation, doc_quality):
113            res = []
114            for item in self._ceda_observation.resultQuality:
115                res.append(item)
116            res.append(createDQ_Element(explaination = doc_quality))
117            self._ceda_observation.resultQuality = res
118       
119    def _processObj(self):         
120        '''
121            @param  cedaObservation: a cedaObservation instance
122        '''
123        #These methods depend exclusively from
124        #self._dataEntityMigration and self._deploymentMigration
125        #consequently I calculate the hash for the referred documents.
126        #If both is the same as
127       
128        if not self._dataEntityHasSameHash:
129            self._assignQuality()
130            self._assignLineage()
131       
132        if not (self._dataEntityHasSameHash and self._deploymentHasSameHash):
133            self._assignDescription()
134            self._assignName()
135            self._assignTitle()
136       
137        if not self._deploymentHasSameHash:
138            pass
139                         
140        #If any of the previous methods has new values this function will return True
141        return not (self._dataEntityHasSameHash and self._deploymentHasSameHash)       
142         
143
144    def _commitMoles3(self):
145        if self._ceda_observation.id is None:
146            self._ceda_observation.publicationState = MO_PublicationStateValue.cl_working
147            Moles3EPB.addCedaObject(self._ceda_observation, self._migrationSessions.molesSession)
148        self._migrationSessions.molesSession.commit()
149        self._deploymentMigration.ceda_observation_id = self._ceda_observation.id       
150       
151    def _commitMigration(self):
152        if self._deploymentMigration.depl_id is None:
153            MigrationEPB.addMigration(self._deploymentMigration, self._migrationSessions.migrationSession)
154                           
155       
156    def process(self):
157        #self._deployment = getAtomDocumentByMO(self._deploymentMigration)
158        #retrieves the deployment'd document
159        self._ceda_observation = None
160
161        #Exists already an associated CEDA_Observation associated to this deployment?
162        if self._deploymentMigration is not None:
163            self._ceda_observation = EPB.searchOrCreate(CEDA_Observation, self._migrationSessions.molesSession, self._deploymentMigration.ceda_observation_id)
164
165        #fills/update the part of the CEDA_Observation                       
166        if self._processObj():
167            self._commitMoles3()
168            self._commitMigration()
169       
170        #process the CEDA_Observation.procedure
171        deploymentDataProcessor = DeploymentDataProcessor(self._deploymentMigration, self._migrationSessions)
172        procedure = deploymentDataProcessor.process()
173        if procedure:
174            self._ceda_observation.procedure = procedure
175            self._commitMoles3()
176           
177        if not self._deploymentHasSameHash: 
178            self._deploymentMigration.doc_hash = getAtomDocumentHashByMO(self._deploymentMigration)
179            self._commitMigration()
180             
181       
182        return self._ceda_observation
Note: See TracBrowser for help on using the repository browser.