1 |
## ------------------------------------------------------------------------- |
2 |
## |
3 |
## $Id: V1.pm,v 1.1 2003/02/09 05:04:32 joko Exp $ |
4 |
## |
5 |
## Copyright (c) 2002, 2003 Andreas Motl <andreas.motl@ilo.de> |
6 |
## |
7 |
## This module is licensed under the same terms as Perl itself. |
8 |
## |
9 |
## ------------------------------------------------------------------------- |
10 |
## $Log: V1.pm,v $ |
11 |
## Revision 1.1 2003/02/09 05:04:32 joko |
12 |
## + initial commit |
13 |
## |
14 |
## ------------------------------------------------------------------------- |
15 |
|
16 |
|
17 |
package Data::Transfer::Sync::API::V1; |
18 |
|
19 |
use strict; |
20 |
use warnings; |
21 |
|
22 |
use mixin::with qw( Data::Transfer::Sync ); |
23 |
|
24 |
|
25 |
# start here |
26 |
|
27 |
use Data::Dumper; |
28 |
|
29 |
# get logger instance |
30 |
my $logger = Log::Dispatch::Config->instance; |
31 |
|
32 |
|
33 |
sub checkOptions { |
34 |
my $self = shift; |
35 |
|
36 |
#print Dumper($self->{options}); |
37 |
|
38 |
my $result = 1; |
39 |
|
40 |
# check - do we have a target node? |
41 |
if (!$self->{options}->{target}->{nodeName}) { |
42 |
$logger->warning( __PACKAGE__ . "->checkOptions: name of target node missing - please check metadata or specify '--target-node=*'."); |
43 |
$result = 0; |
44 |
} |
45 |
|
46 |
# check - do we have a mapping? |
47 |
#if (!$self->{options}->{details}) { |
48 |
# $logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'fieldmap' could be determined."); |
49 |
# $result = 0; |
50 |
#} |
51 |
|
52 |
# TODO: extend! |
53 |
|
54 |
return $result; |
55 |
|
56 |
} |
57 |
|
58 |
|
59 |
sub checkOptions_V1 { |
60 |
my $self = shift; |
61 |
my $opts = shift; |
62 |
|
63 |
my $result = 1; |
64 |
|
65 |
# check - do we have a target node? |
66 |
if (!$opts->{target_node}) { |
67 |
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'target node' could be determined."); |
68 |
$result = 0; |
69 |
} |
70 |
|
71 |
# check - do we have a mapping? |
72 |
if (!$opts->{mapping} && !$opts->{mapping_module}) { |
73 |
$logger->warning( __PACKAGE__ . "->checkOptions: Error while resolving resource metadata - no 'mapping' could be determined."); |
74 |
$result = 0; |
75 |
} |
76 |
|
77 |
return $result; |
78 |
|
79 |
} |
80 |
|
81 |
|
82 |
sub configure_V1 { |
83 |
my $self = shift; |
84 |
$logger->debug( __PACKAGE__ . "->configure"); |
85 |
my @args = @_; |
86 |
if (!isEmpty(\@args)) { |
87 |
my %properties = @_; |
88 |
# merge args to properties |
89 |
map { $self->{$_} = $properties{$_}; } keys %properties; |
90 |
$self->_init(); |
91 |
$self->_initV1(); |
92 |
} else { |
93 |
#print "no args!", "\n"; |
94 |
} |
95 |
#print Dumper($self); |
96 |
$self->{state}->{configured} = 1; |
97 |
return 1; |
98 |
} |
99 |
|
100 |
|
101 |
sub prepareOptions_V1_old { |
102 |
|
103 |
my $self = shift; |
104 |
my $opts = shift; |
105 |
|
106 |
$opts->{mode} ||= ''; |
107 |
$opts->{erase} ||= 0; |
108 |
$opts->{prepare} ||= 0; |
109 |
#$opts->{import} ||= 0; |
110 |
|
111 |
if (!$opts->{source_node}) { |
112 |
$logger->error( __PACKAGE__ . "->prepareOptions failed: Please specify source-node!"); |
113 |
return; |
114 |
} |
115 |
|
116 |
$logger->notice( __PACKAGE__ . "->prepareOptions( source_node $opts->{source_node} mode $opts->{mode} erase $opts->{erase} prepare $opts->{prepare} )"); |
117 |
|
118 |
#if (!$opts->{mapping} || !$opts->{mapping_module}) { |
119 |
if (!$opts->{mapping}) { |
120 |
$logger->warning( __PACKAGE__ . "->prepareOptions: No mapping supplied - please check key 'mappings' in global configuration or specify additional argument '--mapping'."); |
121 |
#return; |
122 |
} |
123 |
|
124 |
$opts->{mapping_module} ||= $opts->{mapping}; |
125 |
my $evstring = "use $opts->{mapping_module};"; |
126 |
eval($evstring); |
127 |
if ($@) { |
128 |
$logger->warning( __PACKAGE__ . "->prepareOptions: error while trying to access mapping - $@"); |
129 |
return; |
130 |
} |
131 |
|
132 |
# resolve mapping metadata (returned from sub) |
133 |
my $mapObject = $opts->{mapping_module}->new(); |
134 |
#print Dumper($map); |
135 |
my $source_node_name = $opts->{source_node}; |
136 |
# check if mapping for certain node is contained in mapping object |
137 |
if (!$mapObject->can($source_node_name)) { |
138 |
$logger->warning( __PACKAGE__ . "->prepareOptions: Can't access mapping for node \"$source_node_name\" - please check $opts->{mapping_module}."); |
139 |
return; |
140 |
} |
141 |
my $map = $mapObject->$source_node_name; |
142 |
|
143 |
#print Dumper($map); |
144 |
|
145 |
# check here if "target" is actually a CODEref - in this case: resolve it - deprecated!!! ??? |
146 |
if (ref $map->{target} eq 'CODE') { |
147 |
$map->{target} = $map->{target}->($source_node_name); |
148 |
} |
149 |
|
150 |
# resolve expressions (on nodename-level) here |
151 |
if ($map->{target} =~ m/^(code|expr):(.+?)$/) { |
152 |
my $target_dynamic_type = $1; |
153 |
my $target_dynamic_expression = $2; |
154 |
if (lc $target_dynamic_type eq 'code') { |
155 |
# $map->{target} = $mapObject->$target_dynamic_expression($map); |
156 |
} |
157 |
} |
158 |
|
159 |
# remove asymmetries from $map (patch keys) |
160 |
$map->{source_node} = $map->{source}; delete $map->{source}; |
161 |
$map->{target_node} = $map->{target}; delete $map->{target}; |
162 |
$map->{mapping} = $map->{details}; delete $map->{details}; |
163 |
$map->{direction} = $map->{mode}; delete $map->{mode}; |
164 |
|
165 |
# defaults (mostly for backward-compatibility) |
166 |
$map->{source_node} ||= $source_node_name; |
167 |
$map->{source_ident} ||= 'storage_method:id'; |
168 |
$map->{target_ident} ||= 'property:oid'; |
169 |
$map->{direction} ||= $opts->{mode}; # | PUSH | PULL | FULL |
170 |
$map->{method} ||= 'checksum'; # | timestamp |
171 |
$map->{source_exclude} ||= [qw( cs )]; |
172 |
|
173 |
# merge map to opts |
174 |
map { $opts->{$_} = $map->{$_}; } keys %$map; |
175 |
|
176 |
#print Dumper($opts); |
177 |
|
178 |
# TODO: move this to checkOptions... |
179 |
|
180 |
# check - do we have a target? |
181 |
if (!$opts->{target_node}) { |
182 |
$logger->warning( __PACKAGE__ . "->prepareOptions: No target given - please check metadata declaration."); |
183 |
return; |
184 |
} |
185 |
|
186 |
|
187 |
#return $opts; |
188 |
return 1; |
189 |
|
190 |
} |
191 |
|
192 |
# backward compatibility - this is active!!! |
193 |
sub options_to_V2 { |
194 |
my $self = shift; |
195 |
my $map = shift; |
196 |
|
197 |
$logger->debug( __PACKAGE__ . "->options_to_V2"); |
198 |
|
199 |
# trace |
200 |
#print Dumper($map); |
201 |
|
202 |
# move the 'target' address |
203 |
$map->{target} = { address => $map->{target} }; |
204 |
$map->{source} = { address => $map->{source} }; |
205 |
|
206 |
$map->{source}->{ident} = $map->{source_ident} if $map->{source_ident}; |
207 |
delete $map->{source_ident}; |
208 |
$map->{source}->{callbacks}->{read} = $map->{source_callbacks_read} if $map->{source_callbacks_read}; |
209 |
delete $map->{source_callbacks_read}; |
210 |
$map->{source}->{callbacks}->{write} = $map->{source_callbacks_write} if $map->{source_callbacks_write}; |
211 |
delete $map->{source_callbacks_write}; |
212 |
$map->{source}->{filter} = $map->{source_filter} if $map->{source_filter}; |
213 |
delete $map->{source_filter}; |
214 |
|
215 |
$map->{target}->{ident} = $map->{target_ident} if $map->{target_ident}; |
216 |
delete $map->{target_ident}; |
217 |
|
218 |
} |
219 |
|
220 |
|
221 |
|
222 |
1; |