/[cvs]/nfo/perl/libs/Data/Transfer/Sync/API.pm
ViewVC logotype

Diff of /nfo/perl/libs/Data/Transfer/Sync/API.pm

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Sun Jan 19 01:23:04 2003 UTC revision 1.6 by joko, Fri Feb 14 12:58:47 2003 UTC
# Line 6  Line 6 
6  ##  ##
7  ##    ----------------------------------------------------------------------------------------  ##    ----------------------------------------------------------------------------------------
8  ##    $Log$  ##    $Log$
9    ##    Revision 1.6  2003/02/14 12:58:47  joko
10    ##    + re-enabled the erase-mechanism
11    ##
12    ##    Revision 1.5  2003/02/11 05:26:04  joko
13    ##    + sub _tellWhatIWillDo
14    ##    + re-enabled "branch to execution path for special targets" mechanism
15    ##
16    ##    Revision 1.4  2003/02/09 05:03:02  joko
17    ##    + minor fix regarding namespace of api versioning extension module
18    ##
19    ##    Revision 1.3  2003/02/09 04:59:27  joko
20    ##    + api versioning mechanism
21    ##    + major structure changes
22    ##    - refactored code to sister modules
23    ##
24    ##    Revision 1.2  2003/01/20 16:59:48  joko
25    ##    + cosmetics and debugging
26    ##
27  ##    Revision 1.1  2003/01/19 01:23:04  joko  ##    Revision 1.1  2003/01/19 01:23:04  joko
28  ##    + new from Data/Transfer/Sync.pm  ##    + new from Data/Transfer/Sync.pm
29  ##  ##
30  ##    ----------------------------------------------------------------------------------------  ##    ----------------------------------------------------------------------------------------
31    
32    
33  package Data::Transfer::Sync::API;  package Data::Transfer::Sync::API;
34    
35  use strict;  use strict;
36  use warnings;  use warnings;
37    
38    use base qw( DesignPattern::Bridge );
39    
40  use mixin::with qw( Data::Transfer::Sync );  use mixin::with qw( Data::Transfer::Sync );
41    
42    
43  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   main  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -   main
44    
45  use Data::Dumper;  use Data::Dumper;
46    use Hash::Merge qw( merge );
47  use Data::Compare::Struct qw( getDifference isEmpty );  
48  use Data::Transform::Deep qw( merge );  use Data::Compare::Struct qw( isEmpty );
49    
50  # get logger instance  # get logger instance
51  my $logger = Log::Dispatch::Config->instance;  my $logger = Log::Dispatch::Config->instance;
52    
53  sub _init {  
54    sub api_constructor {
55    my $self = shift;    my $self = shift;
56      $logger->debug( __PACKAGE__ . "->api_constructor: Loading API");
57      $self->_loadVersionExtensions();
58  }  }
   
 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;  
   }  
59    
60    # check - do we have a mapping?  
61    if (!$opts->{mapping} && !$opts->{mapping_module}) {  sub _loadVersionExtensions {
62      $logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'mapping' could be determined.");    my $self = shift;
63      $result = 0;    my $syncVersion = $self->{sync_version};
64    }    $syncVersion ||= '';
65        $logger->debug( __PACKAGE__ . "->loadVersionExtensions( version='$syncVersion' )");
66    return $result;    #print Dumper($self);
67        #exit;
68      my $module = "API::$syncVersion";
69      $self->load($module);
70  }  }
71    
72  sub checkOptionsV2 {  
73    sub configure {
74    my $self = shift;    my $self = shift;
75    
76  #print Dumper($self->{options});    $logger->debug( __PACKAGE__ . "->configure");
77    
78    my $result = 1;    my @args = @_;
79    
80    # check - do we have a target node?    if (!isEmpty(\@args)) {
81    if (!$self->{options}->{target}->{nodeName}) {      my %properties = @_;
82      $logger->warning( __PACKAGE__ . "->checkOptionsV2: No target given - please check metadata declaration.");      # merge args to properties
83      $result = 0;      #map { $self->{$_} = $properties{$_}; } keys %properties;
84    }  #print Dumper($self);
85      #print Dumper(\%properties);
86    # check - do we have a mapping?      if ($self->{options}) {
87    if (!$self->{options}->{fieldmap}) {      my $options_new = merge($self->{options}, \%properties);
88      $logger->warning( __PACKAGE__ . "->checkOptionsV2: Error while resolving resource metadata - no 'fieldmap' could be determined.");  #print Dumper($options_new);
89      $result = 0;      $self->{options} = $options_new;
90    #print Dumper($self->{options});
91        } else {
92          $self->{options} = \%properties;
93        }
94        $self->_init();
95        #$self->_initV1();
96      } else {
97        #print "no args!", "\n";
98    }    }
     
   # TODO: extend!  
