source: mauRepo/MolesManager/trunk/src/libs/migration/processor/commons.py @ 8015

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

implemented a prototype for the cedaObservation.
Further implementation for the Moles2 migration

Line 
1'''
2Created on 15 Nov 2011
3
4@author: mnagni
5'''
6from ea_model.moles3_4.utilities.mo_responsiblepartyinfo import MO_ResponsiblePartyInfo
7from ea_model.moles3_4.utilities.mo_rolevalue import MO_RoleValue
8from ea_model.moles3_4.utilities.mo_organization import MO_Organization
9from ea_model.iso_19115_2006_metadata_corrigendum.reference_system_information.md_identifier import MD_Identifier
10from httplib import HTTPConnection
11from xml.etree.ElementTree import XML
12import time, datetime
13from libs.migration.db.classes import ObservationMigration
14from ea_model.upcomingiso.ci_organisation import CI_Organisation
15
16base = '/exist/rest/atoms'
17docStatus = ('published', 'working', 'Published') 
18docCollections = ('deployments', 'data_entities', 'deployment_data', 'data_granules')
19
20atomNS = "{http://www.w3.org/2005/Atom}"
21existNS = "{http://exist.sourceforge.net/NS/exist}"
22molesNS = "{http://ndg.nerc.ac.uk/schema/moles2beta}"
23time_format = '%Y-%m-%dT%H:%M:%SZ'
24ihost = 'bora.badc.rl.ac.uk'
25iport = '8080'
26
27def getDocument(source):
28    conn = HTTPConnection(host = ihost, port = iport)
29    conn.connect()
30    req = conn.request('GET', source)
31    res = conn.getresponse()
32    xmlDoc = res.read()
33    '''
34    print (xmlDoc)
35    '''
36    conn.close()
37    return xmlDoc
38
39def getXMLDocument(source):
40    return XML(getDocument(source))
41
42def stringToTimestamp(timestring):
43    return datetime.datetime.fromtimestamp(time.mktime(time.strptime(timestring, time_format)))
44
45def _returnNotNoneText(element):
46    if element is None:
47        return None
48    return element.text
49
50def findMolesCreationDate(resourceXML):
51    creationDate = resourceXML.find('%sentity/%smolesISO/%screated' % (molesNS, molesNS, molesNS))
52    return _returnNotNoneText(creationDate)
53
54def findMolesLineage(resourceXML):
55    lineage = resourceXML.find('%sentity/%smolesISO/%slineage' % (molesNS, molesNS, molesNS))
56    return _returnNotNoneText(lineage)
57
58def findAuthorInResource(resourceXML):
59    author = resourceXML.find('%sauthor/%sname' % (atomNS, atomNS))
60    return _returnNotNoneText(author)
61
62def findID(dataEntXML):
63    id = dataEntXML.find('%sid' % (atomNS))
64    return _returnNotNoneText(id)
65
66def findDeploymentsInDE(dataEntXML):
67    hrefs = dataEntXML.findall('%slink' % (atomNS))
68    depls = []
69    for href in hrefs:
70        rel = href.get('rel')
71        if rel and rel.endswith('/Deployment'):
72            try:
73                link = href.get('href').split('/')[-1]
74                linkName = link.rsplit('__ATOM__')[1]
75                depls.append(linkName)
76            except Exception as ex:
77                print ex
78    return depls
79
80def getResourceRefs(resourceRefs):
81    '''
82        Returns a list of Elements representing the inner resource reference items
83        @param ownerBaseDeployments: the name of the eXist collection name below the 'deployments' one
84    ''' 
85    deployment = getDocument(resourceRefs)
86    XMLDepl = XML(deployment)
87    return XMLDepl.findall('%scollection/%sresource' % (existNS, existNS))
88
89def getCollectionRefs(base):
90    '''
91        Returns a list of Elements representing the inner deployment reference items
92        @param basePublished: the name of the eXist collection name below the 'published' one
93    ''' 
94    collection = getDocument(base)
95    XMLPubl = XML(collection)
96    return XMLPubl.findall('%scollection/%scollection' % (existNS, existNS))
97
98def getResource(source, resourceName):
99    resourceSource = '%s/%s' % (source, resourceName)
100    resourceDoc = getDocument(resourceSource)
101    return XML(resourceDoc)
102
103
104
105def createMO_ResponsiblePartyInfoAsCI_Organization(role, names):
106    '''
107        @param role: a CI_RoleCode/MO_RoleValue assigned to this ResponsibleParty
108        @param names: the list of CI_Party names
109    '''
110    mo_responsableInfo = MO_ResponsiblePartyInfo()
111    mo_responsableInfo.party = []   
112    mo_responsableInfo.role = role
113    for name in names:
114        ci_org = CI_Organisation()
115        ci_org.name = name
116        mo_responsableInfo.party.append(ci_org)
117    return mo_responsableInfo
118
119def createMO_ResponsiblePartyInfo(author):
120    respParty = MO_ResponsiblePartyInfo()
121    respParty.role = MO_RoleValue.cl_author
122    mo_organization = MO_Organization()
123    respParty.party = []
124    respParty.party.append(mo_organization)
125    respParty.party.name = author
126    return respParty
127
128def createMD_Identifier(code):
129    md_identifier = MD_Identifier()
130    md_identifier.code = code
131    return md_identifier
Note: See TracBrowser for help on using the repository browser.