1 | ''' |
---|
2 | Created on 15 Nov 2011 |
---|
3 | |
---|
4 | @author: mnagni |
---|
5 | ''' |
---|
6 | from MolesManager.moles3epb import Moles3EPB |
---|
7 | from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection import \ |
---|
8 | CEDA_ObservationCollection |
---|
9 | from libs.migration.MigrationEPB import MigrationEPB |
---|
10 | from libs.migration.exception.exceptions import MigrationObjectException, NoAssociatedAuthor,\ |
---|
11 | migrationObjectDescription, NoAssociatedDeployments |
---|
12 | from libs.migration.processor.commons import findDeploymentsInDE,\ |
---|
13 | createMD_Identifier, extractContent,\ |
---|
14 | hasAtomDocumentSameHash, createCI_Citation, createCI_Date, findPublishedDate,\ |
---|
15 | isoDateTimeStringToTimeDate, findUpdatedDate, createDate,\ |
---|
16 | calculateHash, extractUpdateFrequency, findDOIInMigrationDocument,\ |
---|
17 | getAtomDocumentHash |
---|
18 | from libs.migration.processor.deployment import DeploymentProcessor |
---|
19 | from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue |
---|
20 | from logging import StreamHandler |
---|
21 | import logging |
---|
22 | from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_datetypecode import CI_DateTypeCode |
---|
23 | from MolesManager.ceda_guid import CedaGUID |
---|
24 | from ea_model.iso_19115_2006_metadata_corrigendum.maintenance_information.md_maintenancefrequencycode import MD_MaintenanceFrequencyCode |
---|
25 | from libs.migration.processor.EPBRepo import EPBRepo |
---|
26 | |
---|
27 | class DataEntityProcessor(object): |
---|
28 | log = logging.getLogger('DataEntityProcessor') |
---|
29 | log.addHandler(StreamHandler()) |
---|
30 | log.setLevel(logging.INFO) |
---|
31 | ''' |
---|
32 | Processes a DataEntityMigration item. Note that each DataEntity is associated to a "dataent_xxxx" file in Moles2 |
---|
33 | ''' |
---|
34 | def __init__(self, dataEntityMigration): |
---|
35 | ''' |
---|
36 | Initializes the class |
---|
37 | @param _dataEntityMigration: the DataEntityMigration instance |
---|
38 | ''' |
---|
39 | if dataEntityMigration is None: |
---|
40 | raise MigrationObjectException("DataEntityProcessor cannot process an None item") |
---|
41 | self._dataEntityMigration = dataEntityMigration |
---|
42 | |
---|
43 | def _processCitation(self, ceda_observationCollection): |
---|
44 | # TDB - Check that if is an update or not! |
---|
45 | contentDict = extractContent(self._dataEntityMigration) |
---|
46 | if not contentDict.has_key('citation'): |
---|
47 | DataEntityProcessor.log.info("The migration object "+ migrationObjectDescription(self._dataEntityMigration) \ |
---|
48 | + " has not associated cedacat:citation") |
---|
49 | else: |
---|
50 | ci_dates = [] |
---|
51 | doc_date = findPublishedDate(self._dataEntityMigration) |
---|
52 | if doc_date: |
---|
53 | i_date = createDate(isoDateTimeStringToTimeDate(doc_date)) |
---|
54 | ci_dates.append(createCI_Date(CI_DateTypeCode.cl_publication, date = i_date)) |
---|
55 | |
---|
56 | doc_date = findUpdatedDate(self._dataEntityMigration) |
---|
57 | if doc_date: |
---|
58 | i_date = createDate(isoDateTimeStringToTimeDate(doc_date)) |
---|
59 | ci_dates.append(createCI_Date(CI_DateTypeCode.cl_revision, date = i_date)) |
---|
60 | |
---|
61 | i_citation = createCI_Citation(title = 'ceda_moles2_citation', date=ci_dates) |
---|
62 | newIdentifier = createMD_Identifier(code = contentDict['citation'], authority=i_citation) |
---|
63 | ceda_observationCollection.identifier.append(newIdentifier) |
---|
64 | |
---|
65 | def _getObservationCollection(self): |
---|
66 | return EPBRepo.moles3EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id) |
---|
67 | |
---|
68 | def _processObservationCollection(self): |
---|
69 | #Moles3 object exists... |
---|
70 | if self._dataEntityMigration.ceda_observation_coll_id: |
---|
71 | # ...and the data entity document has not changed |
---|
72 | if hasAtomDocumentSameHash(self._dataEntityMigration): |
---|
73 | return self._getObservationCollection() |
---|
74 | else: |
---|
75 | self.updateObservationCollection() |
---|
76 | |
---|
77 | #... does not exist so create it |
---|
78 | return self.createNewObservationCollection() |
---|
79 | |
---|
80 | def updateObservationCollection(self): |
---|
81 | """ |
---|
82 | Updated the existing CEDA_ObservationCollection instance binded to the self._dataEntityMigration object. |
---|
83 | @return: the persisted and updated CEDA_ObservationCollection element |
---|
84 | """ |
---|
85 | ceda_observationCollection = self._getObservationCollection() |
---|
86 | # TBD |
---|
87 | #self._processCitation(ceda_observationCollection) |
---|
88 | return ceda_observationCollection |
---|
89 | |
---|
90 | def _update(self): |
---|
91 | # ...and the data entity document has not changed |
---|
92 | if hasAtomDocumentSameHash(self._dataEntityMigration): |
---|
93 | return self._getObservationCollection() |
---|
94 | else: |
---|
95 | return self.updateObservationCollection() |
---|
96 | |
---|
97 | def _create(self): |
---|
98 | """ |
---|
99 | Creates a new CEDA_ObservationCollection instance in the Moles3DB using the self._dataEntityMigration object. |
---|
100 | If successful adds the new instance ID to the related DataEntityMigration object |
---|
101 | @return: the persisted CEDA_ObservationCollection element |
---|
102 | """ |
---|
103 | ceda_observationCollection = CEDA_ObservationCollection() |
---|
104 | self._processCitation(ceda_observationCollection) |
---|
105 | #self._processResult(ceda_observationCollection) |
---|
106 | ceda_observationCollection.publicationState = MO_PublicationStateValue.cl_working |
---|
107 | |
---|
108 | EPBRepo.moles3EPB.persistInstance(ceda_observationCollection) |
---|
109 | EPBRepo.migrationEPB.updateMigrationObject(self._dataEntityMigration, {'ceda_observation_coll_id': ceda_observationCollection.id}) |
---|
110 | |
---|
111 | |
---|
112 | #Adds the CedaGUID |
---|
113 | ceda_guid = CedaGUID() |
---|
114 | ceda_guid.id = calculateHash(self._dataEntityMigration.data_ent_id) |
---|
115 | ceda_guid.ceda_observationcollection = ceda_observationCollection.id |
---|
116 | EPBRepo.moles3EPB.persistInstance(ceda_guid) |
---|
117 | DataEntityProcessor.log.info("GUID for this ObservationCollection: %s" % (ceda_guid.id)) |
---|
118 | |
---|
119 | return ceda_observationCollection |
---|
120 | |
---|
121 | def _processResultAccumulation(self, ceda_observation): |
---|
122 | ceda_observation = EPBRepo.moles3EPB.loadAttributes(ceda_observation, "resultAccumulation") |
---|
123 | if ceda_observation.resultAccumulation is None: |
---|
124 | updateFrequency = extractUpdateFrequency(self._dataEntityMigration) |
---|
125 | if updateFrequency: |
---|
126 | resultAccumulation = MD_MaintenanceFrequencyCode.from_string(updateFrequency) |
---|
127 | EPBRepo.moles3EPB.updateCedaObject(ceda_observation, {'resultAccumulation': resultAccumulation}) |
---|
128 | |
---|
129 | def _processDOI(self, deploymentMigration, ceda_observation, deProcessor, single_deployment): |
---|
130 | doi = findDOIInMigrationDocument(deploymentMigration) |
---|
131 | if single_deployment: |
---|
132 | if doi is None: |
---|
133 | doi = findDOIInMigrationDocument(self._dataEntityMigration) |
---|
134 | #collection_identifier = Moles3EPB.extractCollectionIdentifierByTitle(MD_CODE_MOLES2_CITATION, self.migrationSessions.molesSession) |
---|
135 | #if collection_identifier.count()==1: |
---|
136 | # ceda_observation.identifier.append(collection_identifier.first()) |
---|
137 | |
---|
138 | deProcessor.assignDOI(ceda_observation, doi) |
---|
139 | |
---|
140 | def _processDeploymentMigration(self, deploymentMigration, single_deployment): |
---|
141 | deProcessor = DeploymentProcessor(self._dataEntityMigration, deploymentMigration) |
---|
142 | try: |
---|
143 | DataEntityProcessor.log.info("Processing deployment: %s" % (migrationObjectDescription(deploymentMigration))) |
---|
144 | ceda_observation = deProcessor.process() |
---|
145 | try: |
---|
146 | self._processResultAccumulation(ceda_observation) |
---|
147 | except Exception as ex: |
---|
148 | pass |
---|
149 | try: |
---|
150 | self._processDOI(deploymentMigration, ceda_observation, deProcessor, single_deployment) |
---|
151 | except Exception as ex: |
---|
152 | pass |
---|
153 | |
---|
154 | if not hasAtomDocumentSameHash(deploymentMigration): |
---|
155 | doc_hash = getAtomDocumentHash(deploymentMigration.docStatus, deploymentMigration.docType, deploymentMigration.docOwner, deploymentMigration.docName) |
---|
156 | EPBRepo.migrationEPB.updateMigrationObject(deploymentMigration, {'doc_hash': doc_hash}) |
---|
157 | except NoAssociatedAuthor as ex: |
---|
158 | raise ex |
---|
159 | except Exception as ex: |
---|
160 | #self.migrationSessions.molesSession.rollback() |
---|
161 | #self.migrationSessions.migrationSession.rollback() |
---|
162 | raise MigrationObjectException(ex) |
---|
163 | |
---|
164 | return ceda_observation |
---|
165 | |
---|
166 | def process(self): |
---|
167 | cedaObservationCollection = None |
---|
168 | exs = [] |
---|
169 | DataEntityProcessor.log.info("Processing dataEntity: %s" % (migrationObjectDescription(self._dataEntityMigration))) |
---|
170 | try : |
---|
171 | if self._dataEntityMigration.ceda_observation_coll_id: |
---|
172 | cedaObservationCollection = self._update() |
---|
173 | else: |
---|
174 | cedaObservationCollection = self._create() |
---|
175 | except Exception as ex: |
---|
176 | exs.append(ex) |
---|
177 | return exs |
---|
178 | |
---|
179 | |
---|
180 | #retrieves the associated deployment links from the data_entity |
---|
181 | deploymentsLinks = findDeploymentsInDE(self._dataEntityMigration) |
---|
182 | |
---|
183 | #retrieves the DataEntityMigration sorted by creation date |
---|
184 | deploymentMigrations = EPBRepo.migrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate( \ |
---|
185 | self._dataEntityMigration, deploymentsLinks) |
---|
186 | howManydm = 0 |
---|
187 | if deploymentMigrations: |
---|
188 | howManydm = len(deploymentMigrations) |
---|
189 | if howManydm == 0: |
---|
190 | exs.append(NoAssociatedDeployments(self._dataEntityMigration)) |
---|
191 | for deploymentMigration in deploymentMigrations: |
---|
192 | try: |
---|
193 | ceda_observation = self._processDeploymentMigration(deploymentMigration, howManydm == 1) |
---|
194 | EPBRepo.moles3EPB.updateCedaObject(cedaObservationCollection, {'member': ceda_observation}) |
---|
195 | |
---|
196 | except Exception as ex: |
---|
197 | exs.append(ex) |
---|
198 | except RuntimeError as er: |
---|
199 | print er |
---|
200 | #self.migrationSessions.molesSession.commit() |
---|
201 | |
---|
202 | return exs |
---|