1 | ''' |
---|
2 | Created on 10 Jan 2012 |
---|
3 | |
---|
4 | @author: mnagni |
---|
5 | ''' |
---|
6 | from libs.migration.db.classes import DeploymentDataMigration, DataEntityMigration,\ |
---|
7 | DeploymentsMigration |
---|
8 | from sqlalchemy.sql.expression import asc |
---|
9 | from libs.epb import EPB |
---|
10 | from libs.migration.exception.exceptions import NoDBManager |
---|
11 | from libs.migration.processor.commons import stringToTimestamp |
---|
12 | from sqlalchemy.orm.collections import InstrumentedList |
---|
13 | from sqlalchemy.orm.util import identity_key |
---|
14 | |
---|
15 | |
---|
16 | class MigrationEPB(EPB): |
---|
17 | |
---|
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 |
---|
26 | |
---|
27 | @classmethod |
---|
28 | def persistInstance(self, instance, session = None): |
---|
29 | """ |
---|
30 | Adds a new migration object. |
---|
31 | @param migrationObj: the migration object to add |
---|
32 | @param session: an SQLAlchemy Session object. If None (default) the method creates |
---|
33 | @return an updated, session independent, object instance reflecting the new persisted object |
---|
34 | """ |
---|
35 | intSession = MigrationEPB._getSession(session) |
---|
36 | EPB.persistInstance(instance, intSession) |
---|
37 | if session is None: |
---|
38 | intSession.close() |
---|
39 | #return ret |
---|
40 | |
---|
41 | @classmethod |
---|
42 | 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 |
---|
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 | @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() |
---|
54 | #return intSession.query(mo_type).filter(*[EPB.buildFilter('doc_owner', doc_owner)]).filter(*[EPB.buildFilter('doc_name', doc_name)]).first() |
---|
55 | #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() |
---|
56 | |
---|
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) |
---|
60 | |
---|
61 | @classmethod |
---|
62 | def getDeploymentsMigrationByName(self, migrationObject, doc_name, session = None): |
---|
63 | if migrationObject is None: |
---|
64 | raise Exception("migrationObject is None") |
---|
65 | return self._getMigrationObjectByName(DeploymentsMigration, migrationObject, doc_name, session) |
---|
66 | |
---|
67 | @classmethod |
---|
68 | def getDeploymentDataMigrationByName(self, migrationObject, doc_name, session = None): |
---|
69 | if migrationObject is None: |
---|
70 | raise Exception("migrationObject is None") |
---|
71 | return self._getMigrationObjectByName(DeploymentDataMigration, migrationObject, doc_name, session) |
---|
72 | |
---|
73 | @classmethod |
---|
74 | def getDataEntityMigrationbyPath(self, migrationObject, session = None): |
---|
75 | """ |
---|
76 | Returns the DataEntityMigration associated with the given path |
---|
77 | @param migrationObject: the migration object to look for. If None returns all the DataEntityMigration items |
---|
78 | """ |
---|
79 | intSession = MigrationEPB._getSession(session) |
---|
80 | if migrationObject: |
---|
81 | ret = intSession.query(DataEntityMigration).filter(*[EPB.buildFilter('doc_name', migrationObject.doc_name)]).first() |
---|
82 | else: #then process all the DataEntities |
---|
83 | ret = EPB.getAllObjects(DataEntityMigration, intSession) |
---|
84 | if session is None: |
---|
85 | intSession.close() |
---|
86 | return ret |
---|
87 | |
---|
88 | @classmethod |
---|
89 | def updateMigrationObject(self, migration_object, cols_to_update, session = None): |
---|
90 | """ |
---|
91 | Update and eventually commit a Migration Object in Migration db |
---|
92 | @param ceda_object: the Migration object to persist |
---|
93 | @param dict: a dictionary containing the columns to update for the given migration_object |
---|
94 | @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 |
---|
95 | """ |
---|
96 | intSession = MigrationEPB._getSession(session) |
---|
97 | coll = None |
---|
98 | try: |
---|
99 | coll = intSession.merge(migration_object) |
---|
100 | except Exception as e: |
---|
101 | print e |
---|
102 | if coll != None: |
---|
103 | for k,v in cols_to_update.items(): |
---|
104 | if hasattr(coll, k): |
---|
105 | val = None |
---|
106 | try: |
---|
107 | val = intSession.merge(v) |
---|
108 | except Exception: |
---|
109 | val = v |
---|
110 | coll_k = getattr(coll, k) |
---|
111 | if type(coll_k) == list or type(coll_k) == InstrumentedList: |
---|
112 | if type(val) == list or type(val) == InstrumentedList: |
---|
113 | coll_k.extend(val) |
---|
114 | else: |
---|
115 | coll_k.append(val) |
---|
116 | else: |
---|
117 | 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 |
---|
124 | |
---|
125 | |
---|
126 | @classmethod |
---|
127 | def getNewMigrationSession(self): |
---|
128 | return MigrationEPB._getSession() |
---|
129 | |
---|
130 | @classmethod |
---|
131 | def loadAttributes(self, instance, attributes, session = None): |
---|
132 | """ |
---|
133 | Returns the attribute of an instance. The parameter "attributes" is a single string or a list of attributes |
---|
134 | owned by the instance of "clazz". Furthermore such list may contain |
---|
135 | also the children of the main attributes. For example "attrs" may look |
---|
136 | like |
---|
137 | ['resultAccumulation', 'identifier.authority', 'resultTime.position.dateTime8601.month', \ |
---|
138 | 'relatedParty.party', 'result.source.function', 'permission', \ |
---|
139 | 'geographicExtent', 'phenomenonTime', 'keywords', 'description', \ |
---|
140 | 'inSupportOf.abstract', 'dataLineage'] |
---|
141 | the first parameter refers to the main class so is equivalent to |
---|
142 | clazz.resultAccumulation |
---|
143 | the second parameter is equivalent to invoke |
---|
144 | clazz.identifier.authority |
---|
145 | As single string "attributes" could be as well just 'identifier.authority' |
---|
146 | @param instance: an instance containing the appropriate id |
---|
147 | @param attributes: the attribute value required |
---|
148 | @param session: the session to use for the operation |
---|
149 | @return: a detached instance (or array of instances) of the required attribute. |
---|
150 | """ |
---|
151 | intSession = MigrationEPB._getSession(session) |
---|
152 | instance = intSession.merge(instance) |
---|
153 | EPB.loadAttributes(instance, attributes, session) |
---|
154 | if session is None: |
---|
155 | intSession.close() |
---|
156 | return instance |
---|
157 | |
---|
158 | @classmethod |
---|
159 | def search(self, clazz, inst_id, session = None): |
---|
160 | if clazz is None or inst_id is None: |
---|
161 | return None |
---|
162 | intSession = MigrationEPB._getSession(session) |
---|
163 | ret = EPB.search(clazz, inst_id, intSession) |
---|
164 | if session is None: |
---|
165 | intSession.close() |
---|
166 | return ret |
---|
167 | |
---|
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) |
---|