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

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

small fix

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 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):
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        if migrationObject:
74            ret = self._session.query(DataEntityMigration).filter(*[EPB.buildFilter('doc_name', migrationObject.doc_name)]).first()               
75        else: #then process all the DataEntities
76            ret = EPB.getAllObjects(DataEntityMigration, self._session)                 
77        return ret
78     
79    def updateMigrationObject(self, migration_object, cols_to_update):
80        """
81            Update and eventually commit a Migration Object in Migration db
82            @param ceda_object: the Migration object to persist
83            @param dict: a dictionary containing the columns to update for the given migration_object
84            @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
85        """
86        coll = None
87        try:
88            coll = self._session.merge(migration_object)     
89        except Exception as e:
90            print e               
91        if coll != None:       
92            for k,v in cols_to_update.items():
93                if hasattr(coll, k):
94                    val = None
95                    try:
96                        val = self._session.merge(v)
97                    except Exception:
98                        val = v
99                    coll_k = getattr(coll, k)                       
100                    if type(coll_k) == list or type(coll_k) == InstrumentedList:
101                        if  type(val) == list or type(val) == InstrumentedList:
102                            coll_k.extend(val)
103                        else:
104                            coll_k.append(val)
105                    else:
106                        setattr(coll, k, val)
107        EPB.persistInstance(coll, self._session)                             
108           
109    def loadAttributes(self, instance, attributes):
110        """
111            Returns the attribute of an instance. The parameter "attributes" is a single string or a list of attributes
112            owned by the instance of "clazz". Furthermore such list may contain
113            also the children of the main attributes. For example "attrs" may look
114            like
115            ['resultAccumulation', 'identifier.authority', 'resultTime.position.dateTime8601.month', \
116                      'relatedParty.party', 'result.source.function', 'permission', \
117                      'geographicExtent', 'phenomenonTime', 'keywords', 'description', \
118                      'inSupportOf.abstract', 'dataLineage']
119            the first parameter refers to the main class so is equivalent to
120            clazz.resultAccumulation
121            the second parameter is equivalent to invoke
122            clazz.identifier.authority
123            As single string "attributes" could be as well just 'identifier.authority'
124            @param instance: an instance containing the appropriate id
125            @param attributes: the attribute value required
126            @param session: the session to use for the operation
127            @return: a detached instance (or array of instances) of the required attribute.                     
128        """
129        instance = self._session.merge(instance)
130        EPB.loadAttributes(instance, attributes)                       
131        return instance 
132
133    def search(self, clazz, inst_id):
134        if clazz is None or inst_id is None:
135            return None 
136        ret = EPB.search(clazz, inst_id, self._session)
137        return ret
138         
Note: See TracBrowser for help on using the repository browser.