99    
100    return $result;  #print Dumper($self);
101    #exit;
102    
103      $self->{state}->{configured} = 1;
104      return 1;
105  }  }
106    
   
 sub prepareOptions {  
107    
108    sub setArguments {
109    my $self = shift;    my $self = shift;
110    my $opts = shift;    my $args_raw = shift;
111      $self->{args_raw} = $args_raw;
112    }
113    
114    sub readArguments {
115      my $self = shift;
116    
117        my %syncConfig;
118        tie %syncConfig, 'Tie::IxHash';
119        %syncConfig = (
120          map => {
121            moduleName => $self->{args_raw}->{'mapping-module'},
122          },
123          source => {
124            dbKey => $self->{args_raw}->{source},
125            nodeType => $self->{args_raw}->{'source-type'},
126            nodeName => $self->{args_raw}->{'source-node'},
127          },
128          target => {
129            dbKey => $self->{args_raw}->{target},
130            nodeName => $self->{args_raw}->{'target-node'},
131          },
132          process => {
133            mode => $self->{args_raw}->{mode},
134            erase => $self->{args_raw}->{erase},
135            import => $self->{args_raw}->{import},
136            prepare => $self->{args_raw}->{prepare},
137          },
138    #      metadata => {
139    #        config => $self->{config_metadata},
140    #      }
141        );
142    
143      $self->{args} = \%syncConfig;
144    
145    }
146    
147    
148    # TODO: some feature to show off the progress of synchronization (cur/max * 100)
149    sub syncNodes {
150    
151      my $self = shift;
152      my $args = shift;
153    
154      $logger->notice( "==========================   " . __PACKAGE__ . "->syncNodes   ==============");
155      $logger->debug( __PACKAGE__ . "->syncNodes: starting" );
156    
157  #print Dumper($opts);  #print Dumper($self);
158  #exit;  #exit;
159    
160    $opts->{mode} ||= '';  #print Dumper($self->{options});
161    $opts->{erase} ||= 0;    $self->_prepareOptions();
   $opts->{prepare} ||= 0;  
   #$opts->{import} ||= 0;  
     
   if (!$opts->{source_node}) {  
     $logger->error( __PACKAGE__ . "->prepareOptions failed: Please specify source-node!");  
     return;  
   }  
     
   $logger->notice( __PACKAGE__ . "->prepareOptions( source_node $opts->{source_node} mode $opts->{mode} erase $opts->{erase} prepare $opts->{prepare} )");  
162    
163    #if (!$opts->{mapping} || !$opts->{mapping_module}) {    # trace
164    if (!$opts->{mapping}) {      #print Dumper($self->{options});
165      $logger->warning( __PACKAGE__ . "->prepareOptions: No mapping supplied - please check key 'mappings' in global configuration or specify additional argument '--mapping'.");      #exit;
     #return;  
   }  
166    
167    $opts->{mapping_module} ||= $opts->{mapping};    if (!$self->checkOptions()) {
168    my $evstring = "use $opts->{mapping_module};";      $logger->critical( __PACKAGE__ . "->syncNodes: 'Data::Transfer::Sync::checkOptions' failed.");
   eval($evstring);  
   if ($@) {  
     $logger->warning( __PACKAGE__ . "->prepareOptions: error while trying to access mapping - $@");  
169      return;      return;
170    }    }
171    
172    # resolve mapping metadata (returned from sub)    if (!$self->{state}->{configured}) {
173    my $mapObject = $opts->{mapping_module}->new();      $logger->critical( __PACKAGE__ . "->syncNodes: Synchronization object is not configured/initialized correctly." );
   #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}.");  
174      return;      return;
175    }    }
   my $map = $mapObject->$source_node_name;  
