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

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

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

revision 1.1 by joko, Mon Jan 20 16:58:46 2003 UTC revision 1.2 by joko, Sun Feb 9 05:02:05 2003 UTC
# Line 6  Line 6 
6  ##  ##
7  ##    ----------------------------------------------------------------------------------------  ##    ----------------------------------------------------------------------------------------
8  ##    $Log$  ##    $Log$
9    ##    Revision 1.2  2003/02/09 05:02:05  joko
10    ##    + major structure changes
11    ##    - refactored code to sister modules
12    ##
13  ##    Revision 1.1  2003/01/20 16:58:46  joko  ##    Revision 1.1  2003/01/20 16:58:46  joko
14  ##    + initial check-in: here they are....  ##    + initial check-in: here they are....
15  ##  ##
# Line 31  my $logger = Log::Dispatch::Config->inst Line 35  my $logger = Log::Dispatch::Config->inst
35  use Data::Dumper;  use Data::Dumper;
36    
37    
 sub _preCheckOptions {  
   
   my $self = shift;  
   my $opts = shift;  
   
 #print Dumper($opts);  
 #exit;  
   
   # the type of the to-be-synced item  
   if (!$opts->{source}->{nodeType}) {  
     $logger->error( __PACKAGE__ . "->_preCheckOptions failed: Please specify \"source-type\".");  
     return;  
   }  
   # the name of the (container-) node the items are listed in  
   if (!$opts->{source}->{nodeName}) {  
     $logger->error( __PACKAGE__ . "->_preCheckOptions failed: Please specify \"source-node\".");  
     return;  
   }  
   
   # a "map"-declaration which module to use for mapping- and/or lookup-purposes  
   if (!$opts->{map}) {  
     $logger->warning( __PACKAGE__ . "->_preCheckOptions: No mapping supplied - please check key 'map|mappings' in global configuration or specify additional argument '--mapping-module'.");  
     return;  
   }  
   if (!$opts->{map}->{moduleName}) {  
     $logger->warning( __PACKAGE__ . "->_preCheckOptions: Currently only perl-modules can provide mappings: Please specify one with '--mapping-module'.");  
     return;  
   }  
   
   return 1;  
   
 }  
   
 # TODO: refactor!!! take this list from already established/given metadata  
 sub _buildMetadataV1 {  
   my $self = shift;  
   
   # decompose identifiers for each partner  
   foreach ('source', 'target') {  
       
     # get/set metadata for further processing  
   
     # Partner and Node (e.g.: "L:Country" or "R:countries.csv")  
     if (my $item = $self->{args}->{$_}) {  
       my @item = split(':', $item);  
       $self->{meta}->{$_}->{dbkey} = $item[0];  
       $self->{meta}->{$_}->{node} = $item[1];  
     }  
       
     # Filter  
     if (my $item_filter = $self->{args}->{$_ . '_filter'}) {  
       $self->{meta}->{$_}->{filter} = $item_filter;  
     }  
   
     # IdentProvider  
     if (my $item_ident = $self->{args}->{$_ . '_ident'}) {  
       my @item_ident = split(':', $item_ident);  
       $self->{meta}->{$_}->{IdentProvider} = { method => $item_ident[0], arg => $item_ident[1] };  
     }  
   
 #print Dumper($self->{meta});  
   
     # TODO: ChecksumProvider  
   
     # exclude properties/subnodes  
     if (my $item_exclude = $self->{args}->{$_ . '_exclude'}) {  
       $self->{meta}->{$_}->{subnodes_exclude} = $item_exclude;  
     }  
       
     # TypeProvider  
     if (my $item_type = $self->{args}->{$_ . '_type'}) {  
       my @item_type = split(':', $item_type);  
       $self->{meta}->{$_}->{TypeProvider} = { method => $item_type[0], arg => $item_type[1] };  
     }  
       
     # Callbacks - writers (will be triggered _before_ writing to target)  
     if (my $item_writers = $self->{args}->{$_ . '_callbacks_write'}) {  
       my $descent = $_;     # this is important since the following code inside the map wants to use its own context variables  
       map { $self->{meta}->{$descent}->{Callback}->{write}->{$_}++; } @$item_writers;  
     }  
       
     # Callbacks - readers (will be triggered _after_ reading from source)  
     if (my $item_readers = $self->{args}->{$_ . '_callbacks_read'}) {  
       my $descent = $_;  
       map { $self->{meta}->{$descent}->{Callback}->{read}->{$_}++; } @$item_readers;  
     }  
       
     # resolve storage objects  
     #$self->{$_} = $self->{container}->{storage}->{$self->{meta}->{$_}->{dbkey}};  
     # relink references to metainfo  
     $self->{meta}->{$_}->{storage} = $self->{container}->{storage}->{$self->{meta}->{$_}->{dbkey}};  
     #print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n";  
   }  
   
 }  
   
