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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (hide annotations)
Tue Feb 11 06:28:24 2003 UTC (21 years, 5 months ago) by joko
Branch: MAIN
Changes since 1.2: +34 -11 lines
+ changes to metadata structure

1 joko 1.3 ## $Id: Metadata.pm,v 1.2 2003/02/09 05:02:05 joko Exp $
2 joko 1.1 ##
3     ## Copyright (c) 2002 Andreas Motl <andreas.motl@ilo.de>
4     ##
5     ## See COPYRIGHT section in pod text below for usage and distribution rights.
6     ##
7     ## ----------------------------------------------------------------------------------------
8 joko 1.2 ## $Log: Metadata.pm,v $
9 joko 1.3 ## Revision 1.2 2003/02/09 05:02:05 joko
10     ## + major structure changes
11     ## - refactored code to sister modules
12     ##
13 joko 1.2 ## Revision 1.1 2003/01/20 16:58:46 joko
14     ## + initial check-in: here they are....
15     ##
16 joko 1.1 ## Revision 1.1 2003/01/19 01:23:04 joko
17     ## + new from Data/Transfer/Sync.pm
18     ##
19     ## ----------------------------------------------------------------------------------------
20    
21    
22     package Data::Transfer::Sync::Metadata;
23    
24     use strict;
25     use warnings;
26    
27     use mixin::with qw( Data::Transfer::Sync );
28    
29    
30     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - main
31    
32 joko 1.3 use Data::Dumper;
33     use libdb qw( quotesql );
34     use Data::Transform::Deep qw( refexpr2perlref );
35    
36 joko 1.1 # get logger instance
37     my $logger = Log::Dispatch::Config->instance;
38    
39     # 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.
41 joko 1.2 sub options2metadata {
42 joko 1.1 my $self = shift;
43    
44 joko 1.2 $logger->debug( __PACKAGE__ . "->transformMetadata" );
45    
46 joko 1.3 # trace
47     #print Dumper($self->{options});
48     #print Dumper($self->{args_raw});
49     #exit;
50 joko 1.1
51 joko 1.2 # decompose identifiers and write to metadata (for each descent)
52 joko 1.1 foreach ('source', 'target') {
53    
54     # get/set metadata for further processing
55    
56     $self->{options}->{$_}->{dbKey} ||= '';
57     $self->{options}->{$_}->{nodeType} ||= '';
58     $self->{options}->{$_}->{nodeName} ||= '';
59    
60 joko 1.2 # Partner and Node (compare the V1-notations: "L:Country" or "R:countries.csv")
61 joko 1.1 $self->{meta}->{$_}->{dbKey} = $self->{options}->{$_}->{dbKey};
62     $self->{meta}->{$_}->{nodeType} = $self->{options}->{$_}->{nodeType};
63     $self->{meta}->{$_}->{nodeName} = $self->{options}->{$_}->{nodeName};
64    
65     # Filter
66 joko 1.3 if (my $item_filter = $self->{options}->{$_}->{filter}) {
67 joko 1.1 $self->{meta}->{$_}->{filter} = $item_filter;
68     }
69    
70     # IdentProvider
71     if (my $item_ident = $self->{options}->{$_}->{ident}) {
72     my @item_ident = split(':', $item_ident);
73     $self->{meta}->{$_}->{IdentProvider} = { method => $item_ident[0], arg => $item_ident[1] };
74     }
75    
76     # TODO: ChecksumProvider
77 joko 1.2 # already inside {options} - get it out from there and put into {meta} here!
78 joko 1.1
79     # exclude properties/subnodes
80     if (my $item_exclude = $self->{options}->{$_}->{exclude}) {
81     $self->{meta}->{$_}->{subnodes_exclude} = $item_exclude;
82     }
83    
84     # TypeProvider
85 joko 1.3 # FIXME! this is still Vdeprecated!!!
86 joko 1.1 if (my $item_type = $self->{args}->{$_ . '_type'}) {
87     my @item_type = split(':', $item_type);
88     $self->{meta}->{$_}->{TypeProvider} = { method => $item_type[0], arg => $item_type[1] };
89     }
90 joko 1.3
91     # trace
92     #print Dumper($self);
93     #exit;
94 joko 1.1
95     # Callbacks - writers (will be triggered _before_ writing to target)
96 joko 1.3 if (my $item_writers = $self->{options}->{$_}->{callbacks}->{write}) {
97 joko 1.1 my $descent = $_; # this is important since the following code inside the map wants to use its own context variables
98     map { $self->{meta}->{$descent}->{Callback}->{write}->{$_}++; } @$item_writers;
99     }
100    
101     # Callbacks - readers (will be triggered _after_ reading from source)
102 joko 1.3 if (my $item_readers = $self->{options}->{$_}->{callbacks}->{read}) {
103 joko 1.1 my $descent = $_;
104     map { $self->{meta}->{$descent}->{Callback}->{read}->{$_}++; } @$item_readers;
105     }
106    
107 joko 1.3 # resolve storage handles
108     $self->{meta}->{$_}->{storage} = $self->{options}->{$_}->{storage}->{handle};
109    
110     # transfer storage handle options to metadata
111     #map { $self->{meta}->{$_}->{isIdentAuthority} = 1 } @{$self->{id_authorities}};
112     #map { $self->{meta}->{$_}->{isChecksumAuthority} = 1; } @{$self->{checksum_authorities}};
113     #map { $self->{meta}->{$_}->{isWriteProtected} = 1; } @{$self->{write_protected}};
114     #print Dumper($self->{options}->{$_});
115     #exit;
116     $self->{meta}->{$_}->{isIdentAuthority} = $self->{options}->{$_}->{storage}->{handle}->{locator}->{sync}->{isIdentAuthority};
117     $self->{meta}->{$_}->{isWriteProtected} = $self->{options}->{$_}->{storage}->{handle}->{locator}->{sync}->{isWriteProtected};
118     $self->{meta}->{$_}->{isChecksumAuthority} = $self->{options}->{$_}->{storage}->{handle}->{locator}->{sync}->{isChecksumAuthority};
119    
120 joko 1.1 }
121    
122     #print Dumper($self->{meta});
123     return 1;
124    
125     }
126    
127    
128 joko 1.2 sub options2metadata_accessor {
129 joko 1.1 my $self = shift;
130    
131 joko 1.2 $logger->debug( __PACKAGE__ . "->_transformMetadata_accessor" );
132 joko 1.1
133 joko 1.2 # build accessor metadata (for each descent)
134     foreach my $descent ('source', 'target') {
135 joko 1.1
136 joko 1.2 # the database type plays a role here to handle special nodesets:
137     # there may be a) childless/non-strict nodesets (e.g. a CSV-file)
138     # or b) normal ones - here the node-name corresponds more or less directly to
139     # the physical location in the storage hierarchy below us (e.g. MAPI or LDAP)
140     # or c) sets of nodes which are physically mapped by node-type (e.g. a RDBMS or a ODBMS)
141     my $dbType = $self->{meta}->{$descent}->{storage}->{locator}->{type};
142    
143     # a, b and c are "accessor-type"s (of 'none', 'node-name', 'node-type')
144     # the following code resolves these types to '$accessorType'
145     # and properly sets '$accessorName'
146    
147     # don't continue for certain storage handlers
148     # HACK for DBD::CSV - re-enable for other DBDs
149     #next if $dbType eq 'DBI';
150    
151     # get nodename
152    
153     # get node-name
154     my $nodename = $self->{meta}->{$descent}->{node}; # V1
155     $nodename ||= $self->{meta}->{$descent}->{nodeName}; # V2
156    
157     # check if nodename is actually a CODEref, execute it to get a mapped/filtered target-nodename
158     #print "----", ref $nodename, "\n";
159     #if ($nodename =~ m/CODE/) {
160     # print Dumper($self);
161     # #exit;
162     # $nodename = $nodename->($nodename);
163     #}
164    
165     # determine nodeset accessor -type and -name
166     # defaults
167     my $accessorType = 'none';
168     my $accessorName = '';
169    
170     # if explicit address was given in options, just use it!
171     if (my $address = $self->{options}->{$descent}->{address}) {
172     $accessorType = 'direct-address';
173     $accessorName = $address;
174     }
175    
176     # c)
177     elsif (($dbType eq 'DBI' || $dbType eq 'Tangram') && $self->{meta}->{$descent}->{nodeType}) {
178     $accessorType = 'node-type';
179     $accessorName = $self->{meta}->{$descent}->{nodeType};
180     }
181    
182     # b)
183     elsif ($nodename) {
184     $accessorType = 'node-name';
185     $accessorName = $nodename;
186     }
187    
188     # write accessor information to metadata
189     $self->{meta}->{$descent}->{accessorType} = $accessorType;
190     $self->{meta}->{$descent}->{accessorName} = $accessorName;
191    
192     }
193    
194     # trace
195     #print Dumper($self->{meta});
196     #print Dumper($self->{options});
197     #exit;
198 joko 1.1
199     }
200    
201 joko 1.2
202    
203     sub buildAttributeMap {
204    
205     my $self = shift;
206    
207     # field-structure for building sql
208     # mapping of sql-fieldnames to object-attributes
209     $self->{node}->{map} = {};
210    
211     # manually set ...
212     # ... object-id
213     $self->{node}->{map}->{$self->{meta}->{target}->{IdentProvider}->{arg}} = $self->{node}->{source}->{ident};
214     # ... checksum
215     $self->{node}->{map}->{cs} = $self->{node}->{source}->{checksum};
216    
217     #print "sqlmap: ", Dumper($self->{node}->{map}), "\n";
218    
219     #print Dumper($self);
220     #exit;
221    
222     # for transferring flat structures via simple (1:1) mapping
223     # TODO: diff per property / property value
224    
225     #if ($self->{args}->{mapping}) {
226     # apply mapping from $self->{args}->{mapping} to $self->{node}->{map}
227     #foreach my $key (@{$self->{meta}->{source}->{childnodes}}) {
228     my @childnodes = @{$self->{meta}->{source}->{childnodes}};
229     for (my $mapidx = 0; $mapidx <= $#childnodes; $mapidx++) {
230     #my $map_right = $self->{args}->{mapping}->{$key};
231    
232     $self->{node}->{source}->{propcache} = {};
233     $self->{node}->{target}->{propcache} = {};
234    
235     # get property name
236     $self->{node}->{source}->{propcache}->{property} = $self->{meta}->{source}->{childnodes}->[$mapidx];
237     $self->{node}->{target}->{propcache}->{property} = $self->{meta}->{target}->{childnodes}->[$mapidx];
238     #print "map: $map_right", "\n";
239    
240     # get property value
241     my $value;
242    
243     # detect for callback - old style - (maybe the better???)
244     if (ref($self->{node}->{target}->{map}) eq 'CODE') {
245     #$value = &$map_right($objClone);
246     } else {
247     # plain (scalar?) value
248     #$value = $objClone->{$map_right};
249     $self->{node}->{source}->{propcache}->{value} = $self->{node}->{source}->{payload}->{$self->{node}->{source}->{propcache}->{property}};
250     }
251     #$self->{node}->{map}->{$key} = $value;
252    
253     # detect expression
254     # for transferring deeply nested structures described by expressions
255     #print "val: $self->{node}->{source}->{propcache}->{value}", "\n";
256     if ($self->{node}->{source}->{propcache}->{property} =~ s/^expr://) {
257    
258     # create an anonymous sub to act as callback target dispatcher
259     my $cb_dispatcher = sub {
260     #print "=============== CALLBACK DISPATCHER", "\n";
261     #print "ident: ", $self->{node}->{source}->{ident}, "\n";
262     #return $self->{node}->{source}->{ident};
263    
264     };
265    
266    
267     #print Dumper($self->{node});
268    
269     # build callback map for helper function
270     #my $cbmap = { $self->{meta}->{source}->{IdentProvider}->{arg} => $cb_dispatcher };
271     my $cbmap = {};
272     my $value = refexpr2perlref($self->{node}->{source}->{payload}, $self->{node}->{source}->{propcache}->{property}, $cbmap);
273     $self->{node}->{source}->{propcache}->{value} = $value;
274     }
275    
276     # encode values dependent on type of underlying storage here - expand cases...
277     my $storage_type = $self->{meta}->{target}->{storage}->{locator}->{type};
278     if ($storage_type eq 'DBI') {
279     # ...for sql
280     # quotemeta?
281     $self->{node}->{source}->{propcache}->{value} = quotesql($self->{node}->{source}->{propcache}->{value});
282    
283     } elsif ($storage_type eq 'Tangram') {
284     # iso? utf8 already possible?
285    
286     } elsif ($storage_type eq 'LDAP') {
287     # TODO: encode utf8 here?
288    
289     }
290    
291     # store value to transfer map
292     $self->{node}->{map}->{$self->{node}->{target}->{propcache}->{property}} = $self->{node}->{source}->{propcache}->{value};
293    
294     }
295     #}
296    
297    
298     # TODO: $logger->dump( ... );
299     #$logger->debug( "sqlmap:" . "\n" . Dumper($self->{node}->{map}) );
300     #print "sqlmap: ", Dumper($self->{node}->{map}), "\n";
301     #print "entrystatus: ", Dumper($self->{node}), "\n";
302    
303     }
304 joko 1.1
305     1;

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