source: mauRepo/MolesManager/trunk/src/libs/migration/MigrationEPB.py @ 8460

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/mauRepo/MolesManager/trunk/src/libs/migration/MigrationEPB.py@8460
Revision 8460, 5.6 KB checked in by mnagni, 7 years ago (diff)

Complete - # 22489: CEDA Observation Collection - phenomenonTime
 http://team.ceda.ac.uk/trac/ceda/ticket/22489
Complete - # 22518: The description is broken
 http://team.ceda.ac.uk/trac/ceda/ticket/22518
Complete - # 22488: CEDA Observation Collection - Geographical Extent
 http://team.ceda.ac.uk/trac/ceda/ticket/22488

Now the Moles3EPB explicitly call the "synchronise" method in the SQLAlchemy mapped classes to assure the persistence of the data
Uses the CedaMolesModel? v 0.1.5 which correct a major problem in synchronise the instances with database

Line 
1'''
2Created on 10 Jan 2012
3
4@author: mnagni
5'''
6from libs.migration.db.classes import DeploymentDataMigration, DataEntityMigration,\
7    DeploymentsMigration
8from sqlalchemy.sql.expression import asc
9from libs.epb import EPB
10from sqlalchemy.orm.collections import InstrumentedList
11
12
13class MigrationEPBFactory(EPB):
14   
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
22       
23    def createEPB(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):
36        """
37            Adds a new migration object.
38            @param migrationObj: the migration object to add
39            @param session: an SQLAlchemy Session object. If None (default) the method creates
40            @return an updated, session independent, object instance reflecting the new persisted object
41        """     
42        EPB.persistInstance(instance, self._session)
43        #return ret
44
45    def getAllDeploymentsMigrationByDataEntitySortedByDate(self, dataEntity, deploymentNames):
46        return EPB.getAllObjects(DeploymentsMigration, self._session).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation")).all()
47        #return EPB.getAllObjects(DeploymentsMigration, intSession).filter(*[EPB.buildFilter('doc_owner', dataEntity.doc_owner)]).filter(DeploymentsMigration.doc_name.in_(deploymentNames)).order_by(asc("doc_creation"))       
48        #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"))
49
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()
52        #return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_owner', doc_owner)]).filter(*[EPB.buildFilter('doc_name', doc_name)]).first()       
53        #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()
54
55    def _getMigrationObjectByName(self, mo_type, migrationObject, doc_name):
56        return self._getMigrationObject(mo_type, migrationObject.doc_status, migrationObject.doc_owner, doc_name)             
57
58    def getDeploymentsMigrationByName(self, migrationObject, doc_name):
59        if migrationObject is None:
60            raise Exception("migrationObject is None")
61        return self._getMigrationObjectByName(DeploymentsMigration, migrationObject, doc_name)       
62
63    def getDeploymentDataMigrationByName(self, migrationObject, doc_name):
64        if migrationObject is None:
65            raise Exception("migrationObject is None")
66        return self._getMigrationObjectByName(DeploymentDataMigration, migrationObject, doc_name)
67
68    def getDataEntityMigrationbyPath(self, migrationObject):
69        """
70            Returns the DataEntityMigration associated with the given path
71            @param migrationObject: the migration object to look for. If None returns all the DataEntityMigration items
72        """
73        ret = []
74        if migrationObject:
75            ret.append(self._session.query(DataEntityMigration).filter(*[EPB.buildFilter('doc_name', migrationObject.doc_name)]).first())               
76        else: #then process all the DataEntities
77            res = self._session.query(DataEntityMigration).all()
78            for item in res:
79                ret.append(item)             
80        return ret
81     
82    def updateMigrationObject(self, migration_object, cols_to_update):
83        """
84            Update and eventually commit a Migration Object in Migration db
85            @param ceda_object: the Migration object to persist
86            @param dict: a dictionary containing the columns to update for the given migration_object
87            @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
88        """
89        coll = None
90        try:
91            coll = self._session.merge(migration_object)     
92        except Exception as e:
93            print e               
94        if coll != None:       
95            for k,v in cols_to_update.items():
96                if hasattr(coll, k):
97                    val = None
98                    try:
99                        val = self._session.merge(v)
100                    except Exception:
101                        val = v
102                    coll_k = getattr(coll, k)                       
103                    if type(coll_k) == list or type(coll_k) == InstrumentedList:
104                        if  type(val) == list or type(val) == InstrumentedList:
105                            coll_k.extend(val)
106                        else:
107                            coll_k.append(val)
108                    else:
109                        setattr(coll, k, val)
110        self._session.add(coll)
111        self._session.commit()                           
112
113    def search(self, clazz, inst_id):
114        if clazz is None or inst_id is None:
115            return None 
116        ret = EPB.search(clazz, inst_id, self._session)
117        return ret
Note: See TracBrowser for help on using the repository browser.