source: mauRepo/MolesManager/trunk/src/MolesManager/moles3epb.py @ 8205

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/MolesManager/moles3epb.py@8205
Revision 8205, 9.1 KB checked in by mnagni, 9 years ago (diff)

Complete - # 22416: CEDA MOLES3 - search function - by title keyword
 http://team.ceda.ac.uk/trac/ceda/ticket/22416

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