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, 3, 'User', '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 next if ($patron->nodeType == 3);
82 my $p = new Fieldmapper::actor::user;
83 my $card = new Fieldmapper::actor::card;
84 my $profile_sce = new Fieldmapper::actor::stat_cat_entry_user_map;
86 my $old_profile = $patron->findvalue( 'user_profile' );
88 my $bc = $patron->findvalue( 'user_id' );
90 unless (defined($bc)) {
91 my $xml = $patron->toString;
92 warn "!!! no barcode found in UMS data, user number $count, xml => $xml \n";
102 warn "!!! no uid mapping found for barcode $bc\n";
111 warn "!!! user id lower than 2\n";
115 $card->barcode( $bc );
117 $card->active( 't' );
121 $p->passwd( $patron->findvalue( 'user_pin' ) );
123 my $new_profile = $p_map{$old_profile} || $default_profile;
125 $p->profile( $$profiles{$new_profile} );
128 warn "!!! no new profile found for $old_profile\n";
136 $p->master_account('f');
139 $p->claims_returned_count(0);
140 $p->credit_forward_balance(0);
141 $p->last_xact_id('IMPORT-'.$starttime);
144 $p->barred('t') if ( $patron->findvalue( 'user_status' ) eq 'BARRED' );
146 $p->ident_type( $identtypeid );
147 my $id_val = $patron->findvalue( 'user_altid' );
148 $p->ident_value( $id_val ) if ($id_val);
150 my ($fname,$mname,$lname) = ($patron->findvalue('first_name'),$patron->findvalue('middle_name'),$patron->findvalue('last_name'));
160 $p->first_given_name( $fname );
161 $p->second_given_name( $mname );
162 $p->family_name( $lname );
164 $p->day_phone( $patron->findvalue( 'Address/dayphone' ) );
165 $p->evening_phone( $patron->findvalue( 'Address/homephone' ) );
166 $p->other_phone( $patron->findvalue( 'Address/workphone' ) );
168 my $hlib = $$orgs{$patron->findvalue( 'user_library' )};
171 warn "!!! no home library found in patron record\n";
174 $p->home_ou( $hlib );
176 $p->dob( parse_date( $patron->findvalue( 'birthdate' ) ) );
177 $p->create_date( parse_date( $patron->findvalue( 'user_priv_granted' ) ) );
178 $p->expire_date( parse_date( $patron->findvalue( 'user_priv_expires' ) ) );
180 $p->alert_message("Legacy Import Message: old profile was FIXME")
181 if ($old_profile eq 'FIXME');
184 $net_access = 2 if ($old_profile =~ /^U.I/o);
185 $net_access = 3 if ($old_profile =~ /^X.I/o);
187 $p->net_access_level( $net_access );
189 $profile_sce->target_usr( $uid );
190 $profile_sce->stat_cat( $profileid );
191 $profile_sce->stat_cat_entry( $old_profile );
194 my $mailing_addr_id = $patron->findvalue( 'user_mailingaddr' );
197 for my $addr ( $patron->findnodes( "Address" ) ) {
199 $p->email( $patron->findvalue( 'email' ) );
204 my $line1 = $addr->findvalue( "${prefix}line1" );
205 $prefix = 'std_' if (!$line1);
207 $line1 = $addr->findvalue( "${prefix}line1" );
208 next unless ($line1);
210 my $a = new Fieldmapper::actor::user_address;
212 $a->street1( $line1 );
213 $a->street2( $addr->findvalue( "${prefix}line2" ) );
214 $a->city( $addr->findvalue( "${prefix}city" ) );
215 $a->state( $addr->findvalue( "${prefix}state" ) );
217 $addr->findvalue( "${prefix}zip" ) .
218 '-' . $addr->findvalue( "${prefix}zip4" )
222 $a->valid( 't' ) if ($prefix eq 'std_');
223 $a->valid( 'f' ) if ($prefix eq 'std_' and $a->findvalue( "${prefix}dpvscore" ) < 3);
225 $a->within_city_limits( 'f' );
228 if ($addr->getAttribute('addr_type') == $mailing_addr_id) {
229 $a->address_type( 'LEGACY MAILING' );
231 $a->address_type( 'LEGACY' );
236 if ($prefix eq 'coa_') {
240 $line1 = $addr->findvalue( "${prefix}line1" );
241 next unless ($line1);
243 $a = new Fieldmapper::actor::user_address;
245 $a->street1( $line1 );
246 $a->street2( $addr->findvalue( "${prefix}line2" ) );
247 $a->city( $addr->findvalue( "${prefix}city" ) );
248 $a->state( $addr->findvalue( "${prefix}state" ) );
250 $addr->findvalue( "${prefix}zip" ) .
251 '-' . $addr->findvalue( "${prefix}zip4" )
256 $a->within_city_limits( 'f' );
259 $a->address_type( 'LEGACY' );
265 if ($all_valid eq 'f') {
266 $_->valid('f') for (@addresses);
270 for my $note_field ( qw#note comment voter bus_school Address/phone1 Address/phone2# ) {
271 for my $note ( $patron->findnodes( $note_field) ) {
272 my $a = new Fieldmapper::actor::usr_note;
275 $a->create_date('now');
277 $a->title( "Legacy ".$note->localName );
278 $a->value( $note->textContent );
284 print STDERR "\r$count ".$count/(time - $starttime) unless ($count % 100);
285 print JSON->perl2JSON( $_ )."\n" for ($p,$card,$profile_sce,@addresses,@notes);
299 if ($string eq 'NEVER') {
300 my (undef,undef,undef,$d,$m,$y) = localtime();
301 return sprintf('%04d-%02d-%02d', $y + 1920, $m + 1, $d);
302 } elsif (length($string) == 8 && $string =~ /^(\d{4})(\d{2})(\d{2})$/o) {
303 ($y,$m,$d) = ($1,$2,$3);
304 } elsif ($string =~ /(\d+)\D(\d+)\D(\d+)/o) { #looks like it's parsable
305 if ( length($3) > 2 ) { # looks like mm.dd.yyyy
306 if ( $1 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
307 if ($1 > 12 && $1 < 31 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
308 ($y,$m,$d) = ($3,$2,$1);
309 } elsif ($2 > 12 && $2 < 31 && $1 < 13) {
310 ($y,$m,$d) = ($3,$1,$2);
313 } elsif ( length($1) > 3 ) { # format probably yyyy.mm.dd
314 if ( $3 < 99 && $2 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
315 if ($2 > 12 && $2 < 32 && $3 < 13) { # well, actually it looks like yyyy.dd.mm -- why, I don't konw
316 ($y,$m,$d) = ($1,$3,$2);
317 } elsif ($3 > 12 && $3 < 31 && $2 < 13) {
318 ($y,$m,$d) = ($1,$2,$3);
321 } elsif ( $1 < 99 && $2 < 99 && $3 < 99 && $1 > 0 && $2 > 0 && $3 > 0) {
322 if ($3 < 7) { # probably 2000 or greater, mm.dd.yy
324 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
326 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
329 } else { # probably before 2000, mm.dd.yy
331 if ($1 > 12 && $1 < 32 && $2 < 13) { # well, actually it looks like dd.mm.yyyy
333 } elsif ($2 > 12 && $2 < 32 && $1 < 13) {
341 if ($y && $m && $d) {
343 $date = sprintf('%04d-%02d-%-2d',$y, $m, $d)
344 if (new DateTime ( year => $y, month => $m, day => $d ));