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

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

Complete - # 22489: CEDA Observation Collection - phenomenonTime
 http://team.ceda.ac.uk/trac/ceda/ticket/22489
Complete - # 22488: CEDA Observation Collection - Geographical Extent
 http://team.ceda.ac.uk/trac/ceda/ticket/22488

Line 
1'''
2BSD Licence
3Copyright (c) 2012, Science & Technology Facilities Council (STFC)
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without modification,
7are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10        this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12        this list of conditions and the following disclaimer in the documentation
13        and/or other materials provided with the distribution.
14    * Neither the name of the Science & Technology Facilities Council (STFC)
15        nor the names of its contributors may be used to endorse or promote
16        products derived from this software without specific prior written permission.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29Created on 15 Nov 2011
30
31@author: Maurizio Nagni
32'''
33from libs.migration.processor.commons import findMolesLineage,\
34    createMO_ResponsiblePartyInfo,\
35    DO_BADC, DO_NEODC, findAuthorsInResource, \
36    createMD_Identifier, extractSummary, extractQuality, \
37    hasMOSameHash, getAtomDocumentHashByMO, extractTitle,\
38        createCEDA_Result,\
39    createEX_GeographicBoundingBox, extractGeographicExtentInMigrationDocument, findDownloadLinksInMigrationDocument,\
40    extractContent, createCI_Citation, createCI_Date, createDate,\
41    createTM_Position, createTM_Instant, extractMolesCreationDate,\
42    createDateTime, isoDateTimeStringToTimeDate, extractMolesProviderID,\
43    DO_UKSSDC, createMO_Organization,\
44    createCI_Contact, createCI_Address, createCI_OnlineResource,\
45    createCI_Telephone, extractMolesTemporalRange, isoDateStringToTimeDate,\
46    createTM_Period, findAccessLinksInMigrationDocument,\
47    findLinksInDeployment, createMD_LegalConstraints,\
48    createDQ_Element, createDQ_ConformanceResult, findUpdatedDate,\
49    createMD_Metadata, createMO_OnlineResource, createCEDA_Review, calculateHash,\
50    createCI_ResponsibleParty, extractUpdateFrequency,\
51    findLinksInMigrationDocument, findSubTypeInDPT, extractMolesPublishedDate,\
52    createMD_Keywords, hasMOBeenProcessed, createMO_Individual,\
53    fromDateStringToPhenomenonTime, fromPhenomenonTimeToString,\
54    comparePhenomenonTimes
55from libs.epb import EPB
56from libs.migration.processor.deployment_data import DeploymentDataProcessor
57from libs.migration.exception.exceptions import NoDataLineage,\
58    NoAssociatedAuthor, NoAssociatedDeployments
59from libs.migration.authors import authors
60from logging import StreamHandler
61import logging
62from datetime import date
63from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_onlinefunctioncode import CI_OnLineFunctionCode
64from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_datetypecode import CI_DateTypeCode
65from ea_model.iso_19115_2006_metadata_corrigendum.constraint_information.md_restrictioncode import MD_RestrictionCode
66from copy import deepcopy
67import datetime
68from MolesManager.ceda_guid import CedaGUID
69from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_rolecode import CI_RoleCode
70from ea_model.iso_19115_2006_metadata_corrigendum.maintenance_information.md_maintenancefrequencycode import MD_MaintenanceFrequencyCode
71from ea_model.moles3_4.utilities.ceda_rolevalue import CEDA_RoleValue
72from ea_model.moles3_4.observation.mo_observationpublicationstatevalue import MO_ObservationPublicationStateValue
73from MolesManager.codelist import MM_RoleValue,\
74    MM_ObservationPublicationStateValue, getCLValue
75from ea_model.ceda_metadatamodel.ceda_observation.ceda_observation import CEDA_Observation
76from ea_model.ceda_metadatamodel.ceda_result.ceda_curationvalue import CEDA_CurationValue
77from ea_model.ceda_metadatamodel.ceda_utilities.ceda_reviewstatusvalue import CEDA_ReviewStatusValue
78from ea_model.ceda_metadatamodel.ceda_utilities.ceda_reviewfrequencyvalue import CEDA_ReviewFrequencyValue
79
80MET_GEO_FEATURE = 'Meteorological geographical features'
81ORTHOIMAGERY = 'Orthoimagery'
82           
83class DeploymentProcessor(object):
84    '''
85        Migrates a deployment element in a CEDA_Observation entity
86    '''
87    publisherName = 'CEDA'
88   
89    log = logging.getLogger('DeploymentProcessor')
90    log.addHandler(StreamHandler())
91    log.setLevel(logging.INFO)   
92    def __init__(self, dataEntityMigration, deploymentMigration, epbRepo):
93        '''
94            Initializes the class
95            @param dataEntityMigration: a DataEntityMigration instance
96            @param deploymentMigration: the DeploymentMigration instance
97            @param epbRepo: an instance of EPBRepo             
98        '''             
99        self._dataEntityMigration = dataEntityMigration
100        self._deploymentMigration = deploymentMigration
101        self.epbRepo = epbRepo
102        self._dataEntityHasSameHash = hasMOSameHash(self._dataEntityMigration)
103        self._deploymentHasSameHash = hasMOSameHash(self._deploymentMigration)
104        self._deploymentHasBeenProcessed = hasMOBeenProcessed(self._deploymentMigration)
105
106    def _addResponsiblePartyInfo(self, oldResponsiblePartyInfos, newResponsiblePartyInfo):
107        opi = None
108        for oldPartyInfo in oldResponsiblePartyInfos:
109            if oldPartyInfo.role == newResponsiblePartyInfo.role:
110                opi = oldPartyInfo
111                break
112           
113        if len(oldResponsiblePartyInfos) == 0 or opi is None:
114            oldResponsiblePartyInfos.append(newResponsiblePartyInfo)
115       
116        if opi is not None: 
117            for np in newResponsiblePartyInfo.party:
118                opi.party.append(np)
119       
120
121
122
123    def _extractIndividualsAndOrganizations(self, tmp_auth, whereAreAuthors):
124        if whereAreAuthors is None:
125            raise NoAssociatedDeployments("migrationObject is None")
126        try:
127            if tmp_auth['author'] == 'unknown':
128                doc_authors = findAuthorsInResource(self._dataEntityMigration)
129                tmp_auth = authors[doc_authors['authors']]
130           
131            ret = {'ind': [], 'org': [], 'co_ind': [], 'co_org': []}
132            if tmp_auth['type'] == 'ind':
133                ret['ind'].append(tmp_auth['author'])
134            elif tmp_auth['type'] == 'org':
135                ret['org'].append(tmp_auth['author'])           
136               
137            if tmp_auth['author'] == 'unknown':
138                DeploymentProcessor.log.debug("%s %s %s has unknown author" \
139                                             % (whereAreAuthors.doc_status, whereAreAuthors.doc_owner, whereAreAuthors.doc_name))       
140
141            for item in tmp_auth['co_author_type']:           
142                if (tmp_auth['co_author'][tmp_auth['co_author_type'].index(item)] == 'unknown'):
143                    doc_authors = findAuthorsInResource(self._dataEntityMigration)
144                    tmp_auth = authors[doc_authors['contributors']]
145                    break
146                 
147            for index in range(len(tmp_auth['co_author_type'])): 
148                ptype = tmp_auth['co_author_type'][index]               
149                if ptype == 'ind':
150                    ret['co_ind'].append(tmp_auth['co_author'][index])
151                elif ptype == 'org':
152                    ret['co_org'].append(tmp_auth['co_author'][index])
153           
154                if (tmp_auth['co_author'][index] == 'unknown'):
155                    DeploymentProcessor.log.info("%s %s %s has unknown author" \
156                                                 % (whereAreAuthors.doc_status, whereAreAuthors.doc_owner, whereAreAuthors.doc_name))
157        except Exception as e:
158            print e
159        return ret
160       
161       
162    def updateObservation(self):
163        return EPB.searchOrCreate(CEDA_Observation, self._deploymentMigration.ceda_observation_id)
164
165    def assignDOI(self, observation, doi):
166        if doi and doi.has_key('href'):
167            doi = doi['href'][22:]           
168           
169            #Check if a doi has been already assigned
170            observation = self.epbRepo.moles3EPB.loadAttributes(observation, 'identifier')
171            obs_identifier = observation.identifier
172            if obs_identifier:
173                for ident in obs_identifier:
174                    if ident.code == doi:
175                        return
176           
177            py_date = None
178            cited_responsible = createCI_ResponsibleParty(role=getCLValue(CI_RoleCode.cl_publisher), \
179                                                              organizationName='NERC - British Atmospheric Data Centre')
180            if doi.upper() == '10.5285/E8F43A51-0198-4323-A926-FE69225D57DD':
181                py_date = date(2011, 4, 1)
182            elif doi.upper() == '10.5285/78114093-E2BD-4601-8AE5-3551E62AEF2B':
183                py_date = date(2011, 11, 29)               
184            elif doi.upper() == '10.5285/DB8D8981-1A51-4D6E-81C0-CCED9B921390':
185                py_date = date(2012, 4, 16)
186            elif doi.upper() == '10.5285/639A3714-BC74-46A6-9026-64931F355E07':
187                py_date = date(2012, 4, 16)               
188               
189            if py_date:   
190                dt = createDate(py_date)
191                ci_date = createCI_Date(getCLValue(CI_DateTypeCode.cl_publication), date = dt)
192                i_authority = createCI_Citation("DOI", date = ci_date)
193                identifier = createMD_Identifier(code = doi, authority=i_authority)
194                self.epbRepo.moles3EPB.updateCedaObject(observation, {'identifier': identifier})
195                DeploymentProcessor.log.info("DOI: %s" % (doi))                                 
196
197    def _assignKeywords(self, ceda_observation):
198        if self._deploymentHasSameHash:
199            return
200       
201        provider_id = extractMolesProviderID(self._deploymentMigration)
202        i_keywords = []
203        if provider_id == DO_BADC:
204            i_keywords.append(MET_GEO_FEATURE)
205        if provider_id == DO_NEODC:
206            i_keywords.append(ORTHOIMAGERY)
207        if len(i_keywords) > 0:
208            #Is a first time process?
209            if not self._deploymentHasBeenProcessed: 
210                ceda_observation.keywords.append(createMD_Keywords(i_keywords))
211            else:
212                ceda_observation.keywords.keyword = i_keywords         
213
214    def _assignLineage(self, observation):
215        if self._deploymentHasSameHash:
216            return
217       
218        data_lineage = findMolesLineage(self._dataEntityMigration)
219        if data_lineage is None:
220            raise NoDataLineage(self._dataEntityMigration)
221       
222        if data_lineage != observation.dataLineage:
223            observation.dataLineage = data_lineage 
224
225    def _assignResult(self, observation):
226        if self._deploymentHasSameHash and self._dataEntityHasSameHash:
227            return
228
229        i_sources = []               
230        download = findDownloadLinksInMigrationDocument(self._deploymentMigration)
231        content = None
232        if len(download) == 0:
233            download = findDownloadLinksInMigrationDocument(self._dataEntityMigration)
234            content = extractContent(self._dataEntityMigration)
235        else:
236            content = extractContent(self._deploymentMigration)
237        for dwn in download:
238            int_description = None
239            int_applicationProfile = None
240            if content.has_key('formats'):
241                #int_applicationProfile = content['formats']
242                pass
243            if dwn['href'].startswith('http://badc.nerc.ac.uk/browse') or dwn['href'].startswith('http://neodc.nerc.ac.uk/browse'):
244                int_description = "download directly from archive"   
245            i_sources.append(createMO_OnlineResource(linkage = dwn['href'], name = dwn['title'], \
246                                                   function = getCLValue(CI_OnLineFunctionCode.cl_download), \
247                                                   description = int_description, applicationProfile = int_applicationProfile))
248           
249        dataentity_id = '%s__ATOM__%s' % (self._dataEntityMigration.doc_owner, self._dataEntityMigration.doc_name)
250        dataentity_id = dataentity_id.replace('.atom', '')           
251        infodb_de = self.epbRepo.infodbEPB.getCedaInfoApp_dataentityByDE_ID(dataentity_id)
252        i_logical_path = '/dummy'
253        if infodb_de is None:
254            i_logical_path = dwn['href'][dwn['href'].index('/browse/') + 7:]
255       
256        if infodb_de and infodb_de.has_key('logical_path'):
257            i_logical_path = infodb_de['logical_path']
258               
259        i_category = getCLValue(CEDA_CurationValue.cl_a)                                 
260        if infodb_de and infodb_de.has_key('category') and infodb_de['category']:
261            i_category = getCLValue(CEDA_CurationValue.from_string(infodb_de['category'].lower()))     
262
263        if not self._deploymentHasBeenProcessed:                     
264            observation.result = createCEDA_Result(i_category, i_logical_path, source = i_sources)
265            return 
266       
267        if observation.result.internalPath != i_logical_path:
268            observation.result.internalPath = i_logical_path
269           
270        if observation.result.curationCategory != i_category:
271            observation.result.curationCategory = i_category
272                       
273        #Still have to update observation.result.source
274
275    def _assignPublisherCurator(self, observation):
276        if self._deploymentHasSameHash:
277            return
278           
279        provider_id = extractMolesProviderID(self._deploymentMigration)
280        party = None
281        if provider_id == DO_BADC:
282            i_linkage = 'http://badc.rl.ac.uk'
283            i_onlineResources = createCI_OnlineResource(linkage = i_linkage, name = 'British Atmospheric Data Centre Website')
284            i_address = createCI_Address(deliveryPoint = ['British Atmospheric Data Centre, STFC Rutherford Appleton Laboratory'], \
285                                         electronicMailAddress=['badc@rl.ac.uk'], postalCode='OX11 0QX', country='UK', city='Harwell Oxford')                                   
286            i_phone = createCI_Telephone(voice=['+44(0)1235 446432'])                                 
287            contact = createCI_Contact(phone=i_phone, address=i_address, onlineResource=i_onlineResources)                       
288            party = createMO_Organization(name = "NERC - British Atmospheric Data Centre", contactInfo = [contact])
289        elif provider_id == DO_NEODC:
290            i_linkage = 'http://www.neodc.rl.ac.uk'
291            i_onlineResources = createCI_OnlineResource(linkage = i_linkage, name = 'NERC Earth Observation Data Centre website')
292            i_address = createCI_Address(deliveryPoint = ['NERC - Earth Observation Data Centre, STFC Rutherford Appleton Laboratory'], \
293                                         electronicMailAddress=['neodc@rl.ac.uk'], postalCode='OX11 0QX', country='UK', city='Harwell Oxford')                                   
294            i_phone = createCI_Telephone(voice=['+44(0)1235 446432'])                                 
295            contact = createCI_Contact(phone=i_phone, address=i_address, onlineResource=i_onlineResources)                       
296            party = createMO_Organization(name = 'NERC - Earth Observation Data Centre', contactInfo = [contact])
297        elif provider_id == DO_UKSSDC:
298            i_linkage = 'http://www.ukssdc.rl.ac.uk'
299            i_onlineResources = createCI_OnlineResource(linkage = i_linkage, name = 'UK Solar System Data Centre website')
300            i_address = createCI_Address(deliveryPoint = ['UK Solar System Data Centre, STFC Rutherford Appleton Laboratory'], \
301                                         electronicMailAddress=['support@rl.ac.uk'], postalCode='OX11 0QX', country='UK', city='Harwell Oxford')                                   
302            i_phone = createCI_Telephone(voice=['+44(0)1235 445173'])                                 
303            contact = createCI_Contact(phone=i_phone, address=i_address, onlineResource=i_onlineResources)                       
304            party = createMO_Organization(name = 'NERC - UK Solar System Data Centre', contactInfo = [contact])
305       
306        if party and not self._deploymentHasBeenProcessed:
307            newrp = []
308            newrp.append(createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_publisher), [party]))
309            newrp.append(createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_custodian), deepcopy([party])))
310            newrp.append(createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_distributor), deepcopy([party])))
311            newrp.append(createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_pointofcontact), deepcopy([party])))
312            newrp.append(createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_metadataowner), deepcopy([party])))
313            newrp.append(createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_curator), deepcopy([party])))
314            self.epbRepo.moles3EPB.updateCedaObject(observation, {'relatedParty': newrp})           
315            return       
316        #Still have to update observation.result.source
317
318    def _assignQuality(self, observation):
319        if self._dataEntityHasSameHash:
320            return
321               
322        doc_quality = extractQuality(self._dataEntityMigration)
323        doc_date = findUpdatedDate(self._dataEntityMigration)
324        ci_dates = []           
325        if doc_date:
326            i_date = createDate(isoDateTimeStringToTimeDate(doc_date))               
327            ci_dates.append(createCI_Date(getCLValue(CI_DateTypeCode.cl_revision), date = i_date))           
328        else:
329            i_date = createDate(isoDateTimeStringToTimeDate(datetime.datetime.now()))
330            ci_dates.append(createCI_Date(getCLValue(CI_DateTypeCode.cl_creation), date = i_date))
331
332        i_specification = createCI_Citation(title = "CEDA Data Quality Statement", date=ci_dates)
333        i_dq_result = createDQ_ConformanceResult(doc_quality, True, i_specification)
334        i_quality_element = createDQ_Element(i_dq_result)
335       
336        if not self._deploymentHasBeenProcessed:     
337            observation.resultQuality.append(i_quality_element)
338            return               
339        #Still have to update observation.result.source
340
341    def _assignDescription(self, observation):
342        if self._dataEntityHasSameHash and self._deploymentHasSameHash:
343            return
344       
345        description = extractSummary(self._deploymentMigration)
346        if description is None:
347            description = extractSummary(self._dataEntityMigration)
348           
349        if description:
350            observation.description = description
351
352    def _assignTitle(self, observation): 
353        if self._dataEntityHasSameHash and self._deploymentHasSameHash:
354            return
355                     
356        doc_title = extractTitle(self._deploymentMigration)
357        if doc_title is None:
358            doc_title = extractTitle(self._dataEntityMigration)
359       
360        if doc_title.startswith('deployment_') or doc_title.startswith('Deployment_'):
361            links = findLinksInMigrationDocument(self._deploymentMigration)
362            dptList = links['DPT']
363            if links.has_key('DPT'):
364                doc_title = 'Data from ' + dptList[0]['title']
365                if len(dptList) > 2:
366                    for dpt in dptList[1:-2]:
367                        doc_title += ', ' + dpt['title']
368                    if len(dptList) > 1:
369                        doc_title += ' and ' + dptList[-1]
370                                                       
371            links = findLinksInDeployment(self._deploymentMigration)
372            if links.has_key('OBS'):
373                obsList = []
374                for obs in links['OBS']:
375                    observationStation = self.epbRepo.migrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, obs + '.atom')
376                    obsList.append((extractTitle(observationStation), findSubTypeInDPT(observationStation)))
377               
378                if obsList[0][1] in ['stationary platform' ,'moving platform', 'ship','aircraft','satellite','computer']:
379                    doc_title += ' on '
380                else : 
381                    doc_title += ' at '
382                    doc_title += obsList[0][0]
383                if len(obsList) > 2:
384                    for obs in obsList[1:-2]:
385                        doc_title += ', ' + obs[0]
386                    if len(obsList) > 1:
387                        doc_title += ' and ' + obsList[-1][0]
388           
389            if links.has_key('ACTIVITY'):             
390                for link in links['ACTIVITY']:
391                    activity = self.epbRepo.migrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, link + '.atom')                   
392     
393                    projSubType = findSubTypeInDPT(activity)
394                    doc_title += ' for the ' + extractTitle(activity)
395                    if projSubType[0:14] == 'dgActivityData':
396                        doc_title += ' ' + projSubType[14:]
397                    else :
398                        doc_title += ' ' + projSubType                                                   
399        else:             
400            if doc_title[0:10] != 'Data from' :
401                doc_title = "Data from " + doc_title           
402        auth = createCI_Citation(title = 'ceda_title')                 
403        identifier = createMD_Identifier(code = doc_title, authority = auth)
404       
405        if not self._deploymentHasBeenProcessed: 
406            observation.identifier.append(identifier)
407            return           
408        #Still have to update observation.identifier         
409
410    def _assignGeographicExtent(self, observation):
411        if self._dataEntityHasSameHash and self._deploymentHasSameHash:
412            return 
413       
414        ge = extractGeographicExtentInMigrationDocument(self._deploymentMigration)
415        if not ge:
416            ge = extractGeographicExtentInMigrationDocument(self._dataEntityMigration)
417            if ge:
418                geographicExtent = createEX_GeographicBoundingBox(ge['east'], ge['north'], ge['west'], ge['south'])
419                if self._deploymentHasBeenProcessed:
420                    DeploymentProcessor.log.warn('The _assignGeographicExtent update is skipped because not implemented')
421                observation.geographicExtent.append(geographicExtent)
422            else:
423                print "No Geographic Extent"
424            return         
425        #Still have to update observation.geographicExtent
426           
427    def _assignCreationDate(self, observation):
428        if self._deploymentHasSameHash:
429            return 
430       
431        creation_date = extractMolesPublishedDate(self._deploymentMigration)
432        if creation_date is None:
433            creation_date = extractMolesCreationDate(self._deploymentMigration)
434        py_datetime = isoDateTimeStringToTimeDate(creation_date)
435        date_time = createDateTime(py_datetime)   
436        tm_position = createTM_Position(dateTime8601 = date_time)
437       
438        if not self._deploymentHasBeenProcessed: 
439            observation.resultTime = createTM_Instant(tm_position)
440            return       
441        #Still have to update observation.geographicExtent
442
443    def _assignPhenomenonTime(self, observation):
444        if self._deploymentHasSameHash and getattr(observation, 'phenomenonTime') != None:
445            return 
446             
447        doc_phenomenon_time = extractMolesTemporalRange(self._deploymentMigration) 
448        pt = fromDateStringToPhenomenonTime(doc_phenomenon_time)   
449       
450        if not self._deploymentHasBeenProcessed or not comparePhenomenonTimes(pt,observation.phenomenonTime):
451            self.epbRepo.moles3EPB.updateCedaObject(observation, {'phenomenonTime': pt})
452        #Now update phenomenonTime but has to remove the previous phenomenonTime               
453               
454    def _assignPermission(self, observation):
455        if self._deploymentHasSameHash and self._dataEntityHasSameHash:
456            return 
457       
458        access_link = findAccessLinksInMigrationDocument(self._deploymentMigration)
459        dwn_link = findDownloadLinksInMigrationDocument(self._deploymentMigration)
460        if len(access_link) == 0:
461            access_link = findAccessLinksInMigrationDocument(self._dataEntityMigration) 
462
463        i_accessConstraints = []
464        i_use_limitation = []
465       
466        permission = None
467        if len(access_link) == 0:
468            if len(dwn_link) == 0:
469                dwn_link = findDownloadLinksInMigrationDocument(self._dataEntityMigration)
470                if dwn_link and len(dwn_link) == 1:               
471                    i_use_limitation.append("These data are open access and available through %s." % (dwn_link[0]['href']) )
472                    #i_accessConstraints.append(MD_RestrictionCode.cl_)
473                    observation.permission = createMD_LegalConstraints(useLimitation = i_use_limitation, accessConstrains = i_accessConstraints)
474        else:
475            if access_link and len(access_link) == 1:
476                i_use_limitation.append("Access to these data is restricted. To obtain access please apply for access at: %s" % (access_link[0]['href']))
477                i_accessConstraints.append(getCLValue(MD_RestrictionCode.cl_restricted))
478                observation.permission = createMD_LegalConstraints(useLimitation = i_use_limitation, accessConstrains = i_accessConstraints)
479               
480        if not self._deploymentHasBeenProcessed:
481            observation.permission = permission       
482        #Still have to update observation.permission
483                                   
484        '''                               
485        contentDict = extractContent(self._deploymentMigration)
486        if not contentDict.has_key('access-restricted'):
487            contentDict = extractContent(self._dataEntityMigration)
488        '''           
489
490    def _assignMoles2Link(self, ceda_observation):
491        if self._deploymentHasSameHash:
492            return 
493               
494        i_code = 'http://badc.nerc.ac.uk/view/%s__ATOM__%s' % (self._deploymentMigration.doc_owner, self._deploymentMigration.doc_name)
495        i_code = i_code.replace('.atom', '')
496        #i_code = buildExistDocPath(self._deploymentMigration.doc_status, DT_DEPLOYMENTS, self._deploymentMigration.doc_owner, self._deploymentMigration.doc_name)
497        i_authority = createCI_Citation('moles2url')
498        identifier = createMD_Identifier(code = i_code, authority = i_authority)
499        if not self._deploymentHasBeenProcessed:             
500            ceda_observation.identifier.append(identifier)
501        #Still have to update observation.permission           
502
503    def _assignInternalReview(self, ceda_observation):
504        if self._deploymentHasBeenProcessed:             
505            return
506               
507        i_party = createMO_Individual(name = 'Graham Parton')
508        i_reviewer = createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_processor), [i_party])
509        ceda_observation.metadataManagement.append( \
510            createCEDA_Review(reviewer=i_reviewer, reviewFrequency=getCLValue(CEDA_ReviewFrequencyValue.cl_yearly), \
511                              reviewStatus=getCLValue(CEDA_ReviewStatusValue.cl_required))) 
512
513    def _assignLanguage(self, ceda_observation):
514        if self._deploymentHasBeenProcessed:             
515            return
516       
517        i_date_stamp = createDate(datetime.datetime.now())
518        #i_contact = createCI_Citation("", date = i_date_stamp)
519        i_contact = createCI_ResponsibleParty(getCLValue(CI_RoleCode.cl_user))
520        ceda_observation.metadata = createMD_Metadata(date_stamp=i_date_stamp, contact = [i_contact], language = "English")
521                       
522    def _processResultAccumulation(self, ceda_observation): 
523        if self._dataEntityHasSameHash:             
524            return                     
525
526            updateFrequency = extractUpdateFrequency(self._dataEntityMigration)
527            if updateFrequency:
528                resultAccumulation = MD_MaintenanceFrequencyCode.from_string(updateFrequency)
529                if not self._deploymentHasBeenProcessed:
530                    self.epbRepo.moles3EPB.updateCedaObject(ceda_observation, {'resultAccumulation': resultAccumulation})             
531                    return
532        #Still have to update observation.permission
533                   
534    def _assignName(self, observation):       
535        '''
536            @param relatedPartyInfos: a MO_ResponsiblePartyInfo list
537            @return True if the documents changed, False otherwise
538        '''
539        if self._deploymentHasSameHash and self._dataEntityHasSameHash:
540            return 
541       
542        whereAreAuthors = self._deploymentMigration       
543        doc_authors = findAuthorsInResource(self._deploymentMigration)       
544        if doc_authors['authors'] in [DO_BADC, DO_NEODC]:
545            doc_authors = findAuthorsInResource(self._dataEntityMigration)
546            whereAreAuthors = self._dataEntityMigration
547               
548        ind_names = []
549        org_names = []
550        if authors.has_key(doc_authors['authors']):
551            tmp_auth = authors[doc_authors['authors']]
552            ret = self._extractIndividualsAndOrganizations(tmp_auth, whereAreAuthors)
553
554            if len(ret['ind']) > 0:
555                i_party = createMO_Individual(name = ret['ind'][0])
556                observation.relatedParty.append(createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_author), [i_party]))                 
557            if len(ret['org']) > 0:
558                i_party = createMO_Organization(name = ret['org'][0])
559                observation.relatedParty.append(createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_author), [i_party]))
560           
561            if len(ret['ind']) > 1:
562                ind_names.extend(ret['ind'][1:])                                                       
563            if len(ret['org']) > 1:
564                org_names.extend(ret['org'][1:])
565            if len(ret['co_ind']) > 0:                               
566                ind_names.extend(ret['co_ind'])
567            if len(ret['co_org']) > 0:                               
568                org_names.extend(ret['co_org'])                   
569                       
570        else:
571            raise NoAssociatedAuthor(doc_authors['authors'], migrationObject = whereAreAuthors)
572                     
573        if doc_authors['contributors'] and authors.has_key(doc_authors['contributors']):
574            tmp_auth = authors[doc_authors['contributors']]           
575            ret = self._extractIndividualsAndOrganizations(tmp_auth, whereAreAuthors)
576            ind_names.extend(ret['ind'])
577            ind_names.extend(ret['co_ind'])
578            org_names.extend(ret['org'])
579            org_names.extend(ret['co_org'])
580           
581        i_party = []
582        for nm in ind_names:
583            i_party.append(createMO_Individual(name = nm))
584               
585        for nm in org_names:
586            i_party.append(createMO_Organization(name = nm))
587           
588        if i_party:
589            if not self._deploymentHasBeenProcessed:
590                rp = createMO_ResponsiblePartyInfo(getCLValue(MM_RoleValue.cl_coinvestigator), i_party)
591                self.epbRepo.moles3EPB.updateCedaObject(observation, {'relatedParty': rp})
592                return
593        #Still have to update observation.permission
594   
595    def _execute(self, ceda_observation):
596        self._assignKeywords(ceda_observation)
597        self._assignLineage(ceda_observation)
598        self._assignResult(ceda_observation)
599        self._assignPublisherCurator(ceda_observation)                       
600        self._assignQuality(ceda_observation)       
601        self._assignDescription(ceda_observation)
602        self._assignTitle(ceda_observation)   
603        self._assignGeographicExtent(ceda_observation)               
604        self._assignCreationDate(ceda_observation)       
605        self._assignPhenomenonTime(ceda_observation)       
606        self._assignPermission(ceda_observation)       
607        self._assignMoles2Link(ceda_observation)               
608        self._assignInternalReview(ceda_observation)       
609        self._assignLanguage(ceda_observation)
610        self._processResultAccumulation(ceda_observation)           
611        self._assignName(ceda_observation)
612        #self._assignDOI(ceda_observation)       
613       
614        #Is a first time process?
615        if not hasMOBeenProcessed(self._deploymentMigration):
616            ceda_observation.publicationState = getCLValue(MM_ObservationPublicationStateValue.cl_working)         
617            docHash = getAtomDocumentHashByMO(self._dataEntityMigration)
618            self.epbRepo.moles3EPB.persistInstance(ceda_observation)       
619            self.epbRepo.migrationEPB.updateMigrationObject(self._deploymentMigration, \
620                {'ceda_observation_id': ceda_observation.id,
621                 'doc_hash': docHash})               
622       
623        #Has a proper CEDAGUID?
624        if self.epbRepo.moles3EPB.retrieveGUIDFromInstance(ceda_observation) is None:       
625            ceda_guid = CedaGUID()
626            ceda_guid.id = calculateHash(self._deploymentMigration.depl_id)
627            ceda_guid.ceda_observation = ceda_observation.id
628            self.epbRepo.moles3EPB.persistInstance(ceda_guid)
629            DeploymentProcessor.log.info("GUID for this Observation: %s" % (ceda_guid.id))
630
631
632        if not self._deploymentHasBeenProcessed:               
633            deploymentDataProcessor = DeploymentDataProcessor(self._deploymentMigration, self.epbRepo)               
634            procedure = deploymentDataProcessor.createProcess()
635            project = deploymentDataProcessor.createProject() 
636            self.epbRepo.moles3EPB.updateCedaObject(ceda_observation, {'procedure': procedure, 'inSupportOf': project})
637
638        #Still have to update observation.procedure
639        #Still have to update observation.project
640       
641        return ceda_observation
642       
643    def process(self):
644        ceda_observation = None
645        #Moles3 object exists...
646        if self._deploymentMigration.ceda_observation_id:
647            ceda_observation = self.epbRepo.moles3EPB.search(CEDA_Observation, self._deploymentMigration.ceda_observation_id)
648        else:
649            #... does not exist so create it
650            ceda_observation =  ceda_observation = CEDA_Observation()
651   
652        self._execute(ceda_observation)   
653        return ceda_observation
Note: See TracBrowser for help on using the repository browser.