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