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 ea_model.iso_19115_2006_metadata_corrigendum.reference_system_information.md_identifier import MD_Identifier |
---|
11 | from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_citation import CI_Citation |
---|
12 | from ea_model.moles3_4.observationcollection.mo_observationcollection import MO_ObservationCollection |
---|
13 | from ea_model.moles3_4.observation.mo_observation import MO_Observation |
---|
14 | from ea_model.ceda_metadatamodel.ceda_project.ceda_project import CEDA_Project |
---|
15 | from sqlalchemy import Table, Column, ForeignKey, Integer, String |
---|
16 | from sqlalchemy.orm import mapper |
---|
17 | from MolesManager.ceda_guid import CedaGUID |
---|
18 | |
---|
19 | |
---|
20 | class Moles3EPB(EPB): |
---|
21 | |
---|
22 | _molesDB = None |
---|
23 | |
---|
24 | @classmethod |
---|
25 | def overrrideDBManager(self, dbManager): |
---|
26 | """ |
---|
27 | Sets the MigrationEPB libs.commons_db.DbManager |
---|
28 | """ |
---|
29 | Moles3EPB._molesDB = dbManager |
---|
30 | Moles3EPB._initCEDA_Customization() |
---|
31 | |
---|
32 | @classmethod |
---|
33 | def _initCEDA_Customization(self): |
---|
34 | Moles3EPB._associateCEDA_GUID() |
---|
35 | Moles3EPB._initSearchIndexes() |
---|
36 | |
---|
37 | @classmethod |
---|
38 | def _associateCEDA_GUID(self): |
---|
39 | guid_table = Table('ceda_guid', Moles3EPB._molesDB.metadata, \ |
---|
40 | Column('id', String, primary_key=True), \ |
---|
41 | Column('ceda_observationcollection', Integer, ForeignKey('ceda_observationcollection.id')), |
---|
42 | Column('ceda_observation', Integer, ForeignKey('ceda_observation.id'))) |
---|
43 | mapper(CedaGUID, guid_table) |
---|
44 | Moles3EPB._molesDB.metadata.create_all() |
---|
45 | |
---|
46 | @classmethod |
---|
47 | def _initSearchIndexes(self): |
---|
48 | #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS! |
---|
49 | # We don't want sqlalchemy to know about this column so we add it externally. |
---|
50 | try: |
---|
51 | Moles3EPB._molesDB.engine.execute("alter table md_identifier add column code_search_vector tsvector") |
---|
52 | |
---|
53 | # This indexes the tsvector column |
---|
54 | |
---|
55 | Moles3EPB._molesDB.engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)") |
---|
56 | |
---|
57 | # This sets up the trigger that keeps the tsvector column up to date. |
---|
58 | Moles3EPB._molesDB.engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \ |
---|
59 | for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', code)") |
---|
60 | except Exception as e: |
---|
61 | pass |
---|
62 | |
---|
63 | @classmethod |
---|
64 | def search(self, clazz, inst_id, session = None): |
---|
65 | intSession = Moles3EPB._getSession(session) |
---|
66 | return EPB.search(clazz, inst_id, intSession) |
---|
67 | |
---|
68 | @classmethod |
---|
69 | def searchEager(self, clazz, inst_id, session = None): |
---|
70 | intSession = Moles3EPB._getSession(session) |
---|
71 | return EPB.searchEager(clazz, inst_id, intSession) |
---|
72 | |
---|
73 | @classmethod |
---|
74 | def addCedaObject(self, ceda_observation, session = None, commit = False): |
---|
75 | """ |
---|
76 | Adds and eventually commit a CEDA Object in MOLES3 db |
---|
77 | @param ceda_observation: the CEDA object to persist |
---|
78 | @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 |
---|
79 | @param commit: defines if the object has to be committed immediately or not. |
---|
80 | """ |
---|
81 | intSession = Moles3EPB._getSession(session) |
---|
82 | intSession.add(ceda_observation) |
---|
83 | if commit: |
---|
84 | intSession.commit() |
---|
85 | #Moles3EPB._closeSession(session) |
---|
86 | |
---|
87 | @classmethod |
---|
88 | def retrieveGUIDFromInstance(self, instance, session = None): |
---|
89 | """ |
---|
90 | Returns the CedaGUID object associated with the given instance. |
---|
91 | @param instance: an instance of CEDA_Observation os CEDA_ObservationCollection |
---|
92 | """ |
---|
93 | intSession = Moles3EPB._getSession(session) |
---|
94 | if instance is None: |
---|
95 | return None |
---|
96 | if type(instance) == CEDA_ObservationCollection: |
---|
97 | return intSession.query(CedaGUID).filter(CedaGUID.ceda_observationcollection==instance.id).first() |
---|
98 | elif type(instance) == CEDA_Observation: |
---|
99 | return intSession.query(CedaGUID).filter(CedaGUID.ceda_observation==instance.id).first() |
---|
100 | |
---|
101 | |
---|
102 | @classmethod |
---|
103 | def observationCollectionHasObservation(self, obs_coll_id, obs_id, session = None): |
---|
104 | """ |
---|
105 | Checks if a CEDA_Collection contains a given CEDA_Observation. |
---|
106 | @param obs_coll_id: the CEDA_ObservationColleciton id |
---|
107 | @param obs_id: the CEDA_Observation id |
---|
108 | @return: True if the collection contains the given observation, False otherwise |
---|
109 | """ |
---|
110 | intSession = Moles3EPB._getSession(session) |
---|
111 | return intSession.query(CEDA_ObservationCollection, CEDA_Observation).filter(CEDA_ObservationCollection.id==obs_coll_id).filter(CEDA_Observation.id==obs_id).count() > 0 |
---|
112 | |
---|
113 | @classmethod |
---|
114 | def extractObservationByTitleKeywords(self, keywords, session = None): |
---|
115 | """ |
---|
116 | Loooks for CEDA_Observation containing a specific title (observation.identifier.code) |
---|
117 | @param keywords: a space separated terms string |
---|
118 | @return: a tuple containing a CEDA_Observation satisfying the queryllection.idenfitier element having the title |
---|
119 | """ |
---|
120 | intSession = Moles3EPB._getSession(session) |
---|
121 | # search_vector is a ts_vector column. To search for terms, you use the |
---|
122 | # @@ operator. plainto_tsquery turns a string into a query that can be |
---|
123 | # used with @@. So this adds a where clause like "WHERE search_vector |
---|
124 | # @@ plaint_tsquery(<search string>)" |
---|
125 | q = intSession.query(CEDA_Observation). \ |
---|
126 | join(MO_Observation).join(MO_ObservationCollection.identifier). \ |
---|
127 | filter('md_identifier.code_search_vector @@ to_tsquery(:terms)') |
---|
128 | # This binds the :terms placeholder to the searchterms string. User input |
---|
129 | # should always be put into queries this way to prevent SQL injection. |
---|
130 | q = q.params(terms=keywords) |
---|
131 | return q |
---|
132 | |
---|
133 | |
---|
134 | @classmethod |
---|
135 | def extractCollectionIdentifierByTitle(self, i_title, session = None): |
---|
136 | """ |
---|
137 | Searches for an MD_Identifier from a CEDA_ObservationCollection contains a specific title (observation.identifier.code) |
---|
138 | @param i_title: the CEDA_ObservationCollection.identifier.title value to search for |
---|
139 | @return: a tuple containing a CEDA_ObservationCollection and the CEDA_ObservationCollection.idenfitier element having the title |
---|
140 | """ |
---|
141 | intSession = Moles3EPB._getSession(session) |
---|
142 | return intSession.query(CEDA_ObservationCollection, MD_Identifier). \ |
---|
143 | join(MO_ObservationCollection).join(MO_ObservationCollection.identifier). \ |
---|
144 | join(MD_Identifier.authority).filter(CI_Citation.title.like('%' + i_title + '%')) |
---|
145 | |
---|
146 | @classmethod |
---|
147 | def extractObservationsForProject(self, project, session = None): |
---|
148 | """ |
---|
149 | Searches for the CEDA_Observation associated with a CEDA_Project |
---|
150 | @param project: a CEDA_Project instance |
---|
151 | @return: a tuple containing the associated CEDA_Observation |
---|
152 | """ |
---|
153 | intSession = Moles3EPB._getSession(session) |
---|
154 | return intSession.query(CEDA_Observation). \ |
---|
155 | join(CEDA_Observation, MO_Observation.inSupportOf).filter(CEDA_Project.id == project.id) |
---|
156 | |
---|
157 | @classmethod |
---|
158 | def extractProjectObservationCollections(self, project, session = None): |
---|
159 | """ |
---|
160 | Searches for the Observation_Collections associated with a CEDA_Project |
---|
161 | @param project: a CEDA_Project instance |
---|
162 | @return: a tuple containing the associated CEDA_ObservationCollection |
---|
163 | """ |
---|
164 | intSession = Moles3EPB._getSession(session) |
---|
165 | mo_obs = intSession.query(MO_Observation).join(CEDA_Project).filter(CEDA_Project.id == project.id).subquery() |
---|
166 | obsers = intSession.query(CEDA_Observation).join(mo_obs, CEDA_Observation.id == mo_obs.c.id).one() |
---|
167 | #print "obsers: " + str(intSession.query(CEDA_Observation).join(mo_obs, CEDA_Observation.id == mo_obs.c.id).count()) |
---|
168 | |
---|
169 | cos = intSession.query(CEDA_ObservationCollection).all() |
---|
170 | co = intSession.query(MO_ObservationCollection).join(MO_ObservationCollection.member).filter(MO_ObservationCollection.member.contains(obsers)) |
---|
171 | print co |
---|
172 | print co.all() |
---|
173 | |
---|
174 | observations = intSession.query(MO_ObservationCollection).join(CEDA_Observation). \ |
---|
175 | filter(obsers.any(CEDA_Observation.id==obsers.c.id)) |
---|
176 | print "observation:" + str(observations.count()) |
---|
177 | return observations |
---|
178 | |
---|
179 | @classmethod |
---|
180 | def addObservationToObservationCollection(self, observationCollection, observation, session = None, commit = False): |
---|
181 | """ |
---|
182 | Adds an Observation instance to an ObservationCollection if still not part of the collection. |
---|
183 | @param observationCollection: the collection to update |
---|
184 | @param observation: the observation to add |
---|
185 | @param session: the session to use for the operation |
---|
186 | @param commit: if True commits at the end (defaul False) |
---|
187 | """ |
---|
188 | intSession = Moles3EPB._getSession(session) |
---|
189 | if not self.observationCollectionHasObservation(observationCollection.id, observation.id, intSession): |
---|
190 | observationCollection.member.append(observation) |
---|
191 | if commit: |
---|
192 | intSession.commit() |
---|
193 | |
---|
194 | @classmethod |
---|
195 | def addDataLineageToObservation(self, data_lineage, observation, session = None, commit = False): |
---|
196 | """ |
---|
197 | Adds an data_lineage element to an Observation if still not assigned or not equal. |
---|
198 | @param data_lineage: the quality string to persist |
---|
199 | @param observation: the observation to update |
---|
200 | @param session: the session to use for the operation |
---|
201 | @param commit: if True commits at the end (defaul False) |
---|
202 | """ |
---|
203 | intSession = Moles3EPB._getSession(session) |
---|
204 | if observation.dataLineage != data_lineage: |
---|
205 | observation.dataLineage = data_lineage |
---|
206 | if commit: |
---|
207 | intSession.commit() |
---|
208 | |
---|
209 | @classmethod |
---|
210 | def addDescriptionToObservation(self, description, observation, session = None, commit = False): |
---|
211 | """ |
---|
212 | Adds an description element to an Observation if still not assigned or not equal. |
---|
213 | @param description: the description string to persist |
---|
214 | @param observation: the observation to update |
---|
215 | @param session: the session to use for the operation |
---|
216 | @param commit: if True commits at the end (defaul False) |
---|
217 | """ |
---|
218 | intSession = Moles3EPB._getSession(session) |
---|
219 | if observation.description != description: |
---|
220 | observation.description = description |
---|
221 | if commit: |
---|
222 | intSession.commit() |
---|
223 | |
---|
224 | @classmethod |
---|
225 | def addRelatedPartyInfoToObservation(self, responsibleParty, observation, session = None, commit = False): |
---|
226 | """ |
---|
227 | Adds a relatedParty element to an Observation if still not assigned or not equal. |
---|
228 | @param responsibleParty: the responsibleParty element to persist |
---|
229 | @param observation: the observation to update |
---|
230 | @param session: the session to use for the operation |
---|
231 | @param commit: if True commits at the end (defaul False) |
---|
232 | """ |
---|
233 | intSession = Moles3EPB._getSession(session) |
---|
234 | if not responsibleParty in observation.relatedParty: |
---|
235 | observation.relatedParty.append(responsibleParty) |
---|
236 | #moles3Append(observation, 'relatedParty', responsibleParty) |
---|
237 | if commit: |
---|
238 | intSession.commit() |
---|
239 | |
---|
240 | |
---|
241 | @classmethod |
---|
242 | def getNewMolesSession(self): |
---|
243 | return Moles3EPB._getSession() |
---|
244 | |
---|
245 | @classmethod |
---|
246 | def executeNative(self, sqlNative, session = None): |
---|
247 | intSession = Moles3EPB._getSession(session) |
---|
248 | return EPB.executeNative(sqlNative, intSession) |
---|
249 | |
---|
250 | @classmethod |
---|
251 | def _getSession(self, session = None): |
---|
252 | if Moles3EPB._molesDB is None: |
---|
253 | raise NoDBManager("Moles3EPB") |
---|
254 | return EPB._getSession(Moles3EPB._molesDB, session) |
---|