176    
177  #print Dumper($map);    #print Dumper($args);
178    
179    # check here if "target" is actually a CODEref - in this case: resolve it - deprecated!!! ???    # remember arguments through the whole processing
180    if (ref $map->{target} eq 'CODE') {    $self->{args} = $args;
181      $map->{target} = $map->{target}->($source_node_name);  
182    }    # hash to hold and/or fill in metadata required for the processing
183      $self->{meta} = {};
184        
185    # resolve expressions (on nodename-level) here    # hash to sum up results
186    if ($map->{target} =~ m/^(code|expr):(.+?)$/) {    # TODO: re-implement! (sync-statistics???)
     my $target_dynamic_type = $1;  
     my $target_dynamic_expression = $2;  
     if (lc $target_dynamic_type eq 'code') {  
 #      $map->{target} = $mapObject->$target_dynamic_expression($map);  
     }  
   }  
187    
188    # remove asymmetries from $map (patch keys)    #print Dumper($self->{options}->{process});
   $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 )];  
189    
190    # merge map to opts    # detect synchronization method to determine which optical symbol (directed arrow) to use
191    map { $opts->{$_} = $map->{$_}; } keys %$map;    my $mode = $self->{args}->{mode};   # V1
192      $mode ||= $self->{options}->{process}->{mode};    # V2
193      my $direction_arrow = $self->_getDirectedArrow($mode);
194    
195      # determine code versions
196        my $code_metadata_version;
197        # first, try to use version supplied by mapping-metadata
198        $code_metadata_version = $self->{options}->{metadata}->{version};
199        # if not set, inherit from global 'sync_version'
200        $code_metadata_version ||= $self->{sync_version};
201    
202      # load additional code from versioned namespace into current instance
203        my $dynModule = "Metadata::$code_metadata_version";
204        $self->load($dynModule);
205            
206  #print Dumper($opts);    # build metadata using versioned code and stuff
207        $self->options2metadata();
208        $self->options2metadata_accessor();
209    
210      # branch to execution path for special targets
211      # detect for option 'handler' which could be a CODEref
212        if ($self->{options}->{handler} && ref $self->{options}->{handler} eq 'CODE') {
213          $logger->info( __PACKAGE__ . "->syncNodes: Running (special handler code - no generic sync!) on '$self->{options}->{target}->{dbKey}' with MODE $self->{options}->{process}->{mode}, NODE $self->{options}->{target}->{address}");
214          #print Dumper($self);
215          #exit;
216          # don't do this any more - it wasn't very nice somehow ...
217          #$self->{options}->{handler}->($self->{app}, $self->{options});
218          # .... now: better let the parent application scope handle this via callback
219            # not any more required for this: $self->{app} inside here (which isn't the app we mean here)
220            # required for this: getting the options out of here: establish some getter method! ($self->getOptions(...))
221            # so....
222            #$self->{__bridge}->{parent_ref}->
223          
224          # ahh okay, DesignPattern::Bridge moves closer to some Class::Inner!???
225            # so...
226            # similar like above - but it isn't very nice anyway ...  (no privateness, but: who cares?)
227            #print Dumper($self->{__bridge});
228            # just take the global application instance and
229            # throw it into the context of the mapping module - this is heavy!  ;-) (but again, who cares...)
230            # TODO: handle this more abstract *sometime*
231            #$self->{options}->{handler}->($self->{__bridge}->{parent}->{app}, $self->{options});
232            $self->{options}->{handler}->($self->{__bridge}->{parent}->{process}, $self->{options});
233          
234          return;
235        }
236    
237      # TODO: execution path branch V2!!!
238        # option1: wrap this via callback to parent scope (like current impl. mechanism)
239        # option2: branch directly from here (this needs refactoring of the sub handler)
240    
241      # tracing
242        #print Dumper($self);
243        #exit;
244    
245      $logger->info( __PACKAGE__ . "->syncNodes: source=$self->{meta}->{source}->{dbKey}/$self->{meta}->{source}->{nodeName} [$self->{meta}->{source}->{nodeType}] $direction_arrow target=$self->{meta}->{target}->{dbKey}/$self->{meta}->{target}->{nodeName} [$self->{meta}->{target}->{nodeType}]" );
246    
247      return if !$self->buildFieldmapping();
248      return if !$self->_touchNodeSet();
249      return if !$self->_prepare_sync();
250      $self->_tellWhatIWillDo();
251    
252      # tracing
253        #print Dumper($self);
254        #print Dumper($self->{args});
255        #print Dumper($self->{options});
256        #print Dumper($self->{meta});
257        #print Dumper($self->{metadata});
258        #exit;
259    
260    # TODO: move this to checkOptions...    # finally, tell the core to start the synchronization process
261      $self->_run();
262        
263    # check - do we have a target?  }
   if (!$opts->{target_node}) {  
     $logger->warning( __PACKAGE__ . "->prepareOptions: No target given - please check metadata declaration.");  
     return;  
   }  
