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