6 |
## |
## |
7 |
## ---------------------------------------------------------------------------------------- |
## ---------------------------------------------------------------------------------------- |
8 |
## $Log$ |
## $Log$ |
9 |
|
## Revision 1.8 2002/12/15 02:03:09 joko |
10 |
|
## + fixed logging-messages |
11 |
|
## + additional metadata-checks |
12 |
|
## |
13 |
|
## Revision 1.7 2002/12/13 21:49:34 joko |
14 |
|
## + sub configure |
15 |
|
## + sub checkOptions |
16 |
|
## |
17 |
## Revision 1.6 2002/12/06 04:49:10 jonen |
## Revision 1.6 2002/12/06 04:49:10 jonen |
18 |
## + disabled output-puffer here |
## + disabled output-puffer here |
19 |
## |
## |
50 |
use warnings; |
use warnings; |
51 |
|
|
52 |
use Data::Dumper; |
use Data::Dumper; |
53 |
|
#use Hash::Merge qw( merge ); |
54 |
|
|
55 |
use misc::HashExt; |
use misc::HashExt; |
56 |
use libp qw( md5_base64 ); |
use libp qw( md5_base64 ); |
57 |
use libdb qw( quotesql hash2Sql ); |
use libdb qw( quotesql hash2Sql ); |
66 |
sub new { |
sub new { |
67 |
my $invocant = shift; |
my $invocant = shift; |
68 |
my $class = ref($invocant) || $invocant; |
my $class = ref($invocant) || $invocant; |
69 |
my $self = { @_ }; |
my $self = {}; |
70 |
$logger->debug( __PACKAGE__ . "->new(@_)" ); |
$logger->debug( __PACKAGE__ . "->new(@_)" ); |
71 |
bless $self, $class; |
bless $self, $class; |
72 |
$self->_init(); |
$self->configure(@_); |
73 |
return $self; |
return $self; |
74 |
} |
} |
75 |
|
|
76 |
|
|
77 |
|
sub configure { |
78 |
|
my $self = shift; |
79 |
|
my @args = @_; |
80 |
|
if (!isEmpty(\@args)) { |
81 |
|
my %properties = @_; |
82 |
|
# merge args to properties |
83 |
|
map { $self->{$_} = $properties{$_}; } keys %properties; |
84 |
|
$self->_init(); |
85 |
|
} else { |
86 |
|
#print "no args!", "\n"; |
87 |
|
} |
88 |
|
#print Dumper($self); |
89 |
|
} |
90 |
|
|
91 |
sub _init { |
sub _init { |
92 |
my $self = shift; |
my $self = shift; |
93 |
|
|
94 |
|
$self->{configured} = 1; |
95 |
|
|
96 |
# build new container if necessary |
# build new container if necessary |
97 |
$self->{container} = Data::Storage::Container->new() if !$self->{container}; |
$self->{container} = Data::Storage::Container->new() if !$self->{container}; |
110 |
} |
} |
111 |
|
|
112 |
|
|
113 |
|
sub prepareOptions { |
114 |
|
|
115 |
|
my $self = shift; |
116 |
|
my $opts = shift; |
117 |
|
|
118 |
|
#print Dumper($opts); |
119 |
|
|
120 |
|
$opts->{mode} ||= ''; |
121 |
|
$opts->{erase} ||= 0; |
122 |
|
#$opts->{import} ||= 0; |
123 |
|
|
124 |
|
$logger->info( __PACKAGE__ . "->prepareOptions( source_node $opts->{source_node} mode $opts->{mode} erase $opts->{erase} prepare $opts->{prepare} )"); |
125 |
|
|
126 |
|
if (!$opts->{mapping} || !$opts->{mapping_module}) { |
127 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: No mapping supplied - please check key 'mappings' in BizWorks/Config.pm"); |
128 |
|
} |
129 |
|
|
130 |
|
my $evstring = "use $opts->{mapping_module};"; |
131 |
|
eval($evstring); |
132 |
|
if ($@) { |
133 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: error while trying to access mapping - $@"); |
134 |
|
return; |
135 |
|
} |
136 |
|
|
137 |
|
# resolve mapping metadata (returned from sub) |
138 |
|
my $mapObject = $opts->{mapping_module}->new(); |
139 |
|
#print Dumper($map); |
140 |
|
my $source_node_name = $opts->{source_node}; |
141 |
|
# check if mapping for certain node is contained in mapping object |
142 |
|
if (!$mapObject->can($source_node_name)) { |
143 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: Can't access mapping for node \"$source_node_name\" - please check $opts->{mapping_module}."); |
144 |
|
return; |
145 |
|
} |
146 |
|
my $map = $mapObject->$source_node_name; |
147 |
|
|
148 |
|
# remove asymmetries from $map (patch keys) |
149 |
|
$map->{source_node} = $map->{source}; delete $map->{source}; |
150 |
|
$map->{target_node} = $map->{target}; delete $map->{target}; |
151 |
|
$map->{mapping} = $map->{details}; delete $map->{details}; |
152 |
|
$map->{direction} = $map->{mode}; delete $map->{mode}; |
153 |
|
|
154 |
|
# defaults (mostly for backward-compatibility) |
155 |
|
$map->{source_node} ||= $source_node_name; |
156 |
|
$map->{source_ident} ||= 'storage_method:id'; |
157 |
|
$map->{target_ident} ||= 'property:oid'; |
158 |
|
$map->{direction} ||= $opts->{mode}; # | PUSH | PULL | FULL |
159 |
|
$map->{method} ||= 'checksum'; # | timestamp |
160 |
|
$map->{source_exclude} ||= [qw( cs )]; |
161 |
|
|
162 |
|
# merge map to opts |
163 |
|
map { $opts->{$_} = $map->{$_}; } keys %$map; |
164 |
|
|
165 |
|
#print Dumper($opts); |
166 |
|
|
167 |
|
# TODO: move this to checkOptions... |
168 |
|
|
169 |
|
# check - do we have a target? |
170 |
|
if (!$opts->{target_node}) { |
171 |
|
$logger->warning( __PACKAGE__ . "->prepareOptions: No target given - please check metadata declaration."); |
172 |
|
return; |
173 |
|
} |
174 |
|
|
175 |
|
|
176 |
|
#return $opts; |
177 |
|
return 1; |
178 |
|
|
179 |
|
} |
180 |
|
|
181 |
|
|
182 |
|
sub checkOptions { |
183 |
|
my $self = shift; |
184 |
|
my $opts = shift; |
185 |
|
|
186 |
|
my $result = 1; |
187 |
|
|
188 |
|
# check - do we have a target node? |
189 |
|
if (!$opts->{target_node}) { |
190 |
|
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'target node' could be determined."); |
191 |
|
$result = 0; |
192 |
|
} |
193 |
|
|
194 |
|
# check - do we have a mapping? |
195 |
|
if (!$opts->{mapping} && !$opts->{mapping_module}) { |
196 |
|
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'mapping' could be determined."); |
197 |
|
$result = 0; |
198 |
|
} |
199 |
|
|
200 |
|
return $result; |
201 |
|
|
202 |
|
} |
203 |
|
|
204 |
|
|
205 |
# 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) |
206 |
sub syncNodes { |
sub syncNodes { |
207 |
|
|
208 |
my $self = shift; |
my $self = shift; |
209 |
my $args = shift; |
my $args = shift; |
210 |
|
|
211 |
|
if (!$self->{configured}) { |
212 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Synchronization object is not configured/initialized correctly." ); |
213 |
|
return; |
214 |
|
} |
215 |
|
|
216 |
# remember arguments through the whole processing |
# remember arguments through the whole processing |
217 |
$self->{args} = $args; |
$self->{args} = $args; |
218 |
|
|
235 |
} |
} |
236 |
|
|
237 |
# decompose identifiers for each partner |
# decompose identifiers for each partner |
238 |
# TODO: take this list from already established/given metadata |
# TODO: refactor!!! take this list from already established/given metadata |
239 |
foreach ('source', 'target') { |
foreach ('source', 'target') { |
240 |
|
|
241 |
# get/set metadata for further processing |
# get/set metadata for further processing |
290 |
#print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; |
#print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; |
291 |
} |
} |
292 |
|
|
293 |
|
#print Dumper($self->{meta}); |
294 |
|
|
295 |
$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}" ); |
296 |
|
|
297 |
# build mapping |
# build mapping |
319 |
|
|
320 |
} |
} |
321 |
|
|
322 |
|
#print Dumper($self->{meta}); |
323 |
|
|
324 |
# check partners/nodes: does partner exist / is node available? |
# check partners/nodes: does partner exist / is node available? |
325 |
foreach my $partner (keys %{$self->{meta}}) { |
foreach my $partner (keys %{$self->{meta}}) { |
326 |
next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI'; # for DBD::CSV - re-enable for others |
|
327 |
|
# 1. check partners & storages |
328 |
|
if (!$self->{meta}->{$partner}) { |
329 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Could not find partner '$partner' in configuration metadata." ); |
330 |
|
return; |
331 |
|
} |
332 |
|
|
333 |
|
my $dbkey = $self->{meta}->{$partner}->{dbkey}; |
334 |
|
|
335 |
|
if (!$self->{meta}->{$partner}->{storage}) { |
336 |
|
$logger->critical( __PACKAGE__ . "->syncNodes: Could not access storage of partner '$partner' (named '$dbkey'), looks like a configuration-error." ); |
337 |
|
return; |
338 |
|
} |
339 |
|
|
340 |
|
# TODO: |
341 |
|
# 2. check if partners (and nodes?) are actually available.... |
342 |
|
# eventually pre-check mode of access-attempt (read/write) here to provide an "early-croak" if possible |
343 |
|
|
344 |
|
# 3. check nodes |
345 |
|
next if $self->{meta}->{$partner}->{storage}->{locator}->{type} eq 'DBI'; # HACK for DBD::CSV - re-enable for others |
346 |
|
# get node-name |
347 |
my $node = $self->{meta}->{$partner}->{node}; |
my $node = $self->{meta}->{$partner}->{node}; |
348 |
if (!$self->{meta}->{$partner}->{storage}->existsChildNode($node)) { |
if (!$self->{meta}->{$partner}->{storage}->existsChildNode($node)) { |
349 |
$logger->critical( __PACKAGE__ . "->syncNodes: Could not reach \"$node\" at \"$partner\"." ); |
$logger->critical( __PACKAGE__ . "->syncNodes: Could not reach node \"$node\" at partner \"$partner\"." ); |
350 |
return; |
return; |
351 |
} |
} |
352 |
|
|
353 |
} |
} |
354 |
|
|
355 |
# TODO: |
# TODO: |
929 |
#print $action, "\n"; |
#print $action, "\n"; |
930 |
#$action = "anc"; |
#$action = "anc"; |
931 |
#print "yai", "\n"; |
#print "yai", "\n"; |
932 |
|
|
933 |
|
#print Dumper($map); |
934 |
|
#delete $map->{cs}; |
935 |
|
|
936 |
if (lc($action) eq 'insert') { |
if (lc($action) eq 'insert') { |
937 |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT'); |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_INSERT'); |
938 |
} elsif (lc $action eq 'update') { |
} elsif (lc $action eq 'update') { |
940 |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit); |
$sql_main = hash2Sql($self->{meta}->{$descent}->{node}, $map, 'SQL_UPDATE', $crit); |
941 |
} |
} |
942 |
|
|
943 |
#print "sql: ", $sql_main, "\n"; |
#$sql_main = "UPDATE currencies_csv SET oid='abcdef' WHERE text='Australian Dollar' AND key='AUD';"; |
944 |
#exit; |
#$sql_main = "UPDATE currencies_csv SET oid='huhu2' WHERE ekey='AUD'"; |
945 |
|
|
946 |
|
#print "sql: ", $sql_main, "\n"; |
947 |
|
#exit; |
948 |
|
|
949 |
# transfer data |
# transfer data |
950 |
my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main); |
my $sqlHandle = $self->{meta}->{$descent}->{storage}->sendCommand($sql_main); |
951 |
|
|
952 |
|
#exit; |
953 |
|
|
954 |
# handle errors |
# handle errors |
955 |
if ($sqlHandle->err) { |
if ($sqlHandle->err) { |
956 |
#if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; } |
#if ($self->{args}->{debug}) { print "sql-error with statement: $sql_main", "\n"; } |
1139 |
return; |
return; |
1140 |
} |
} |
1141 |
|
|
1142 |
my $result = $self->{meta}->{$descent}->{storage}->sendQuery({ |
#print "yai!", "\n"; |
1143 |
|
|
1144 |
|
my $query = { |
1145 |
node => $self->{meta}->{$descent}->{node}, |
node => $self->{meta}->{$descent}->{node}, |
1146 |
subnodes => [qw( cs )], |
subnodes => [qw( cs )], |
1147 |
criterias => [ |
criterias => [ |
1149 |
op => 'eq', |
op => 'eq', |
1150 |
val => $ident }, |
val => $ident }, |
1151 |
] |
] |
1152 |
}); |
}; |
1153 |
|
|
1154 |
|
#print Dumper($query); |
1155 |
|
|
1156 |
|
my $result = $self->{meta}->{$descent}->{storage}->sendQuery($query); |
1157 |
|
|
1158 |
my $entry = $result->getNextEntry(); |
my $entry = $result->getNextEntry(); |
1159 |
|
|
1160 |
|
#print Dumper($entry); |
1161 |
|
#print "pers: " . $self->{meta}->{$descent}->{storage}->is_persistent($entry), "\n"; |
1162 |
|
#my $state = $self->{meta}->{$descent}->{storage}->_fetch_object_state($entry, { name => 'TransactionHop' } ); |
1163 |
|
#print Dumper($state); |
1164 |
|
|
1165 |
my $status = $result->getStatus(); |
my $status = $result->getStatus(); |
1166 |
|
|
1167 |
#print Dumper($status); |
#print Dumper($status); |
1176 |
|
|
1177 |
# 1st level - hard error |
# 1st level - hard error |
1178 |
if ($status && $status->{err}) { |
if ($status && $status->{err}) { |
1179 |
$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}" ); |
1180 |
return; |
return; |
1181 |
} |
} |
1182 |
|
|