<s> from __future__ import division <EOL> from xml . auto_gen import XMLDAOListBase <EOL> from sql . auto_gen import SQLDAOListBase <EOL> from vistrails . core . system import get_elementtree_library <EOL> from vistrails . db import VistrailsDBException <EOL> from vistrails . db . versions . v1_0_4 import version as my_version <EOL> from vistrails . db . versions . v1_0_4 . domain import DBGroup , DBWorkflow , DBVistrail , DBLog , DBRegistry , DBMashuptrail <EOL> root_set = set ( [ DBVistrail . vtType , DBWorkflow . vtType , <EOL> DBLog . vtType , DBRegistry . vtType , DBMashuptrail . vtType ] ) <EOL> ElementTree = get_elementtree_library ( ) <EOL> class DAOList ( dict ) : <EOL> def __init__ ( self ) : <EOL> self [ '<STR_LIT>' ] = XMLDAOListBase ( ) <EOL> self [ '<STR_LIT>' ] = SQLDAOListBase ( ) <EOL> def parse_xml_file ( self , filename ) : <EOL> return ElementTree . parse ( filename ) <EOL> def write_xml_file ( self , filename , tree ) : <EOL> def indent ( elem , level = <NUM_LIT:0> ) : <EOL> i = "<STR_LIT:\n>" + level * "<STR_LIT:U+0020>" <EOL> if len ( elem ) : <EOL> if not elem . text or not elem . text . strip ( ) : <EOL> elem . text = i + "<STR_LIT:U+0020>" <EOL> if not elem . tail or not elem . tail . strip ( ) : <EOL> elem . tail = i <EOL> for elem in elem : <EOL> indent ( elem , level + <NUM_LIT:1> ) <EOL> if not elem . tail or not elem . tail . strip ( ) : <EOL> elem . tail = i <EOL> else : <EOL> if level and ( not elem . tail or not elem . tail . strip ( ) ) : <EOL> elem . tail = i <EOL> indent ( tree . getroot ( ) ) <EOL> tree . write ( filename ) <EOL> def read_xml_object ( self , vtType , node ) : <EOL> return self [ '<STR_LIT>' ] [ vtType ] . fromXML ( node ) <EOL> def write_xml_object ( self , obj , node = None ) : <EOL> res_node = self [ '<STR_LIT>' ] [ obj . vtType ] . toXML ( obj , node ) <EOL> return res_node <EOL> def open_from_xml ( self , filename , vtType , tree = None ) : <EOL> """<STR_LIT>""" <EOL> if tree is None : <EOL> tree = self . parse_xml_file ( filename ) <EOL> vistrail = self . read_xml_object ( vtType , tree . getroot ( ) ) <EOL> return vistrail <EOL> def save_to_xml ( self , obj , filename , tags , version = None ) : <EOL> """<STR_LIT>""" <EOL> root = self . write_xml_object ( obj ) <EOL> if version is None : <EOL> version = my_version <EOL> root . set ( '<STR_LIT:version>' , version ) <EOL> for k , v in tags . iteritems ( ) : <EOL> root . set ( k , v ) <EOL> tree = ElementTree . ElementTree ( root ) <EOL> self . write_xml_file ( filename , tree ) <EOL> def open_from_db ( self , db_connection , vtType , id = None , lock = False , <EOL> global_props = None ) : <EOL> all_objects = { } <EOL> if global_props is None : <EOL> global_props = { } <EOL> if id is not None : <EOL> global_props [ '<STR_LIT:id>' ] = id <EOL> res_objects = self [ '<STR_LIT>' ] [ vtType ] . get_sql_columns ( db_connection , <EOL> global_props , <EOL> lock ) <EOL> if len ( res_objects ) > <NUM_LIT:1> : <EOL> raise VistrailsDBException ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( vtType , id ) ) <EOL> elif len ( res_objects ) <= <NUM_LIT:0> : <EOL> raise VistrailsDBException ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( vtType , id ) ) <EOL> all_objects . update ( res_objects ) <EOL> res = res_objects . values ( ) [ <NUM_LIT:0> ] <EOL> global_props = { '<STR_LIT>' : res . db_id , <EOL> '<STR_LIT>' : res . vtType } <EOL> daoList = [ ] <EOL> dbCommandList = [ ] <EOL> for dao_type , dao in self [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if dao_type in root_set : <EOL> continue <EOL> daoList . append ( [ dao_type , dao , None ] ) <EOL> dbCommand = dao . get_sql_select ( db_connection , global_props , lock ) <EOL> dbCommandList . append ( dbCommand ) <EOL> results = self [ '<STR_LIT>' ] [ vtType ] . executeSQLGroup ( db_connection , <EOL> dbCommandList , True ) <EOL> for i in xrange ( len ( daoList ) ) : <EOL> daoList [ i ] [ <NUM_LIT:2> ] = results [ i ] <EOL> for dao_type , dao , data in daoList : <EOL> current_objs = dao . process_sql_columns ( data , global_props ) <EOL> all_objects . update ( current_objs ) <EOL> if dao_type == DBGroup . vtType : <EOL> for key , obj in current_objs . iteritems ( ) : <EOL> new_props = { '<STR_LIT>' : key [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : global_props [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : global_props [ '<STR_LIT>' ] } <EOL> res_obj = self . open_from_db ( db_connection , <EOL> DBWorkflow . vtType , <EOL> None , lock , new_props ) <EOL> res_dict = { } <EOL> res_dict [ ( res_obj . vtType , res_obj . db_id ) ] = res_obj <EOL> all_objects . update ( res_dict ) <EOL> for key , obj in all_objects . iteritems ( ) : <EOL> if key [ <NUM_LIT:0> ] == vtType and key [ <NUM_LIT:1> ] == id : <EOL> continue <EOL> self [ '<STR_LIT>' ] [ obj . vtType ] . from_sql_fast ( obj , all_objects ) <EOL> for obj in all_objects . itervalues ( ) : <EOL> obj . is_dirty = False <EOL> obj . is_new = False <EOL> return res <EOL> def open_many_from_db ( self , db_connection , vtType , ids , lock = False ) : <EOL> """<STR_LIT>""" <EOL> log_dao = self [ '<STR_LIT>' ] [ vtType ] <EOL> selects = [ log_dao . get_sql_select ( db_connection , { '<STR_LIT:id>' : id } , lock ) <EOL> for id in ids ] <EOL> results = log_dao . executeSQLGroup ( db_connection , selects , True ) <EOL> objects = [ ] <EOL> selects = [ ] <EOL> all_objects_dict = { } <EOL> daoList = [ ] <EOL> selects = [ ] <EOL> global_props = { } <EOL> for id , data in zip ( ids , results ) : <EOL> res_objects = log_dao . process_sql_columns ( data , global_props ) <EOL> if len ( res_objects ) > <NUM_LIT:1> : <EOL> raise VistrailsDBException ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( vtType , id ) ) <EOL> elif len ( res_objects ) <= <NUM_LIT:0> : <EOL> raise VistrailsDBException ( "<STR_LIT>" <EOL> "<STR_LIT>" % ( vtType , id ) ) <EOL> all_objects = { } <EOL> all_objects_dict [ id ] = all_objects <EOL> all_objects . update ( res_objects ) <EOL> objects . append ( res_objects . values ( ) [ <NUM_LIT:0> ] ) <EOL> for dao_type , dao in self [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if dao_type in root_set : <EOL> continue <EOL> daoList . append ( [ id , dao_type , dao , None ] ) <EOL> dbCommand = dao . get_sql_select ( db_connection , global_props , lock ) <EOL> selects . append ( dbCommand ) <EOL> results = self [ '<STR_LIT>' ] [ vtType ] . executeSQLGroup ( db_connection , <EOL> selects , True ) <EOL> for i in xrange ( len ( daoList ) ) : <EOL> daoList [ i ] [ <NUM_LIT:3> ] = results [ i ] <EOL> for id , dao_type , dao , data in daoList : <EOL> all_objects = all_objects_dict [ id ] <EOL> current_objs = dao . process_sql_columns ( data , global_props ) <EOL> all_objects . update ( current_objs ) <EOL> if dao_type == DBGroup . vtType : <EOL> for key , obj in current_objs . iteritems ( ) : <EOL> new_props = { '<STR_LIT>' : key [ <NUM_LIT:1> ] , <EOL> '<STR_LIT>' : global_props [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : global_props [ '<STR_LIT>' ] } <EOL> res_obj = self . open_from_db ( db_connection , <EOL> DBWorkflow . vtType , <EOL> None , lock , new_props ) <EOL> res_dict = { } <EOL> res_dict [ ( res_obj . vtType , res_obj . db_id ) ] = res_obj <EOL> all_objects . update ( res_dict ) <EOL> for id , all_objects in all_objects_dict . iteritems ( ) : <EOL> for key , obj in all_objects . iteritems ( ) : <EOL> if key [ <NUM_LIT:0> ] == vtType and key [ <NUM_LIT:1> ] == id : <EOL> continue <EOL> self [ '<STR_LIT>' ] [ obj . vtType ] . from_sql_fast ( obj , all_objects ) <EOL> for id , dao_type , dao , data in daoList : <EOL> all_objects = all_objects_dict [ id ] <EOL> for obj in all_objects . itervalues ( ) : <EOL> obj . is_dirty = False <EOL> obj . is_new = False <EOL> return objects <EOL> def save_to_db ( self , db_connection , obj , do_copy = False , global_props = None ) : <EOL> if do_copy == '<STR_LIT>' : <EOL> do_copy = True <EOL> elif do_copy and obj . db_id is not None : <EOL> obj . db_id = None <EOL> children = obj . db_children ( ) <EOL> children . reverse ( ) <EOL> if global_props is None : <EOL> global_props = { '<STR_LIT>' : obj . vtType } <EOL> child = children [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . set_sql_columns ( db_connection , child , <EOL> global_props , do_copy ) <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . to_sql_fast ( child , do_copy ) <EOL> global_props = { '<STR_LIT>' : child . db_id , <EOL> '<STR_LIT>' : child . vtType } <EOL> if not do_copy : <EOL> for ( child , _ , _ ) in children : <EOL> for c in child . db_deleted_children ( True ) : <EOL> self [ '<STR_LIT>' ] [ c . vtType ] . delete_sql_column ( db_connection , <EOL> c , <EOL> global_props ) <EOL> child = children . pop ( <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> child . is_dirty = False <EOL> child . is_new = False <EOL> if not len ( children ) : <EOL> return <EOL> dbCommandList = [ ] <EOL> writtenChildren = [ ] <EOL> for ( child , _ , _ ) in children : <EOL> dbCommand = self [ '<STR_LIT>' ] [ child . vtType ] . set_sql_command ( <EOL> db_connection , child , global_props , do_copy ) <EOL> if dbCommand is not None : <EOL> dbCommandList . append ( dbCommand ) <EOL> writtenChildren . append ( child ) <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . to_sql_fast ( child , do_copy ) <EOL> results = self [ '<STR_LIT>' ] [ children [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . vtType ] . executeSQLGroup ( <EOL> db_connection , <EOL> dbCommandList , False ) <EOL> resultDict = dict ( zip ( writtenChildren , results ) ) <EOL> for ( child , _ , _ ) in children : <EOL> if child in resultDict : <EOL> lastId = resultDict [ child ] <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . set_sql_process ( child , <EOL> global_props , <EOL> lastId ) <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . to_sql_fast ( child , do_copy ) <EOL> if child . vtType == DBGroup . vtType : <EOL> if child . db_workflow : <EOL> new_props = { '<STR_LIT>' : global_props [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : global_props [ '<STR_LIT>' ] } <EOL> is_dirty = child . db_workflow . is_dirty <EOL> child . db_workflow . db_entity_type = DBWorkflow . vtType <EOL> child . db_workflow . is_dirty = is_dirty <EOL> self . save_to_db ( db_connection , child . db_workflow , do_copy , <EOL> new_props ) <EOL> def save_many_to_db ( self , db_connection , objList , do_copy = False ) : <EOL> if do_copy == '<STR_LIT>' : <EOL> do_copy = True <EOL> if not len ( objList ) : <EOL> return <EOL> childrenDict = { } <EOL> global_propsDict = { } <EOL> dbCommandList = [ ] <EOL> writtenChildren = [ ] <EOL> for obj in objList : <EOL> if do_copy and obj . db_id is not None : <EOL> obj . db_id = None <EOL> children = obj . db_children ( ) <EOL> children . reverse ( ) <EOL> global_props = { '<STR_LIT>' : obj . vtType } <EOL> child = children [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> dbCommand = self [ '<STR_LIT>' ] [ child . vtType ] . set_sql_command ( <EOL> db_connection , child , global_props , do_copy ) <EOL> if dbCommand is not None : <EOL> dbCommandList . append ( dbCommand ) <EOL> writtenChildren . append ( child ) <EOL> childrenDict [ child ] = children <EOL> global_propsDict [ child ] = global_props <EOL> results = self [ '<STR_LIT>' ] [ children [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . vtType ] . executeSQLGroup ( <EOL> db_connection , <EOL> dbCommandList , False ) <EOL> resultDict = dict ( zip ( writtenChildren , results ) ) <EOL> dbCommandList = [ ] <EOL> writtenChildren = [ ] <EOL> for child , children in childrenDict . iteritems ( ) : <EOL> if child in resultDict : <EOL> lastId = resultDict [ child ] <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . set_sql_process ( <EOL> child , global_propsDict [ child ] , lastId ) <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . to_sql_fast ( child , do_copy ) <EOL> global_props = { '<STR_LIT>' : child . db_id , <EOL> '<STR_LIT>' : child . vtType } <EOL> global_propsDict [ child ] = global_props <EOL> if not do_copy : <EOL> for ( child , _ , _ ) in childrenDict [ child ] : <EOL> for c in child . db_deleted_children ( True ) : <EOL> self [ '<STR_LIT>' ] [ c . vtType ] . delete_sql_column ( db_connection , <EOL> c , <EOL> global_props ) <EOL> child = children . pop ( <NUM_LIT:0> ) [ <NUM_LIT:0> ] <EOL> child . is_dirty = False <EOL> child . is_new = False <EOL> for ( child , _ , _ ) in children : <EOL> dbCommand = self [ '<STR_LIT>' ] [ child . vtType ] . set_sql_command ( <EOL> db_connection , child , global_props , do_copy ) <EOL> if dbCommand is not None : <EOL> dbCommandList . append ( dbCommand ) <EOL> writtenChildren . append ( child ) <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . to_sql_fast ( child , do_copy ) <EOL> results = self [ '<STR_LIT>' ] [ children [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] . vtType ] . executeSQLGroup ( <EOL> db_connection , <EOL> dbCommandList , False ) <EOL> resultDict = dict ( zip ( writtenChildren , results ) ) <EOL> for child , children in childrenDict . iteritems ( ) : <EOL> global_props = global_propsDict [ child ] <EOL> for ( child , _ , _ ) in children : <EOL> if child in resultDict : <EOL> lastId = resultDict [ child ] <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . set_sql_process ( child , <EOL> global_props , <EOL> lastId ) <EOL> self [ '<STR_LIT>' ] [ child . vtType ] . to_sql_fast ( child , do_copy ) <EOL> if child . vtType == DBGroup . vtType : <EOL> if child . db_workflow : <EOL> new_props = { '<STR_LIT>' : global_props [ '<STR_LIT>' ] , <EOL> '<STR_LIT>' : global_props [ '<STR_LIT>' ] } <EOL> is_dirty = child . db_workflow . is_dirty <EOL> child . db_workflow . db_entity_type = DBWorkflow . vtType <EOL> child . db_workflow . is_dirty = is_dirty <EOL> self . save_to_db ( db_connection , child . db_workflow , do_copy , <EOL> new_props ) <EOL> def delete_from_db ( self , db_connection , type , obj_id ) : <EOL> if type not in root_set : <EOL> raise VistrailsDBException ( "<STR_LIT>" % type ) <EOL> id_str = str ( obj_id ) <EOL> for ( dao_type , dao ) in self [ '<STR_LIT>' ] . iteritems ( ) : <EOL> if dao_type not in root_set : <EOL> db_cmd = self [ '<STR_LIT>' ] [ type ] . createSQLDelete ( dao . table , <EOL> { '<STR_LIT>' : type , <EOL> '<STR_LIT>' : id_str } ) <EOL> self [ '<STR_LIT>' ] [ type ] . executeSQL ( db_connection , db_cmd , False ) <EOL> db_cmd = self [ '<STR_LIT>' ] [ type ] . createSQLDelete ( self [ '<STR_LIT>' ] [ type ] . table , <EOL> { '<STR_LIT:id>' : id_str } ) <EOL> self [ '<STR_LIT>' ] [ type ] . executeSQL ( db_connection , db_cmd , False ) <EOL> def serialize ( self , object ) : <EOL> root = self . write_xml_object ( object ) <EOL> return ElementTree . tostring ( root ) <EOL> def unserialize ( self , str , obj_type ) : <EOL> def set_dirty ( obj ) : <EOL> for child , _ , _ in obj . db_children ( ) : <EOL> if child . vtType == DBGroup . vtType : <EOL> if child . db_workflow : <EOL> set_dirty ( child . db_workflow ) <EOL> child . is_dirty = True <EOL> child . is_new = True <EOL> try : <EOL> root = ElementTree . fromstring ( str ) <EOL> obj = self . read_xml_object ( obj_type , root ) <EOL> set_dirty ( obj ) <EOL> return obj <EOL> except SyntaxError , e : <EOL> msg = "<STR_LIT>" % str <EOL> raise VistrailsDBException ( msg ) <EOL> return None </s>
