| 1 |
joko |
1.1 |
## $Id: API.pm,v 1.1 2003/01/19 01:23:04 joko Exp $ |
| 2 |
|
|
## |
| 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 |
|
|
## $Log: API.pm,v $ |
| 9 |
|
|
## Revision 1.1 2003/01/19 01:23:04 joko |
| 10 |
|
|
## + new from Data/Transfer/Sync.pm |
| 11 |
|
|
## |
| 12 |
|
|
## ---------------------------------------------------------------------------------------- |
| 13 |
|
|
|
| 14 |
|
|
|
| 15 |
|
|
package Data::Transfer::Sync::Metadata; |
| 16 |
|
|
|
| 17 |
|
|
use strict; |
| 18 |
|
|
use warnings; |
| 19 |
|
|
|
| 20 |
|
|
use mixin::with qw( Data::Transfer::Sync ); |
| 21 |
|
|
|
| 22 |
|
|
|
| 23 |
|
|
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - main |
| 24 |
|
|
|
| 25 |
|
|
# get logger instance |
| 26 |
|
|
my $logger = Log::Dispatch::Config->instance; |
| 27 |
|
|
|
| 28 |
|
|
use Data::Dumper; |
| 29 |
|
|
|
| 30 |
|
|
|
| 31 |
|
|
sub _preCheckOptions { |
| 32 |
|
|
|
| 33 |
|
|
my $self = shift; |
| 34 |
|
|
my $opts = shift; |
| 35 |
|
|
|
| 36 |
|
|
#print Dumper($opts); |
| 37 |
|
|
#exit; |
| 38 |
|
|
|
| 39 |
|
|
# the type of the to-be-synced item |
| 40 |
|
|
if (!$opts->{source}->{nodeType}) { |
| 41 |
|
|
$logger->error( __PACKAGE__ . "->_preCheckOptions failed: Please specify \"source-type\"."); |
| 42 |
|
|
return; |
| 43 |
|
|
} |
| 44 |
|
|
# the name of the (container-) node the items are listed in |
| 45 |
|
|
if (!$opts->{source}->{nodeName}) { |
| 46 |
|
|
$logger->error( __PACKAGE__ . "->_preCheckOptions failed: Please specify \"source-node\"."); |
| 47 |
|
|
return; |
| 48 |
|
|
} |
| 49 |
|
|
|
| 50 |
|
|
# a "map"-declaration which module to use for mapping- and/or lookup-purposes |
| 51 |
|
|
if (!$opts->{map}) { |
| 52 |
|
|
$logger->warning( __PACKAGE__ . "->_preCheckOptions: No mapping supplied - please check key 'map|mappings' in global configuration or specify additional argument '--mapping-module'."); |
| 53 |
|
|
return; |
| 54 |
|
|
} |
| 55 |
|
|
if (!$opts->{map}->{moduleName}) { |
| 56 |
|
|
$logger->warning( __PACKAGE__ . "->_preCheckOptions: Currently only perl-modules can provide mappings: Please specify one with '--mapping-module'."); |
| 57 |
|
|
return; |
| 58 |
|
|
} |
| 59 |
|
|
|
| 60 |
|
|
return 1; |
| 61 |
|
|
|
| 62 |
|
|
} |
| 63 |
|
|
|
| 64 |
|
|
# TODO: refactor!!! take this list from already established/given metadata |
| 65 |
|
|
sub _buildMetadataV1 { |
| 66 |
|
|
my $self = shift; |
| 67 |
|
|
|
| 68 |
|
|
# decompose identifiers for each partner |
| 69 |
|
|
foreach ('source', 'target') { |
| 70 |
|
|
|
| 71 |
|
|
# get/set metadata for further processing |
| 72 |
|
|
|
| 73 |
|
|
# Partner and Node (e.g.: "L:Country" or "R:countries.csv") |
| 74 |
|
|
if (my $item = $self->{args}->{$_}) { |
| 75 |
|
|
my @item = split(':', $item); |
| 76 |
|
|
$self->{meta}->{$_}->{dbkey} = $item[0]; |
| 77 |
|
|
$self->{meta}->{$_}->{node} = $item[1]; |
| 78 |
|
|
} |
| 79 |
|
|
|
| 80 |
|
|
# Filter |
| 81 |
|
|
if (my $item_filter = $self->{args}->{$_ . '_filter'}) { |
| 82 |
|
|
$self->{meta}->{$_}->{filter} = $item_filter; |
| 83 |
|
|
} |
| 84 |
|
|
|
| 85 |
|
|
# IdentProvider |
| 86 |
|
|
if (my $item_ident = $self->{args}->{$_ . '_ident'}) { |
| 87 |
|
|
my @item_ident = split(':', $item_ident); |
| 88 |
|
|
$self->{meta}->{$_}->{IdentProvider} = { method => $item_ident[0], arg => $item_ident[1] }; |
| 89 |
|
|
} |
| 90 |
|
|
|
| 91 |
|
|
#print Dumper($self->{meta}); |
| 92 |
|
|
|
| 93 |
|
|
# TODO: ChecksumProvider |
| 94 |
|
|
|
| 95 |
|
|
# exclude properties/subnodes |
| 96 |
|
|
if (my $item_exclude = $self->{args}->{$_ . '_exclude'}) { |
| 97 |
|
|
$self->{meta}->{$_}->{subnodes_exclude} = $item_exclude; |
| 98 |
|
|
} |
| 99 |
|
|
|
| 100 |
|
|
# TypeProvider |
| 101 |
|
|
if (my $item_type = $self->{args}->{$_ . '_type'}) { |
| 102 |
|
|
my @item_type = split(':', $item_type); |
| 103 |
|
|
$self->{meta}->{$_}->{TypeProvider} = { method => $item_type[0], arg => $item_type[1] }; |
| 104 |
|
|
} |
| 105 |
|
|
|
| 106 |
|
|
# Callbacks - writers (will be triggered _before_ writing to target) |
| 107 |
|
|
if (my $item_writers = $self->{args}->{$_ . '_callbacks_write'}) { |
| 108 |
|
|
my $descent = $_; # this is important since the following code inside the map wants to use its own context variables |
| 109 |
|
|
map { $self->{meta}->{$descent}->{Callback}->{write}->{$_}++; } @$item_writers; |
| 110 |
|
|
} |
| 111 |
|
|
|
| 112 |
|
|
# Callbacks - readers (will be triggered _after_ reading from source) |
| 113 |
|
|
if (my $item_readers = $self->{args}->{$_ . '_callbacks_read'}) { |
| 114 |
|
|
my $descent = $_; |
| 115 |
|
|
map { $self->{meta}->{$descent}->{Callback}->{read}->{$_}++; } @$item_readers; |
| 116 |
|
|
} |
| 117 |
|
|
|
| 118 |
|
|
# resolve storage objects |
| 119 |
|
|
#$self->{$_} = $self->{container}->{storage}->{$self->{meta}->{$_}->{dbkey}}; |
| 120 |
|
|
# relink references to metainfo |
| 121 |
|
|
$self->{meta}->{$_}->{storage} = $self->{container}->{storage}->{$self->{meta}->{$_}->{dbkey}}; |
| 122 |
|
|
#print "iiiiisprov: ", Dumper($self->{meta}->{$_}->{storage}), "\n"; |
| 123 |
|
|
} |
| 124 |
|
|
|
| 125 |
|
|
} |
| 126 |
|
|
|
| 127 |
|
|
|
| 128 |
|
|
# TODO: refactor (still)!!! take this list from already established/given metadata .... |
| 129 |
|
|
# .... also internally: passing around and mungling these metadata doesn't make sense anymore. |
| 130 |
|
|
sub _buildMetadataV2 { |
| 131 |
|
|
my $self = shift; |
| 132 |
|
|
|
| 133 |
|
|
#print Dumper($self->{options}); |
| 134 |
|
|
|
| 135 |
|
|
# decompose identifiers for each partner |
| 136 |
|
|
foreach ('source', 'target') { |
| 137 |
|
|
|
| 138 |
|
|
# get/set metadata for further processing |
| 139 |
|
|
|
| 140 |
|
|
$self->{options}->{$_}->{dbKey} ||= ''; |
| 141 |
|
|
$self->{options}->{$_}->{nodeType} ||= ''; |
| 142 |
|
|
$self->{options}->{$_}->{nodeName} ||= ''; |
| 143 |
|
|
|
| 144 |
|
|
# Partner and Node (e.g.: "L:Country" or "R:countries.csv") |
| 145 |
|
|
$self->{meta}->{$_}->{dbKey} = $self->{options}->{$_}->{dbKey}; |
| 146 |
|
|
$self->{meta}->{$_}->{nodeType} = $self->{options}->{$_}->{nodeType}; |
| 147 |
|
|
$self->{meta}->{$_}->{nodeName} = $self->{options}->{$_}->{nodeName}; |
| 148 |
|
|
|
| 149 |
|
|
# Filter |
| 150 |
|
|
if (my $item_filter = $self->{args}->{$_ . '_filter'}) { |
| 151 |
|
|
$self->{meta}->{$_}->{filter} = $item_filter; |
| 152 |
|
|
} |
| 153 |
|
|
|
| 154 |
|
|
# IdentProvider |
| 155 |
|
|
if (my $item_ident = $self->{options}->{$_}->{ident}) { |
| 156 |
|
|
my @item_ident = split(':', $item_ident); |
| 157 |
|
|
$self->{meta}->{$_}->{IdentProvider} = { method => $item_ident[0], arg => $item_ident[1] }; |
| 158 |
|
|
} |
| 159 |
|
|
|
| 160 |
|
|
# TODO: ChecksumProvider |
| 161 |
|
|
|
| 162 |
|
|
# exclude properties/subnodes |
| 163 |
|
|
if (my $item_exclude = $self->{options}->{$_}->{exclude}) { |
| 164 |
|
|
$self->{meta}->{$_}->{subnodes_exclude} = $item_exclude; |
| 165 |
|
|
} |
| 166 |
|
|
|
| 167 |
|
|
# TypeProvider |
| 168 |
|
|
if (my $item_type = $self->{args}->{$_ . '_type'}) { |
| 169 |
|
|
my @item_type = split(':', $item_type); |
| 170 |
|
|
$self->{meta}->{$_}->{TypeProvider} = { method => $item_type[0], arg => $item_type[1] }; |
| 171 |
|
|
} |
| 172 |
|
|
|
| 173 |
|
|
# Callbacks - writers (will be triggered _before_ writing to target) |
| 174 |
|
|
if (my $item_writers = $self->{args}->{$_ . '_callbacks_write'}) { |
| 175 |
|
|
my $descent = $_; # this is important since the following code inside the map wants to use its own context variables |
| 176 |
|
|
map { $self->{meta}->{$descent}->{Callback}->{write}->{$_}++; } @$item_writers; |
| 177 |
|
|
} |
| 178 |
|
|
|
| 179 |
|
|
# Callbacks - readers (will be triggered _after_ reading from source) |
| 180 |
|
|
if (my $item_readers = $self->{args}->{$_ . '_callbacks_read'}) { |
| 181 |
|
|
my $descent = $_; |
| 182 |
|
|
map { $self->{meta}->{$descent}->{Callback}->{read}->{$_}++; } @$item_readers; |
| 183 |
|
|
} |
| 184 |
|
|
|
| 185 |
|
|
# resolve storage objects |
| 186 |
|
|
$self->{meta}->{$_}->{storage} = $self->{options}->{$_}->{storage}->{handle}; |
| 187 |
|
|
} |
| 188 |
|
|
|
| 189 |
|
|
#print Dumper($self->{meta}); |
| 190 |
|
|
return 1; |
| 191 |
|
|
|
| 192 |
|
|
} |
| 193 |
|
|
|
| 194 |
|
|
|
| 195 |
|
|
sub _buildFieldmappingV1 { |
| 196 |
|
|
my $self = shift; |
| 197 |
|
|
|
| 198 |
|
|
# build mapping |
| 199 |
|
|
# incoming: and Array of node map entries (Array or Hash) - e.g. |
| 200 |
|
|
# [ 'source:item_name' => 'target:class_val' ] |
| 201 |
|
|
# { source => 'event->startDateTime', target => 'begindate' } |
| 202 |
|
|
foreach (@{$self->{args}->{mapping}}) { |
| 203 |
|
|
if (ref $_ eq 'ARRAY') { |
| 204 |
|
|
my @entry1 = split(':', $_->[0]); |
| 205 |
|
|
my @entry2 = split(':', $_->[1]); |
| 206 |
|
|
my $descent = []; |
| 207 |
|
|
my $node = []; |
| 208 |
|
|
$descent->[0] = $entry1[0]; |
| 209 |
|
|
$descent->[1] = $entry2[0]; |
| 210 |
|
|
$node->[0] = $entry1[1]; |
| 211 |
|
|
$node->[1] = $entry2[1]; |
| 212 |
|
|
push @{$self->{meta}->{$descent->[0]}->{childnodes}}, $node->[0]; |
| 213 |
|
|
push @{$self->{meta}->{$descent->[1]}->{childnodes}}, $node->[1]; |
| 214 |
|
|
} elsif (ref $_ eq 'HASH') { |
| 215 |
|
|
foreach my $entry_key (keys %$_) { |
| 216 |
|
|
my $entry_val = $_->{$entry_key}; |
| 217 |
|
|
push @{$self->{meta}->{$entry_key}->{childnodes}}, $entry_val; |
| 218 |
|
|
} |
| 219 |
|
|
} |
| 220 |
|
|
|
| 221 |
|
|
} |
| 222 |
|
|
|
| 223 |
|
|
return 1; |
| 224 |
|
|
|
| 225 |
|
|
} |
| 226 |
|
|
|
| 227 |
|
|
|
| 228 |
|
|
1; |