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

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

The tables are correctly generated but still some problem with class initialization.
Migration is partially working.
Starting the deployment structure implementation.

Line 
1'''
2Created on 15 Nov 2011
3
4@author: mnagni
5'''
6from libs.migration.processor.commons import getXMLDocument,\
7    createMO_ResponsiblePartyInfoAsCI_Organization, findMolesLineage,\
8    findAuthorInResource
9from libs.migration.db.dbConnection import findCEDAObservationByDeplID
10from ea_model.moles3_4.utilities.mo_rolevalue import MO_RoleValue
11from libs.commons_db import doInsertOrUpdate
12from libs.migration.exception.exceptions import NoDataLineage
13from ea_model.ceda_metadatamodel.ceda_observation.ceda_observation import CEDA_Observation
14
15class DeploymentProcessor(object):
16    '''
17        Migrates a deployment element in a CEDA_Observation entity
18    '''
19    publisherName = 'CEDA'
20    '''
21        Processes a Deployment item.
22        @var _deplObj: a ObservationMigration instance
23        @var _dataEntity: the associated DataEntityProcessor
24        @var _resource: an elementTree Element representing XML document processed by this instance
25        @var _ceda_observation: the CEDA_Observation associated with this instance 
26    '''   
27   
28    def __init__(self, deplObj, deProcessor):
29        '''
30            Initializes the class
31            @param deplObj: a ObservationMigration entity
32            @param deProcessor: the associated DataEntityProcessor
33        '''             
34        self._deplObj = deplObj
35        self._deProcessor = deProcessor
36
37    def _existsCEDAasPublisher(self):
38        for rp in self._ceda_observation.relatedParty:
39            if rp.role == MO_RoleValue.cl_publisher:
40                for party in rp.parties:
41                    if party.name == DeploymentProcessor.publisherName:
42                        return True
43        return False
44                   
45    def _extractResponsiblePartyInfo(self, authors):
46        '''
47            @param authors: a list of authors (after the first are considered co-authors)
48        '''
49        if not authors:
50            return
51       
52        parsedParties = []
53        if (len(set(authors) & set(['badc.nerc.ac.uk', 'neodc.nerc.ac.uk'])) > 0):
54            parsedParties.append(self._createResponsibleParty('Centre for Environmental Data Archive', MO_RoleValue.cl_curator))
55            authorsCSV = findAuthorInResource(self._deProcessor.dataEntity)
56            if (authorsCSV):
57                authors = authorsCSV.split(',')
58            else:
59                return       
60        #First is assumend to be the author
61        parsedParties.append(self._createResponsibleParty(authors[0], MO_RoleValue.cl_author))
62        if (authors.remove(authors[0])):
63            for coauthor in authors.remove(authors[0]):
64                parsedParties.append(self._createResponsibleParty(coauthor, MO_RoleValue.cl_author))
65
66        return parsedParties           
67
68    def _createResponsibleParty(self, name, role):
69        '''
70            @param name: The CI_Party names
71            @param role: The assigned role           
72        '''
73        return createMO_ResponsiblePartyInfoAsCI_Organization(name, role)
74
75    def _addResponsiblePartyInfo(self, oldResponsiblePartyInfos, newResponsiblePartyInfo):
76        for oldPartyInfo in oldResponsiblePartyInfos:
77            if oldPartyInfo.role == newResponsiblePartyInfo.role:
78                oldPartyInfo.party.append(newResponsiblePartyInfo.party)
79
80
81    def _checkResponsiblePartyExist(self, oldResponsiblePartyInfos, newResponsiblePartyInfos):
82        '''
83            Compares two MO_ResponsiblePartyInfo lists. For each MO_ResponsiblePartyInfo in the oldResponsiblePartyInfos it
84            - deletes it from the oldResponsiblePartyInfos if it does NOT exists in the newResponsiblePartyInfos
85            - deletes it from the newResponsiblePartyInfos if it does exists in the newResponsiblePartyInfos
86            @param oldResponsiblePartyInfos: the old MO_ResponsiblePartyInfo list
87            @param newResponsiblePartyInfos: the new MO_ResponsiblePartyInfo list
88        '''                       
89        for oldPartyInfo in oldResponsiblePartyInfos:
90            exists = False
91            for newPartyInfo in newResponsiblePartyInfos:
92                if oldPartyInfo.role == newPartyInfo.role:
93                    exists = True
94                    self._checkPartyExist(oldPartyInfo, newPartyInfo)
95            if not exists:
96                oldResponsiblePartyInfos.remove(oldPartyInfo)           
97                           
98    def _checkPartyExist(self, oldPartyInfo, newPartyInfo):
99        '''
100            Compares two MO_ResponsiblePartyInfo-s. For each CI_Party in the oldPartyInfo it
101            - deletes it from the oldPartyInfo if it does NOT exists in the newPartyInfo
102            - deletes it from the newPartyInfo if it does exists in the oldPartyInfo
103            @param oldPartyInfo: the old MO_ResponsiblePartyInfo
104            @param newPartyInfo: the new MO_ResponsiblePartyInfo 
105        '''
106        for oldParty in oldPartyInfo:
107            exists = False
108            for newParty in newPartyInfo:               
109                if oldParty.name == newParty.name:
110                    exists = True                   
111                    newPartyInfo.remove(newParty)
112            oldPartyInfo.remove(oldParty) 
113
114    def _assignName(self, relatedPartyInfos):
115        '''
116            @param relatedPartyInfos: a MO_ResponsiblePartyInfo list
117        '''       
118        authorCSV = findAuthorInResource(self._deployment)
119        if (not authorCSV):
120            return
121        newResponsiblePartyInfos = self._extractResponsiblePartyInfo(authorCSV.split(','))
122        #This methods check prepare the insert/update of the responsiblePartyInfos
123        self._checkResponsiblePartyExist(relatedPartyInfos, newResponsiblePartyInfos)
124        for newResponsiblePartyInfo in newResponsiblePartyInfos:
125            self._addResponsiblePartyInfo(relatedPartyInfos, newResponsiblePartyInfo)
126       
127        '''
128        if not self._existsCEDAasPublisher():
129            rp = self._ceda_observation.relatedParty
130            rp.append(createMO_ResponsiblePartyInfoAsCI_Organization(MO_RoleValue.cl_publisher, DeploymentProcessor.publisherName))
131        '''
132
133    def _assignLineage(self):
134        self._ceda_observation.dataLineage = findMolesLineage(self._deProcessor.dataEntity)
135        if self._ceda_observation.dataLineage is None:
136            raise NoDataLineage(self._de_path)
137       
138    def _assignTitle(self):
139        pass 
140       
141    def _processObj(self, cedaObservation):
142        '''
143            @param  cedaObservation: a relatedPartiesInfo instance
144        '''
145        self._assignName(cedaObservation.relatedParty)
146        self._assignLineage()
147        self._assignTitle()
148       
149       
150    def process(self):
151        self._de_path = '%s/%s' % (self._deplObj.exist_path, self._deplObj.depl_name)
152        print    self._de_path
153        #retrieves the deployment'd document         
154        self._deployment = getXMLDocument(self._de_path)
155        self._ceda_observation = None
156        isUpdate = True
157        #Exists already an associated CEDA_Observation associated to this deployment?
158        if self._deplObj is not None and self._deplObj.obs_id is not None:
159            self._ceda_observation = findCEDAObservationByDeplID(self._deplObj.obs_id, self._deProcessor.session)
160
161        #If does not exist creates a new one           
162        if self._ceda_observation is None:
163            isUpdate = False
164            self._ceda_observation = CEDA_Observation()
165            self._ceda_observation.relatedParty = []
166
167        #fills/update the CEDA_Observation                       
168        self._processObj(self._ceda_observation)
169        #insert/update the CEDA_Observation
170        doInsertOrUpdate([self._ceda_observation], self._deProcessor.session, isUpdate)
171        self._deProcessor.session.commit()
172        self._deplObj.obs_id = self._ceda_observation.ceda_observation_id
173        doInsertOrUpdate([self._deplObj], self._deProcessor.session, True)
174        self._deProcessor.session.commit()
175        return self._ceda_observation
176
177
Note: See TracBrowser for help on using the repository browser.