5 use lib '/openils/lib/perl5';
8 use OpenSRF::EX qw/:try/;
9 use OpenSRF::AppSession;
10 use OpenSRF::Utils::SettingsClient;
11 use OpenILS::Utils::Fieldmapper;
12 use Digest::MD5 qw/md5_hex/;
16 use Time::HiRes qw/time/;
19 my ($file,$config,$profileid,$identtypeid,$default_profile,$profile_map,$usermap) =
20 ('return_file_0623-2.xml', '/openils/conf/bootstrap.conf', 1, 1, 1, 'profile.map');
23 'usermap=s' => \$usermap,
25 'config=s' => \$config,
26 'default_profile=i' => \$default_profile,
27 'profile_map=s' => \$profile_map,
28 'profile_statcat_id=i' => \$profileid,
29 'identtypeid=i' => \$identtypeid,
35 while (my $line = <F>) {
37 my ($b,$i) = split(/\|/, $line);
38 $b =~ s/^\s*(\S+)\s*$/$1/o;
39 $i =~ s/^\s*(\S+)\s*$/$1/o;
48 while (my $line = <F>) {
50 my ($b,$i) = split(/\|/, $line);
51 $b =~ s/^\s*(\S+)\s*$/$1/o;
52 $i =~ s/^\s*(\S+)\s*$/$1/o;
58 my $doc = XML::LibXML->new->parse_file($file);
60 OpenSRF::System->bootstrap_client( config_file => $config );
61 Fieldmapper->import(IDL => OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
63 my $cstore = OpenSRF::AppSession->create( 'open-ils.cstore' );
65 my $profiles = $cstore->request(
66 'open-ils.cstore.direct.permission.grp_tree.search.atomic',
67 { id => { '!=' => undef } },
70 my $orgs = $cstore->request(
71 'open-ils.cstore.direct.actor.org_unit.search.atomic',
72 { id => { '!=' => undef } },
75 $profiles = { map { ($_->name => $_->id) } @$profiles };
76 $orgs = { map { ($_->shortname => $_->id) } @$orgs };
80 for my $patron ( $doc->documentElement->childNodes ) {
81 my $p = new Fieldmapper::actor::user;
82 my $card = new Fieldmapper::actor::card;
83 my $profile_sce = new Fieldmapper::actor::stat_cat_entry_user_map;
85 my $old_profile = $patron->findvalue( 'user_profile' );
87 my $bc = $patron->findvalue( 'user_id' );
107 $card->barcode( $bc );
109 $card->active( 't' );
113 $p->passwd( $patron->findvalue( 'user_pin' ) );
115 my $new_profile = $p_map{$old_profile};
116 unless ($new_profile) {
121 $p->profile( $$profiles{$new_profile} || $default_profile );
126 $p->master_account('f');
129 $p->claims_returned_count(0);
130 $p->credit_forward_balance(0);
131 $p->last_xact_id('IMPORT-'.$starttime);
134 $p->barred('t') if ( $patron->findvalue( 'user_status' ) eq 'BARRED' );
136 $p->ident_type( $identtypeid );
137 my $id_val = $patron->findvalue( 'user_altid' );
138 $p->ident_value( $id_val ) if ($id_val);
140 my ($fname,$mname,$lname) = ($patron->findvalue('first_name'),$patron->findvalue('middle_name'),$patron->findvalue('last_name'));
150 $p->first_given_name( $fname );
151 $p->second_given_name( $mname );
152 $p->family_name( $lname );
154 $p->day_phone( $patron->findvalue( 'Address/dayphone' ) );
155 $p->evening_phone( $patron->findvalue( 'Address/homephone' ) );
156 $p->other_phone( $patron->findvalue( 'Address/workphone' ) );
157 $p->email( $patron->findvalue( 'email' ) );
159 my $hlib = $$orgs{$patron->findvalue( 'user_library' )};
164 $p->home_ou( $hlib );
166 $p->dob( parse_date( $patron->findvalue( 'birthdate' ) ) );
167 $p->create_date( parse_date( $patron->findvalue( 'user_priv_granted' ) ) );
168 $p->expire_date( parse_date( $patron->findvalue( 'user_priv_expires' ) ) );
170 $p->alert_message("Legacy Import Message: old profile was FIXME")
171 if ($old_profile eq 'FIXME');
174 $net_access = 2 if ($old_profile =~ /^U.I/o);
175 $net_access = 3 if ($old_profile =~ /^X.I/o);
177 $p->net_access_level( $net_access );
179 $profile_sce->target_usr( $uid );
180 $profile_sce->stat_cat( $profileid );
181 $profile_sce->stat_cat_entry( $old_profile );
184 my $mailing_addr_id = $patron->findvalue( 'user_mailingaddr' );
186 for my $addr ( $patron->findnodes( "Address" ) ) {
189 my $line1 = $addr->findvalue( "${prefix}line1" );
190 $prefix = 'std_' if (!$line1);
192 $line1 = $addr->findvalue( "${prefix}line1" );
193 next unless ($line1);
195 my $a = new Fieldmapper::actor::user_address;
197 $a->street1( $line1 );
198 $a->street2( $addr->findvalue( "${prefix}line2" ) );
199 $a->city( $addr->findvalue( "${prefix}city" ) );
200 $a->state( $addr->findvalue( "${prefix}state" ) );
202 $addr->findvalue( "${prefix}zip" ) .
203 '-' . $addr->findvalue( "${prefix}zip4" )
207 $a->valid( 't' ) if ($prefix eq 'std_');
209 $a->within_city_limits( 'f' );
212 if ($addr->getAttribute('addr_type') == $mailing_addr_id) {
213 $a->address_type( 'LEGACY MAILING' );
215 $a->address_type( 'LEGACY' );
220 if ($prefix eq 'coa_') {
223 $line1 = $addr->findvalue( "${prefix}line1" );
224 next unless ($line1);
226 $a = new Fieldmapper::actor::user_address;
228 $a->street1( $line1 );
229 $a->street2( $addr->findvalue( "${prefix}line2" ) );
230 $a->city( $addr->findvalue( "${prefix}city" ) );
231 $a->state( $addr->findvalue( "${prefix}state" ) );
233 $addr->findvalue( "${prefix}zip" ) .
234 '-' . $addr->findvalue( "${prefix}zip4" )
238 $a->valid( 't' ) if ($prefix eq 'std_');
240 $a->within_city_limits( 'f' );
243 $a->address_type( 'LEGACY' );
250 for my $note_field ( qw#note comment voter bus_school Address/phone1 Address/phone2# ) {
251 for my $note ( $patron->findnodes( $note_field) ) {
252 my $a = new Fieldmapper::actor::usr_note;
255 $a->create_date('now');
257 $a->title( "Legacy ".$note->localName );
258 $a->value( $note->textContent );
264 print STDERR "\r$count ".$count/(time - $starttime) unless ($count % 100);
265 print JSON->perl2JSON( $_ )."\n" for ($p,$card,$profile_sce,@addresses,@notes);
279 if ($string eq 'NEVER') {
280 my (undef,undef,undef,$d,$m,$y) = localtime();
281 return sprintf('%04d-%02d-%02d', $y + 1920, $m + 1, $d);
282 } elsif (length($string) == 8 && $string =~ /^(\d{4})(\d{2})(\d{2})$/o) {
283 ($y,$m,$d) = ($1,$2,$3);
284 } elsif ($string =~ /(\d+)\D(\d+)\D(\d+)/o) { #looks like it's parsable
285 if ( length($3) > 2 ) { # looks like mm.dd.yyyy
286 if ( $1 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
287 if ($1 > 12 && $1 < 31 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
288 ($y,$m,$d) = ($3,$2,$1);
289 } elsif ($2 > 12 && $2 < 31 && $1 < 13) {
290 ($y,$m,$d) = ($3,$1,$2);
293 } elsif ( length($1) > 3 ) { # format probably yyyy.mm.dd
294 if ( $3 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
295 if ($2 > 12 && $2 < 32 && $3 < 13) { # well, actually it looks like yyyy.dd.mm -- why, I don't konw
296 ($y,$m,$d) = ($1,$3,$2);
297 } elsif ($3 > 12 && $3 < 31 && $2 < 13) {
298 ($y,$m,$d) = ($1,$2,$3);
301 } elsif ( $1 < 99 && $2 < 99 && $3 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
302 if ($3 < 7) { # probably 2000 or greater, mm.dd.yy
304 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
306 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
309 } else { # probably before 2000, mm.dd.yy
311 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
313 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
321 if ($y && $m && $d) {
323 $date = sprintf('%04d-%02d-%-2d',$y, $m, $d)
324 if (new DateTime ( year => $y, month => $m, day => $d ));