Exemple #1
0
 def __rollback(self, state):
     source_entity = state.entity
     ent_cls = type(source_entity)
     status = state.status
     if status != ENTITY_STATUS.CLEAN:
         mongo_coll = self.__get_mongo_collection(ent_cls)
         ent_oid = getattr(source_entity, '_id')
         if status == ENTITY_STATUS.NEW:
             mongo_coll.remove(ent_oid)
             if __debug__:
                 self.__logger.debug("Rollback INSERT entity OID %s."
                                     % ent_oid)
         else:
             if status == ENTITY_STATUS.DELETED:
                 data = transform_incoming(ent_cls, source_entity)
                 mongo_coll.insert(data)
                 if __debug__:
                     self.__logger.debug('Rollback REMOVE entity OID %s: '
                                         '%s.' % (ent_oid, data.items()))
             else:
                 assert status == ENTITY_STATUS.DIRTY
                 EntityState.set_state_data(source_entity,
                                            state.clean_data)
                 data = transform_incoming(ent_cls, source_entity)
                 mongo_coll.update({'_id':ent_oid}, data)
                 if __debug__:
                     self.__logger.debug('Rollback UPDATE entity OID %s: '
                                         '%s.' % (ent_oid, data.items()))
Exemple #2
0
 def __persist(self, state):
     source_entity = state.entity
     ent_cls = type(source_entity)
     status = state.status
     if status != ENTITY_STATUS.CLEAN:
         mongo_coll = self.__get_mongo_collection(ent_cls)
         ent_id = source_entity.id
         ent_oid = getattr(source_entity, '_id')
         if ent_id is None or ent_oid is None:
             raise ValueError('Can not persist entity with ID value set '
                              'to None.')
         if status == ENTITY_STATUS.DELETED:
             mongo_coll.remove(ent_oid)
             if __debug__:
                 self.__logger.debug("REMOVEd entity OID %s." % ent_oid)
         else:
             data = transform_incoming(ent_cls, source_entity)
             if status == ENTITY_STATUS.NEW:
                 mongo_coll.insert(data)
                 if __debug__:
                     self.__logger.debug('INSERTed entity OID %s: %s.'
                                         % (ent_oid, data.items()))
             else:
                 assert status == ENTITY_STATUS.DIRTY
                 mongo_coll.update({'_id':ent_oid}, data)
                 if __debug__:
                     self.__logger.debug('UPDATEed entity OID %s: %s.'
                                         % (ent_oid, data.items()))