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

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

Fixed some gui bugs

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        EPB.persistInstance(coll, self._session)                             
111           
112    def loadAttributes(self, instance, attributes):
113        """
114            Returns the attribute of an instance. The parameter "attributes" is a single string or a list of attributes
115            owned by the instance of "clazz". Furthermore such list may contain
116            also the children of the main attributes. For example "attrs" may look
117            like
118            ['resultAccumulation', 'identifier.authority', 'resultTime.position.dateTime8601.month', \
119                      'relatedParty.party', 'result.source.function', 'permission', \
120                      'geographicExtent', 'phenomenonTime', 'keywords', 'description', \
121                      'inSupportOf.abstract', 'dataLineage']
122            the first parameter refers to the main class so is equivalent to
123            clazz.resultAccumulation
124            the second parameter is equivalent to invoke
125            clazz.identifier.authority
126            As single string "attributes" could be as well just 'identifier.authority'
127            @param instance: an instance containing the appropriate id
128            @param attributes: the attribute value required
129            @param session: the session to use for the operation
130            @return: a detached instance (or array of instances) of the required attribute.                     
131        """
132        instance = self._session.merge(instance)
133        EPB.loadAttributes(instance, attributes)                       
134        return instance 
135
136    def search(self, clazz, inst_id):
137        if clazz is None or inst_id is None:
138            return None 
139        ret = EPB.search(clazz, inst_id, self._session)
140        return ret
141         
Note: See TracBrowser for help on using the repository browser.