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