1 |
## ------------------------------------------------------------------------- |
2 |
## $Id: Map.pm,v 1.1 2003/05/13 08:10:11 joko Exp $ |
3 |
## |
4 |
## Copyright (c) 2002 Andreas Motl <andreas.motl@ilo.de> |
5 |
## |
6 |
## See COPYRIGHT section in associated pod text |
7 |
## or below for usage and distribution rights. |
8 |
## |
9 |
## ------------------------------------------------------------------------- |
10 |
## $Log: Map.pm,v $ |
11 |
## Revision 1.1 2003/05/13 08:10:11 joko |
12 |
## initial commit, code from Metadata.pm |
13 |
## |
14 |
## |
15 |
## ------------------------------------------------------------------------- |
16 |
|
17 |
|
18 |
package Data::Transfer::Sync::Map; |
19 |
|
20 |
use strict; |
21 |
use warnings; |
22 |
|
23 |
use mixin::with qw( Data::Transfer::Sync ); |
24 |
|
25 |
|
26 |
use Data::Dumper; |
27 |
use shortcuts::database qw( quotesql ); |
28 |
use Data::Mungle::Code::Ref qw( ref_slot ); |
29 |
|
30 |
# get logger instance |
31 |
my $logger = Log::Dispatch::Config->instance; |
32 |
|
33 |
# This actually - but virtually - maps data from scope to scope.!? |
34 |
# Physical injection/modification doesn't occour here! This is the job of the core/engine. |
35 |
sub buildAttributeMap { |
36 |
|
37 |
my $self = shift; |
38 |
|
39 |
# Field-structure for building sql mapping of sql-fieldnames to object-attributes. |
40 |
# This is fed to "shortcuts::database::hash2sql" somewhere later... (outside this method). |
41 |
$self->{node}->{map} = {}; |
42 |
|
43 |
# Manually inject into target map: ... |
44 |
# ... object-id. |
45 |
$self->{node}->{map}->{$self->{meta}->{target}->{IdentProvider}->{arg}} = $self->{node}->{source}->{ident}; |
46 |
# ... checksum. |
47 |
$self->{node}->{map}->{cs} = $self->{node}->{source}->{checksum}; |
48 |
|
49 |
# debug point |
50 |
#print "sqlmap: ", Dumper($self->{node}->{map}), "\n"; |
51 |
#print Dumper($self); |
52 |
#exit; |
53 |
|
54 |
# for transferring flat structures via simple (1:1) mapping |
55 |
# TODO: diff per property / property value |
56 |
|
57 |
# Apply mapping from $self->{args}->{mapping} to $self->{node}->{map}. |
58 |
#if ($self->{args}->{mapping}) { |
59 |
#foreach my $key (@{$self->{meta}->{source}->{childnodes}}) { |
60 |
my @childnodes = @{$self->{meta}->{source}->{childnodes}}; |
61 |
for (my $mapidx = 0; $mapidx <= $#childnodes; $mapidx++) { |
62 |
|
63 |
# deprecated? |
64 |
#my $map_right = $self->{args}->{mapping}->{$key}; |
65 |
#print "map: $map_right", "\n"; |
66 |
|
67 |
# Clear caching containers. |
68 |
$self->{node}->{source}->{propcache} = {}; |
69 |
$self->{node}->{target}->{propcache} = {}; |
70 |
|
71 |
|
72 |
# A - Transfer name/value pairs from distinct source and target objects to single local cache container "propcache". |
73 |
|
74 |
# get property name |
75 |
$self->{node}->{source}->{propcache}->{property} = $self->{meta}->{source}->{childnodes}->[$mapidx]; |
76 |
$self->{node}->{target}->{propcache}->{property} = $self->{meta}->{target}->{childnodes}->[$mapidx]; |
77 |
|
78 |
# get property value |
79 |
my $value; |
80 |
|
81 |
# 1. Detect for callback - deprecated / old style - (maybe the better???) |
82 |
if (ref($self->{node}->{target}->{map}) eq 'CODE') { |
83 |
#$value = &$map_right($objClone); |
84 |
} else { |
85 |
# plain (scalar?) value |
86 |
#$value = $objClone->{$map_right}; |
87 |
$self->{node}->{source}->{propcache}->{value} = $self->{node}->{source}->{payload}->{$self->{node}->{source}->{propcache}->{property}}; |
88 |
} |
89 |
#$self->{node}->{map}->{$key} = $value; |
90 |
|
91 |
|
92 |
# B - Inspect the property for special cases: |
93 |
# a) expr:{expression} - encoded expression should be resolved by "ref_slot" |
94 |
# b) code:{symbol} - encoded symbol should be resolved (used) as object method at runtime |
95 |
|
96 |
# B.1. Detect expression for transferring deeply nested structures described by expressions |
97 |
# e.g.: "expr:event->startDateTime" should assume "event" being an |
98 |
# object-reference and get its (hopefully scalar) attribute value "startDateTime". |
99 |
#print "val: $self->{node}->{source}->{propcache}->{value}", "\n"; |
100 |
if ($self->{node}->{source}->{propcache}->{property} =~ s/^expr://) { |
101 |
|
102 |
# debug point |
103 |
#print Dumper($self->{node}); |
104 |
|
105 |
# V1 - create an anonymous sub to act as callback target dispatcher |
106 |
my $cb_dispatcher = sub { |
107 |
#print "=============== CALLBACK DISPATCHER", "\n"; |
108 |
#print "ident: ", $self->{node}->{source}->{ident}, "\n"; |
109 |
#return $self->{node}->{source}->{ident}; |
110 |
}; |
111 |
|
112 |
# V2 - build callback map for helper function |
113 |
#print Dumper($self->{node}->{source}->{payload}); |
114 |
#my $cbmap = { $self->{meta}->{source}->{IdentProvider}->{arg} => $cb_dispatcher }; |
115 |
|
116 |
# FIXED - 2004-05-11: providing an empty hashref to ref_slot seems to be a bad idea here.... |
117 |
#my $cbmap = {}; |
118 |
my $cbmap = undef; |
119 |
my $value = ref_slot($self->{node}->{source}->{payload}, $self->{node}->{source}->{propcache}->{property}, $cbmap); |
120 |
|
121 |
# new of 2003-05-12: Assertion: assume ref_slot resolves to scalar. (no ref!) |
122 |
# Otherwise: Invalidate virtual object completely (skip sync). |
123 |
# TODO: Warn to STDOUT or somewhere else here? |
124 |
|
125 |
#print "prop: ", $self->{node}->{source}->{propcache}->{property}, "\n"; |
126 |
#print "val: $value", "\n"; |
127 |
#if ($value =~ m/HASH/) { |
128 |
#print Dumper($value); |
129 |
#print Dumper($self->{node}->{source}->{payload}); |
130 |
#} |
131 |
#return if ref $value; |
132 |
#next if ref $value; |
133 |
|
134 |
$self->{node}->{source}->{propcache}->{value} = $value; |
135 |
} |
136 |
|
137 |
# B.2. FIXME: Get detection for coderefs / runtime-symbols from API::_prepareOptions(...) into this place! |
138 |
|
139 |
|
140 |
# C - Value encoding |
141 |
|
142 |
# encode values dependent on type of underlying storage here - expand cases... |
143 |
# TODO: Handle encoding from/to quote.sql, quote.meta, utf-8, latin, ascii in a more generic/abstract way here! |
144 |
# Hint: utf-8 encoding is *essential* for XML marshalling. By now we do it manually somewhere else, |
145 |
# but keep in mind when redesigning this interface, that encoding also might be handled by external modules as well... |
146 |
my $storage_type = $self->{meta}->{target}->{storage}->{locator}->{type}; |
147 |
if ($storage_type eq 'DBI') { |
148 |
# ...for sql |
149 |
# quotemeta? |
150 |
$self->{node}->{source}->{propcache}->{value} = |
151 |
quotesql($self->{node}->{source}->{propcache}->{value}); |
152 |
|
153 |
} elsif ($storage_type eq 'Tangram') { |
154 |
# iso? utf8 already possible? |
155 |
|
156 |
} elsif ($storage_type eq 'LDAP') { |
157 |
# TODO: encode utf8 here? |
158 |
|
159 |
} |
160 |
|
161 |
|
162 |
# D - Finally transfer name/value pair to target map |
163 |
|
164 |
# store value to transfer map |
165 |
$self->{node}->{map}->{$self->{node}->{target}->{propcache}->{property}} = |
166 |
$self->{node}->{source}->{propcache}->{value}; |
167 |
|
168 |
} |
169 |
#} |
170 |
|
171 |
# debug point |
172 |
# TODO: $logger->dump( ... ); |
173 |
#$logger->debug( "sqlmap:" . "\n" . Dumper($self->{node}->{map}) ); |
174 |
#print "sqlmap: ", Dumper($self->{node}->{map}), "\n"; |
175 |
#print "entrystatus: ", Dumper($self->{node}), "\n"; |
176 |
|
177 |
# signal good |
178 |
return 1; |
179 |
|
180 |
} |
181 |
|
182 |
1; |
183 |
__END__ |