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

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

settings.py paths are now relative
deployment correctly fill the ResponsiblePartyInfo? (But still not mapped in the DB. Waiting for an updated library)
add a logerror to wsgi.conf

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