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

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

Complete - # 22397: CEDA Observation geographicExtent
 http://team.ceda.ac.uk/trac/ceda/ticket/22397

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
12
13#molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT)
14
15class Moles3EPB(EPB):
16
17    _molesDB = None
18    #_migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT)
19   
20    @classmethod
21    def overrrideDBManager(self, dbManager):
22        """
23            Sets the MigrationEPB libs.commons_db.DbManager
24        """
25        Moles3EPB._molesDB = dbManager
26
27    @classmethod       
28    def search(self, clazz, inst_id, session = None):
29        intSession = Moles3EPB._getSession(session)
30        return EPB.search(clazz, inst_id, intSession)
31
32    @classmethod       
33    def searchEager(self, clazz, inst_id, session = None):
34        intSession = Moles3EPB._getSession(session)
35        return EPB.searchEager(clazz, inst_id, intSession)
36
37    @classmethod       
38    def addCedaObject(self, ceda_observation, session = None, commit = False):
39        """
40            Adds and eventually commit a CEDA Object in MOLES3 db
41            @param ceda_observation: the CEDA object to persist
42            @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
43            @param commit: defines if the object has to be committed immediately or not.
44        """
45        intSession = Moles3EPB._getSession(session)
46        intSession.add(ceda_observation)
47        if commit:
48            intSession.commit()
49        #Moles3EPB._closeSession(session)
50
51   
52    @classmethod
53    def observationCollectionHasObservation(self, obs_coll_id, obs_id, session = None):
54        """
55            Checks if a CEDA_Collection contains a given CEDA_Observation.
56            @param obs_coll_id: the CEDA_ObservationColleciton id
57            @param obs_id: the CEDA_Observation id
58            @return: True if the collection contains the given observation, False otherwise 
59        """
60        intSession = Moles3EPB._getSession(session)
61        return intSession.query(CEDA_ObservationCollection, CEDA_Observation).filter(CEDA_ObservationCollection.id==obs_coll_id).filter(CEDA_Observation.id==obs_id).count() > 0
62
63    @classmethod
64    def extractObservationbytitle(self, title, session = None):
65        """
66            Checks if a CEDA_Observation contains a specific title (observation.identifier.code)
67            @param title: the CEDA_Observation title
68            @return: a collection of CEDA_Observation 
69        """
70        intSession = Moles3EPB._getSession(session)
71        return intSession.query(CEDA_Observation).filter(CEDA_Observation.identifier.code.like(title))
72   
73    @classmethod
74    def addObservationToObservationCollection(self, observationCollection, observation, session = None, commit = False):
75        """
76            Adds an Observation instance to an ObservationCollection if still not part of the collection.
77            @param observationCollection: the collection to update
78            @param observation: the observation to add
79            @param session: the session to use for the operation
80            @param commit: if True commits at the end (defaul False)
81        """
82        intSession = Moles3EPB._getSession(session)
83        if not self.observationCollectionHasObservation(observationCollection.id, observation.id, intSession):
84            moles3Append(observationCollection, 'member', observation)
85            if commit:
86                intSession.commit()
87    @classmethod
88    def addDocumentQuality(self, doc_quality, observation, session = None, commit = False):
89        """
90            Adds an resultQuality element to an Observation if still not assigned or not equal.
91            @param doc_quality: the quality string to persist
92            @param observation: the observation to update
93            @param session: the session to use for the operation
94            @param commit: if True commits at the end (defaul False)
95        """       
96        intSession = Moles3EPB._getSession(session)
97        for rq in observation.resultQuality:
98            for res in rq.result:
99                if hasattr(res, 'explaination') and res.explaination == doc_quality:
100                    return True
101                    moles3Append(observation, observation.resultQuality, createDQ_Element(explaination = doc_quality))
102   
103    @classmethod
104    def addDataLineageToObservation(self, data_lineage, observation, session = None, commit = False):
105        """
106            Adds an data_lineage element to an Observation if still not assigned or not equal.
107            @param data_lineage: the quality string to persist
108            @param observation: the observation to update
109            @param session: the session to use for the operation
110            @param commit: if True commits at the end (defaul False)
111        """       
112        intSession = Moles3EPB._getSession(session)
113        if observation.dataLineage != data_lineage:
114            observation.dataLineage = data_lineage
115        if commit:
116            intSession.commit()               
117
118    @classmethod
119    def addDescriptionToObservation(self, description, observation, session = None, commit = False):
120        """
121            Adds an description element to an Observation if still not assigned or not equal.
122            @param description: the description string to persist
123            @param observation: the observation to update
124            @param session: the session to use for the operation
125            @param commit: if True commits at the end (defaul False)
126        """       
127        intSession = Moles3EPB._getSession(session)
128        if observation.description != description:
129            observation.description = description
130        if commit:
131            intSession.commit()
132
133    @classmethod
134    def addRelatedPartyInfoToObservation(self, responsibleParty, observation, session = None, commit = False):
135        """
136            Adds a relatedParty element to an Observation if still not assigned or not equal.
137            @param responsibleParty: the responsibleParty element to persist
138            @param observation: the observation to update
139            @param session: the session to use for the operation
140            @param commit: if True commits at the end (defaul False)
141        """         
142        intSession = Moles3EPB._getSession(session)
143        if not responsibleParty in observation.relatedParty:
144            observation.relatedParty.append(responsibleParty)                           
145            #moles3Append(observation, 'relatedParty', responsibleParty)
146        if commit:
147            intSession.commit()
148
149    @classmethod
150    def addIdentifierToObservation(self, identifier, observation, session = None, commit = False):
151        """
152            Adds an identifier element to an Observation if still not assigned or not equal.
153            @param identifier: the responsibleParty element to persist
154            @param observation: the observation to update
155            @param session: the session to use for the operation
156            @param commit: if True commits at the end (defaul False)
157        """         
158        intSession = Moles3EPB._getSession(session)
159        if not identifier in observation.identifier:
160            observation.identifier.append(identifier)
161            #moles3Append(observation, 'identifier', identifier)
162        if commit:
163            intSession.commit()
164
165    @classmethod       
166    def getNewMolesSession(self):
167            return Moles3EPB._getSession()
168
169    @classmethod
170    def executeNative(self, sqlNative, session = None):
171        intSession = Moles3EPB._getSession(session)
172        return EPB.executeNative(sqlNative, intSession) 
173
174    @classmethod
175    def _getSession(self, session = None):
176        if Moles3EPB._molesDB is None:
177            raise NoDBManager("Moles3EPB")
178        return EPB._getSession(Moles3EPB._molesDB, session)
Note: See TracBrowser for help on using the repository browser.