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

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

Implementation of tickets #22390

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