--- nfo/perl/libs/Data/Transfer/Sync.pm 2002/12/06 04:49:10 1.6 +++ nfo/perl/libs/Data/Transfer/Sync.pm 2002/12/13 21:49:34 1.7 @@ -1,4 +1,4 @@ -## $Id: Sync.pm,v 1.6 2002/12/06 04:49:10 jonen Exp $ +## $Id: Sync.pm,v 1.7 2002/12/13 21:49:34 joko Exp $ ## ## Copyright (c) 2002 Andreas Motl ## @@ -6,6 +6,10 @@ ## ## ---------------------------------------------------------------------------------------- ## $Log: Sync.pm,v $ +## Revision 1.7 2002/12/13 21:49:34 joko +## + sub configure +## + sub checkOptions +## ## Revision 1.6 2002/12/06 04:49:10 jonen ## + disabled output-puffer here ## @@ -42,6 +46,8 @@ use warnings; use Data::Dumper; +#use Hash::Merge qw( merge ); + use misc::HashExt; use libp qw( md5_base64 ); use libdb qw( quotesql hash2Sql ); @@ -56,16 +62,32 @@ sub new { my $invocant = shift; my $class = ref($invocant) || $invocant; - my $self = { @_ }; + my $self = {}; $logger->debug( __PACKAGE__ . "->new(@_)" ); bless $self, $class; - $self->_init(); + $self->configure(@_); return $self; } +sub configure { + my $self = shift; + my @args = @_; + if (!isEmpty(\@args)) { + my %properties = @_; + # merge args to properties + map { $self->{$_} = $properties{$_}; } keys %properties; + $self->_init(); + } else { + #print "no args!", "\n"; + } + #print Dumper($self); +} + sub _init { my $self = shift; + + $self->{configured} = 1; # build new container if necessary $self->{container} = Data::Storage::Container->new() if !$self->{container}; @@ -84,12 +106,109 @@ } +sub prepareOptions { + + my $self = shift; + my $opts = shift; + +#print Dumper($opts); + + $opts->{mode} ||= ''; + $opts->{erase} ||= 0; + #$opts->{import} ||= 0; + + $logger->info( __PACKAGE__ . "->prepareOptions( source_node $opts->{source_node} mode $opts->{mode} erase $opts->{erase} prepare $opts->{prepare} )"); + + if (!$opts->{mapping} || !$opts->{mapping_module}) { + $logger->warning( __PACKAGE__ . "->prepareOptions: No mapping supplied - please check key 'mappings' in BizWorks/Config.pm"); + } + + my $evstring = "use $opts->{mapping_module};"; + eval($evstring); + if ($@) { + $logger->warning( __PACKAGE__ . "->prepareOptions: error while trying to access mapping - $@"); + return; + } + + # resolve mapping metadata (returned from sub) + my $mapObject = $opts->{mapping_module}->new(); + #print Dumper($map); + my $source_node_name = $opts->{source_node}; + # check if mapping for certain node is contained in mapping object + if (!$mapObject->can($source_node_name)) { + $logger->warning( __PACKAGE__ . "->prepareOptions: Can't access mapping for node \"$source_node_name\" - please check $opts->{mapping_module}."); + return; + } + my $map = $mapObject->$source_node_name; + + # remove asymmetries from $map (patch keys) + $map->{source_node} = $map->{source}; delete $map->{source}; + $map->{target_node} = $map->{target}; delete $map->{target}; + $map->{mapping} = $map->{details}; delete $map->{details}; + $map->{direction} = $map->{mode}; delete $map->{mode}; + + # defaults (mostly for backward-compatibility) + $map->{source_node} ||= $source_node_name; + $map->{source_ident} ||= 'storage_method:id'; + $map->{target_ident} ||= 'property:oid'; + $map->{direction} ||= $opts->{mode}; # | PUSH | PULL | FULL + $map->{method} ||= 'checksum'; # | timestamp + $map->{source_exclude} ||= [qw( cs )]; + + # merge map to opts + map { $opts->{$_} = $map->{$_}; } keys %$map; + +#print Dumper($opts); + + # TODO: move this to checkOptions... + + # check - do we have a target? + if (!$opts->{target_node}) { + $logger->warning( __PACKAGE__ . "->prepareOptions: No target given - please check metadata declaration."); + return; + } + + + #return $opts; + return 1; + +} + + +sub checkOptions { + my $self = shift; + my $opts = shift; + + my $result = 1; + + # check - do we have a target node? + if (!$opts->{target_node}) { + $logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'target node' could be determined."); + $result = 0; + } + + # check - do we have a mapping? + if (!$opts->{mapping} && !$opts->{mapping_module}) { + $logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'mapping' could be determined."); + $result = 0; + } + + return $result; + +} + + # TODO: some feature to show off the progress of synchronization (cur/max * 100) sub syncNodes { my $self = shift; my $args = shift; + if (!$self->{configured}) { + $logger->critical( __PACKAGE__ . "->syncNodes: Synchronization object is not configured/initialized correctly." ); + return; + } + # remember arguments through the whole processing $self->{args} = $args; @@ -112,7 +231,7 @@ } # decompose identifiers for each partner - # TODO: take this list from already established/given metadata + # TODO: refactor!!! take this list from already established/given metadata foreach ('source', 'target') { # get/set metadata for further processing @@ -167,6 +286,8 @@ #print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; } +#print Dumper($self->{meta}); + $logger->info( __PACKAGE__ . "->syncNodes: source=$self->{meta}->{source}->{dbkey}/$self->{meta}->{source}->{node} $direction_arrow target=$self->{meta}->{target}->{dbkey}/$self->{meta}->{target}->{node}" ); # build mapping @@ -194,6 +315,8 @@ } +#print Dumper($self->{meta}); + # check partners/nodes: does partner exist / is node available? foreach my $partner (keys %{$self->{meta}}) { next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI'; # for DBD::CSV - re-enable for others @@ -781,6 +904,10 @@ #print $action, "\n"; #$action = "anc"; #print "yai", "\n"; + +#print Dumper($map); +#delete $map->{cs}; + if (lc($action) eq 'insert') { $sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT'); } elsif (lc $action eq 'update') { @@ -788,12 +915,17 @@ $sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit); } - #print "sql: ", $sql_main, "\n"; - #exit; +#$sql_main = "UPDATE currencies_csv SET oid='abcdef' WHERE text='Australian Dollar' AND key='AUD';"; +#$sql_main = "UPDATE currencies_csv SET oid='huhu2' WHERE ekey='AUD'"; + +#print "sql: ", $sql_main, "\n"; +#exit; # transfer data my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main); +#exit; + # handle errors if ($sqlHandle->err) { #if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; } @@ -982,7 +1114,9 @@ return; } - my $result = $self->{meta}->{$descent}->{storage}->sendQuery({ +#print "yai!", "\n"; + + my $query = { node => $self->{meta}->{$descent}->{node}, subnodes => [qw( cs )], criterias => [ @@ -990,9 +1124,19 @@ op => 'eq', val => $ident }, ] - }); + }; + +#print Dumper($query); + + my $result = $self->{meta}->{$descent}->{storage}->sendQuery($query); my $entry = $result->getNextEntry(); + +#print Dumper($entry); +#print "pers: " . $self->{meta}->{$descent}->{storage}->is_persistent($entry), "\n"; +#my $state = $self->{meta}->{$descent}->{storage}->_fetch_object_state($entry, { name => 'TransactionHop' } ); +#print Dumper($state); + my $status = $result->getStatus(); #print Dumper($status); @@ -1007,7 +1151,7 @@ # 1st level - hard error if ($status && $status->{err}) { - $logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - hard error (that's ok)" ); + $logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - hard error (that's ok): $status->{err}" ); return; }