1 | ''' |
---|
2 | Created on 10 Jan 2012 |
---|
3 | |
---|
4 | @author: mnagni |
---|
5 | ''' |
---|
6 | from libs.epb import EPB |
---|
7 | from libs.migration.exception.exceptions import NoDBManager |
---|
8 | from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection import CEDA_ObservationCollection |
---|
9 | from ea_model.ceda_metadatamodel.ceda_observation.ceda_observation import CEDA_Observation |
---|
10 | from libs.migration.processor.check.check import moles3Append |
---|
11 | from libs.migration.processor.commons import createDQ_Element |
---|
12 | from ea_model.iso_19115_2006_metadata_corrigendum.reference_system_information.md_identifier import MD_Identifier |
---|
13 | from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_citation import CI_Citation |
---|
14 | from ea_model.moles3_4.observationcollection.mo_observationcollection import MO_ObservationCollection |
---|
15 | |
---|
16 | #molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT) |
---|
17 | |
---|
18 | class Moles3EPB(EPB): |
---|
19 | |
---|
20 | _molesDB = None |
---|
21 | #_migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT) |
---|
22 | |
---|
23 | @classmethod |
---|
24 | def overrrideDBManager(self, dbManager): |
---|
25 | """ |
---|
26 | Sets the MigrationEPB libs.commons_db.DbManager |
---|
27 | """ |
---|
28 | Moles3EPB._molesDB = dbManager |
---|
29 | |
---|
30 | @classmethod |
---|
31 | def search(self, clazz, inst_id, session = None): |
---|
32 | intSession = Moles3EPB._getSession(session) |
---|
33 | return EPB.search(clazz, inst_id, intSession) |
---|
34 | |
---|
35 | @classmethod |
---|
36 | def searchEager(self, clazz, inst_id, session = None): |
---|
37 | intSession = Moles3EPB._getSession(session) |
---|
38 | return EPB.searchEager(clazz, inst_id, intSession) |
---|
39 | |
---|
40 | @classmethod |
---|
41 | def addCedaObject(self, ceda_observation, session = None, commit = False): |
---|
42 | """ |
---|
43 | Adds and eventually commit a CEDA Object in MOLES3 db |
---|
44 | @param ceda_observation: the CEDA object to persist |
---|
45 | @param session: the external session to use. If None a new session will be open to add and commit the object and then closed at the exit. The object is committed |
---|
46 | @param commit: defines if the object has to be committed immediately or not. |
---|
47 | """ |
---|
48 | intSession = Moles3EPB._getSession(session) |
---|
49 | intSession.add(ceda_observation) |
---|
50 | if commit: |
---|
51 | intSession.commit() |
---|
52 | #Moles3EPB._closeSession(session) |
---|
53 | |
---|
54 | |
---|
55 | @classmethod |
---|
56 | def observationCollectionHasObservation(self, obs_coll_id, obs_id, session = None): |
---|
57 | """ |
---|
58 | Checks if a CEDA_Collection contains a given CEDA_Observation. |
---|
59 | @param obs_coll_id: the CEDA_ObservationColleciton id |
---|
60 | @param obs_id: the CEDA_Observation id |
---|
61 | @return: True if the collection contains the given observation, False otherwise |
---|
62 | """ |
---|
63 | intSession = Moles3EPB._getSession(session) |
---|
64 | return intSession.query(CEDA_ObservationCollection, CEDA_Observation).filter(CEDA_ObservationCollection.id==obs_coll_id).filter(CEDA_Observation.id==obs_id).count() > 0 |
---|
65 | |
---|
66 | @classmethod |
---|
67 | def extractCollectionIdentifierByTitle(self, i_title, session = None): |
---|
68 | """ |
---|
69 | Loooks for an MD_Identifier from a CEDA_ObservationCollection contains a specific title (observation.identifier.code) |
---|
70 | @param i_title: the CEDA_ObservationCollection.identifier.title value to search for |
---|
71 | @return: a tuple containing a CEDA_ObservationCollection and the CEDA_ObservationCollection.idenfitier element having the title |
---|
72 | """ |
---|
73 | intSession = Moles3EPB._getSession(session) |
---|
74 | return intSession.query(CEDA_ObservationCollection, MD_Identifier). \ |
---|
75 | join(MO_ObservationCollection).join(MO_ObservationCollection.identifier). \ |
---|
76 | join(MD_Identifier.authority).filter(CI_Citation.title.like('%' + i_title + '%')) |
---|
77 | |
---|
78 | @classmethod |
---|
79 | def addObservationToObservationCollection(self, observationCollection, observation, session = None, commit = False): |
---|
80 | """ |
---|
81 | Adds an Observation instance to an ObservationCollection if still not part of the collection. |
---|
82 | @param observationCollection: the collection to update |
---|
83 | @param observation: the observation to add |
---|
84 | @param session: the session to use for the operation |
---|
85 | @param commit: if True commits at the end (defaul False) |
---|
86 | """ |
---|
87 | intSession = Moles3EPB._getSession(session) |
---|
88 | if not self.observationCollectionHasObservation(observationCollection.id, observation.id, intSession): |
---|
89 | moles3Append(observationCollection, 'member', observation) |
---|
90 | if commit: |
---|
91 | intSession.commit() |
---|
92 | |
---|
93 | @classmethod |
---|
94 | def addDataLineageToObservation(self, data_lineage, observation, session = None, commit = False): |
---|
95 | """ |
---|
96 | Adds an data_lineage element to an Observation if still not assigned or not equal. |
---|
97 | @param data_lineage: the quality string to persist |
---|
98 | @param observation: the observation to update |
---|
99 | @param session: the session to use for the operation |
---|
100 | @param commit: if True commits at the end (defaul False) |
---|
101 | """ |
---|
102 | intSession = Moles3EPB._getSession(session) |
---|
103 | if observation.dataLineage != data_lineage: |
---|
104 | observation.dataLineage = data_lineage |
---|
105 | if commit: |
---|
106 | intSession.commit() |
---|
107 | |
---|
108 | @classmethod |
---|
109 | def addDescriptionToObservation(self, description, observation, session = None, commit = False): |
---|
110 | """ |
---|
111 | Adds an description element to an Observation if still not assigned or not equal. |
---|
112 | @param description: the description string to persist |
---|
113 | @param observation: the observation to update |
---|
114 | @param session: the session to use for the operation |
---|
115 | @param commit: if True commits at the end (defaul False) |
---|
116 | """ |
---|
117 | intSession = Moles3EPB._getSession(session) |
---|
118 | if observation.description != description: |
---|
119 | observation.description = description |
---|
120 | if commit: |
---|
121 | intSession.commit() |
---|
122 | |
---|
123 | @classmethod |
---|
124 | def addRelatedPartyInfoToObservation(self, responsibleParty, observation, session = None, commit = False): |
---|
125 | """ |
---|
126 | Adds a relatedParty element to an Observation if still not assigned or not equal. |
---|
127 | @param responsibleParty: the responsibleParty element to persist |
---|
128 | @param observation: the observation to update |
---|
129 | @param session: the session to use for the operation |
---|
130 | @param commit: if True commits at the end (defaul False) |
---|
131 | """ |
---|
132 | intSession = Moles3EPB._getSession(session) |
---|
133 | if not responsibleParty in observation.relatedParty: |
---|
134 | observation.relatedParty.append(responsibleParty) |
---|
135 | #moles3Append(observation, 'relatedParty', responsibleParty) |
---|
136 | if commit: |
---|
137 | intSession.commit() |
---|
138 | |
---|
139 | @classmethod |
---|
140 | def addIdentifierToObservation(self, identifier, observation, session = None, commit = False): |
---|
141 | """ |
---|
142 | Adds an identifier element to an Observation if still not assigned or not equal. |
---|
143 | @param identifier: the responsibleParty element to persist |
---|
144 | @param observation: the observation to update |
---|
145 | @param session: the session to use for the operation |
---|
146 | @param commit: if True commits at the end (defaul False) |
---|
147 | """ |
---|
148 | intSession = Moles3EPB._getSession(session) |
---|
149 | if not identifier in observation.identifier: |
---|
150 | #observation.identifier.append(identifier) |
---|
151 | moles3Append(observation, 'identifier', identifier) |
---|
152 | if commit: |
---|
153 | intSession.commit() |
---|
154 | |
---|
155 | @classmethod |
---|
156 | def getNewMolesSession(self): |
---|
157 | return Moles3EPB._getSession() |
---|
158 | |
---|
159 | @classmethod |
---|
160 | def executeNative(self, sqlNative, session = None): |
---|
161 | intSession = Moles3EPB._getSession(session) |
---|
162 | return EPB.executeNative(sqlNative, intSession) |
---|
163 | |
---|
164 | @classmethod |
---|
165 | def _getSession(self, session = None): |
---|
166 | if Moles3EPB._molesDB is None: |
---|
167 | raise NoDBManager("Moles3EPB") |
---|
168 | return EPB._getSession(Moles3EPB._molesDB, session) |
---|