264    
265    
266    #return $opts;  my $c_string_default = '';
267    return 1;  sub c_string {
268      my $value = shift;
269      $value ||= "[$c_string_default]";
270      return $value;
271    }
272    
273    sub _tellWhatIWillDo {
274      my $self = shift;
275    
276    
277    #return;
278      
279      # trace
280        #print Dumper($self->{meta});
281        #exit;
282      
283      $c_string_default = 'n/a';
284      my $source = c_string($self->{opt}->{'source'});
285      my $source_node = c_string($self->{opt}->{'source-node'});
286      my $source_type = c_string($self->{opt}->{'source-type'});
287      my $target = c_string($self->{opt}->{'target'});
288      my $target_node = c_string($self->{opt}->{'target-node'});
289      my $target_type = c_string($self->{opt}->{'target-type'});
290      
291      my $mapping_module = c_string($self->{opt}->{'mapping-module'});
292      my $mode = uc c_string($self->{opt}->{'mode'});
293    
294      #my $ql = "$mode INTO $source NODE $source_node TYPE $source_type SELECT NODE $target_node TYPE $target_type FROM $target USING MODULE $mapping_module;";
295      #$logger->notice( __PACKAGE__ . ": $ql" );
296      my $ql = <<EOT;
297    
298      FETCH DATA
299        FROM STORAGE $self->{meta}->{source}->{dbKey}
300        AT NODE $self->{meta}->{source}->{accessorName}.$self->{meta}->{source}->{nodeName}
301        USING IDENTIFIER $self->{meta}->{source}->{IdentProvider}->{method}.$self->{meta}->{source}->{IdentProvider}->{arg}
302      CONVERT DATA
303        CAST FROM $self->{meta}->{source}->{nodeType} TO $self->{meta}->{target}->{nodeType}
304        MAP ATTRIBUTES FROM @{$self->{meta}->{source}->{childnodes}} TO @{$self->{meta}->{target}->{childnodes}}
305      STORE DATA
306        TO STORAGE $self->{meta}->{target}->{dbKey}
307        AT NODE $self->{meta}->{target}->{accessorName}.$self->{meta}->{target}->{nodeName}
308        USING IDENTIFIER $self->{meta}->{target}->{IdentProvider}->{method}.$self->{meta}->{target}->{IdentProvider}->{arg}
309    EOT
310      
311      
312      $logger->notice( $ql );
313      
314      #exit;
315      return;
316      
317      my $actioning = ucfirst $self->{opt}->{'action'} . 'ing';
318      
319      # FIXME: this is weird!
320      my $long = <<EOT;
321    
322        - $actioning data of type $target_type and
323          filtered by $target_node from the storage named $target
324          to the storage named $source - filtered by $source_node.
325        - Will attempt to convert the data to $source_type.
326    EOT
327      chomp($long);
328      $logger->notice( __PACKAGE__ . ": $long" );
329      
330  }  }
331    
332  sub prepareOptionsV2 {  
333    sub _prepareOptions {
334    
335    my $self = shift;    my $self = shift;
336    my $opts = shift;    
337      my $opts = $self->{args};
338    
339    # patch options    # patch options
340        $opts->{source}->{nodeName} ||= '';
341        $opts->{target}->{nodeName} ||= '';
342      $opts->{process}->{mode} ||= '';      $opts->{process}->{mode} ||= '';
     $opts->{process}->{prepare} ||= 0;  
343      $opts->{process}->{erase} ||= 0;      $opts->{process}->{erase} ||= 0;
344        $opts->{process}->{prepare} ||= 0;
345    
346      # defaults (mostly for backward-compatibility to V1 -
347      # but code mungled here out of prepareOptions_V1 from Version::V1)
348        $opts->{metadata}->{syncMethod} ||= 'checksum';                # | timestamp
349        $opts->{source}->{ident} ||= 'storage_method:id';
350        $opts->{source}->{exclude} ||= [qw( cs )];
351        $opts->{target}->{ident} ||= 'property:oid';
352        #$map->{source_node} ||= $source_node_name;
353        #$map->{direction} ||= $opts->{mode};         # | PUSH | PULL | FULL
354    
355    # pre-check options    # pre-check options
356      if (!$self->_preCheckOptions($opts)) {      if (!$self->_preCheckOptions($opts)) {
357        $logger->error( __PACKAGE__ . "->prepareOptionsV2: _preCheckOptions failed.");        $logger->error( __PACKAGE__ . "->_prepareOptions: _preCheckOptions failed.");
358        return;        return;
359      }      }
360    
361    # inform user about option preparation    # inform user about option preparation
362    $logger->notice( __PACKAGE__ . "->prepareOptionsV2( mode='$opts->{process}->{mode}', source->node='$opts->{source}->{nodeName}', target->node='$opts->{target}->{nodeName}', erase='$opts->{process}->{erase}', prepare='$opts->{process}->{prepare}' )");    $logger->debug( __PACKAGE__ . "->_prepareOptions( source.node='$opts->{source}->{nodeName}', target.node='$opts->{target}->{nodeName}', mode='$opts->{process}->{mode}', e='$opts->{process}->{erase}', p='$opts->{process}->{prepare}' )");
363    
364    # try to load mapping-metadata-container    # try to load mapping-metadata-container
365    #my $mapObject = getNewPerlObjectByPkgName($opts->{map}->{moduleName});      # How? Create a new instance of the given
366    my $mapObject = DesignPattern::Object->fromPackage($opts->{map}->{moduleName});      # perl module/package name in ->{...}->{moduleName}.
367        # This instance is used later in the innards of the sync,
368        # that's why the module (class) should have a certain layout
369        # enabling the core to use it for gathering metadata while processing.
370        my $mapObject = DesignPattern::Object->fromPackage($opts->{map}->{moduleName});
371    
372    # try to resolve map from metadata-container    # try to resolve map from metadata-container
373    
# Line 205  sub prepareOptionsV2 { Line 375  sub prepareOptionsV2 {
375      my $source_nodeType = $opts->{source}->{nodeType};      my $source_nodeType = $opts->{source}->{nodeType};
376            
377      # check if mapping for certain node is contained in mapping object      # check if mapping for certain node is contained in mapping object
378      if (!$mapObject->can($source_nodeType)) {      if (!$mapObject || !$mapObject->can($source_nodeType)) {
379        $logger->warning( __PACKAGE__ . "->prepareOptionsV2: Can't access mapping for source-type=\"$source_nodeType\" - please check \"$opts->{map}->{moduleName}\".");        $logger->warning( __PACKAGE__ . "->_prepareOptions: Can't access mapping for source-type=\"$source_nodeType\" - please check \"$opts->{map}->{moduleName}\".");
380        return;        return;
381      }      }
382    
   
383      # get map      # get map
384      my $map = $mapObject->$source_nodeType;      my $map = $mapObject->$source_nodeType;
385      #print Dumper($map);      #print Dumper($map);
386        
387  =pod        my $map_version = $map->{metadata}->{version};
388        # FIXME: backward compatibility
389        if (!$map_version) {
390          $self->options_to_V2($map);
391        }
392    
393        # trace
394          #print Dumper($map);
395          #exit;
396          #print "ref: ", ref $map->{target}, "\n";
397          #print "target: ", $map->{target}, "\n";
398        
399      # check here if "target" is actually a CODEref - in this case: resolve it - deprecated!!! ???      # check here if "target" is actually a CODEref - in this case: resolve it - deprecated!!! ???
400      if (ref $map->{target}->{address} eq 'CODE') {      if (ref $map->{target}->{address} eq 'CODE') {
401        $map->{target}->{address} = $map->{target}->{address}->($source_nodeType);        $map->{target}->{address} = $map->{target}->{address}->($source_nodeType);
402      }      }
403            
404      # resolve expressions (on nodename-level) here      # resolve expressions (on nodename-level) here
405      if ($map->{target}->{address} =~ m/^(code|expr):(.+?)$/) {      elsif ($map->{target}->{address} =~ m/^(code|expr):(.+?)$/) {
406        my $target_dynamic_type = $1;        my $target_dynamic_type = $1;
407        my $target_dynamic_expression = $2;        my $target_dynamic_expression = $2;
408        if (lc $target_dynamic_type eq 'code') {        if (lc $target_dynamic_type eq 'code') {
409          $map->{target} = $mapObject->$target_dynamic_expression($map);          $map->{target} = $mapObject->$target_dynamic_expression($map);
410        }        }
411      }      }
 =cut  
412    
413    #map { $opts->{$_} = $map->{$_}; } keys %$map;    # merging - V1
414    my $opts_merged = merge( $opts, $map );      #map { $opts->{$_} = $map->{$_}; } keys %$map;
415        # trace
416          #print Dumper($self->{options});
417      
418      # merging - V2
419    
420        # merge local map with local opts
421        
422          # delete undef'd items in $map
423        
424          # enable cloning
425          # FIXME: do we really need cloning here? trade safety/encapsulation for speed?
426          Hash::Merge::set_clone_behavior(1);
427          Hash::Merge::set_behavior( 'RIGHT_PRECEDENT' );
428          #Hash::Merge::set_behavior( 'STORAGE_PRECEDENT' );
429          #Hash::Merge::set_behavior( 'RETAINMENT_PRECEDENT' );
430          # TODO: add an option to Hash::Merge not to overwrite set items with undefined/empty/not assigned ones
431          my $locals_merged = merge( $opts, $map );
432    
433        # trace
434          #print Dumper($opts);
435          #print Dumper($map);
436          #print Dumper($locals_merged);
437          #exit;
438    
439        # merge local-merged ones with instance-wide options
440          Hash::Merge::set_clone_behavior(0);
441          $self->{options} = merge( $self->{options}, $locals_merged );
442      
443        # trace
444          #print Dumper($self->{options});
445          #exit;
446      
447    
   $self->{options} = $opts_merged;  
448    $self->{state}->{options_ready} = 1;    $self->{state}->{options_ready} = 1;
449    
450    return 1;    return 1;
451    
452  }  }
   
   
 sub configure {  
   my $self = shift;  
   my @args = @_;  
   if (!isEmpty(\@args)) {  
     my %properties = @_;  
     # merge args to properties  
     map { $self->{$_} = $properties{$_}; } keys %properties;  
     $self->_init();  
     $self->_initV1();  
   } else {  
     #print "no args!", "\n";  
   }  
   #print Dumper($self);  
   $self->{state}->{configured} = 1;  
   return 1;  
 }  
   
 sub configureV2 {  
   my $self = shift;  
   my @args = @_;  
   if (!isEmpty(\@args)) {  
     my %properties = @_;  
     # merge args to properties  
     #map { $self->{$_} = $properties{$_}; } keys %properties;  
     $self->{options} = merge($self->{options}, \%properties);  
     $self->_init();  
     #$self->_initV1();  
   } else {  
     #print "no args!", "\n";  
   }  
453    
 #print Dumper($self);  
454    
455    $self->{state}->{configured} = 1;  sub _preCheckOptions {
   return 1;  
 }  
   
   
 # TODO: some feature to show off the progress of synchronization (cur/max * 100)  
 sub syncNodes {  
456    
457    my $self = shift;    my $self = shift;
458    my $args = shift;    my $opts = shift;
459    
460    if (!$self->{state}->{configured}) {    # trace
461      $logger->critical( __PACKAGE__ . "->syncNodes: Synchronization object is not configured/initialized correctly." );      #print Dumper($opts);
462        #exit;
463        
464      if (!$opts->{process}->{mode}) {
465        $logger->error( __PACKAGE__ . "->_preCheckOptions failed: Please specify \"--action=(load|save)\".");
466      return;      return;
467    }    }
468    
469    # remember arguments through the whole processing    # the type of the to-be-synced item
470    $self->{args} = $args;    if (!$opts->{source}->{nodeType}) {
471        $logger->error( __PACKAGE__ . "->_preCheckOptions failed: Please specify \"source-type\".");
472    $logger->debug( __PACKAGE__ . "->syncNodes: starting" );      return;
   
   # hash to hold and/or fill in metadata required for the processing  
   $self->{meta} = {};  
     
   # hash to sum up results  
   my $direction_arrow = '';  
   
   # detect synchronization method to determine which optical symbol (directed arrow) to use  
   if (lc $self->{args}->{direction} eq 'push') {  
     $direction_arrow = '->';  
   } elsif (lc $self->{args}->{direction} eq 'pull') {  
     $direction_arrow = '<-';  
   } elsif (lc $self->{args}->{direction} eq 'full') {  
     $direction_arrow = '<->';  
   } else {  
473    }    }
474      # the name of the (container-) node the items are listed in
475    print Dumper($self);    if (!$opts->{source}->{nodeName}) {
476    #exit;      $logger->error( __PACKAGE__ . "->_preCheckOptions failed: Please specify \"source-node\".");
477        return;
   if (!$self->{metadata}->{version} || $self->{metadata}->{version} < 0.2) {  
     $self->_buildMetadataV1();  
   } else {  
     $self->_buildMetadataV1();  
     #$self->_buildMetadataV2();  
478    }    }
479    
480  #print Dumper($self);    # a "map"-declaration which module to use for mapping- and/or lookup-purposes
481  #print Dumper($self->{meta});    if (!$opts->{map}) {
482        $logger->warning( __PACKAGE__ . "->_preCheckOptions: No mapping supplied - please check key 'map|mappings' in global configuration or specify additional argument '--mapping-module'.");
483    $logger->info( __PACKAGE__ . "->syncNodes: source=$self->{meta}->{source}->{dbkey}/$self->{meta}->{source}->{node} $direction_arrow target=$self->{meta}->{target}->{dbkey}/$self->{meta}->{target}->{node}" );      return;
484      }
485    $self->_buildFieldmappingV1();    if (!$opts->{map}->{moduleName}) {
486        $logger->warning( __PACKAGE__ . "->_preCheckOptions: Currently only perl-modules can provide mappings: Please specify one with '--mapping-module=My::Mapping::Module'.");
487        return;
488      }
489    
490  #print Dumper($self->{meta});    return 1;
     
   # check partners/nodes: does partner exist / is node available?  
   foreach my $partner (keys %{$self->{meta}}) {  
       
     # 1. check partners & storages  
     if (!$self->{meta}->{$partner}) {  
       $logger->critical( __PACKAGE__ . "->syncNodes: Could not find partner '$partner' in configuration metadata." );  
       return;  
     }  
491    
492      my $dbkey = $self->{meta}->{$partner}->{dbkey};  }
493    
 #print Dumper($self->{meta});  
494    
     if (!$self->{meta}->{$partner}->{storage}) {  
       $logger->critical( __PACKAGE__ . "->syncNodes: Could not access storage of partner '$partner' (named '$dbkey'), looks like a configuration-error." );  
       return;  
     }  
       
     # TODO:  
     # 2. check if partners (and nodes?) are actually available....  
     # eventually pre-check mode of access-attempt (read/write) here to provide an "early-croak" if possible  
       
 #    print Dumper($self->{meta}->{$partner}->{storage}->{locator});  
     
     my $dbType = $self->{meta}->{$partner}->{storage}->{locator}->{type};  
 #print "dbType: $dbType", "\n";  
       
     # 3. check nodes  
     next if $dbType eq 'DBI';    # HACK for DBD::CSV - re-enable for others  
     # get node-name  
 #      print Dumper($self);  
     #print Dumper($self->{meta}->{$partner});  
     #print "öö", $self->{meta}->{$partner}->{node}, "\n";  
     my $nodename = $self->{meta}->{$partner}->{node};  
     # check if nodename is actually a CODEref, execute it to get a mapped/filtered target-nodename  
495    
496  #print "nodename: $nodename", "\n";  sub _prepare_sync {
497    
498      $logger->info( __PACKAGE__ . "->syncNodes: Accessing dbType=\"$dbType\", nodename=\"$nodename\"." );    my $self = shift;
       
 =pod  
     #print "----", ref $nodename, "\n";  
     if ($nodename =~ m/CODE/) {  
       print Dumper($self);  
       #exit;  
       $nodename = $nodename->($nodename);  
     }  
 =cut      
       
     if (!$self->{meta}->{$partner}->{storage}->existsChildNode($nodename)) {  
       $logger->critical( __PACKAGE__ . "->syncNodes: Could not reach node \"$nodename\" at partner \"$partner\"." );  
       return;  
     }  
       
   }  
