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

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

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