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

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

The migration process now includes
1) deployment_data files
2) XML document hash
3) full Dojo support


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