78 |
warn "### from_subfields = ", dump( $from_subfields ),$/; |
warn "### from_subfields = ", dump( $from_subfields ),$/; |
79 |
warn "### to_subfields = ", dump( $to_subfields ),$/; |
warn "### to_subfields = ", dump( $to_subfields ),$/; |
80 |
|
|
81 |
my $fields_re = join('|', keys %$to_subfields ); |
our $_template; |
82 |
|
|
83 |
my $fill_in; |
$_template->{fields_re} = { |
84 |
my @marc_out; |
isis => join('|', keys %$from_subfields ), |
85 |
|
marc => join('|', keys %$to_subfields ), |
86 |
|
}; |
87 |
|
|
88 |
my $marc_template; |
my @marc_out; |
|
_parse_template( $args->{marc_template}, $marc_template ); |
|
89 |
|
|
90 |
sub _parse_template { |
sub _parse_template { |
91 |
my ( $templates, $marc_template_order ) = @_; |
my ( $name, $templates ) = @_; |
92 |
|
|
93 |
|
my $fields_re = $_template->{fields_re}->{ $name } || die "can't find $name in ",dump( $_template->{fields_re} ); |
94 |
|
|
95 |
foreach my $template ( @{ $templates } ) { |
foreach my $template ( @{ $templates } ) { |
96 |
our $count = {}; |
our $count = {}; |
97 |
our @marc_order = (); |
our @order = (); |
98 |
sub my_count { |
sub my_count { |
99 |
my $sf = shift; |
my $sf = shift; |
100 |
my $nr = $count->{$sf}++; |
my $nr = $count->{$sf}++; |
101 |
push @marc_order, [ $sf, $nr ]; |
push @order, [ $sf, $nr ]; |
102 |
return $sf . $nr; |
return $sf . $nr; |
103 |
} |
} |
104 |
my $pos_template = $template; |
my $pos_template = $template; |
105 |
$pos_template =~ s/($fields_re)/my_count($1)/ge; |
$pos_template =~ s/($fields_re)/my_count($1)/ge; |
106 |
my $count_key = dump( $count ); |
my $count_key = dump( $count ); |
107 |
warn "### template: |$template| -> |$pos_template| count = $count_key marc_order = ",dump( @marc_order ),$/; |
warn "### template: |$template| -> |$pos_template| count = $count_key order = ",dump( @order ),$/; |
108 |
$marc_template->{pos}->{ $count_key } = $pos_template; |
$_template->{$name}->{pos}->{ $count_key } = $pos_template; |
109 |
$marc_template->{order}->{ $pos_template } = [ @marc_order ]; |
$_template->{$name}->{order}->{ $pos_template } = [ @order ]; |
110 |
} |
} |
111 |
warn "### from ",dump( $templates ), " created ", dump( $marc_template ); |
warn "### from ",dump( $templates ), " using $fields_re created ", dump( $_template ); |
112 |
} |
} |
113 |
|
|
114 |
warn "### marc_template = ",dump( $marc_template ); |
_parse_template( 'marc', $args->{marc_template} ); |
115 |
|
_parse_template( 'isis', $args->{isis_template} ); |
116 |
|
warn "### _template = ",dump( $_template ); |
117 |
|
|
118 |
my $m; |
my $m; |
119 |
|
|
141 |
|
|
142 |
warn "### new_r = ",dump( $new_r ); |
warn "### new_r = ",dump( $new_r ); |
143 |
|
|
144 |
my $from_count_key = dump( $to_count ); |
my $count_key = { |
145 |
|
from => dump( $from_count ), |
146 |
|
to => dump( $to_count), |
147 |
|
}; |
148 |
|
|
149 |
|
warn "### count_key = ",dump( $count_key ), $/; |
150 |
|
|
151 |
|
my $processed_templates = 0; |
152 |
|
|
153 |
|
# this defines order of traversal |
154 |
|
foreach ( qw/isis:from marc:to/ ) { |
155 |
|
my ($name,$count_name) = split(/:/); |
156 |
|
warn "## traverse $name $count_name\n"; |
157 |
|
|
158 |
|
my $ckey = $count_key->{$count_name} || die "can't find count_key $count_name in ",dump( $count_key ); |
159 |
|
|
160 |
|
my $template = $_template->{$name}->{pos}->{ $ckey } || next; |
161 |
|
$processed_templates++; |
162 |
|
|
163 |
|
warn "### selected template: |$template|\n"; |
164 |
|
|
165 |
|
our $fill_in = {}; |
166 |
|
|
167 |
|
my @templates = split(/\|/, $template ); |
168 |
|
@templates = ( $template ); |
169 |
|
|
170 |
|
foreach my $sf ( @templates ) { |
171 |
|
sub fill_in { |
172 |
|
my ( $name, $r, $sf, $nr ) = @_; |
173 |
|
my ( $from_sf, $from_nr, $v ); |
174 |
|
if ( $name eq 'marc' ) { |
175 |
|
( $from_sf, $from_nr ) = @{ $new_r->{$sf}->[$nr] }; |
176 |
|
} else { |
177 |
|
( $from_sf, $from_nr ) = ( $sf, $nr ); |
178 |
|
} |
179 |
|
my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr"; |
180 |
|
warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ), $/; |
181 |
|
if ( ref( $v ) eq 'ARRAY' ) { |
182 |
|
$fill_in->{$sf}->[$nr] = $v->[$from_nr]; |
183 |
|
return $v->[$from_nr]; |
184 |
|
} elsif ( $from_nr == 0 ) { |
185 |
|
$fill_in->{$sf}->[$nr] = $v; |
186 |
|
return $v; |
187 |
|
} else { |
188 |
|
die "requested subfield $from_sf/$from_nr but it's ",dump( $v ); |
189 |
|
} |
190 |
|
} |
191 |
|
my $fields_re = $_template->{fields_re}->{ $name } || die "can't find $name in ",dump( $_template->{fields_re} ); |
192 |
|
warn "#### $sf <<<< $fields_re\n"; |
193 |
|
$sf =~ s/($fields_re)(\d+)/fill_in($name,$r,$1,$2)/ge; |
194 |
|
warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/; |
195 |
|
} |
196 |
|
|
197 |
warn "### from_count = ",dump( $from_count ), $/; |
warn "## template: |$template|\n## _template->$name = ",dump( $_template->{$name} ); |
|
warn "### to_count = ",dump( $to_count ), $/; |
|
198 |
|
|
199 |
my $template = $marc_template->{pos}->{ $from_count_key } || |
$sf_pos = $#m; |
|
die "I don't have template for:\n$from_count_key\n## available templates\n", dump( $marc_template->{pos} ); |
|
200 |
|
|
201 |
warn "### selected template: |$template|\n"; |
foreach my $sf ( @{ $_template->{$name}->{order}->{$template} } ) { |
202 |
|
my ( $sf, $nr ) = @$sf; |
203 |
$fill_in = {}; |
my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr"; |
204 |
|
warn "++ $sf/$nr |$v|\n"; |
205 |
my @templates = split(/\|/, $template ); |
push @$m, ( $sf, $v ); |
|
@templates = ( $template ); |
|
|
|
|
|
foreach my $sf ( @templates ) { |
|
|
sub fill_in { |
|
|
my ( $r, $sf, $nr ) = @_; |
|
|
my ( $from_sf, $from_nr ) = @{ $new_r->{ $sf }->[ $nr ] }; |
|
|
my $v = $r->{ $from_sf }; # || die "no $from_sf/$from_nr"; |
|
|
warn "#### fill_in( $sf, $nr ) = $from_sf/$from_nr >>>> ",dump( $v ), $/; |
|
|
if ( ref( $v ) eq 'ARRAY' ) { |
|
|
$fill_in->{$sf}->[$nr] = $v->[$from_nr]; |
|
|
return $v->[$from_nr]; |
|
|
} elsif ( $from_nr == 0 ) { |
|
|
$fill_in->{$sf}->[$nr] = $v; |
|
|
return $v; |
|
|
} else { |
|
|
die "requested subfield $from_sf/$from_nr but it's ",dump( $v ); |
|
|
} |
|
206 |
} |
} |
|
warn "#### $sf <<<< $fields_re\n"; |
|
|
$sf =~ s/($fields_re)(\d+)/fill_in($r,$1,$2)/ge; |
|
|
warn "#### >>>> $sf with fill_in = ",dump( $fill_in ),$/; |
|
|
} |
|
207 |
|
|
208 |
warn "## template: |$template|\n## marc_template = ",dump( $marc_template ); |
warn "#### >>>> created MARC record: ", dump( $m ); |
209 |
|
|
210 |
foreach my $sf ( @{ $marc_template->{order}->{$template} } ) { |
push @marc_out, $m; |
|
my ( $sf, $nr ) = @$sf; |
|
|
my $v = $fill_in->{$sf}->[$nr] || die "can't find fill_in $sf/$nr"; |
|
|
warn "++ $sf/$nr |$v|\n"; |
|
|
push @$m, ( $sf, $v ); |
|
211 |
} |
} |
212 |
|
|
213 |
warn "#### >>>> created marc: ", dump( $m ); |
die "I don't have template for fields ",dump( $count_key ), "\n## available templates\n", dump( $_template ) unless $processed_templates; |
214 |
|
warn ">>> $processed_templates templates applied to data\n"; |
|
push @marc_out, $m; |
|
215 |
} |
} |
216 |
|
|
217 |
warn "### marc_template produced: ",dump( @marc_out ); |
|
218 |
|
warn "### marc_template produced following MARC records: ",dump( @marc_out ); |
219 |
|
|
220 |
foreach my $marc ( @marc_out ) { |
foreach my $marc ( @marc_out ) { |
221 |
warn "+++ ",dump( $marc ); |
warn "+++ ",dump( $marc ); |