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 {
56 my $e = new_editor(authtoken => $auth, xact => 1);
58 return $e->die_event unless $e->checkauth;
59 return $e->die_event unless $e->allowed('CREATE_BIB_IMPORT_QUEUE', $owner);
61 my $queue = new Fieldmapper::vandelay::bib_queue();
62 $queue->name( $name );
63 $queue->owner( $owner );
64 $queue->queue_type( $type ) if ($type);
65 $queue->queue_purpose( $purpose ) if ($purpose);
67 my $new_id = $e->create_vandelay_bib_queue( $queue );
68 $e->die_event unless ($new_id);
74 __PACKAGE__->register_method(
75 api_name => "open-ils.vandelay.bib_queue.create",
76 method => "create_bib_queue",
82 sub create_auth_queue {
91 my $e = new_editor(authtoken => $auth, xact => 1);
93 return $e->die_event unless $e->checkauth;
94 return $e->die_event unless $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $owner);
96 my $queue = new Fieldmapper::vandelay::authority_queue();
97 $queue->name( $name );
98 $queue->owner( $owner );
99 $queue->queue_type( $type ) if ($type);
100 $queue->queue_purpose( $purpose ) if ($purpose);
102 my $new_id = $e->create_vandelay_authority_queue( $queue );
103 $e->die_event unless ($new_id);
109 __PACKAGE__->register_method(
110 api_name => "open-ils.vandelay.authority_queue.create",
111 method => "create_auth_queue",
116 sub add_record_to_bib_queue {
123 my $e = new_editor(authtoken => $auth, xact => 1);
125 $queue = $e->retrieve_vandelay_bib_queue($queue);
127 return $e->die_event unless $e->checkauth;
128 return $e->die_event unless
129 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
130 $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
132 my $new_id = _add_auth_rec($e, $marc, $queue->id);
134 $e->die_event unless ($new_id);
140 __PACKAGE__->register_method(
141 api_name => "open-ils.vandelay.queued_bib_record.create",
142 method => "add_record_to_bib_queue",
152 my $rec = new Fieldmapper::vandelay::queued_bib_record();
154 $rec->queue( $queue );
156 return $e->create_vandelay_queued_bib_record( $rec );
159 sub add_record_to_authority_queue {
166 my $e = new_editor(authtoken => $auth, xact => 1);
168 $queue = $e->retrieve_vandelay_authority_queue($queue);
170 return $e->die_event unless $e->checkauth;
171 return $e->die_event unless
172 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
173 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
175 my $new_id = _add_auth_rec($e, $marc, $queue->id);
177 $e->die_event unless ($new_id);
183 __PACKAGE__->register_method(
184 api_name => "open-ils.vandelay.queued_authority_record.create",
185 method => "add_record_to_authority_queue",
195 my $rec = new Fieldmapper::vandelay::queued_authority_record();
197 $rec->queue( $queue );
199 return $e->create_vandelay_queued_authority_record( $rec );
206 my $fingerprint = shift;
209 my $e = new_editor(authtoken => $auth, xact => 1);
211 if ($self->{record_type} eq 'bib') {
212 return $e->die_event unless $e->checkauth;
213 return $e->die_event unless
214 ($e->allowed('CREATE_BIB_IMPORT_QUEUE', undef, $queue) ||
215 $e->allowed('CREATE_BIB_IMPORT_QUEUE', $queue->owner));
217 return $e->die_event unless $e->checkauth;
218 return $e->die_event unless
219 ($e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', undef, $queue) ||
220 $e->allowed('CREATE_AUTHORITY_IMPORT_QUEUE', $queue->owner));
223 my $method = 'open-ils.vandelay.queued_'.$self->{record_type}.'_record.create';
224 $method = $self->method_lookup( $method );
226 my $cache = new OpenSRF::Utils::Cache();
228 my $data = $cache->get_cache('vandelay_import_spool_' . $fingerprint);
229 $data = decode_base64($data);
231 my $fh = new IO::Scalar \$data;
233 my $batch = new MARC::Batch ( $type, $fh );
237 while (my $r = $batch->next) {
239 (my $xml = $rec->as_xml_record()) =~ s/\n//sog;
240 $xml =~ s/^<\?xml.+\?\s*>//go;
241 $xml =~ s/>\s+</></go;
242 $xml =~ s/\p{Cc}//go;
243 $xml = $self->entityize($xml);
244 $xml =~ s/[\x00-\x1f]//go;
246 if ($self->{record_type} eq 'bib') {
247 _add_bib_rec( $e, $xml, $queue );
249 _add_auth_rec( $e, $xml, $queue );
253 $client->respond( $count );
256 $log->warn("Encountered a bad record at Vandelay ingest: ".$error);
263 __PACKAGE__->register_method(
264 api_name => "open-ils.vandelay.bib.process_spool",
265 method => "process_spool",
270 __PACKAGE__->register_method(
271 api_name => "open-ils.vandelay.auth.process_spool",
272 method => "process_spool",
275 record_type => 'auth'