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

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

Global commit. In future Mylyn should manage a better per-ticket commit policy

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