499    
500    # TODO:    # TODO:
501    #   + if action == PUSH: start processing    #   + if action == PUSH: start processing
502    #  -+  if action == PULL: swap metadata and start processing    #  -+  if action == PULL: swap metadata and start processing
503    #   -  if action == FULL: start processing, then swap metadata and (re-)start processing    #   -  if action == FULL: start processing, then swap metadata and (re-)start processing
504    
505  #print Dumper($self->{args});    #print "dir: ", $self->{args}->{direction}, "\n";
506    
507    # manipulate metainfo according to direction of synchronization    # manipulate metainfo according to direction of synchronization
508    if (lc $self->{args}->{direction} eq 'push') {    if (lc $self->{options}->{process}->{mode} eq 'push') {
509      # just do it ...      # just do it ...    (don't modify any metadata)
510    } elsif (lc $self->{args}->{direction} eq 'pull') {    
511      #print "=======SWAP", "\n";    } elsif (lc $self->{options}->{process}->{mode} eq 'pull') {
512      # swap      # swap source and target metadata
513        # TODO: introduce different mechanism to make more then two partners (descents) possible
514      ($self->{meta}->{source}, $self->{meta}->{target}) =      ($self->{meta}->{source}, $self->{meta}->{target}) =
515          ($self->{meta}->{target}, $self->{meta}->{source});          ($self->{meta}->{target}, $self->{meta}->{source});
516    } elsif (lc $self->{args}->{direction} eq 'full') {      #($self->{options}->{source}, $self->{options}->{target}) =
517        #    ($self->{options}->{target}, $self->{options}->{source});
518      
519      } elsif (lc $self->{options}->{process}->{mode} eq 'full') {
520        # TODO:
521        # do a pull first and a push afterwards
522        # this requires us to be called somehow recursively - just one recursion level  ;-)
523      
524    } else {    } else {
525        # TODO: are there any other synchronization modes besides PULL, PUSH, FULL?
526      
527    }    }
528    
529    # import flag means: prepare the source node to be syncable    # import flag means: prepare the source node to be syncable
530    # 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
531    if ($self->{args}->{prepare}) {    if ($self->{options}->{process}->{prepare}) {
532      $self->_prepareNode_MetaProperties('source');      $self->_prepareNode_MetaProperties('source');
533      $self->_prepareNode_DummyIdent('source');      $self->_prepareNode_DummyIdent('source');
534      #return;      #return;
# Line 417  sub syncNodes { Line 537  sub syncNodes {
537        
538    # erase flag means: erase the target    # erase flag means: erase the target
539    #if ($opts->{erase}) {    #if ($opts->{erase}) {
540    if ($self->{args}->{erase}) {    if ($self->{options}->{process}->{erase}) {
541      # TODO: move this method to the scope of the synchronization core and wrap it around different handlers      # TODO: move this method to the scope of the synchronization core and wrap it around different handlers
542      #print "ERASE", "\n";      #print "ERASE", "\n";
543      $self->_erase_all('target');      $self->_erase_all('target');
544    }    }
545      
546      return 1;
547    
548    $self->_syncNodes();  }
549    
550    sub _getDirectedArrow {
551      my $self = shift;
552      my $mode = shift;
553      $mode ||= '';
554      
555      if (lc $mode eq 'push') {
556        return '->';
557      } elsif (lc $mode eq 'pull') {
558        return '<-';
559      } elsif (lc $mode eq 'full') {
560        return '<->';
561      } else {
562        return '';
563      }
564  }  }
565    
566  1;  1;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.6

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed