- Timestamp:
- 01/04/12 18:07:50 (8 years ago)
- Location:
- mauRepo/MolesManager/trunk/src
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
mauRepo/MolesManager/trunk/src/MolesManager/moles3epb.py
r8203 r8205 13 13 from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_citation import CI_Citation 14 14 from ea_model.moles3_4.observationcollection.mo_observationcollection import MO_ObservationCollection 15 from ea_model.moles3_4.observation.mo_observation import MO_Observation 15 16 16 17 #molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT) … … 63 64 intSession = Moles3EPB._getSession(session) 64 65 return intSession.query(CEDA_ObservationCollection, CEDA_Observation).filter(CEDA_ObservationCollection.id==obs_coll_id).filter(CEDA_Observation.id==obs_id).count() > 0 66 67 @classmethod 68 def extractObservationByTitleKeywords(self, keywords, session = None): 69 """ 70 Loooks for CEDA_Observation containing a specific title (observation.identifier.code) 71 @param i_title: a sspace separated terms string 72 @return: a tuple containing a CEDA_Observation satisfying the queryllection.idenfitier element having the title 73 """ 74 intSession = Moles3EPB._getSession(session) 75 # search_vector is a ts_vector column. To search for terms, you use the 76 # @@ operator. plainto_tsquery turns a string into a query that can be 77 # used with @@. So this adds a where clause like "WHERE search_vector 78 # @@ plaint_tsquery(<search string>)" 79 q = intSession.query(CEDA_Observation). \ 80 join(MO_Observation).join(MO_ObservationCollection.identifier). \ 81 filter('md_identifier.code_search_vector @@ to_tsquery(:terms)') 82 # This binds the :terms placeholder to the searchterms string. User input 83 # should always be put into queries this way to prevent SQL injection. 84 q = q.params(terms=keywords) 85 return q 86 65 87 66 88 @classmethod -
mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py
r8155 r8205 11 11 from libs.commons_db import DbManager 12 12 from libs.migration.MigrationEPB import MigrationEPB 13 from ea_model.iso_19115_2006_metadata_corrigendum.reference_system_information.md_identifier import MD_Identifier 13 14 14 15 … … 21 22 22 23 24 def _initSearchIndexes(self, db_manager): 25 #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS! 26 # We don't want sqlalchemy to know about this column so we add it externally. 27 try: 28 db_manager._engine.execute("alter table md_identifier add column code_search_vector tsvector") 29 30 # This indexes the tsvector column 31 db_manager._engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)") 32 except Exception as e: 33 pass 34 35 # This sets up the trigger that keeps the tsvector column up to date. 36 db_manager._engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \ 37 for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', 'code')") 38 23 39 24 40 def _getMolesSession(self): … … 30 46 Moles3EPB.overrrideDBManager(molesDB) 31 47 MolesSessionMiddleware._epbInitialized = True 48 49 self._initSearchIndexes(molesDB) 50 32 51 return Moles3EPB.getNewMolesSession() 33 52 else: -
mauRepo/MolesManager/trunk/src/libs/migration/tests/moles3epbmothods.py
r8198 r8205 11 11 import logging 12 12 from logging import StreamHandler 13 from ea_model.iso_19115_2006_metadata_corrigendum.reference_system_information.md_identifier import MD_Identifier 14 from test_utils import createObservationCollection 15 from ea_model.ceda_metadatamodel.ceda_observationcollection.ceda_observationcollection import CEDA_ObservationCollection 16 from ea_model.moles3_4.observationcollection.mo_observationcollection import MO_ObservationCollection 17 from ea_model.iso_19115_2006_metadata_corrigendum.citation_and_responsible_party_information.ci_citation import CI_Citation 13 from test_utils import createObservationCollection, createObservation 14 from sqlalchemy.dialects import postgres 18 15 19 16 … … 23 20 Moles3EPBTest.molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT) 24 21 Moles3EPB.overrrideDBManager(Moles3EPBTest.molesDB) 22 self._initSearchIndexes(Moles3EPBTest.molesDB) 25 23 self.logging = logging.getLogger('Moles3EPBTest') 26 24 self.logging.addHandler(StreamHandler()) … … 32 30 33 31 def runTest(self): 34 self.checkExtractCollectionIdentifierByTitle() 32 self.checkExtractCollectionIdentifierByTitle() 33 self.checkExtractObservationByTitleKeywords() 35 34 36 35 def checkExtractCollectionIdentifierByTitle(self): … … 51 50 session.commit() 52 51 52 def checkExtractObservationByTitleKeywords(self): 53 session = Moles3EPB.getNewMolesSession(); 54 observation = createObservation() 55 56 self.logging.info('Stores an new CEDA_Observation') 57 Moles3EPB.addCedaObject(observation, session) 58 session.commit() 59 60 obs = Moles3EPB.extractObservationByTitleKeywords('test_code', session) 61 self.assertTrue(obs.count() > 1, "Cannot find a CEDA_Observation") 62 obs = Moles3EPB.extractObservationByTitleKeywords('dummy_code', session) 63 self.assertTrue(obs.count() == 0, "Should NOT find a CEDA_Observation") 64 65 def _initSearchIndexes(self, db_manager): 66 #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS! 67 # We don't want sqlalchemy to know about this column so we add it externally. 68 try: 69 db_manager._engine.execute("alter table md_identifier add column code_search_vector tsvector") 70 71 # This indexes the tsvector column 72 db_manager._engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)") 73 74 # This sets up the trigger that keeps the tsvector column up to date. 75 db_manager._engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \ 76 for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', 'code')") 77 except Exception as e: 78 pass 79 80 81 53 82 if __name__ == "__main__": 54 83 #import sys;sys.argv = ['', 'Test.testName'] -
mauRepo/MolesManager/trunk/src/libs/migration/tests/test_utils.py
r8198 r8205 7 7 from libs.migration.processor.commons import createMD_Identifier,\ 8 8 createCI_Citation 9 from ea_model.ceda_metadatamodel.ceda_observation.ceda_observation import CEDA_Observation 9 10 10 11 def createObservationCollection(): … … 19 20 observationCollection.identifier.append(i_identifier) 20 21 return observationCollection 22 23 def createObservation(): 24 ''' 25 Creates a CEDA_Observation with the following data: 26 oc.identifier[0].code = 'test_code' 27 oc.identifier[0].authority.title = 'test_title' 28 oc.identifier[1].code = 'mau_code' 29 oc.identifier[1].authority.title = 'mau_title' 30 ''' 31 observation = CEDA_Observation() 32 i_identifier = createMD_Identifier(code = 'test_code', authority=createCI_Citation('test_title')) 33 observation.identifier.append(i_identifier) 34 i_identifier = createMD_Identifier(code = 'mau_code', authority=createCI_Citation('mau_title')) 35 observation.identifier.append(i_identifier) 36 observation.dataLineage = "test_dataLineage" 37 return observation
Note: See TracChangeset
for help on using the changeset viewer.