Changeset 8325 for mauRepo/MolesManager


Ignore:
Timestamp:
03/06/12 20:29:12 (8 years ago)
Author:
mnagni
Message:

Great improvement on how SA alchemy session is managed

Location:
mauRepo/MolesManager/trunk/src
Files:
1 added
11 edited

Legend:

Unmodified
Added
Removed
  • mauRepo/MolesManager/trunk/src/MolesManager/moles3epb.py

    r8323 r8325  
    1919from ea_model.moles3_4.utilities.mo_rolevalue import MO_RoleValue 
    2020from sqlalchemy.orm.collections import InstrumentedList 
    21 from sqlalchemy.orm.util import identity_key 
    22  
    23  
    24 class Moles3EPB(EPB): 
    25  
    26     _molesDB = None 
     21 
     22 
     23class Moles3EPBFactory(EPB): 
    2724    
    28     @classmethod 
    29     def overrrideDBManager(self, dbManager): 
    30         """ 
    31             Sets the MigrationEPB libs.commons_db.DbManager 
    32         """ 
    33         Moles3EPB._molesDB = dbManager 
    34         Moles3EPB._initCEDA_Customization() 
    35  
    36     @classmethod 
     25    def __init__(self, dbManager): 
     26        self._dbManager = dbManager 
     27        self._initCEDA_Customization()    
     28         
     29 
    3730    def _initCEDA_Customization(self): 
    38         Moles3EPB._associateCEDA_GUID() 
    39         Moles3EPB._initSearchIndexes()         
    40          
    41     @classmethod 
     31        self._associateCEDA_GUID() 
     32        self._initSearchIndexes()         
     33         
    4234    def _associateCEDA_GUID(self): 
    43         guid_table = Table('ceda_guid', Moles3EPB._molesDB.metadata, \ 
     35        guid_table = Table('ceda_guid', self._dbManager.metadata, \ 
    4436                           Column('id', String, primary_key=True), \ 
    4537                           Column('ceda_observationcollection', Integer, ForeignKey('ceda_observationcollection.id')),  
    4638                           Column('ceda_observation', Integer, ForeignKey('ceda_observation.id'))) 
    4739        mapper(CedaGUID, guid_table) 
    48         Moles3EPB._molesDB.metadata.create_all() 
    49  
    50     @classmethod 
     40        self._dbManager.metadata.create_all() 
     41 
    5142    def _initSearchIndexes(self): 
    5243        #To Be Done - CHECK IF THE COLUMN ALREADY EXISTS! 
    5344        # We don't want sqlalchemy to know about this column so we add it externally. 
    5445        try: 
    55             Moles3EPB._molesDB.engine.execute("alter table md_identifier add column code_search_vector tsvector")                  
     46            self._dbManager.engine.execute("alter table md_identifier add column code_search_vector tsvector")                  
    5647 
    5748            # This indexes the tsvector column 
    5849 
    59             Moles3EPB._molesDB.engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)") 
     50            self._dbManager.engine.execute("create index md_identifier_code_search_index on md_identifier using gin(code_search_vector)") 
    6051 
    6152            # This sets up the trigger that keeps the tsvector column up to date. 
    62             Moles3EPB._molesDB.engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \ 
     53            self._dbManager.engine.execute("create trigger md_identifier_code_search_update before update or insert on md_identifier \ 
    6354                for each row execute procedure tsvector_update_trigger('code_search_vector', 'pg_catalog.english', code)")                         
    6455        except Exception as e: 
    6556            pass 
    6657 
    67     @classmethod         
    68     def searchEager(self, clazz, inst_id, session = None): 
    69         intSession = Moles3EPB._getSession(session) 
    70         ret = EPB.searchEager(clazz, inst_id, intSession) 
    71         if session is None: 
    72             intSession.close() 
    73         return ret      
    74  
    75     @classmethod         
    76     def persistInstance(self, instance, session = None): 
     58    def _getSession(self): 
     59        if self._dbManager is not None: 
     60            return self._dbManager.createDbSession()                 
     61        return None 
     62         
     63    def createMoles3EPB(self): 
     64        return Moles3EPB(self._getSession()) 
     65 
     66class Moles3EPB(object): 
     67 
     68    def __init__(self, session): 
     69        self._session = session 
     70         
     71    def close(self): 
     72        return self._session.close()         
     73         
     74    def searchEager(self, clazz, inst_id): 
     75        return EPB.searchEager(clazz, inst_id)      
     76      
     77    def persistInstance(self, instance): 
    7778        """ 
    7879            Adds a new migration object. 
     
    8283            @return an updated, session independent, object instance reflecting the new persisted object  
    8384        """       
    84         intSession = Moles3EPB._getSession(session) 
    85         EPB.persistInstance(instance, intSession) 
    86         if session is None: 
    87             intSession.close() 
    88         #return ret 
    89          
    90  
    91     @classmethod         
    92     def updateCedaObject(self, ceda_object, cols_to_update, session = None): 
     85        EPB.persistInstance(instance, self._session) 
     86         
     87       
     88    def updateCedaObject(self, ceda_object, cols_to_update): 
    9389        """ 
    9490            Update and eventually commit a CEDA Object in MOLES3 db. 
     
    9995            @return: the given instance with the updated attributes. 
    10096        """ 
    101         intSession = Moles3EPB._getSession(session) 
    10297        coll = None 
    10398        try: 
    104             coll = intSession.merge(ceda_object) 
     99            coll = self._session.merge(ceda_object) 
    105100        except Exception as e: 
    106101            print e 
     
    110105                    val = None 
    111106                    try: 
    112                         val = intSession.merge(v) 
     107                        val = self._session.merge(v) 
    113108                    except Exception: 
    114109                        val = v 
     
    123118                    else: 
    124119                        setattr(coll, k, val) 
    125         EPB.persistInstance(coll, intSession)                          
    126         if session is None: 
    127             intSession.close()           
    128  
    129     @classmethod 
    130     def retrieveGUIDFromInstance(self, instance, session = None): 
     120        EPB.persistInstance(coll, self._session)                                  
     121 
     122    def retrieveGUIDFromInstance(self, instance): 
    131123        """ 
    132124            Returns the CedaGUID object associated with the given instance. 
    133125            @param instance: an instance of CEDA_Observation os CEDA_ObservationCollection   
    134126        """ 
    135         intSession = Moles3EPB._getSession(session) 
    136127        if instance is None: 
    137128            return None 
    138129        if type(instance) == CEDA_ObservationCollection: 
    139             return intSession.query(CedaGUID).filter(CedaGUID.ceda_observationcollection==instance.id).first() 
     130            return self._session.query(CedaGUID).filter(CedaGUID.ceda_observationcollection==instance.id).first() 
    140131        elif type(instance) == CEDA_Observation: 
    141             return intSession.query(CedaGUID).filter(CedaGUID.ceda_observation==instance.id).first() 
    142         if session is None: 
    143             intSession.close()           
     132            return self._session.query(CedaGUID).filter(CedaGUID.ceda_observation==instance.id).first()         
    144133     
    145     @classmethod 
    146     def observationCollectionHasObservation(self, obs_coll_id, obs_id, session = None): 
     134    def observationCollectionHasObservation(self, obs_coll_id, obs_id): 
    147135        """ 
    148136            Checks if a CEDA_Collection contains a given CEDA_Observation. 
     
    151139            @return: True if the collection contains the given observation, False otherwise   
    152140        """ 
    153         intSession = Moles3EPB._getSession(session) 
    154         ret = intSession.query(CEDA_ObservationCollection, CEDA_Observation).filter(CEDA_ObservationCollection.id==obs_coll_id).filter(CEDA_Observation.id==obs_id).count() > 0 
    155         if session is None: 
    156             intSession.close() 
     141        ret = self._session.query(CEDA_ObservationCollection, CEDA_Observation).filter(CEDA_ObservationCollection.id==obs_coll_id).filter(CEDA_Observation.id==obs_id).count() > 0 
    157142        return ret 
    158143 
    159     @classmethod 
    160     def observationAuthor(self, obs_id, session = None): 
     144    def observationAuthor(self, obs_id): 
    161145        """ 
    162146            Lists the CEDA_Observation author. 
     
    164148            @return: True if the collection contains the given observation, False otherwise   
    165149        """ 
    166         intSession = Moles3EPB._getSession(session) 
    167         ret = intSession.query(MO_ResponsiblePartyInfo).join(MO_Observation). \ 
     150        ret = self._session.query(MO_ResponsiblePartyInfo).join(MO_Observation). \ 
    168151            filter(MO_ResponsiblePartyInfo.role == MO_RoleValue.cl_author). \ 
    169             filter(MO_Observation.id == obs_id) 
    170         if session is None: 
    171             intSession.close()          
     152            filter(MO_Observation.id == obs_id)        
    172153        return ret 
    173154 
    174     @classmethod 
    175     def extractObservationByTitleKeywords(self, keywords, session = None): 
     155    def extractObservationByTitleKeywords(self, keywords): 
    176156        """ 
    177157            Loooks for CEDA_Observation containing a specific title (observation.identifier.code) 
     
    179159            @return: a tuple containing a CEDA_Observation satisfying the queryllection.idenfitier element having the title   
    180160        """                 
    181         intSession = Moles3EPB._getSession(session) 
    182161        # search_vector is a ts_vector column. To search for terms, you use the  
    183162        # @@ operator. plainto_tsquery turns a string into a query that can be  
    184163        # used with @@. So this adds a where clause like "WHERE search_vector  
    185164        # @@ plaint_tsquery(<search string>)" 
    186         q = intSession.query(CEDA_Observation). \ 
     165        q = self._session.query(CEDA_Observation). \ 
    187166            join(MO_Observation).join(MO_ObservationCollection.identifier). \ 
    188167            filter('md_identifier.code_search_vector @@ to_tsquery(:terms)') 
     
    193172 
    194173 
    195     @classmethod 
    196     def extractCollectionIdentifierByTitle(self, i_title, session = None): 
     174    def extractCollectionIdentifierByTitle(self, i_title): 
    197175        """ 
    198176            Searches for an MD_Identifier from a CEDA_ObservationCollection contains a specific title (observation.identifier.code) 
     
    200178            @return: a tuple containing a CEDA_ObservationCollection and the CEDA_ObservationCollection.idenfitier element having the title   
    201179        """ 
    202         intSession = Moles3EPB._getSession(session) 
    203         return intSession.query(CEDA_ObservationCollection, MD_Identifier). \ 
     180        return self._session.query(CEDA_ObservationCollection, MD_Identifier). \ 
    204181            join(MO_ObservationCollection).join(MO_ObservationCollection.identifier). \ 
    205182            join(MD_Identifier.authority).filter(CI_Citation.title.like('%' + i_title + '%')) 
    206183 
    207     @classmethod 
    208     def extractObservationsForProject(self, project, session = None): 
     184    def extractObservationsForProject(self, project): 
    209185        """ 
    210186            Searches for the CEDA_Observation associated with a CEDA_Project 
     
    212188            @return: a tuple containing the associated CEDA_Observation   
    213189        """ 
    214         intSession = Moles3EPB._getSession(session) 
    215         return intSession.query(CEDA_Observation). \ 
     190        return self._session.query(CEDA_Observation). \ 
    216191            join(CEDA_Observation, MO_Observation.inSupportOf).filter(CEDA_Project.id == project.id) 
    217192 
    218     @classmethod 
    219     def extractProjectObservationCollections(self, project, session = None): 
     193    def extractProjectObservationCollections(self, project): 
    220194        """ 
    221195            Searches for the Observation_Collections associated with a CEDA_Project 
     
    223197            @return: a tuple containing the associated CEDA_ObservationCollection   
    224198        """ 
    225         intSession = Moles3EPB._getSession(session) 
    226         mo_obs = intSession.query(MO_Observation).join(CEDA_Project).filter(CEDA_Project.id == project.id).subquery()       
    227         obsers = intSession.query(CEDA_Observation).join(mo_obs, CEDA_Observation.id == mo_obs.c.id).one() 
     199        mo_obs = self._session.query(MO_Observation).join(CEDA_Project).filter(CEDA_Project.id == project.id).subquery()       
     200        obsers = self._session.query(CEDA_Observation).join(mo_obs, CEDA_Observation.id == mo_obs.c.id).one() 
    228201        #print "obsers: " + str(intSession.query(CEDA_Observation).join(mo_obs, CEDA_Observation.id == mo_obs.c.id).count()) 
    229202         
    230         cos = intSession.query(CEDA_ObservationCollection).all() 
    231         co = intSession.query(MO_ObservationCollection).join(MO_ObservationCollection.member).filter(MO_ObservationCollection.member.contains(obsers)) 
    232          
    233         observations = intSession.query(MO_ObservationCollection).join(CEDA_Observation). \ 
     203        cos = self._session.query(CEDA_ObservationCollection).all() 
     204        co = self._session.query(MO_ObservationCollection).join(MO_ObservationCollection.member).filter(MO_ObservationCollection.member.contains(obsers)) 
     205         
     206        observations = self._session.query(MO_ObservationCollection).join(CEDA_Observation). \ 
    234207            filter(obsers.any(CEDA_Observation.id==obsers.c.id)) 
    235208        print "observation:" + str(observations.count()) 
    236209        return observations 
    237210 
    238     @classmethod         
    239     def getNewMolesSession(self): 
    240             return Moles3EPB._getSession() 
    241  
    242     @classmethod 
    243     def search(self, clazz, inst_id, session = None): 
    244         intSession = Moles3EPB._getSession(session) 
    245         ret = EPB.search(clazz, inst_id, intSession) 
    246         if session is None: 
    247             intSession.close() 
     211    def search(self, clazz, inst_id): 
     212        ret = EPB.search(clazz, inst_id, self._session) 
    248213        return ret 
    249      
    250     @classmethod     
    251     def searchSelectiveLoad(self, clazz, inst_id, attributes, session = None):  
     214       
     215    def searchSelectiveLoad(self, clazz, inst_id, attributes):  
    252216        """ 
    253217            Searches a required instance by id loading selectively \ 
     
    271235            @return the required instance               
    272236        """                
    273         intSession = Moles3EPB._getSession(session) 
    274         ret = EPB.searchSelectiveLoad(clazz, inst_id, attributes, intSession) 
    275         if session is None: 
    276             intSession.close() 
     237        ret = EPB.searchSelectiveLoad(clazz, inst_id, attributes, self._session) 
    277238        return ret     
    278239     
    279     @classmethod 
    280     def loadAttributes(self, instance, attributes, session = None): 
     240    def loadAttributes(self, instance, attributes): 
    281241        """ 
    282242            Returns the attribute of an instance. The parameter "attributes" is a single string or a list of attributes  
     
    298258            @return: the given instance filled with the required attributes.                      
    299259        """ 
    300         intSession = Moles3EPB._getSession(session) 
    301         instance = intSession.merge(instance) 
    302         EPB.loadAttributes(instance, attributes, intSession)         
    303         if session is None: 
    304             intSession.close()               
     260        instance = self._session.merge(instance) 
     261        EPB.loadAttributes(instance, attributes, self._session)                   
    305262        return instance  
    306263 
    307     @classmethod 
    308     def executeNative(self, sqlNative, session = None): 
    309         intSession = Moles3EPB._getSession(session) 
    310         return EPB.executeNative(sqlNative, intSession)   
    311  
    312     @classmethod 
    313     def _getSession(self, session = None): 
    314         if Moles3EPB._molesDB is None: 
    315             raise NoDBManager("Moles3EPB") 
    316         return EPB._getSession(Moles3EPB._molesDB, session) 
     264    def executeNative(self, sqlNative): 
     265        return EPB.executeNative(sqlNative, self._session)   
  • mauRepo/MolesManager/trunk/src/MolesManager/molesSessionMiddleware.py

    r8323 r8325  
    44@author: mnagni 
    55''' 
    6 from MolesManager.moles3epb import Moles3EPB 
     6from MolesManager.moles3epb import Moles3EPBFactory 
    77from libs.migration.client import MigrationThread 
    88from MolesManager.settings import RUN_MIGRATION, MIGRATION_INTERVAL,\ 
     
    1010    MOLES3_DB_SCRIPT, EVENTS_DB, INFO_DB_CONNECTION 
    1111from libs.commons_db import DbManager 
    12 from libs.migration.MigrationEPB import MigrationEPB 
    1312from libs.migration.InfodbEPB import InfodbEPB 
    14 import logging 
     13from libs.migration.MigrationEPB import MigrationEPBFactory 
    1514 
    1615 
     
    2019    #This attribute should be not here. 
    2120    #unfortunately I cannot find any start/stop signals from Django 
    22     _migrationThread = MigrationThread(interval = MIGRATION_INTERVAL) 
     21    #_migrationThread = MigrationThread(interval = MIGRATION_INTERVAL) 
    2322    _epbInitialized = False 
    24      
    25     def _getNewMolesSession(self): 
    26         session = Moles3EPB.getNewMolesSession() 
    27         return session 
    28      
     23    _moles3EPBFactory = None 
     24    _migrationEPBFactory = None 
    2925     
    3026    def _doInitialization(self): 
     
    3329            
    3430        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT) 
    35         MigrationEPB.overrrideDBManager(migrationDB)        
     31        MolesSessionMiddleware._migrationEPBFactory = MigrationEPBFactory(migrationDB)                
    3632 
    3733        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB) 
    38         Moles3EPB.overrrideDBManager(molesDB) 
     34        MolesSessionMiddleware._moles3EPBFactory = Moles3EPBFactory(molesDB) 
     35         
     36        MolesSessionMiddleware._migrationThread = MigrationThread( \ 
     37            MolesSessionMiddleware._moles3EPBFactory.createMoles3EPB(), \ 
     38            MolesSessionMiddleware._migrationEPBFactory.createMigrationEPB(), \ 
     39            interval = MIGRATION_INTERVAL ) 
    3940        MolesSessionMiddleware._epbInitialized = True       
    4041         
     
    7071            @deprecated: request.moles_session is going to be removed from all GUI-related instances 
    7172        ''' 
    72         #request.moles_session = self._getMolesSession() 
     73        request.moles3EPB = MolesSessionMiddleware._moles3EPBFactory.createMoles3EPB() 
    7374 
    7475 
    7576    def process_response(self, request, response): 
    76         """ 
    77         if hasattr(request, 'moles_session'): 
    78             request.moles_session.close() 
    79         """ 
     77        request.moles3EPB.close() 
    8078        return response 
    8179 
  • mauRepo/MolesManager/trunk/src/MolesManager/views/cedaObservationView.py

    r8323 r8325  
    3232    if record: 
    3333        c['coObs'] = mark_safe(DJEncoder().escapeForJSON(DJEncoder().encode(record))) 
    34         guid = Moles3EPB.retrieveGUIDFromInstance(record) 
     34        guid = request.moles3EPB.retrieveGUIDFromInstance(record) 
    3535        if guid: 
    3636            c['guid'] = guid.id 
     
    4646                      'geographicExtent', 'phenomenonTime', 'keywords', 'description', \ 
    4747                      'inSupportOf.abstract', 'dataLineage'] 
    48     return Moles3EPB.searchSelectiveLoad(CEDA_Observation, obs_id, eagerloadthese) 
     48    return request.moles3EPB.searchSelectiveLoad(CEDA_Observation, obs_id, eagerloadthese) 
    4949    #return Moles3EPB.searchEager(CEDA_Observation, obs_id) 
  • mauRepo/MolesManager/trunk/src/libs/migration/MigrationEPB.py

    r8323 r8325  
    88from sqlalchemy.sql.expression import asc 
    99from libs.epb import EPB 
    10 from libs.migration.exception.exceptions import NoDBManager 
    11 from libs.migration.processor.commons import stringToTimestamp 
    1210from sqlalchemy.orm.collections import InstrumentedList 
    13 from sqlalchemy.orm.util import identity_key 
    1411 
    1512 
    16 class MigrationEPB(EPB): 
     13class MigrationEPBFactory(EPB): 
    1714     
    18     _migrationDB = None 
    19      
    20     @classmethod 
    21     def overrrideDBManager(self, dbManager): 
    22         """ 
    23             Sets the MigrationEPB libs.commons_db.DbManager 
    24         """ 
    25         MigrationEPB._migrationDB = dbManager          
     15    def __init__(self, dbManager): 
     16        self._dbManager = dbManager 
     17 
     18    def _getSession(self): 
     19        if self._dbManager is not None: 
     20            return self._dbManager.createDbSession()                 
     21        return None 
    2622         
    27     @classmethod 
    28     def persistInstance(self, instance, session = None): 
     23    def createMigrationEPB(self): 
     24        return MigrationEPB(self._getSession()) 
     25         
     26         
     27class MigrationEPB(object): 
     28             
     29    def __init__(self, session): 
     30        self._session = session             
     31 
     32    def close(self): 
     33        return self._session.close() 
     34             
     35    def persistInstance(self, instance): 
    2936        """ 
    3037            Adds a new migration object. 
     
    3340            @return an updated, session independent, object instance reflecting the new persisted object  
    3441        """       
    35         intSession = MigrationEPB._getSession(session) 
    36         EPB.persistInstance(instance, intSession) 
    37         if session is None: 
    38             intSession.close() 
     42        EPB.persistInstance(instance, self._session) 
    3943        #return ret 
    4044 
    41     @classmethod 
    4245    def getAllDeploymentsMigrationByDataEntitySortedByDate(self, dataEntity, deploymentNames): 
    43         intSession = MigrationEPB._getSession(None) 
    44         res = EPB.getAllObjects(DeploymentsMigration, intSession).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation")).all() 
    45         intSession.close() 
    46         return res 
     46        return EPB.getAllObjects(DeploymentsMigration, self._session).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation")).all() 
    4747        #return EPB.getAllObjects(DeploymentsMigration, intSession).filter(*[EPB.buildFilter('doc_owner', dataEntity.doc_owner)]).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation"))         
    4848        #return EPB.getAllObjects(DeploymentsMigration, intSession).filter(*[EPB.buildFilter('doc_status', dataEntity.doc_status)]).filter(*[EPB.buildFilter('doc_owner', dataEntity.doc_owner)]).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation")) 
    4949 
    50     @classmethod 
    51     def _getMigrationObject(self, mo_type, doc_status, doc_owner, doc_name, session = None): 
    52         intSession = MigrationEPB._getSession(session) 
    53         return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_name', doc_name)]).first() 
     50    def _getMigrationObject(self, mo_type, doc_status, doc_owner, doc_name): 
     51        return self._session.query(mo_type).filter(*[EPB.buildFilter('doc_name', doc_name)]).first() 
    5452        #return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_owner', doc_owner)]).filter(*[EPB.buildFilter('doc_name', doc_name)]).first()         
    5553        #return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_status', doc_status)]).filter(*[EPB.buildFilter('doc_owner', doc_owner)]).filter(*[EPB.buildFilter('doc_name', doc_name)]).first() 
    5654 
    57     @classmethod 
    58     def _getMigrationObjectByName(self, mo_type, migrationObject, doc_name, session = None): 
    59         return self._getMigrationObject(mo_type, migrationObject.doc_status, migrationObject.doc_owner, doc_name, session)              
     55    def _getMigrationObjectByName(self, mo_type, migrationObject, doc_name): 
     56        return self._getMigrationObject(mo_type, migrationObject.doc_status, migrationObject.doc_owner, doc_name)              
    6057 
    61     @classmethod 
    62     def getDeploymentsMigrationByName(self, migrationObject, doc_name, session = None): 
     58    def getDeploymentsMigrationByName(self, migrationObject, doc_name): 
    6359        if migrationObject is None: 
    6460            raise Exception("migrationObject is None") 
    65         return self._getMigrationObjectByName(DeploymentsMigration, migrationObject, doc_name, session)         
     61        return self._getMigrationObjectByName(DeploymentsMigration, migrationObject, doc_name)         
    6662 
    67     @classmethod 
    68     def getDeploymentDataMigrationByName(self, migrationObject, doc_name, session = None): 
     63    def getDeploymentDataMigrationByName(self, migrationObject, doc_name): 
    6964        if migrationObject is None: 
    7065            raise Exception("migrationObject is None") 
    71         return self._getMigrationObjectByName(DeploymentDataMigration, migrationObject, doc_name, session) 
     66        return self._getMigrationObjectByName(DeploymentDataMigration, migrationObject, doc_name) 
    7267 
    73     @classmethod 
    74     def getDataEntityMigrationbyPath(self, migrationObject, session = None): 
     68    def getDataEntityMigrationbyPath(self, migrationObject): 
    7569        """ 
    7670            Returns the DataEntityMigration associated with the given path 
    7771            @param migrationObject: the migration object to look for. If None returns all the DataEntityMigration items 
    7872        """ 
    79         intSession = MigrationEPB._getSession(session) 
    8073        if migrationObject: 
    81             ret = intSession.query(DataEntityMigration).filter(*[EPB.buildFilter('doc_name', migrationObject.doc_name)]).first()                
     74            ret = self._session.query(DataEntityMigration).filter(*[EPB.buildFilter('doc_name', migrationObject.doc_name)]).first()                
    8275        else: #then process all the DataEntities 
    83             ret = EPB.getAllObjects(DataEntityMigration, intSession) 
    84         if session is None: 
    85             intSession.close()                    
     76            ret = EPB.getAllObjects(DataEntityMigration)                   
    8677        return ret 
    87  
    88     @classmethod         
    89     def updateMigrationObject(self, migration_object, cols_to_update, session = None): 
     78      
     79    def updateMigrationObject(self, migration_object, cols_to_update): 
    9080        """ 
    9181            Update and eventually commit a Migration Object in Migration db 
     
    9484            @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 
    9585        """ 
    96         intSession = MigrationEPB._getSession(session) 
    9786        coll = None 
    9887        try: 
    99             coll = intSession.merge(migration_object)      
     88            coll = self._session.merge(migration_object)      
    10089        except Exception as e: 
    10190            print e                
     
    10594                    val = None 
    10695                    try: 
    107                         val = intSession.merge(v) 
     96                        val = self._session.merge(v) 
    10897                    except Exception: 
    10998                        val = v 
     
    116105                    else: 
    117106                        setattr(coll, k, val) 
    118         EPB.persistInstance(coll, intSession)                         
    119         #intSession.commit()         
    120         if session is None: 
    121             intSession.close() 
    122         #if coll: 
    123         #    return coll 
     107        EPB.persistInstance(coll, self._session)                              
    124108             
    125  
    126     @classmethod 
    127     def getNewMigrationSession(self): 
    128         return MigrationEPB._getSession() 
    129  
    130     @classmethod 
    131     def loadAttributes(self, instance, attributes, session = None): 
     109    def loadAttributes(self, instance, attributes): 
    132110        """ 
    133111            Returns the attribute of an instance. The parameter "attributes" is a single string or a list of attributes  
     
    149127            @return: a detached instance (or array of instances) of the required attribute.                      
    150128        """ 
    151         intSession = MigrationEPB._getSession(session) 
    152         instance = intSession.merge(instance) 
    153         EPB.loadAttributes(instance, attributes, session)         
    154         if session is None: 
    155             intSession.close()                 
     129        instance = self._session.merge(instance) 
     130        EPB.loadAttributes(instance, attributes)                         
    156131        return instance   
    157132 
    158     @classmethod 
    159     def search(self, clazz, inst_id, session = None): 
     133    def search(self, clazz, inst_id): 
    160134        if clazz is None or inst_id is None: 
    161135            return None  
    162         intSession = MigrationEPB._getSession(session) 
    163         ret = EPB.search(clazz, inst_id, intSession) 
    164         if session is None: 
    165             intSession.close() 
     136        ret = EPB.search(clazz, inst_id, self._session) 
    166137        return ret  
    167138          
    168  
    169     @classmethod 
    170     def _getSession(self, session = None): 
    171         if MigrationEPB._migrationDB is None: 
    172             raise NoDBManager("MigrationEPB") 
    173         return EPB._getSession(MigrationEPB._migrationDB, session) 
  • mauRepo/MolesManager/trunk/src/libs/migration/client.py

    r8147 r8325  
    1010import time 
    1111from threading import Timer 
     12from libs.migration.processor.EPBRepo import EPBRepo 
    1213 
    1314class MigrationThread(threading.Thread): 
    1415    """ 
    1516        Constructs a scheduler for the Moles2 to Moles3 migration. 
     17        Only one instance should be running for each python interpreter  
    1618        @param interval: define the minimal time, in seconds, between an execution and the successive.  
    1719        If the current migration run takes more than the defined interval the next will starts 
    1820        as soon the previous ends.          
    1921    """ 
    20     def __init__(self, interval=600): 
     22     
     23    def __init__(self, moles3EPB, migrationEPB, interval=600): 
     24        EPBRepo.moles3EPB = moles3EPB 
     25        EPBRepo.migrationEPB = migrationEPB 
     26         
    2127        threading.Thread.__init__(self) 
    2228        self._doRun = True    
  • mauRepo/MolesManager/trunk/src/libs/migration/processor/dataEntity.py

    r8323 r8325  
    2323from MolesManager.ceda_guid import CedaGUID 
    2424from ea_model.iso_19115_2006_metadata_corrigendum.maintenance_information.md_maintenancefrequencycode import MD_MaintenanceFrequencyCode 
     25from libs.migration.processor.EPBRepo import EPBRepo 
    2526 
    2627class DataEntityProcessor(object): 
     
    6364     
    6465    def _getObservationCollection(self): 
    65         return Moles3EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id) 
     66        return EPBRepo.moles3EPB.search(CEDA_ObservationCollection, self._dataEntityMigration.ceda_observation_coll_id) 
    6667     
    6768    def _processObservationCollection(self):                                    
     
    105106        ceda_observationCollection.publicationState = MO_PublicationStateValue.cl_working 
    106107                   
    107         Moles3EPB.persistInstance(ceda_observationCollection) 
    108          
    109         MigrationEPB.updateMigrationObject(self._dataEntityMigration, {'ceda_observation_coll_id': ceda_observationCollection.id}) 
     108        EPBRepo.moles3EPB.persistInstance(ceda_observationCollection) 
     109         
     110        EPBRepo.migrationEPB.updateMigrationObject(self._dataEntityMigration, {'ceda_observation_coll_id': ceda_observationCollection.id}) 
    110111 
    111112         
     
    114115        ceda_guid.id = calculateHash(self._dataEntityMigration.data_ent_id) 
    115116        ceda_guid.ceda_observationcollection = ceda_observationCollection.id 
    116         Moles3EPB.persistInstance(ceda_guid) 
     117        EPBRepo.moles3EPB.persistInstance(ceda_guid) 
    117118        DataEntityProcessor.log.info("GUID for this ObservationCollection: %s" % (ceda_guid.id)) 
    118119         
     
    120121 
    121122    def _processResultAccumulation(self, ceda_observation):         
    122         ceda_observation = Moles3EPB.loadAttributes(ceda_observation, "resultAccumulation")         
     123        ceda_observation = EPBRepo.moles3EPB.loadAttributes(ceda_observation, "resultAccumulation")         
    123124        if ceda_observation.resultAccumulation is None: 
    124125            updateFrequency = extractUpdateFrequency(self._dataEntityMigration) 
    125126            if updateFrequency: 
    126127                resultAccumulation = MD_MaintenanceFrequencyCode.from_string(updateFrequency) 
    127                 Moles3EPB.updateCedaObject(ceda_observation, {'resultAccumulation': resultAccumulation}) 
     128                EPBRepo.moles3EPB.updateCedaObject(ceda_observation, {'resultAccumulation': resultAccumulation}) 
    128129 
    129130    def _processDOI(self, deploymentMigration, ceda_observation, deProcessor, single_deployment):         
     
    154155                if not hasAtomDocumentSameHash(deploymentMigration): 
    155156                    doc_hash = getAtomDocumentHash(deploymentMigration.docStatus, deploymentMigration.docType, deploymentMigration.docOwner, deploymentMigration.docName) 
    156                     MigrationEPB.updateMigrationObject(deploymentMigration, {'doc_hash': doc_hash})                 
     157                    EPBRepo.migrationEPB.updateMigrationObject(deploymentMigration, {'doc_hash': doc_hash})                 
    157158        except NoAssociatedAuthor as ex: 
    158159            raise ex                  
     
    182183         
    183184        #retrieves the DataEntityMigration sorted by creation date 
    184         deploymentMigrations = MigrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate( \ 
     185        deploymentMigrations = EPBRepo.migrationEPB.getAllDeploymentsMigrationByDataEntitySortedByDate( \ 
    185186                                                self._dataEntityMigration, deploymentsLinks) 
    186187        howManydm = 0 
     
    194195                 
    195196                #Check if a doi has been already assigned      
    196                 cedaObservationCollection = Moles3EPB.loadAttributes(cedaObservationCollection, 'member')                            
     197                cedaObservationCollection = EPBRepo.moles3EPB.loadAttributes(cedaObservationCollection, 'member')                            
    197198                member = cedaObservationCollection.member 
    198199                if member and not (ceda_observation in member): 
    199                     Moles3EPB.updateCedaObject(cedaObservationCollection, {'member': ceda_observation}) 
     200                    EPBRepo.moles3EPB.updateCedaObject(cedaObservationCollection, {'member': ceda_observation}) 
    200201                                       
    201202            except Exception as ex: 
  • mauRepo/MolesManager/trunk/src/libs/migration/processor/deployment.py

    r8323 r8325  
    5050from ea_model.iso_19115_2006_metadata_corrigendum.maintenance_information.md_maintenancefrequencycode import MD_MaintenanceFrequencyCode 
    5151from ea_model.ceda_metadatamodel.ceda_result.ceda_curationvalue import CEDA_CurationValue 
     52from libs.migration.processor.EPBRepo import EPBRepo 
    5253 
    5354class DeploymentProcessor(object): 
     
    230231                obsList = [] 
    231232                for obs in links['OBS']: 
    232                     observationStation = MigrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, obs + '.atom') 
     233                    observationStation = EPBRepo.migrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, obs + '.atom') 
    233234                    obsList.append((extractTitle(observationStation), findSubTypeInDPT(observationStation))) 
    234235                 
     
    246247            if links.has_key('ACTIVITY'):              
    247248                for link in links['ACTIVITY']: 
    248                     activity = MigrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, link + '.atom')                     
     249                    activity = EPBRepo.migrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, link + '.atom')                     
    249250      
    250251                    projSubType = findSubTypeInDPT(activity) 
     
    347348             
    348349            #Check if a doi has been already assigned 
    349             observation = Moles3EPB.loadAttributes(observation, 'identifier') 
     350            observation = EPBRepo.moles3EPB.loadAttributes(observation, 'identifier') 
    350351            obs_identifier = observation.identifier 
    351352            if obs_identifier: 
     
    371372                i_authority = createCI_Citation("DOI", date = ci_date) 
    372373                identifier = createMD_Identifier(code = doi, authority=i_authority) 
    373                 Moles3EPB.updateCedaObject(observation, {'identifier': identifier}) 
     374                EPBRepo.moles3EPB.updateCedaObject(observation, {'identifier': identifier}) 
    374375                DeploymentProcessor.log.info("DOI: %s" % (doi))                                   
    375376 
     
    513514        self._assignUpdateFrequency(ceda_observation)     
    514515        self._assignName(ceda_observation)   
    515         Moles3EPB.persistInstance(ceda_observation)         
    516         MigrationEPB.updateMigrationObject(self._deploymentMigration, {'ceda_observation_id': ceda_observation.id}) 
     516        EPBRepo.moles3EPB.persistInstance(ceda_observation)         
     517        EPBRepo.migrationEPB.updateMigrationObject(self._deploymentMigration, {'ceda_observation_id': ceda_observation.id}) 
    517518         
    518519        #Adds the CedaGUID         
     
    520521        ceda_guid.id = calculateHash(self._deploymentMigration.depl_id) 
    521522        ceda_guid.ceda_observation = ceda_observation.id 
    522         Moles3EPB.persistInstance(ceda_guid) 
     523        EPBRepo.moles3EPB.persistInstance(ceda_guid) 
    523524        DeploymentProcessor.log.info("GUID for this Observation: %s" % (ceda_guid.id)) 
    524525                 
     
    531532        #Temporary commented because CEDA_Project.subProject is not correctly mapped to the DB 
    532533        project = deploymentDataProcessor.createProject(links)  
    533         Moles3EPB.updateCedaObject(ceda_observation, {'procedure': procedure, 'inSupportOf': project}) 
     534        EPBRepo.moles3EPB.updateCedaObject(ceda_observation, {'procedure': procedure, 'inSupportOf': project}) 
    534535         
    535536        return ceda_observation 
    536537 
    537538    def _getObservation(self): 
    538         return Moles3EPB.search(CEDA_Observation, self._deploymentMigration.ceda_observation_id) 
     539        return EPBRepo.moles3EPB.search(CEDA_Observation, self._deploymentMigration.ceda_observation_id) 
    539540             
    540541    def _update(self): 
  • mauRepo/MolesManager/trunk/src/libs/migration/processor/deployment_data.py

    r8323 r8325  
    1111from MolesManager.moles3epb import Moles3EPB 
    1212from ea_model.moles3_4.utilities.mo_publicationstatevalue import MO_PublicationStateValue 
     13from libs.migration.processor.EPBRepo import EPBRepo 
    1314 
    1415class DeploymentDataProcessor(object): 
     
    1819         
    1920    def _commitDeploymentMigration(self, associateWithCedaObservation, dataProductionTool, dataProductionToolField): 
    20         Moles3EPB.persistInstance(associateWithCedaObservation) 
    21         MigrationEPB.updateMigrationObject(dataProductionTool, {dataProductionToolField: associateWithCedaObservation.id}) 
     21        EPBRepo.moles3EPB.persistInstance(associateWithCedaObservation) 
     22        EPBRepo.migrationEPB.updateMigrationObject(dataProductionTool, {dataProductionToolField: associateWithCedaObservation.id}) 
    2223                                    
    2324         
     
    2627        if links.has_key('ACTIVITY'): 
    2728            for link in links['ACTIVITY']: 
    28                 activity = MigrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, link + '.atom') 
     29                activity = EPBRepo.migrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, link + '.atom') 
    2930                i_abstract = findSummary(activity) 
    3031                doc_link = findDocumentationInMigrationDocument(activity) 
     
    4546        hasCedaAcquisition = False 
    4647        for dpt in links['DPT']: 
    47             dataProductionTool = MigrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, dpt + '.atom') 
     48            dataProductionTool = EPBRepo.migrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, dpt + '.atom') 
    4849             
    4950            #has the document changed?             
     
    7980        #if not a DPT.subType == 'model' then.... 
    8081        for obs in links['OBS']: 
    81             observationStation = MigrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, obs + '.atom') 
     82            observationStation = EPBRepo.migrationEPB.getDeploymentDataMigrationByName(self._deploymentMigration, obs + '.atom') 
    8283  
    8384            #has the document changed?             
  • mauRepo/MolesManager/trunk/src/libs/migration/processor/loadResources.py

    r8323 r8325  
    1111from libs.migration.db.classes import DeploymentDataMigration,\ 
    1212    DeploymentsMigration, DataEntityMigration 
    13 from libs.migration.MigrationEPB import MigrationEPB 
     13from libs.migration.processor.EPBRepo import EPBRepo 
    1414 
    1515class LoadResources(object): 
    16     """ 
    17      Loads informations from the eXist database to populate reference table for the migrations 
    18     """ 
    19     def __init__(self): 
    20         pass 
    2116 
    2217    def process(self): 
     
    5146        migrationObject.doc_creation = stringToTimestamp(docCreation) 
    5247        try: 
    53             MigrationEPB.persistInstance(migrationObject) 
     48            EPBRepo.migrationEPB.persistInstance(migrationObject) 
    5449        except Exception as e: 
    5550            print e 
     
    9388          
    9489        #The document has been already classified 
    95         if doc_id is None or MigrationEPB.search(migrationClass, doc_id) is not None: 
     90        if doc_id is None or EPBRepo.migrationEPB.search(migrationClass, doc_id) is not None: 
    9691            return 
    9792         
  • mauRepo/MolesManager/trunk/src/libs/migration/processor/migrationProcess.py

    r8323 r8325  
    88import logging 
    99from logging import StreamHandler 
     10from libs.migration.processor.EPBRepo import EPBRepo 
    1011 
    1112class MigrationProcess(): 
     
    3132        exs = [] 
    3233        data_ents = [] 
    33         data_ents.append(MigrationEPB.getDataEntityMigrationbyPath(dataEntityObject)) 
     34        data_ents.append(EPBRepo.migrationEPB.getDataEntityMigrationbyPath(dataEntityObject)) 
    3435        """ 
    3536        #Does the migrationObject exists? 
  • mauRepo/MolesManager/trunk/src/libs/migration/tests/migrationprocess.py

    r8323 r8325  
    66from unittest import TestCase 
    77from libs.commons_db import DbManager 
    8 from libs.migration.MigrationEPB import MigrationEPB 
     8from libs.migration.MigrationEPB import MigrationEPB, MigrationEPBFactory 
    99from libs.migration.processor.loadResources import LoadResources 
    1010from libs.migration.db.classes import MigrationObject 
    1111from libs.migration.processor.commons import DO_BADC, DS_pUBLISHED, DS_PUBLISHED 
    12 from MolesManager.moles3epb import Moles3EPB 
     12from MolesManager.moles3epb import Moles3EPB, Moles3EPBFactory 
    1313from libs.migration.processor.migrationProcess import MigrationProcess 
    1414from testconfig import MIGRATION_DB_CONNECTION,\ 
     
    1616from libs.migration.InfodbEPB import InfodbEPB 
    1717from MolesManager.settings import EVENTS_DB 
     18from libs.migration.client import EPBRepo 
    1819 
    1920class LoadResourceTest(TestCase): 
     
    2122    classdocs 
    2223    ''' 
    23  
     24     
    2425    def setUp(self): 
    2526        migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT, sql_echo=True) 
    2627        #migrationDB = DbManager(MIGRATION_DB_CONNECTION, MIGRATION_DB_SCRIPT) 
    27         MigrationEPB.overrrideDBManager(migrationDB)         
     28        #MigrationEPB.overrrideDBManager(migrationDB)       
    2829 
    2930        molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, sql_echo=True, session_event_manager=EVENTS_DB) 
    30         #molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB) 
    31         Moles3EPB.overrrideDBManager(molesDB) 
     31        #molesDB = DbManager(MOLES3_DB_CONNECTION, MOLES3_DB_SCRIPT, session_event_manager=EVENTS_DB)          
     32         
    3233         
    3334        #infoDB = DbManager(INFO_DB_CONNECTION, sql_echo=True) 
    3435        infoDB = DbManager(INFO_DB_CONNECTION)         
    3536        InfodbEPB.overrrideDBManager(infoDB) 
     37         
     38        moles3Factory = Moles3EPBFactory(molesDB) 
     39        migrationFactory = MigrationEPBFactory(migrationDB) 
     40         
     41        EPBRepo.moles3EPB = moles3Factory.createMoles3EPB()  
     42        EPBRepo.migrationEPB = migrationFactory.createMigrationEPB() 
    3643         
    3744        lr = LoadResources() 
Note: See TracChangeset for help on using the changeset viewer.