| 6 |
## |
## |
| 7 |
## ---------------------------------------------------------------------------------------- |
## ---------------------------------------------------------------------------------------- |
| 8 |
## $Log$ |
## $Log$ |
| 9 |
|
## Revision 1.7 2002/12/13 21:49:34 joko |
| 10 |
|
## + sub configure |
| 11 |
|
## + sub checkOptions |
| 12 |
|
## |
| 13 |
|
## Revision 1.6 2002/12/06 04:49:10 jonen |
| 14 |
|
## + disabled output-puffer here |
| 15 |
|
## |
| 16 |
## Revision 1.5 2002/12/05 08:06:05 joko |
## Revision 1.5 2002/12/05 08:06:05 joko |
| 17 |
## + bugfix with determining empty fields (Null) with DBD::CSV |
## + bugfix with determining empty fields (Null) with DBD::CSV |
| 18 |
## + debugging |
## + debugging |
| 46 |
use warnings; |
use warnings; |
| 47 |
|
|
| 48 |
use Data::Dumper; |
use Data::Dumper; |
| 49 |
|
#use Hash::Merge qw( merge ); |
| 50 |
|
|
| 51 |
use misc::HashExt; |
use misc::HashExt; |
| 52 |
use libp qw( md5_base64 ); |
use libp qw( md5_base64 ); |
| 53 |
use libdb qw( quotesql hash2Sql ); |
use libdb qw( quotesql hash2Sql ); |
| 57 |
# get logger instance |
# get logger instance |
| 58 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
| 59 |
|
|
| 60 |
|
$| = 1; |
| 61 |
|
|
| 62 |
sub new { |
sub new { |
| 63 |
my $invocant = shift; |
my $invocant = shift; |
| 64 |
my $class = ref($invocant) || $invocant; |
my $class = ref($invocant) || $invocant; |
| 65 |
my $self = { @_ }; |
my $self = {}; |
| 66 |
$logger->debug( __PACKAGE__ . "->new(@_)" ); |
$logger->debug( __PACKAGE__ . "->new(@_)" ); |
| 67 |
bless $self, $class; |
bless $self, $class; |
| 68 |
$self->_init(); |
$self->configure(@_); |
| 69 |
return $self; |
return $self; |
| 70 |
} |
} |
| 71 |
|
|
| 72 |
|
|
| 73 |
|
sub configure { |
| 74 |
|
my $self = shift; |
| 75 |
|
my @args = @_; |
| 76 |
|
if (!isEmpty(\@args)) { |
| 77 |
|
my %properties = @_; |
| 78 |
|
# merge args to properties |
| 79 |
|
map { $self->{$_} = $properties{$_}; } keys %properties; |
| 80 |
|
$self->_init(); |
| 81 |
|
} else { |
| 82 |
|
#print "no args!", "\n"; |
| 83 |
|
} |
| 84 |
|
#print Dumper($self); |
| 85 |
|
} |
| 86 |
|
|
| 87 |
sub _init { |
sub _init { |
| 88 |
my $self = shift; |
my $self = shift; |
| 89 |
|
|
| 90 |
|
$self->{configured} = 1; |
| 91 |
|
|
| 92 |
# build new container if necessary |
# build new container if necessary |
| 93 |
$self->{container} = Data::Storage::Container->new() if !$self->{container}; |
$self->{container} = Data::Storage::Container->new() if !$self->{container}; |
| 106 |
} |
} |
| 107 |
|
|
| 108 |
|
|
| 109 |
|
sub prepareOptions { |
| 110 |
|
|
| 111 |
|
my $self = shift; |
| 112 |
|
my $opts = shift; |
| 113 |
|
|
| 114 |
|
#print Dumper($opts); |
| 115 |
|
|
| 116 |
|
$opts->{mode} ||= ''; |
| 117 |
|
$opts->{erase} ||= 0; |
| 118 |
|
#$opts->{import} ||= 0; |
| 119 |
|
|
| 120 |
|
$logger->info( __PACKAGE__ . "->prepareOptions( source_node $opts->{source_node} mode $opts->{mode} erase $opts->{erase} prepare $opts->{prepare} )"); |
| 121 |
|
|
| 122 |
|
if (!$opts->{mapping} || !$opts->{mapping_module}) { |
| 123 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: No mapping supplied - please check key 'mappings' in BizWorks/Config.pm"); |
| 124 |
|
} |
| 125 |
|
|
| 126 |
|
my $evstring = "use $opts->{mapping_module};"; |
| 127 |
|
eval($evstring); |
| 128 |
|
if ($@) { |
| 129 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: error while trying to access mapping - $@"); |
| 130 |
|
return; |
| 131 |
|
} |
| 132 |
|
|
| 133 |
|
# resolve mapping metadata (returned from sub) |
| 134 |
|
my $mapObject = $opts->{mapping_module}->new(); |
| 135 |
|
#print Dumper($map); |
| 136 |
|
my $source_node_name = $opts->{source_node}; |
| 137 |
|
# check if mapping for certain node is contained in mapping object |
| 138 |
|
if (!$mapObject->can($source_node_name)) { |
| 139 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: Can't access mapping for node \"$source_node_name\" - please check $opts->{mapping_module}."); |
| 140 |
|
return; |
| 141 |
|
} |
| 142 |
|
my $map = $mapObject->$source_node_name; |
| 143 |
|
|
| 144 |
|
# remove asymmetries from $map (patch keys) |
| 145 |
|
$map->{source_node} = $map->{source}; delete $map->{source}; |
| 146 |
|
$map->{target_node} = $map->{target}; delete $map->{target}; |
| 147 |
|
$map->{mapping} = $map->{details}; delete $map->{details}; |
| 148 |
|
$map->{direction} = $map->{mode}; delete $map->{mode}; |
| 149 |
|
|
| 150 |
|
# defaults (mostly for backward-compatibility) |
| 151 |
|
$map->{source_node} ||= $source_node_name; |
| 152 |
|
$map->{source_ident} ||= 'storage_method:id'; |
| 153 |
|
$map->{target_ident} ||= 'property:oid'; |
| 154 |
|
$map->{direction} ||= $opts->{mode}; # | PUSH | PULL | FULL |
| 155 |
|
$map->{method} ||= 'checksum'; # | timestamp |
| 156 |
|
$map->{source_exclude} ||= [qw( cs )]; |
| 157 |
|
|
| 158 |
|
# merge map to opts |
| 159 |
|
map { $opts->{$_} = $map->{$_}; } keys %$map; |
| 160 |
|
|
| 161 |
|
#print Dumper($opts); |
| 162 |
|
|
| 163 |
|
# TODO: move this to checkOptions... |
| 164 |
|
|
| 165 |
|
# check - do we have a target? |
| 166 |
|
if (!$opts->{target_node}) { |
| 167 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: No target given - please check metadata declaration."); |
| 168 |
|
return; |
| 169 |
|
} |
| 170 |
|
|
| 171 |
|
|
| 172 |
|
#return $opts; |
| 173 |
|
return 1; |
| 174 |
|
|
| 175 |
|
} |
| 176 |
|
|
| 177 |
|
|
| 178 |
|
sub checkOptions { |
| 179 |
|
my $self = shift; |
| 180 |
|
my $opts = shift; |
| 181 |
|
|
| 182 |
|
my $result = 1; |
| 183 |
|
|
| 184 |
|
# check - do we have a target node? |
| 185 |
|
if (!$opts->{target_node}) { |
| 186 |
|
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'target node' could be determined."); |
| 187 |
|
$result = 0; |
| 188 |
|
} |
| 189 |
|
|
| 190 |
|
# check - do we have a mapping? |
| 191 |
|
if (!$opts->{mapping} && !$opts->{mapping_module}) { |
| 192 |
|
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'mapping' could be determined."); |
| 193 |
|
$result = 0; |
| 194 |
|
} |
| 195 |
|
|
| 196 |
|
return $result; |
| 197 |
|
|
| 198 |
|
} |
| 199 |
|
|
| 200 |
|
|
| 201 |
# TODO: some feature to show off the progress of synchronization (cur/max * 100) |
# TODO: some feature to show off the progress of synchronization (cur/max * 100) |
| 202 |
sub syncNodes { |
sub syncNodes { |
| 203 |
|
|
| 204 |
my $self = shift; |
my $self = shift; |
| 205 |
my $args = shift; |
my $args = shift; |
| 206 |
|
|
| 207 |
|
if (!$self->{configured}) { |
| 208 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Synchronization object is not configured/initialized correctly." ); |
| 209 |
|
return; |
| 210 |
|
} |
| 211 |
|
|
| 212 |
# remember arguments through the whole processing |
# remember arguments through the whole processing |
| 213 |
$self->{args} = $args; |
$self->{args} = $args; |
| 214 |
|
|
| 231 |
} |
} |
| 232 |
|
|
| 233 |
# decompose identifiers for each partner |
# decompose identifiers for each partner |
| 234 |
# TODO: take this list from already established/given metadata |
# TODO: refactor!!! take this list from already established/given metadata |
| 235 |
foreach ('source', 'target') { |
foreach ('source', 'target') { |
| 236 |
|
|
| 237 |
# get/set metadata for further processing |
# get/set metadata for further processing |
| 286 |
#print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; |
#print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; |
| 287 |
} |
} |
| 288 |
|
|
| 289 |
|
#print Dumper($self->{meta}); |
| 290 |
|
|
| 291 |
$logger->info( __PACKAGE__ . "->syncNodes: source=$self->{meta}->{source}->{dbkey}/$self->{meta}->{source}->{node} $direction_arrow target=$self->{meta}->{target}->{dbkey}/$self->{meta}->{target}->{node}" ); |
$logger->info( __PACKAGE__ . "->syncNodes: source=$self->{meta}->{source}->{dbkey}/$self->{meta}->{source}->{node} $direction_arrow target=$self->{meta}->{target}->{dbkey}/$self->{meta}->{target}->{node}" ); |
| 292 |
|
|
| 293 |
# build mapping |
# build mapping |
| 315 |
|
|
| 316 |
} |
} |
| 317 |
|
|
| 318 |
|
#print Dumper($self->{meta}); |
| 319 |
|
|
| 320 |
# check partners/nodes: does partner exist / is node available? |
# check partners/nodes: does partner exist / is node available? |
| 321 |
foreach my $partner (keys %{$self->{meta}}) { |
foreach my $partner (keys %{$self->{meta}}) { |
| 322 |
next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI'; # for DBD::CSV - re-enable for others |
next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI'; # for DBD::CSV - re-enable for others |
| 904 |
#print $action, "\n"; |
#print $action, "\n"; |
| 905 |
#$action = "anc"; |
#$action = "anc"; |
| 906 |
#print "yai", "\n"; |
#print "yai", "\n"; |
| 907 |
|
|
| 908 |
|
#print Dumper($map); |
| 909 |
|
#delete $map->{cs}; |
| 910 |
|
|
| 911 |
if (lc($action) eq 'insert') { |
if (lc($action) eq 'insert') { |
| 912 |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT'); |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT'); |
| 913 |
} elsif (lc $action eq 'update') { |
} elsif (lc $action eq 'update') { |
| 915 |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit); |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit); |
| 916 |
} |
} |
| 917 |
|
|
| 918 |
#print "sql: ", $sql_main, "\n"; |
#$sql_main = "UPDATE currencies_csv SET oid='abcdef' WHERE text='Australian Dollar' AND key='AUD';"; |
| 919 |
#exit; |
#$sql_main = "UPDATE currencies_csv SET oid='huhu2' WHERE ekey='AUD'"; |
| 920 |
|
|
| 921 |
|
#print "sql: ", $sql_main, "\n"; |
| 922 |
|
#exit; |
| 923 |
|
|
| 924 |
# transfer data |
# transfer data |
| 925 |
my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main); |
my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main); |
| 926 |
|
|
| 927 |
|
#exit; |
| 928 |
|
|
| 929 |
# handle errors |
# handle errors |
| 930 |
if ($sqlHandle->err) { |
if ($sqlHandle->err) { |
| 931 |
#if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; } |
#if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; } |
| 1114 |
return; |
return; |
| 1115 |
} |
} |
| 1116 |
|
|
| 1117 |
my $result = $self->{meta}->{$descent}->{storage}->sendQuery({ |
#print "yai!", "\n"; |
| 1118 |
|
|
| 1119 |
|
my $query = { |
| 1120 |
node => $self->{meta}->{$descent}->{node}, |
node => $self->{meta}->{$descent}->{node}, |
| 1121 |
subnodes => [qw( cs )], |
subnodes => [qw( cs )], |
| 1122 |
criterias => [ |
criterias => [ |
| 1124 |
op => 'eq', |
op => 'eq', |
| 1125 |
val => $ident }, |
val => $ident }, |
| 1126 |
] |
] |
| 1127 |
}); |
}; |
| 1128 |
|
|
| 1129 |
|
#print Dumper($query); |
| 1130 |
|
|
| 1131 |
|
my $result = $self->{meta}->{$descent}->{storage}->sendQuery($query); |
| 1132 |
|
|
| 1133 |
my $entry = $result->getNextEntry(); |
my $entry = $result->getNextEntry(); |
| 1134 |
|
|
| 1135 |
|
#print Dumper($entry); |
| 1136 |
|
#print "pers: " . $self->{meta}->{$descent}->{storage}->is_persistent($entry), "\n"; |
| 1137 |
|
#my $state = $self->{meta}->{$descent}->{storage}->_fetch_object_state($entry, { name => 'TransactionHop' } ); |
| 1138 |
|
#print Dumper($state); |
| 1139 |
|
|
| 1140 |
my $status = $result->getStatus(); |
my $status = $result->getStatus(); |
| 1141 |
|
|
| 1142 |
#print Dumper($status); |
#print Dumper($status); |
| 1151 |
|
|
| 1152 |
# 1st level - hard error |
# 1st level - hard error |
| 1153 |
if ($status && $status->{err}) { |
if ($status && $status->{err}) { |
| 1154 |
$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}" ); |
| 1155 |
return; |
return; |
| 1156 |
} |
} |
| 1157 |
|
|