38    
39  # TODO: refactor (still)!!! take this list from already established/given metadata ....  # TODO: refactor (still)!!! take this list from already established/given metadata ....
40  # .... also internally: passing around and mungling these metadata doesn't make sense anymore.  # .... also internally: passing around and mungling these metadata doesn't make sense anymore.
41  sub _buildMetadataV2 {  sub options2metadata {
42    my $self = shift;    my $self = shift;
43    
44      $logger->debug( __PACKAGE__ . "->transformMetadata" );
45    
46  #print Dumper($self->{options});  #print Dumper($self->{options});
47    
48    # decompose identifiers for each partner    # decompose identifiers and write to metadata (for each descent)
49    foreach ('source', 'target') {    foreach ('source', 'target') {
50            
51      # get/set metadata for further processing      # get/set metadata for further processing
# Line 144  sub _buildMetadataV2 { Line 54  sub _buildMetadataV2 {
54      $self->{options}->{$_}->{nodeType} ||= '';      $self->{options}->{$_}->{nodeType} ||= '';
55      $self->{options}->{$_}->{nodeName} ||= '';      $self->{options}->{$_}->{nodeName} ||= '';
56    
57      # Partner and Node (e.g.: "L:Country" or "R:countries.csv")      # Partner and Node (compare the V1-notations: "L:Country" or "R:countries.csv")
58      $self->{meta}->{$_}->{dbKey} = $self->{options}->{$_}->{dbKey};      $self->{meta}->{$_}->{dbKey} = $self->{options}->{$_}->{dbKey};
59      $self->{meta}->{$_}->{nodeType} = $self->{options}->{$_}->{nodeType};      $self->{meta}->{$_}->{nodeType} = $self->{options}->{$_}->{nodeType};
60      $self->{meta}->{$_}->{nodeName} = $self->{options}->{$_}->{nodeName};      $self->{meta}->{$_}->{nodeName} = $self->{options}->{$_}->{nodeName};
# Line 161  sub _buildMetadataV2 { Line 71  sub _buildMetadataV2 {
71      }      }
72    
73      # TODO: ChecksumProvider      # TODO: ChecksumProvider
74        # already inside {options} - get it out from there and put into {meta} here!
75    
76      # exclude properties/subnodes      # exclude properties/subnodes
77      if (my $item_exclude = $self->{options}->{$_}->{exclude}) {      if (my $item_exclude = $self->{options}->{$_}->{exclude}) {
# Line 195  sub _buildMetadataV2 { Line 106  sub _buildMetadataV2 {
106  }  }
107    
108    
109  sub _buildFieldmappingV1 {  sub options2metadata_accessor {
110    my $self = shift;    my $self = shift;
111    
112    # build mapping    $logger->debug( __PACKAGE__ . "->_transformMetadata_accessor" );
   # incoming: and Array of node map entries (Array or Hash) - e.g.  
   #   [ 'source:item_name' => 'target:class_val' ]  
   #   { source => 'event->startDateTime', target => 'begindate' }  
   foreach (@{$self->{args}->{mapping}}) {  
     if (ref $_ eq 'ARRAY') {  
       my @entry1 = split(':', $_->[0]);  
       my @entry2 = split(':', $_->[1]);  
       my $descent = [];  
       my $node = [];  
       $descent->[0] = $entry1[0];  
       $descent->[1] = $entry2[0];  
       $node->[0] = $entry1[1];  
       $node->[1] = $entry2[1];  
       push @{$self->{meta}->{$descent->[0]}->{childnodes}}, $node->[0];  
       push @{$self->{meta}->{$descent->[1]}->{childnodes}}, $node->[1];  
     } elsif (ref $_ eq 'HASH') {  
       foreach my $entry_key (keys %$_) {  
         my $entry_val = $_->{$entry_key};  
         push @{$self->{meta}->{$entry_key}->{childnodes}}, $entry_val;  
       }  
     }  
113    
114    }    # build accessor metadata (for each descent)
115      foreach my $descent ('source', 'target') {
116    
117    return 1;        # the database type plays a role here to handle special nodesets:
118          # there may be a) childless/non-strict nodesets (e.g. a CSV-file)
119          # or b) normal ones - here the node-name corresponds more or less directly to
120          # the physical location in the storage hierarchy below us (e.g. MAPI or LDAP)
121          # or c) sets of nodes which are physically mapped by node-type (e.g. a RDBMS or a ODBMS)
122          my $dbType = $self->{meta}->{$descent}->{storage}->{locator}->{type};
123          
124            # a, b and c are "accessor-type"s (of 'none', 'node-name', 'node-type')
125            # the following code resolves these types to '$accessorType'
126            # and properly sets '$accessorName'
127    
128            # don't continue for certain storage handlers
129              # HACK for DBD::CSV - re-enable for other DBDs
130              #next if $dbType eq 'DBI';
131      
132            # get nodename
133    
134              # get node-name
135                my $nodename = $self->{meta}->{$descent}->{node};   # V1
136                $nodename ||= $self->{meta}->{$descent}->{nodeName};    # V2
137    
138              # check if nodename is actually a CODEref, execute it to get a mapped/filtered target-nodename
139                #print "----", ref $nodename, "\n";
140                #if ($nodename =~ m/CODE/) {
141                #  print Dumper($self);
142                #  #exit;
143                #  $nodename = $nodename->($nodename);
144                #}
145    
146            # determine nodeset accessor -type and -name
147              # defaults
148              my $accessorType = 'none';
149              my $accessorName = '';
150              
151              # if explicit address was given in options, just use it!
152              if (my $address = $self->{options}->{$descent}->{address}) {
153                $accessorType = 'direct-address';
154                $accessorName = $address;
155              }
156              
157              # c)
158              elsif (($dbType eq 'DBI' || $dbType eq 'Tangram') && $self->{meta}->{$descent}->{nodeType}) {
159                $accessorType = 'node-type';
160                $accessorName = $self->{meta}->{$descent}->{nodeType};
161              }
162    
163              # b)
164              elsif ($nodename) {
165                $accessorType = 'node-name';
166                $accessorName = $nodename;
167              }
168    
169          # write accessor information to metadata
170          $self->{meta}->{$descent}->{accessorType} = $accessorType;
171          $self->{meta}->{$descent}->{accessorName} = $accessorName;
172        
173        }
174    
175      # trace
176        #print Dumper($self->{meta});
177        #print Dumper($self->{options});
178        #exit;
179    
180  }  }
181    
182    
183    
184    sub buildAttributeMap {
185    
186     my $self = shift;
187    
188      # field-structure for building sql
189      # mapping of sql-fieldnames to object-attributes
190        $self->{node}->{map} = {};
191    
192        # manually set ...
193          # ... object-id
194          $self->{node}->{map}->{$self->{meta}->{target}->{IdentProvider}->{arg}} = $self->{node}->{source}->{ident};
195          # ... checksum
196          $self->{node}->{map}->{cs} = $self->{node}->{source}->{checksum};
197    
198    #print "sqlmap: ", Dumper($self->{node}->{map}), "\n";
199    
200    #print Dumper($self);
201    #exit;
202    
203        # for transferring flat structures via simple (1:1) mapping
204        # TODO: diff per property / property value
205    
206        #if ($self->{args}->{mapping}) {
207          # apply mapping from $self->{args}->{mapping} to $self->{node}->{map}
208          #foreach my $key (@{$self->{meta}->{source}->{childnodes}}) {
209          my @childnodes = @{$self->{meta}->{source}->{childnodes}};
210          for (my $mapidx = 0; $mapidx <= $#childnodes; $mapidx++) {
211            #my $map_right = $self->{args}->{mapping}->{$key};
212            
213            $self->{node}->{source}->{propcache} = {};
214            $self->{node}->{target}->{propcache} = {};
215            
216            # get property name
217            $self->{node}->{source}->{propcache}->{property} = $self->{meta}->{source}->{childnodes}->[$mapidx];
218            $self->{node}->{target}->{propcache}->{property} = $self->{meta}->{target}->{childnodes}->[$mapidx];
219            #print "map: $map_right", "\n";
220    
221            # get property value
222            my $value;
223            
224            # detect for callback - old style - (maybe the better???)
225            if (ref($self->{node}->{target}->{map}) eq 'CODE') {
226              #$value = &$map_right($objClone);
227            } else {
228              # plain (scalar?) value
229              #$value = $objClone->{$map_right};
230              $self->{node}->{source}->{propcache}->{value} = $self->{node}->{source}->{payload}->{$self->{node}->{source}->{propcache}->{property}};
231            }
232            #$self->{node}->{map}->{$key} = $value;
233            
234            # detect expression
235            # for transferring deeply nested structures described by expressions
236            #print "val: $self->{node}->{source}->{propcache}->{value}", "\n";
237            if ($self->{node}->{source}->{propcache}->{property} =~ s/^expr://) {
238              
239              # create an anonymous sub to act as callback target dispatcher
240                my $cb_dispatcher = sub {
241                  #print "===============  CALLBACK DISPATCHER", "\n";
242                  #print "ident: ", $self->{node}->{source}->{ident}, "\n";
243                  #return $self->{node}->{source}->{ident};
244                  
245                };
246              
247    
248    #print Dumper($self->{node});
249              
250              # build callback map for helper function
251              #my $cbmap = { $self->{meta}->{source}->{IdentProvider}->{arg} => $cb_dispatcher };
252              my $cbmap = {};
253              my $value = refexpr2perlref($self->{node}->{source}->{payload}, $self->{node}->{source}->{propcache}->{property}, $cbmap);
254              $self->{node}->{source}->{propcache}->{value} = $value;
255            }
256    
257            # encode values dependent on type of underlying storage here - expand cases...
258            my $storage_type = $self->{meta}->{target}->{storage}->{locator}->{type};
259            if ($storage_type eq 'DBI') {
260              # ...for sql
261              # quotemeta?
262              $self->{node}->{source}->{propcache}->{value} = quotesql($self->{node}->{source}->{propcache}->{value});
263    
264            } elsif ($storage_type eq 'Tangram') {
265              # iso? utf8 already possible?
266            
267            } elsif ($storage_type eq 'LDAP') {
268              # TODO: encode utf8 here?
269    
270            }
271    
272            # store value to transfer map
273            $self->{node}->{map}->{$self->{node}->{target}->{propcache}->{property}} = $self->{node}->{source}->{propcache}->{value};
274    
275          }
276        #}
277    
278        
279      # TODO: $logger->dump( ... );
280      #$logger->debug( "sqlmap:" . "\n" . Dumper($self->{node}->{map}) );
281    #print "sqlmap: ", Dumper($self->{node}->{map}), "\n";
282    #print "entrystatus: ", Dumper($self->{node}), "\n";
283    
284    }
285    
286  1;  1;

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

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