1 package OpenILS::Application::Vandelay;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
5 use Unicode::Normalize;
6 use OpenSRF::EX qw/:try/;
8 use OpenSRF::AppSession;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenSRF::Utils::Cache;
12 use OpenILS::Utils::Fieldmapper;
13 use OpenILS::Utils::CStoreEditor qw/:funcs/;
19 use OpenILS::Utils::Fieldmapper;
21 use Time::HiRes qw(time);
23 use OpenSRF::Utils::Logger qw/:level/;
24 my $log = 'OpenSRF::Utils::Logger';
40 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
44 # --------------------------------------------------------------------------------
47 sub create_bib_queue {
55 my $e = new_editor(authtoken => $auth, xact => 1);
57 return $e->die_event unless $e->checkauth;
58 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE', $owner);
60 my $queue = new Fieldmapper::vandelay::bib_queue();
61 $queue->name( $name );
62 $queue->owner( $owner );
63 $queue->queue_type( $type ) if ($type);
64 $queue->queue_purpose( $purpose ) if ($purpose);
66 my $new_id = $e->create_vandelay_bib_queue( $queue );
67 $e->die_event unless ($new_id);
73 __PACKAGE__->register_method(
74 api_name => "open-ils.vandelay.bib_queue.create",
75 method => "create_bib_queue",
81 sub create_auth_queue {
89 my $e = new_editor(authtoken => $auth, xact => 1);
91 return $e->die_event unless $e->checkauth;
92 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $owner);
94 my $queue = new Fieldmapper::vandelay::authority_queue();
95 $queue->name( $name );
96 $queue->owner( $owner );
97 $queue->queue_type( $type ) if ($type);
98 $queue->queue_purpose( $purpose ) if ($purpose);
100 my $new_id = $e->create_vandelay_authority_queue( $queue );
101 $e->die_event unless ($new_id);
107 __PACKAGE__->register_method(
108 api_name => "open-ils.vandelay.authority_queue.create",
109 method => "create_auth_queue",
114 sub add_record_to_bib_queue {
122 my $e = new_editor(authtoken => $auth, xact => 1);
124 $queue = $e->retrieve_vandelay_bib_queue($queue);
126 return $e->die_event unless $e->checkauth;
127 return $e->die_event unless
128 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
129 $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
131 my $new_id = _add_auth_rec($e, $marc, $queue->id, $purpose);
133 $e->die_event unless ($new_id);
139 __PACKAGE__->register_method(
140 api_name => "open-ils.vandelay.queued_bib_record.create",
141 method => "add_record_to_bib_queue",
152 my $rec = new Fieldmapper::vandelay::queued_bib_record();
154 $rec->queue( $queue );
155 $rec->purpose( $purpose ) if ($purpose);
157 return $e->create_vandelay_queued_bib_record( $rec );
160 sub add_record_to_authority_queue {
168 my $e = new_editor(authtoken => $auth, xact => 1);
170 $queue = $e->retrieve_vandelay_authority_queue($queue);
172 return $e->die_event unless $e->checkauth;
173 return $e->die_event unless
174 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
175 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
177 my $new_id = _add_auth_rec($e, $marc, $queue->id, $purpose);
179 $e->die_event unless ($new_id);
185 __PACKAGE__->register_method(
186 api_name => "open-ils.vandelay.queued_authority_record.create",
187 method => "add_record_to_authority_queue",
197 my $rec = new Fieldmapper::vandelay::queued_authority_record();
199 $rec->queue( $queue );
200 $rec->purpose( $purpose ) if ($purpose);
202 return $e->create_vandelay_queued_authority_record( $rec );
209 my $fingerprint = shift;
212 my $e = new_editor(authtoken => $auth, xact => 1);
214 if ($self->{record_type} eq 'bib') {
215 return $e->die_event unless $e->checkauth;
216 return $e->die_event unless
217 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
218 $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
220 return $e->die_event unless $e->checkauth;
221 return $e->die_event unless
222 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
223 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
226 my $method = 'open-ils.vandelay.queued_'.$self->{record_type}.'_record.create';
227 $method = $self->method_lookup( $method );
229 my $cache = new OpenSRF::Utils::Cache();
231 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
232 my $purpose = $data->{purpose};
233 $data = decode_base64($data->{marc});
235 my $fh = new IO::Scalar \$data;
237 my $batch = new MARC::Batch ( $type, $fh );
241 while (my $r = $batch->next) {
243 (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
244 $xml =~ s/^<\?xml.+\?\s*>//go;
245 $xml =~ s/>\s+</></go;
246 $xml =~ s/\p{Cc}//go;
247 $xml = $self->entityize($xml);
248 $xml =~ s/[\x00-\x1f]//go;
250 if ($self->{record_type} eq 'bib') {
251 _add_bib_rec( $e, $xml, $queue, $purpose );
253 _add_auth_rec( $e, $xml, $queue, $purpose );
257 $client->respond( $count );
260 $log->warn("Encountered a bad record at Vandelay ingest: ".$error);
267 __PACKAGE__->register_method(
268 api_name => "open-ils.vandelay.bib.process_spool",
269 method => "process_spool",
274 __PACKAGE__->register_method(
275 api_name => "open-ils.vandelay.auth.process_spool",
276 method => "process_spool",
279 record_type => 'auth'