1 | ''' |
---|
2 | BSD Licence |
---|
3 | Copyright (c) 2012, Science & Technology Facilities Council (STFC) |
---|
4 | All rights reserved. |
---|
5 | |
---|
6 | Redistribution and use in source and binary forms, with or without modification, |
---|
7 | are permitted provided that the following conditions are met: |
---|
8 | |
---|
9 | * Redistributions of source code must retain the above copyright notice, |
---|
10 | this list of conditions and the following disclaimer. |
---|
11 | * Redistributions in binary form must reproduce the above copyright notice, |
---|
12 | this list of conditions and the following disclaimer in the documentation |
---|
13 | and/or other materials provided with the distribution. |
---|
14 | * Neither the name of the Science & Technology Facilities Council (STFC) |
---|
15 | nor the names of its contributors may be used to endorse or promote |
---|
16 | products derived from this software without specific prior written permission. |
---|
17 | |
---|
18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
---|
19 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
---|
20 | THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
---|
21 | PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS |
---|
22 | BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, |
---|
23 | OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
---|
24 | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
---|
25 | HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
---|
26 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
27 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
28 | |
---|
29 | Created on 15 Nov 2011 |
---|
30 | |
---|
31 | @author: Maurizio Nagni |
---|
32 | ''' |
---|
33 | from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection import \ |
---|
34 | CEDA_ObservationCollection |
---|
35 | from libs.migration.exception.exceptions import MigrationObjectException, NoAssociatedAuthor,\ |
---|
36 | migrationObjectDescription, NoAssociatedDeployments |
---|
37 | from libs.migration.processor.commons import findDeploymentsInDE,\ |
---|
38 | createMD_Identifier, extractContent,\ |
---|
39 | hasMOSameHash, createCI_Citation, createCI_Date, findPublishedDate,\ |
---|
40 | isoDateTimeStringToTimeDate, findUpdatedDate, createDate,\ |
---|
41 | calculateHash, findDOIInMigrationDocument,\ |
---|
42 | hasMOBeenProcessed, getAtomDocumentHashByMO, extractTitle |
---|
43 | from libs.migration.processor.deployment import DeploymentProcessor |
---|
44 | from logging import StreamHandler |
---|
45 | import logging |
---|
46 | from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_datetypecode import CI_DateTypeCode |
---|
47 | from MolesManager.ceda_guid import CedaGUID |
---|
48 | from ea_model.moles3_4.observation.mo_observationpublicationstatevalue import MO_ObservationPublicationStateValue |
---|
49 | from MolesManager.codelist import MM_ObservationPublicationStateValue,\ |
---|
50 | getCLValue |
---|
51 | |
---|
52 | CEDA_TITLE = 'ceda_title' |
---|
53 | |
---|
54 | class DataEntityProcessor(object): |
---|
55 | log = logging.getLogger('DataEntityProcessor') |
---|
56 | log.addHandler(StreamHandler()) |
---|
57 | log.setLevel(logging.INFO) |
---|
58 | ''' |
---|
59 | Processes a DataEntityMigration item. Note that each DataEntity is associated to a "dataent_xxxx" file in Moles2 |
---|
60 | ''' |
---|
61 | def __init__(self, dataEntityMigration, epbRepo): |
---|
62 | ''' |
---|
63 | Initializes the class |
---|
64 | @param _dataEntityMigration: the DataEntityMigration instance |
---|
65 | @param epbRepo: an instance of EPBRepo |
---|
66 | ''' |
---|
67 | #if dataEntityMigration is None: |
---|
68 | # raise MigrationObjectException("DataEntityProcessor cannot process an None item") |
---|
69 | self._dataEntityMigration = dataEntityMigration |
---|
70 | self._dataEntityHasSameHash = hasMOSameHash(self._dataEntityMigration) |
---|
71 | self.epbRepo = epbRepo |
---|
72 | |
---|
73 | def _checkGeographicExtent(self, ceda_observationCollection): |
---|
74 | union = self.moles3epb.getUnifyObservationCollectionGE(ceda_observationCollection) |
---|
75 | |
---|
76 | def _processTitle(self, ceda_observationCollection): |
---|
77 | ititle = extractTitle(self._dataEntityMigration) |
---|
78 | if ceda_observationCollection.identifier: |
---|
79 | for ident in ceda_observationCollection.identifier: |
---|
80 | if ident.authority.title == CEDA_TITLE: |
---|
81 | if ident.code != ititle: |
---|
82 | ident.code = ititle |
---|
83 | else: |
---|
84 | return |
---|
85 | |
---|
86 | #Else create new |
---|
87 | i_citation = createCI_Citation(title = CEDA_TITLE) |
---|
88 | newIdentifier = createMD_Identifier(code = ititle, authority=i_citation) |
---|
89 | ceda_observationCollection.identifier.append(newIdentifier) |
---|
90 | |
---|
91 | def _processCitation(self, ceda_observationCollection): |
---|
92 | contentDict = extractContent(self._dataEntityMigration) |
---|
93 | if not contentDict.has_key('citation'): |
---|
94 | DataEntityProcessor.log.info("The migration object "+ migrationObjectDescription(self._dataEntityMigration) \ |
---|
95 | + " has not associated cedacat:citation") |
---|
96 | else: |
---|
97 | ci_dates = [] |
---|
98 | doc_date = findPublishedDate(self._dataEntityMigration) |
---|
99 | if doc_date: |
---|
100 | i_date = createDate(isoDateTimeStringToTimeDate(doc_date)) |
---|
101 | ci_dates.append(createCI_Date(CI_DateTypeCode.cl_publication, date = i_date)) |
---|
102 | |
---|
103 | doc_date = findUpdatedDate(self._dataEntityMigration) |
---|
104 | if doc_date: |
---|
105 | i_date = createDate(isoDateTimeStringToTimeDate(doc_date)) |
---|
106 | ci_dates.append(createCI_Date(CI_DateTypeCode.cl_revision, date = i_date)) |
---|
107 | |
---|
108 | i_citation = createCI_Citation(title = 'ceda_moles2_citation', date=ci_dates) |
---|
109 | newIdentifier = createMD_Identifier(code = contentDict['citation'], authority=i_citation) |
---|
110 | ceda_observationCollection.identifier.append(newIdentifier) |
---|
111 | |
---|
112 | def _execute(self, ceda_observationCollection): |
---|
113 | """ |
---|
114 | Creates a new CEDA_ObservationCollection instance in the Moles3DB using the self._dataEntityMigration object. |
---|
115 | If successful adds the new instance ID to the related DataEntityMigration object |
---|
116 | @return: the persisted CEDA_ObservationCollection element |
---|
117 | """ |
---|
118 | |
---|
119 | self._processTitle(ceda_observationCollection) |
---|
120 | |
---|
121 | if not self._dataEntityHasSameHash: |
---|
122 | self._processCitation(ceda_observationCollection) |
---|
123 | #self._processResult(ceda_observationCollection) |
---|
124 | |
---|
125 | #Is a first time process? |
---|
126 | if not hasMOBeenProcessed(self._dataEntityMigration): |
---|
127 | docHash = getAtomDocumentHashByMO(self._dataEntityMigration) |
---|
128 | ceda_observationCollection.publicationState = getCLValue(MM_ObservationPublicationStateValue.cl_working) |
---|
129 | self.epbRepo.moles3EPB.persistInstance(ceda_observationCollection) |
---|
130 | self.epbRepo.migrationEPB.updateMigrationObject(self._dataEntityMigration, \ |
---|
131 | {'ceda_observation_coll_id': ceda_observationCollection.id, \ |
---|
132 | 'doc_hash': docHash}) |
---|
133 | |
---|
134 | #Has to updated the hash? |
---|
135 | if not self._dataEntityHasSameHash and hasMOBeenProcessed(self._dataEntityMigration): |
---|
136 | docHash = getAtomDocumentHashByMO(self._dataEntityMigration) |
---|
137 | self.epbRepo.migrationEPB.updateMigrationObject(self._dataEntityMigration, \ |
---|
138 | {'doc_hash': docHash}) |
---|
139 | |
---|
140 | #Has a proper CEDAGUID? |
---|
141 | if self.epbRepo.moles3EPB.retrieveGUIDFromInstance(ceda_observationCollection) is None: |
---|
142 | #Adds the CedaGUID |
---|
143 | ceda_guid = CedaGUID() |
---|
144 | ceda_guid.id = calculateHash(self._dataEntityMigration.data_ent_id) |
---|
145 | setattr(ceda_guid, 'ceda_observationcollection', ceda_observationCollection.id) |
---|
146 | self.epbRepo.moles3EPB.persistInstance(ceda_guid) |
---|
147 | DataEntityProcessor.log.info("GUID for this ObservationCollection: %s" % (ceda_guid.id)) |
---|
148 | |
---|
149 | def _processDOI(self, deploymentMigration, ceda_observation, deProcessor, single_deployment): |
---|
150 | doi = findDOIInMigrationDocument(deploymentMigration) |
---|
151 | if single_deployment: |
---|
152 | if doi is None: |
---|
153 | doi = findDOIInMigrationDocument(self._dataEntityMigration) |
---|
154 | #collection_identifier = Moles3EPB.extractCollectionIdentifierByTitle(MD_CODE_MOLES2_CITATION, self.migrationSessions.molesSession) |
---|
155 | #if collection_identifier.count()==1: |
---|
156 | # ceda_observation.identifier.append(collection_identifier.first()) |
---|
157 | deProcessor.assignDOI(ceda_observation, doi) |
---|
158 | |
---|
159 | def _processDeploymentMigration(self, deploymentMigration, single_deployment): |
---|
160 | deProcessor = DeploymentProcessor(self._dataEntityMigration, deploymentMigration, self.epbRepo) |
---|
161 | try: |
---|
162 | DataEntityProcessor.log.info("Processing deployment: %s" % (migrationObjectDescription(deploymentMigration))) |
---|
163 | ceda_observation = deProcessor.process() |
---|
164 | try: |
---|
165 | self._processDOI(deploymentMigration, ceda_observation, deProcessor, single_deployment) |
---|
166 | except Exception as ex: |
---|
167 | pass |
---|
168 | except NoAssociatedAuthor as ex: |
---|
169 | raise ex |
---|
170 | except Exception as ex: |
---|
171 | #self.migrationSessions.molesSession.rollback() |
---|
172 | #self.migrationSessions.migrationSession.rollback() |
---|
173 | raise MigrationObjectException(ex) |
---|
174 | |
---|
175 | return ceda_observation |
---|
176 | |
---|
177 | def process(self): |
---|
178 | obsColl = None |
---|
179 | exs = [] |
---|
180 | DataEntityProcessor.log.info("Processing dataEntity: %s" % (migrationObjectDescription(self._dataEntityMigration))) |
---|
181 | try : |
---|
182 | if self._dataEntityMigration.ceda_observation_coll_id: |
---|
183 | #obsColl = self.epbRepo.moles3EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id) |
---|
184 | obsColl = self.epbRepo.moles3EPB.searchSelectiveLoad(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id, \ |
---|
185 | ['identifier']) |
---|
186 | else: |
---|
187 | obsColl = CEDA_ObservationCollection() |
---|
188 | self._execute(obsColl) |
---|
189 | except Exception as ex: |
---|
190 | exs.append(ex) |
---|
191 | return exs |
---|
192 | |
---|
193 | #retrieves the associated deployment links from the data_entity |
---|
194 | deploymentsLinks = findDeploymentsInDE(self._dataEntityMigration) |
---|
195 | #retrieves the DataEntityMigration sorted by creation date |
---|
196 | deploymentMigrations = self.epbRepo.migrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate( \ |
---|
197 | self._dataEntityMigration, deploymentsLinks) |
---|
198 | |
---|
199 | if deploymentMigrations is None or len(deploymentMigrations) == 0: |
---|
200 | return exs |
---|
201 | |
---|
202 | howManydm = 0 |
---|
203 | if deploymentMigrations: |
---|
204 | howManydm = len(deploymentMigrations) |
---|
205 | if howManydm == 0: |
---|
206 | exs.append(NoAssociatedDeployments(self._dataEntityMigration)) |
---|
207 | for deploymentMigration in deploymentMigrations: |
---|
208 | try: |
---|
209 | ceda_observation = self._processDeploymentMigration(deploymentMigration, howManydm == 1) |
---|
210 | #Is a first time process? |
---|
211 | if not self.epbRepo.moles3EPB.observationCollectionHasObservation(getattr(obsColl, 'id'), getattr(ceda_observation, 'id')): |
---|
212 | self.epbRepo.moles3EPB.updateCedaObject(obsColl, {'member': ceda_observation}) |
---|
213 | |
---|
214 | except Exception as ex: |
---|
215 | exs.append(ex) |
---|
216 | |
---|
217 | self._checkGeographicExtent(obsColl) |
---|
218 | |
---|
219 | return exs |
---|