6 |
## |
## |
7 |
## ---------------------------------------------------------------------------------------- |
## ---------------------------------------------------------------------------------------- |
8 |
## $Log$ |
## $Log$ |
9 |
|
## Revision 1.10 2002/12/19 01:07:16 joko |
10 |
|
## + fixed output done via $logger |
11 |
|
## |
12 |
|
## Revision 1.9 2002/12/16 07:02:34 jonen |
13 |
|
## + added comment |
14 |
|
## |
15 |
|
## Revision 1.8 2002/12/15 02:03:09 joko |
16 |
|
## + fixed logging-messages |
17 |
|
## + additional metadata-checks |
18 |
|
## |
19 |
|
## Revision 1.7 2002/12/13 21:49:34 joko |
20 |
|
## + sub configure |
21 |
|
## + sub checkOptions |
22 |
|
## |
23 |
|
## Revision 1.6 2002/12/06 04:49:10 jonen |
24 |
|
## + disabled output-puffer here |
25 |
|
## |
26 |
|
## Revision 1.5 2002/12/05 08:06:05 joko |
27 |
|
## + bugfix with determining empty fields (Null) with DBD::CSV |
28 |
|
## + debugging |
29 |
|
## + updated comments |
30 |
|
## |
31 |
|
## Revision 1.4 2002/12/03 15:54:07 joko |
32 |
|
## + {import}-flag is now {prepare}-flag |
33 |
|
## |
34 |
|
## Revision 1.3 2002/12/01 22:26:59 joko |
35 |
|
## + minor cosmetics for logging |
36 |
|
## |
37 |
|
## Revision 1.2 2002/12/01 04:43:25 joko |
38 |
|
## + mapping deatil entries may now be either an ARRAY or a HASH |
39 |
|
## + erase flag is used now (for export-operations) |
40 |
|
## + expressions to refer to values inside deep nested structures |
41 |
|
## - removed old mappingV2-code |
42 |
|
## + cosmetics |
43 |
|
## + sub _erase_all |
44 |
|
## |
45 |
## Revision 1.1 2002/11/29 04:45:50 joko |
## Revision 1.1 2002/11/29 04:45:50 joko |
46 |
## + initial check in |
## + initial check in |
47 |
## |
## |
56 |
use warnings; |
use warnings; |
57 |
|
|
58 |
use Data::Dumper; |
use Data::Dumper; |
59 |
|
#use Hash::Merge qw( merge ); |
60 |
|
|
61 |
use misc::HashExt; |
use misc::HashExt; |
62 |
use libp qw( md5_base64 ); |
use libp qw( md5_base64 ); |
63 |
use libdb qw( quotesql hash2Sql ); |
use libdb qw( quotesql hash2Sql ); |
64 |
use Data::Transform::OO qw( hash2object ); |
use Data::Transform::Deep qw( hash2object refexpr2perlref ); |
65 |
use Data::Compare::Struct qw( getDifference isEmpty ); |
use Data::Compare::Struct qw( getDifference isEmpty ); |
66 |
|
|
67 |
# get logger instance |
# get logger instance |
68 |
my $logger = Log::Dispatch::Config->instance; |
my $logger = Log::Dispatch::Config->instance; |
69 |
|
|
70 |
|
$| = 1; |
71 |
|
|
72 |
sub new { |
sub new { |
73 |
my $invocant = shift; |
my $invocant = shift; |
74 |
my $class = ref($invocant) || $invocant; |
my $class = ref($invocant) || $invocant; |
75 |
my $self = { @_ }; |
my $self = {}; |
76 |
$logger->debug( __PACKAGE__ . "->new(@_)" ); |
$logger->debug( __PACKAGE__ . "->new(@_)" ); |
77 |
bless $self, $class; |
bless $self, $class; |
78 |
$self->_init(); |
$self->configure(@_); |
79 |
return $self; |
return $self; |
80 |
} |
} |
81 |
|
|
82 |
|
|
83 |
|
sub configure { |
84 |
|
my $self = shift; |
85 |
|
my @args = @_; |
86 |
|
if (!isEmpty(\@args)) { |
87 |
|
my %properties = @_; |
88 |
|
# merge args to properties |
89 |
|
map { $self->{$_} = $properties{$_}; } keys %properties; |
90 |
|
$self->_init(); |
91 |
|
} else { |
92 |
|
#print "no args!", "\n"; |
93 |
|
} |
94 |
|
#print Dumper($self); |
95 |
|
} |
96 |
|
|
97 |
sub _init { |
sub _init { |
98 |
my $self = shift; |
my $self = shift; |
99 |
|
|
100 |
|
$self->{configured} = 1; |
101 |
|
|
102 |
# build new container if necessary |
# build new container if necessary |
103 |
$self->{container} = Data::Storage::Container->new() if !$self->{container}; |
$self->{container} = Data::Storage::Container->new() if !$self->{container}; |
116 |
} |
} |
117 |
|
|
118 |
|
|
119 |
|
sub prepareOptions { |
120 |
|
|
121 |
|
my $self = shift; |
122 |
|
my $opts = shift; |
123 |
|
|
124 |
|
#print Dumper($opts); |
125 |
|
|
126 |
|
$opts->{mode} ||= ''; |
127 |
|
$opts->{erase} ||= 0; |
128 |
|
#$opts->{import} ||= 0; |
129 |
|
|
130 |
|
$logger->notice( __PACKAGE__ . "->prepareOptions( source_node $opts->{source_node} mode $opts->{mode} erase $opts->{erase} prepare $opts->{prepare} )"); |
131 |
|
|
132 |
|
if (!$opts->{mapping} || !$opts->{mapping_module}) { |
133 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: No mapping supplied - please check key 'mappings' in BizWorks/Config.pm"); |
134 |
|
} |
135 |
|
|
136 |
|
my $evstring = "use $opts->{mapping_module};"; |
137 |
|
eval($evstring); |
138 |
|
if ($@) { |
139 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: error while trying to access mapping - $@"); |
140 |
|
return; |
141 |
|
} |
142 |
|
|
143 |
|
# resolve mapping metadata (returned from sub) |
144 |
|
my $mapObject = $opts->{mapping_module}->new(); |
145 |
|
#print Dumper($map); |
146 |
|
my $source_node_name = $opts->{source_node}; |
147 |
|
# check if mapping for certain node is contained in mapping object |
148 |
|
if (!$mapObject->can($source_node_name)) { |
149 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: Can't access mapping for node \"$source_node_name\" - please check $opts->{mapping_module}."); |
150 |
|
return; |
151 |
|
} |
152 |
|
my $map = $mapObject->$source_node_name; |
153 |
|
|
154 |
|
# remove asymmetries from $map (patch keys) |
155 |
|
$map->{source_node} = $map->{source}; delete $map->{source}; |
156 |
|
$map->{target_node} = $map->{target}; delete $map->{target}; |
157 |
|
$map->{mapping} = $map->{details}; delete $map->{details}; |
158 |
|
$map->{direction} = $map->{mode}; delete $map->{mode}; |
159 |
|
|
160 |
|
# defaults (mostly for backward-compatibility) |
161 |
|
$map->{source_node} ||= $source_node_name; |
162 |
|
$map->{source_ident} ||= 'storage_method:id'; |
163 |
|
$map->{target_ident} ||= 'property:oid'; |
164 |
|
$map->{direction} ||= $opts->{mode}; # | PUSH | PULL | FULL |
165 |
|
$map->{method} ||= 'checksum'; # | timestamp |
166 |
|
$map->{source_exclude} ||= [qw( cs )]; |
167 |
|
|
168 |
|
# merge map to opts |
169 |
|
map { $opts->{$_} = $map->{$_}; } keys %$map; |
170 |
|
|
171 |
|
#print Dumper($opts); |
172 |
|
|
173 |
|
# TODO: move this to checkOptions... |
174 |
|
|
175 |
|
# check - do we have a target? |
176 |
|
if (!$opts->{target_node}) { |
177 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: No target given - please check metadata declaration."); |
178 |
|
return; |
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
#return $opts; |
183 |
|
return 1; |
184 |
|
|
185 |
|
} |
186 |
|
|
187 |
|
|
188 |
|
sub checkOptions { |
189 |
|
my $self = shift; |
190 |
|
my $opts = shift; |
191 |
|
|
192 |
|
my $result = 1; |
193 |
|
|
194 |
|
# check - do we have a target node? |
195 |
|
if (!$opts->{target_node}) { |
196 |
|
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'target node' could be determined."); |
197 |
|
$result = 0; |
198 |
|
} |
199 |
|
|
200 |
|
# check - do we have a mapping? |
201 |
|
if (!$opts->{mapping} && !$opts->{mapping_module}) { |
202 |
|
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'mapping' could be determined."); |
203 |
|
$result = 0; |
204 |
|
} |
205 |
|
|
206 |
|
return $result; |
207 |
|
|
208 |
|
} |
209 |
|
|
210 |
|
|
211 |
# 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) |
212 |
sub syncNodes { |
sub syncNodes { |
213 |
|
|
214 |
my $self = shift; |
my $self = shift; |
215 |
my $args = shift; |
my $args = shift; |
216 |
|
|
217 |
|
if (!$self->{configured}) { |
218 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Synchronization object is not configured/initialized correctly." ); |
219 |
|
return; |
220 |
|
} |
221 |
|
|
222 |
# remember arguments through the whole processing |
# remember arguments through the whole processing |
223 |
$self->{args} = $args; |
$self->{args} = $args; |
224 |
|
|
241 |
} |
} |
242 |
|
|
243 |
# decompose identifiers for each partner |
# decompose identifiers for each partner |
244 |
# TODO: take this list from already established/given metadata |
# TODO: refactor!!! take this list from already established/given metadata |
245 |
foreach ('source', 'target') { |
foreach ('source', 'target') { |
246 |
|
|
247 |
# get/set metadata for further processing |
# get/set metadata for further processing |
296 |
#print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; |
#print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; |
297 |
} |
} |
298 |
|
|
299 |
|
#print Dumper($self->{meta}); |
300 |
|
|
301 |
$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}" ); |
302 |
|
|
303 |
# build mapping |
# build mapping |
304 |
|
# incoming: and Array of node map entries (Array or Hash) - e.g. |
305 |
|
# [ 'source:item_name' => 'target:class_val' ] |
306 |
|
# { source => 'event->startDateTime', target => 'begindate' } |
307 |
foreach (@{$self->{args}->{mapping}}) { |
foreach (@{$self->{args}->{mapping}}) { |
308 |
my @key1 = split(':', $_->[0]); |
if (ref $_ eq 'ARRAY') { |
309 |
my @key2 = split(':', $_->[1]); |
my @entry1 = split(':', $_->[0]); |
310 |
push @{$self->{meta}->{$key1[0]}->{childnodes}}, $key1[1]; |
my @entry2 = split(':', $_->[1]); |
311 |
push @{$self->{meta}->{$key2[0]}->{childnodes}}, $key2[1]; |
my $descent = []; |
312 |
|
my $node = []; |
313 |
|
$descent->[0] = $entry1[0]; |
314 |
|
$descent->[1] = $entry2[0]; |
315 |
|
$node->[0] = $entry1[1]; |
316 |
|
$node->[1] = $entry2[1]; |
317 |
|
push @{$self->{meta}->{$descent->[0]}->{childnodes}}, $node->[0]; |
318 |
|
push @{$self->{meta}->{$descent->[1]}->{childnodes}}, $node->[1]; |
319 |
|
} elsif (ref $_ eq 'HASH') { |
320 |
|
foreach my $entry_key (keys %$_) { |
321 |
|
my $entry_val = $_->{$entry_key}; |
322 |
|
push @{$self->{meta}->{$entry_key}->{childnodes}}, $entry_val; |
323 |
|
} |
324 |
|
} |
325 |
|
|
326 |
} |
} |
327 |
|
|
328 |
|
#print Dumper($self->{meta}); |
329 |
|
|
330 |
# check partners/nodes: does partner exist / is node available? |
# check partners/nodes: does partner exist / is node available? |
331 |
foreach my $partner (keys %{$self->{meta}}) { |
foreach my $partner (keys %{$self->{meta}}) { |
332 |
next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI'; # for DBD::CSV - re-enable for others |
|
333 |
|
# 1. check partners & storages |
334 |
|
if (!$self->{meta}->{$partner}) { |
335 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Could not find partner '$partner' in configuration metadata." ); |
336 |
|
return; |
337 |
|
} |
338 |
|
|
339 |
|
my $dbkey = $self->{meta}->{$partner}->{dbkey}; |
340 |
|
|
341 |
|
if (!$self->{meta}->{$partner}->{storage}) { |
342 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Could not access storage of partner '$partner' (named '$dbkey'), looks like a configuration-error." ); |
343 |
|
return; |
344 |
|
} |
345 |
|
|
346 |
|
# TODO: |
347 |
|
# 2. check if partners (and nodes?) are actually available.... |
348 |
|
# eventually pre-check mode of access-attempt (read/write) here to provide an "early-croak" if possible |
349 |
|
|
350 |
|
# 3. check nodes |
351 |
|
next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI'; # HACK for DBD::CSV - re-enable for others |
352 |
|
# get node-name |
353 |
my $node = $self->{meta}->{$partner}->{node}; |
my $node = $self->{meta}->{$partner}->{node}; |
354 |
if (!$self->{meta}->{$partner}->{storage}->existsChildNode($node)) { |
if (!$self->{meta}->{$partner}->{storage}->existsChildNode($node)) { |
355 |
$logger->critical( __PACKAGE__ . "->syncNodes: Could not reach \"$node\" at \"$partner\"." ); |
$logger->critical( __PACKAGE__ . "->syncNodes: Could not reach node \"$node\" at partner \"$partner\"." ); |
356 |
return; |
return; |
357 |
} |
} |
358 |
|
|
359 |
} |
} |
360 |
|
|
361 |
# TODO: |
# TODO: |
379 |
|
|
380 |
# import flag means: prepare the source node to be syncable |
# import flag means: prepare the source node to be syncable |
381 |
# this is useful if there are e.g. no "ident" or "checksum" columns yet inside a DBI like (row-based) storage |
# this is useful if there are e.g. no "ident" or "checksum" columns yet inside a DBI like (row-based) storage |
382 |
if ($self->{args}->{import}) { |
if ($self->{args}->{prepare}) { |
383 |
$self->_prepareNode_MetaProperties('source'); |
$self->_prepareNode_MetaProperties('source'); |
384 |
$self->_prepareNode_DummyIdent('source'); |
$self->_prepareNode_DummyIdent('source'); |
385 |
#return; |
#return; |
386 |
#$self->_erase_all($opts->{source_node}); |
#$self->_erase_all($opts->{source_node}); |
387 |
} |
} |
388 |
|
|
389 |
|
# erase flag means: erase the target |
390 |
|
#if ($opts->{erase}) { |
391 |
|
if ($self->{args}->{erase}) { |
392 |
|
# TODO: move this method to the scope of the synchronization core and wrap it around different handlers |
393 |
|
#print "ERASE", "\n"; |
394 |
|
$self->_erase_all('target'); |
395 |
|
} |
396 |
|
|
397 |
$self->_syncNodes(); |
$self->_syncNodes(); |
398 |
|
|
399 |
} |
} |
418 |
$results ||= $self->_getNodeList('source', $filter); |
$results ||= $self->_getNodeList('source', $filter); |
419 |
} |
} |
420 |
|
|
421 |
# get reference to node list from convenient method provided by corehandle |
# get reference to node list from convenient method provided by CORE-HANDLE |
422 |
#$results ||= $self->{source}->getListUnfiltered($self->{meta}->{source}->{node}); |
#$results ||= $self->{source}->getListUnfiltered($self->{meta}->{source}->{node}); |
423 |
#$results ||= $self->{meta}->{source}->{storage}->getListUnfiltered($self->{meta}->{source}->{node}); |
#$results ||= $self->{meta}->{source}->{storage}->getListUnfiltered($self->{meta}->{source}->{node}); |
424 |
$results ||= $self->_getNodeList('source'); |
$results ||= $self->_getNodeList('source'); |
496 |
my $identOK = $self->_resolveNodeIdent('source'); |
my $identOK = $self->_resolveNodeIdent('source'); |
497 |
#if (!$identOK && lc $self->{args}->{direction} ne 'import') { |
#if (!$identOK && lc $self->{args}->{direction} ne 'import') { |
498 |
if (!$identOK) { |
if (!$identOK) { |
499 |
$logger->critical( __PACKAGE__ . "->syncNodes: Can not synchronize: No ident found in source node, maybe try to \"import\" this node first." ); |
#print Dumper($self->{meta}->{source}); |
500 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: No ident found in source node \"$self->{meta}->{source}->{node}\", try to \"prepare\" this node first?" ); |
501 |
return; |
return; |
502 |
} |
} |
503 |
|
|
504 |
#print "statload", "\n"; |
#print "statload", "\n"; |
505 |
#print "ident: ", $self->{node}->{source}->{ident}, "\n"; |
#print "ident: ", $self->{node}->{source}->{ident}, "\n"; |
506 |
|
#print Dumper($self->{node}); |
507 |
|
|
508 |
my $statOK = $self->_statloadNode('target', $self->{node}->{source}->{ident}); |
my $statOK = $self->_statloadNode('target', $self->{node}->{source}->{ident}); |
509 |
|
|
510 |
|
#print Dumper($self->{node}); |
511 |
|
|
512 |
# mark node as new either if there's no ident or if stat/load failed |
# mark node as new either if there's no ident or if stat/load failed |
513 |
if (!$statOK) { |
if (!$statOK) { |
594 |
$tc->{attempt_new}++; |
$tc->{attempt_new}++; |
595 |
$self->_doTransferToTarget('insert'); |
$self->_doTransferToTarget('insert'); |
596 |
# asymmetry: refetch node from target to re-calculate new ident and checksum (TODO: is IdentAuthority of relevance here?) |
# asymmetry: refetch node from target to re-calculate new ident and checksum (TODO: is IdentAuthority of relevance here?) |
597 |
|
#print Dumper($self->{node}); |
598 |
$self->_statloadNode('target', $self->{node}->{target}->{ident}, 1); |
$self->_statloadNode('target', $self->{node}->{target}->{ident}, 1); |
599 |
$self->_readChecksum('target'); |
$self->_readChecksum('target'); |
600 |
|
|
620 |
# change ident in source (take from target), if transfer was ok and target is an IdentAuthority |
# change ident in source (take from target), if transfer was ok and target is an IdentAuthority |
621 |
# this is (for now) called a "retransmit" indicated by a "r"-character when verbosing |
# this is (for now) called a "retransmit" indicated by a "r"-character when verbosing |
622 |
if ($self->{node}->{status}->{ok} && $self->{meta}->{target}->{storage}->{isIdentAuthority}) { |
if ($self->{node}->{status}->{ok} && $self->{meta}->{target}->{storage}->{isIdentAuthority}) { |
623 |
|
print "r" if $self->{verbose}; |
624 |
#print Dumper($self->{meta}); |
#print Dumper($self->{meta}); |
625 |
#print Dumper($self->{node}); |
#print Dumper($self->{node}); |
626 |
#exit; |
#exit; |
627 |
$self->_doModifySource_IdentChecksum($self->{node}->{target}->{ident}); |
$self->_doModifySource_IdentChecksum($self->{node}->{target}->{ident}); |
|
print "r" if $self->{verbose}; |
|
628 |
} |
} |
629 |
|
|
630 |
print ":" if $self->{verbose}; |
print ":" if $self->{verbose}; |
634 |
print "\n" if $self->{verbose}; |
print "\n" if $self->{verbose}; |
635 |
|
|
636 |
# build user-message from some stats |
# build user-message from some stats |
637 |
my $msg = "stats: $tc"; |
my $msg = "statistics: $tc"; |
638 |
|
|
639 |
if ($tc->{error_per_row}) { |
if ($tc->{error_per_row}) { |
640 |
$msg .= "\n"; |
$msg .= "\n"; |
641 |
$msg .= "errors:" . "\n"; |
$msg .= "errors from \"error_per_row\":" . "\n"; |
642 |
$msg .= Dumper($tc->{error_per_row}); |
$msg .= Dumper($tc->{error_per_row}); |
643 |
} |
} |
644 |
|
|
651 |
} |
} |
652 |
|
|
653 |
|
|
654 |
|
# refactor this as some core-function to do a generic dump resolving data-encapsulations of e.g. Set::Object |
655 |
sub _dumpCompact { |
sub _dumpCompact { |
656 |
my $self = shift; |
my $self = shift; |
657 |
|
|
663 |
my $item = {}; |
my $item = {}; |
664 |
foreach my $key (keys %$_) { |
foreach my $key (keys %$_) { |
665 |
my $val = $_->{$key}; |
my $val = $_->{$key}; |
666 |
|
|
667 |
|
#print Dumper($val); |
668 |
|
|
669 |
if (ref $val eq 'Set::Object') { |
if (ref $val eq 'Set::Object') { |
670 |
#print "========================= SET", "\n"; |
#print "========================= SET", "\n"; |
671 |
#print Dumper($val); |
#print Dumper($val); |
672 |
#print Dumper($val->members()); |
#print Dumper($val->members()); |
673 |
#$val = $val->members(); |
#$val = $val->members(); |
674 |
#$vars->[$count]->{$key} = $val->members() if $val->can("members"); |
#$vars->[$count]->{$key} = $val->members() if $val->can("members"); |
675 |
#$item->{$key} = $val->members() if $val->can("members"); |
#$item->{$key} = $val->members() if $val->can("members"); |
676 |
$item->{$key} = $val->members(); |
$item->{$key} = $val->members(); |
677 |
#print Dumper($vars->[$count]->{$key}); |
#print Dumper($vars->[$count]->{$key}); |
678 |
|
|
679 |
} else { |
} else { |
680 |
$item->{$key} = $val; |
$item->{$key} = $val; |
681 |
} |
} |
682 |
|
|
683 |
} |
} |
684 |
push @data, $item; |
push @data, $item; |
685 |
$count++; |
$count++; |
686 |
} |
} |
687 |
|
|
688 |
#print "Dump:", "\n"; |
#print "Dump:", Dumper(@data), "\n"; |
|
#print Dumper(@data); |
|
689 |
|
|
690 |
$Data::Dumper::Indent = 0; |
$Data::Dumper::Indent = 0; |
691 |
my $result = Dumper(@data); |
my $result = Dumper(@data); |
692 |
$Data::Dumper::Indent = 2; |
$Data::Dumper::Indent = 2; |
693 |
return $result; |
return $result; |
694 |
|
|
695 |
} |
} |
696 |
|
|
697 |
|
|
785 |
for (my $mapidx = 0; $mapidx <= $#childnodes; $mapidx++) { |
for (my $mapidx = 0; $mapidx <= $#childnodes; $mapidx++) { |
786 |
#my $map_right = $self->{args}->{mapping}->{$key}; |
#my $map_right = $self->{args}->{mapping}->{$key}; |
787 |
|
|
788 |
|
$self->{node}->{source}->{propcache} = {}; |
789 |
|
$self->{node}->{target}->{propcache} = {}; |
790 |
|
|
791 |
# get property name |
# get property name |
792 |
$self->{node}->{source}->{propcache}->{property} = $self->{meta}->{source}->{childnodes}->[$mapidx]; |
$self->{node}->{source}->{propcache}->{property} = $self->{meta}->{source}->{childnodes}->[$mapidx]; |
793 |
$self->{node}->{target}->{propcache}->{property} = $self->{meta}->{target}->{childnodes}->[$mapidx]; |
$self->{node}->{target}->{propcache}->{property} = $self->{meta}->{target}->{childnodes}->[$mapidx]; |
805 |
$self->{node}->{source}->{propcache}->{value} = $self->{node}->{source}->{payload}->{$self->{node}->{source}->{propcache}->{property}}; |
$self->{node}->{source}->{propcache}->{value} = $self->{node}->{source}->{payload}->{$self->{node}->{source}->{propcache}->{property}}; |
806 |
} |
} |
807 |
#$self->{node}->{map}->{$key} = $value; |
#$self->{node}->{map}->{$key} = $value; |
808 |
|
|
809 |
|
# detect expression |
810 |
|
# for transferring deeply nested structures described by expressions |
811 |
|
#print "val: $self->{node}->{source}->{propcache}->{value}", "\n"; |
812 |
|
if ($self->{node}->{source}->{propcache}->{property} =~ s/^expr://) { |
813 |
|
|
814 |
|
# create an anonymous sub to act as callback target dispatcher |
815 |
|
my $cb_dispatcher = sub { |
816 |
|
#print "=============== CALLBACK DISPATCHER", "\n"; |
817 |
|
#print "ident: ", $self->{node}->{source}->{ident}, "\n"; |
818 |
|
#return $self->{node}->{source}->{ident}; |
819 |
|
|
820 |
|
}; |
821 |
|
|
822 |
|
|
823 |
|
#print Dumper($self->{node}); |
824 |
|
|
825 |
|
# build callback map for helper function |
826 |
|
#my $cbmap = { $self->{meta}->{source}->{IdentProvider}->{arg} => $cb_dispatcher }; |
827 |
|
my $cbmap = {}; |
828 |
|
my $value = refexpr2perlref($self->{node}->{source}->{payload}, $self->{node}->{source}->{propcache}->{property}, $cbmap); |
829 |
|
$self->{node}->{source}->{propcache}->{value} = $value; |
830 |
|
} |
831 |
|
|
832 |
# encode values dependent on type of underlying storage here - expand cases... |
# encode values dependent on type of underlying storage here - expand cases... |
833 |
my $storage_type = $self->{meta}->{target}->{storage}->{locator}->{type}; |
my $storage_type = $self->{meta}->{target}->{storage}->{locator}->{type}; |
834 |
if ($storage_type eq 'DBI') { |
if ($storage_type eq 'DBI') { |
835 |
# ...for sql |
# ...for sql |
836 |
$self->{node}->{source}->{propcache}->{value} = quotesql($self->{node}->{source}->{propcache}->{value}); |
$self->{node}->{source}->{propcache}->{value} = quotesql($self->{node}->{source}->{propcache}->{value}); |
837 |
} elsif ($storage_type eq 'Tangram') { |
} |
838 |
|
elsif ($storage_type eq 'Tangram') { |
839 |
|
# iso? utf8 already possible? |
840 |
|
|
841 |
} elsif ($storage_type eq 'LDAP') { |
} elsif ($storage_type eq 'LDAP') { |
842 |
# TODO: encode utf8 here? |
# TODO: encode utf8 here? |
843 |
} |
} |
848 |
} |
} |
849 |
} |
} |
850 |
|
|
|
#print "self->{entry}: ", Dumper($self->{node}), "\n"; exit; |
|
|
|
|
|
# for transferring deeply nested structures described by expressions |
|
|
# this currently does not work! |
|
|
# TODO: re-enable this! |
|
|
if ($self->{args}->{mappingV2}) { |
|
|
|
|
|
# apply mapping from $self->{args}->{mappingV2} to $self->{node}->{map} |
|
|
foreach my $mapStep (@{$self->{args}->{mappingV2}}) { |
|
|
|
|
|
# prepare left/right keys/values |
|
|
my $left_key = $mapStep->{left}; |
|
|
my $left_val = _resolveMapStepExpr( $self->{node}->{source}->{payload}, $mapStep->{left} ); |
|
|
my $right_key = $mapStep->{right}; |
|
|
my $right_val = ( $mapStep->{right} ); |
|
|
#print "map: $map_right", "\n"; |
|
|
|
|
|
if ($mapStep->{method}) { |
|
|
if ($mapStep->{method} eq 'v:1') { |
|
|
$left_val = $left_key; |
|
|
} |
|
|
} |
|
|
|
|
|
#$self->{node}->{map}->{$key} = $value; |
|
|
#if ( grep(!/$right_key/, @{$self->{args}->{exclude}}) ) { |
|
|
$self->{node}->{map}->{$right_key} = $self->{R}->quoteSql($left_val); |
|
|
#} |
|
|
} |
|
|
} |
|
851 |
|
|
852 |
# TODO: $logger->dump( ... ); |
# TODO: $logger->dump( ... ); |
853 |
#$logger->debug( "sqlmap:" . "\n" . Dumper($self->{node}->{map}) ); |
#$logger->debug( "sqlmap:" . "\n" . Dumper($self->{node}->{map}) ); |
921 |
} |
} |
922 |
} |
} |
923 |
|
|
924 |
|
|
925 |
|
#print Dumper($self->{meta}); |
926 |
|
|
927 |
# DBI speaks SQL |
# DBI speaks SQL |
928 |
if ($self->{meta}->{$descent}->{storage}->{locator}->{type} eq 'DBI') { |
if ($self->{meta}->{$descent}->{storage}->{locator}->{type} eq 'DBI') { |
936 |
#print $action, "\n"; |
#print $action, "\n"; |
937 |
#$action = "anc"; |
#$action = "anc"; |
938 |
#print "yai", "\n"; |
#print "yai", "\n"; |
939 |
|
|
940 |
|
#print Dumper($map); |
941 |
|
#delete $map->{cs}; |
942 |
|
|
943 |
if (lc($action) eq 'insert') { |
if (lc($action) eq 'insert') { |
944 |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT'); |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT'); |
945 |
} elsif (lc $action eq 'update') { |
} elsif (lc $action eq 'update') { |
947 |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit); |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit); |
948 |
} |
} |
949 |
|
|
950 |
#print "sql: ", $sql_main, "\n"; |
#$sql_main = "UPDATE currencies_csv SET oid='abcdef' WHERE text='Australian Dollar' AND key='AUD';"; |
951 |
#exit; |
#$sql_main = "UPDATE currencies_csv SET oid='huhu2' WHERE ekey='AUD'"; |
952 |
|
|
953 |
|
#print "sql: ", $sql_main, "\n"; |
954 |
|
#exit; |
955 |
|
|
956 |
# transfer data |
# transfer data |
957 |
my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main); |
my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main); |
958 |
|
|
959 |
|
#exit; |
960 |
|
|
961 |
# handle errors |
# handle errors |
962 |
if ($sqlHandle->err) { |
if ($sqlHandle->err) { |
963 |
#if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; } |
#if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; } |
1033 |
hash2object($object, $map); |
hash2object($object, $map); |
1034 |
|
|
1035 |
# ... and re-update@orm. |
# ... and re-update@orm. |
1036 |
|
#print Dumper($object); |
1037 |
$self->{meta}->{$descent}->{storage}->update($object); |
$self->{meta}->{$descent}->{storage}->update($object); |
1038 |
|
|
1039 |
# asymmetry: get ident after insert |
# asymmetry: get ident after insert |
1040 |
# TODO: |
# TODO: |
1041 |
# - just do this if it is an IdentAuthority |
# - just do this if it is an IdentAuthority |
1042 |
# - use IdentProvider metadata here |
# - use IdentProvider metadata here |
1043 |
$self->{node}->{$descent}->{ident} = $self->{meta}->{$descent}->{storage}->id($object); |
#print Dumper($self->{meta}->{$descent}); |
1044 |
|
my $oid = $self->{meta}->{$descent}->{storage}->id($object); |
1045 |
|
#print "oid: $oid", "\n"; |
1046 |
|
$self->{node}->{$descent}->{ident} = $oid; |
1047 |
|
|
1048 |
|
|
1049 |
} elsif (lc $action eq 'update') { |
} elsif (lc $action eq 'update') { |
1140 |
#print "\n", "Attempt to fetch entry implicitely by ident failed: no ident given! This may result in an insert if no write-protection is in the way.", "\n"; |
#print "\n", "Attempt to fetch entry implicitely by ident failed: no ident given! This may result in an insert if no write-protection is in the way.", "\n"; |
1141 |
return; |
return; |
1142 |
} |
} |
1143 |
|
|
1144 |
|
# patch for DBD::CSV |
1145 |
|
if ($ident && $ident eq 'Null') { |
1146 |
|
return; |
1147 |
|
} |
1148 |
|
|
1149 |
|
#print "yai!", "\n"; |
1150 |
|
|
1151 |
my $result = $self->{meta}->{$descent}->{storage}->sendQuery({ |
my $query = { |
1152 |
node => $self->{meta}->{$descent}->{node}, |
node => $self->{meta}->{$descent}->{node}, |
1153 |
subnodes => [qw( cs )], |
subnodes => [qw( cs )], |
1154 |
criterias => [ |
criterias => [ |
1156 |
op => 'eq', |
op => 'eq', |
1157 |
val => $ident }, |
val => $ident }, |
1158 |
] |
] |
1159 |
}); |
}; |
1160 |
|
|
1161 |
|
#print Dumper($query); |
1162 |
|
|
1163 |
|
my $result = $self->{meta}->{$descent}->{storage}->sendQuery($query); |
1164 |
|
|
1165 |
my $entry = $result->getNextEntry(); |
my $entry = $result->getNextEntry(); |
1166 |
|
|
1167 |
|
#print Dumper($entry); |
1168 |
|
#print "pers: " . $self->{meta}->{$descent}->{storage}->is_persistent($entry), "\n"; |
1169 |
|
#my $state = $self->{meta}->{$descent}->{storage}->_fetch_object_state($entry, { name => 'TransactionHop' } ); |
1170 |
|
#print Dumper($state); |
1171 |
|
|
1172 |
my $status = $result->getStatus(); |
my $status = $result->getStatus(); |
1173 |
|
|
1174 |
|
#print Dumper($status); |
1175 |
|
|
1176 |
# TODO: enhance error handling (store inside tc) |
# TODO: enhance error handling (store inside tc) |
1177 |
#if (!$row) { |
#if (!$row) { |
1178 |
# print "\n", "row error", "\n"; |
# print "\n", "row error", "\n"; |
1179 |
# next; |
# next; |
1180 |
#} |
#} |
1181 |
if (($status && $status->{err}) || !$entry) { |
|
1182 |
#$logger->critical( __PACKAGE__ . "->_loadNode (ident=\"$ident\") failed" ); |
# these checks run before actually loading payload- and meta-data to node-container |
1183 |
return; |
|
1184 |
} |
# 1st level - hard error |
1185 |
|
if ($status && $status->{err}) { |
1186 |
|
$logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - hard error (that's ok): $status->{err}" ); |
1187 |
|
return; |
1188 |
|
} |
1189 |
|
|
1190 |
|
# 2nd level - logical (empty/notfound) error |
1191 |
|
if (($status && $status->{empty}) || !$entry) { |
1192 |
|
$logger->debug( __PACKAGE__ . "->_statloadNode (ident=\"$ident\") failed - logical error (that's ok)" ); |
1193 |
|
#print "no entry (logical)", "\n"; |
1194 |
|
return; |
1195 |
|
} |
1196 |
|
|
1197 |
|
#print Dumper($entry); |
1198 |
|
|
1199 |
# was: |
# was: |
1200 |
# $self->{node}->{$descent}->{ident} = $ident; |
# $self->{node}->{$descent}->{ident} = $ident; |
1201 |
# is: |
# is: |
1202 |
# TODO: re-resolve ident from entry via metadata "IdentProvider" |
# TODO: re-resolve ident from entry via metadata "IdentProvider" here - like elsewhere |
1203 |
$self->{node}->{$descent}->{ident} = $ident; |
$self->{node}->{$descent}->{ident} = $ident; |
1204 |
$self->{node}->{$descent}->{payload} = $entry; |
$self->{node}->{$descent}->{payload} = $entry; |
1205 |
|
|
1206 |
} |
} |
1207 |
|
|
1208 |
return 1; |
return 1; |
1226 |
$self->{meta}->{source}->{IdentProvider}->{arg} => $ident_new, |
$self->{meta}->{source}->{IdentProvider}->{arg} => $ident_new, |
1227 |
cs => $self->{node}->{target}->{checksum}, |
cs => $self->{node}->{target}->{checksum}, |
1228 |
}; |
}; |
1229 |
#print Dumper($map); |
|
1230 |
#print Dumper($self->{node}); |
#print Dumper($map); |
1231 |
#exit; |
#print Dumper($self->{node}); |
1232 |
|
#exit; |
1233 |
|
|
1234 |
$self->_modifyNode('source', 'update', $map); |
$self->_modifyNode('source', 'update', $map); |
1235 |
} |
} |
1236 |
|
|
1312 |
} |
} |
1313 |
my $crit = join ' AND ', @crits; |
my $crit = join ' AND ', @crits; |
1314 |
print "p" if $self->{verbose}; |
print "p" if $self->{verbose}; |
1315 |
|
|
1316 |
|
#print Dumper($map); |
1317 |
|
#print Dumper($crit); |
1318 |
|
|
1319 |
$self->_modifyNode($descent, 'update', $map, $crit); |
$self->_modifyNode($descent, 'update', $map, $crit); |
1320 |
$i++; |
$i++; |
1321 |
} |
} |
1331 |
# TODO: handle this in an abstract way (wipe out use of 'source' and/or 'target' inside core) |
# TODO: handle this in an abstract way (wipe out use of 'source' and/or 'target' inside core) |
1332 |
sub _otherSide { |
sub _otherSide { |
1333 |
my $self = shift; |
my $self = shift; |
1334 |
my $side = shift; |
my $descent = shift; |
1335 |
return 'source' if $side eq 'target'; |
return 'source' if $descent eq 'target'; |
1336 |
return 'target' if $side eq 'source'; |
return 'target' if $descent eq 'source'; |
1337 |
return ''; |
return ''; |
1338 |
} |
} |
1339 |
|
|
1340 |
|
sub _erase_all { |
1341 |
|
my $self = shift; |
1342 |
|
my $descent = shift; |
1343 |
|
#my $node = shift; |
1344 |
|
my $node = $self->{meta}->{$descent}->{node}; |
1345 |
|
$self->{meta}->{$descent}->{storage}->eraseAll($node); |
1346 |
|
} |
1347 |
|
|
1348 |
|
|
1349 |
=pod |
=pod |
1350 |
|
|