1 package OpenILS::Application::SuperCat;
6 # All OpenSRF applications must be based on OpenSRF::Application or
7 # a subclass thereof. Makes sense, eh?
8 use OpenSRF::Application;
9 use base qw/OpenSRF::Application/;
11 # This is the client class, used for connecting to open-ils.storage
12 use OpenSRF::AppSession;
14 # This is an extention of Error.pm that supplies some error types to throw
15 use OpenSRF::EX qw(:try);
17 # This is a helper class for querying the OpenSRF Settings application ...
18 use OpenSRF::Utils::SettingsClient;
20 # ... and here we have the built in logging helper ...
21 use OpenSRF::Utils::Logger qw($logger);
23 # ... and this is our OpenILS object (en|de)coder and psuedo-ORM package.
24 use OpenILS::Utils::Fieldmapper;
27 # We'll be working with XML, so...
30 use Unicode::Normalize;
42 # we need an XML parser
43 $_parser = new XML::LibXML;
45 $logger->debug("Got here!");
48 $_xslt = new XML::LibXSLT;
50 # parse the MODS xslt ...
51 my $mods3_xslt = $_parser->parse_file(
52 OpenSRF::Utils::SettingsClient
54 ->config_value( dirs => 'xsl' ).
55 "/MARC21slim2MODS3.xsl"
57 # and stash a transformer
58 $record_xslt{mods3}{xslt} = $_xslt->parse_stylesheet( $mods3_xslt );
59 $record_xslt{mods3}{namespace_uri} = 'http://www.loc.gov/mods/v3';
60 $record_xslt{mods3}{docs} = 'http://www.loc.gov/mods/';
61 $record_xslt{mods3}{schema_location} = 'http://www.loc.gov/standards/mods/v3/mods-3-1.xsd';
63 # parse the MODS xslt ...
64 my $mods_xslt = $_parser->parse_file(
65 OpenSRF::Utils::SettingsClient
67 ->config_value( dirs => 'xsl' ).
68 "/MARC21slim2MODS.xsl"
70 # and stash a transformer
71 $record_xslt{mods}{xslt} = $_xslt->parse_stylesheet( $mods_xslt );
72 $record_xslt{mods}{namespace_uri} = 'http://www.loc.gov/mods/';
73 $record_xslt{mods}{docs} = 'http://www.loc.gov/mods/';
74 $record_xslt{mods}{schema_location} = 'http://www.loc.gov/standards/mods/mods.xsd';
76 $logger->debug("Got here!");
78 # parse the ATOM entry xslt ...
79 my $atom_xslt = $_parser->parse_file(
80 OpenSRF::Utils::SettingsClient
82 ->config_value( dirs => 'xsl' ).
83 "/MARC21slim2ATOM.xsl"
85 # and stash a transformer
86 $record_xslt{atom}{xslt} = $_xslt->parse_stylesheet( $atom_xslt );
87 $record_xslt{atom}{namespace_uri} = 'http://www.w3.org/2005/Atom';
88 $record_xslt{atom}{docs} = 'http://www.ietf.org/rfc/rfc4287.txt';
90 # parse the RDFDC xslt ...
91 my $rdf_dc_xslt = $_parser->parse_file(
92 OpenSRF::Utils::SettingsClient
94 ->config_value( dirs => 'xsl' ).
95 "/MARC21slim2RDFDC.xsl"
97 # and stash a transformer
98 $record_xslt{rdf_dc}{xslt} = $_xslt->parse_stylesheet( $rdf_dc_xslt );
99 $record_xslt{rdf_dc}{namespace_uri} = 'http://purl.org/dc/elements/1.1/';
100 $record_xslt{rdf_dc}{schema_location} = 'http://purl.org/dc/elements/1.1/';
102 $logger->debug("Got here!");
104 # parse the SRWDC xslt ...
105 my $srw_dc_xslt = $_parser->parse_file(
106 OpenSRF::Utils::SettingsClient
108 ->config_value( dirs => 'xsl' ).
109 "/MARC21slim2SRWDC.xsl"
111 # and stash a transformer
112 $record_xslt{srw_dc}{xslt} = $_xslt->parse_stylesheet( $srw_dc_xslt );
113 $record_xslt{srw_dc}{namespace_uri} = 'info:srw/schema/1/dc-schema';
114 $record_xslt{srw_dc}{schema_location} = 'http://www.loc.gov/z3950/agency/zing/srw/dc-schema.xsd';
116 $logger->debug("Got here!");
118 # parse the OAIDC xslt ...
119 my $oai_dc_xslt = $_parser->parse_file(
120 OpenSRF::Utils::SettingsClient
122 ->config_value( dirs => 'xsl' ).
123 "/MARC21slim2OAIDC.xsl"
125 # and stash a transformer
126 $record_xslt{oai_dc}{xslt} = $_xslt->parse_stylesheet( $oai_dc_xslt );
127 $record_xslt{oai_dc}{namespace_uri} = 'http://www.openarchives.org/OAI/2.0/oai_dc/';
128 $record_xslt{oai_dc}{schema_location} = 'http://www.openarchives.org/OAI/2.0/oai_dc.xsd';
130 $logger->debug("Got here!");
132 # parse the RSS xslt ...
133 my $rss_xslt = $_parser->parse_file(
134 OpenSRF::Utils::SettingsClient
136 ->config_value( dirs => 'xsl' ).
137 "/MARC21slim2RSS2.xsl"
139 # and stash a transformer
140 $record_xslt{rss2}{xslt} = $_xslt->parse_stylesheet( $rss_xslt );
142 $logger->debug("Got here!");
144 # and finally, a storage server session
146 register_record_transforms();
151 sub register_record_transforms {
152 for my $type ( keys %record_xslt ) {
153 __PACKAGE__->register_method(
154 method => 'retrieve_record_transform',
155 api_name => "open-ils.supercat.record.$type.retrieve",
160 Returns the \U$type\E representation of the requested bibliographic record
165 desc => 'An OpenILS biblio::record_entry id',
169 { desc => "The bib record in \U$type\E",
178 my $stuff = NFC(shift());
179 $stuff =~ s/([\x{0080}-\x{fffd}])/sprintf('&#x%X;',ord($1))/sgoe;
184 sub retrieve_record_marcxml {
189 my $_storage = OpenSRF::AppSession->create( 'open-ils.storage' );
194 ->request( 'open-ils.storage.direct.biblio.record_entry.retrieve' => $rid )
200 __PACKAGE__->register_method(
201 method => 'retrieve_record_marcxml',
202 api_name => 'open-ils.supercat.record.marcxml.retrieve',
207 Returns the MARCXML representation of the requested bibliographic record
212 desc => 'An OpenILS biblio::record_entry id',
216 { desc => 'The bib record in MARCXML',
221 sub retrieve_record_transform {
226 (my $transform = $self->api_name) =~ s/^.+record\.([^\.]+)\.retrieve$/$1/o;
228 my $_storage = OpenSRF::AppSession->create( 'open-ils.storage' );
230 warn "Fetching record entry $rid\n";
231 my $marc = $_storage->request(
232 'open-ils.storage.direct.biblio.record_entry.retrieve',
235 warn "Fetched record entry $rid\n";
237 return entityize($record_xslt{$transform}{xslt}->transform( $_parser->parse_string( $marc ) )->toString);
241 sub retrieve_metarecord_mods {
246 my $_storage = OpenSRF::AppSession->create( 'open-ils.storage' );
251 # Get the metarecord in question
254 'open-ils.storage.direct.metabib.metarecord.retrieve' => $rid
257 # Now get the map of all bib records for the metarecord
260 'open-ils.storage.direct.metabib.metarecord_source_map.search.metarecord.atomic',
264 $logger->debug("Adding ".scalar(@$recs)." bib record to the MODS of the metarecord");
266 # and retrieve the lead (master) record as MODS
268 $self ->method_lookup('open-ils.supercat.record.mods.retrieve')
269 ->run($mr->master_record);
270 my $master_mods = $_parser->parse_string($master)->documentElement;
271 $master_mods->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
273 # ... and a MODS clone to populate, with guts removed.
274 my $mods = $_parser->parse_string($master)->documentElement;
275 $mods->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
276 ($mods) = $mods->findnodes('//mods:mods');
277 $mods->removeChildNodes;
279 # Add the metarecord ID as a (locally defined) info URI
280 my $recordInfo = $mods
282 ->createElement("mods:recordInfo");
284 my $recordIdentifier = $mods
286 ->createElement("mods:recordIdentifier");
288 my ($year,$month,$day) = reverse( (localtime)[3,4,5] );
293 $recordIdentifier->appendTextNode(
294 sprintf("tag:open-ils.org,$year-\%0.2d-\%0.2d:metabib-metarecord/$id", $month, $day)
297 $recordInfo->appendChild($recordIdentifier);
298 $mods->appendChild($recordInfo);
300 # Grab the title, author and ISBN for the master record and populate the metarecord
301 my ($title) = $master_mods->findnodes( './mods:titleInfo[not(@type)]' );
304 $title->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
305 $title = $mods->ownerDocument->importNode($title);
306 $mods->appendChild($title);
309 my ($author) = $master_mods->findnodes( './mods:name[mods:role/mods:text[text()="creator"]]' );
311 $author->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
312 $author = $mods->ownerDocument->importNode($author);
313 $mods->appendChild($author);
316 my ($isbn) = $master_mods->findnodes( './mods:identifier[@type="isbn"]' );
318 $isbn->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
319 $isbn = $mods->ownerDocument->importNode($isbn);
320 $mods->appendChild($isbn);
323 # ... and loop over the constituent records
324 for my $map ( @$recs ) {
328 $self ->method_lookup('open-ils.supercat.record.mods.retrieve')
331 my $part_mods = $_parser->parse_string($rec);
332 $part_mods->documentElement->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
333 ($part_mods) = $part_mods->findnodes('//mods:mods');
335 for my $node ( ($part_mods->findnodes( './mods:subject' )) ) {
336 $node->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
337 $node = $mods->ownerDocument->importNode($node);
338 $mods->appendChild( $node );
341 my $relatedItem = $mods
343 ->createElement("mods:relatedItem");
345 $relatedItem->setAttribute( type => 'constituent' );
347 my $identifier = $mods
349 ->createElement("mods:identifier");
351 $identifier->setAttribute( type => 'uri' );
353 my $subRecordInfo = $mods
355 ->createElement("mods:recordInfo");
357 my $subRecordIdentifier = $mods
359 ->createElement("mods:recordIdentifier");
361 my $subid = $map->source;
362 $subRecordIdentifier->appendTextNode(
363 sprintf("tag:open-ils.org,$year-\%0.2d-\%0.2d:biblio-record_entry/$subid",
368 $subRecordInfo->appendChild($subRecordIdentifier);
370 $relatedItem->appendChild( $subRecordInfo );
372 my ($tor) = $part_mods->findnodes( './mods:typeOfResource' );
373 $tor->setNamespace( "http://www.loc.gov/mods/", "mods", 1 ) if ($tor);
374 $tor = $mods->ownerDocument->importNode($tor) if ($tor);
375 $relatedItem->appendChild($tor) if ($tor);
377 if ( my ($part_isbn) = $part_mods->findnodes( './mods:identifier[@type="isbn"]' ) ) {
378 $part_isbn->setNamespace( "http://www.loc.gov/mods/", "mods", 1 );
379 $part_isbn = $mods->ownerDocument->importNode($part_isbn);
380 $relatedItem->appendChild( $part_isbn );
383 $isbn = $mods->appendChild( $part_isbn->cloneNode(1) );
387 $mods->appendChild( $relatedItem );
391 $_storage->disconnect;
393 return entityize($mods->toString);
396 __PACKAGE__->register_method(
397 method => 'retrieve_metarecord_mods',
398 api_name => 'open-ils.supercat.metarecord.mods.retrieve',
403 Returns the MODS representation of the requested metarecord
407 { name => 'metarecordId',
408 desc => 'An OpenILS metabib::metarecord id',
412 { desc => 'The metarecord in MODS',
417 sub list_metarecord_formats {
420 { namespace_uri => 'http://www.loc.gov/mods/',
421 docs => 'http://www.loc.gov/mods/',
422 schema_location => 'http://www.loc.gov/standards/mods/mods.xsd',
427 for my $type ( keys %metarecord_xslt ) {
430 { namespace_uri => $metarecord_xslt{$type}{namespace_uri},
431 docs => $metarecord_xslt{$type}{docs},
432 schema_location => $metarecord_xslt{$type}{schema_location},
439 __PACKAGE__->register_method(
440 method => 'list_metarecord_formats',
441 api_name => 'open-ils.supercat.metarecord.formats',
446 Returns the list of valid metarecord formats that supercat understands.
449 { desc => 'The format list',
455 sub list_record_formats {
458 { namespace_uri => 'http://www.loc.gov/MARC21/slim',
459 docs => 'http://www.loc.gov/marcxml/',
460 schema_location => 'http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd',
465 for my $type ( keys %record_xslt ) {
468 { namespace_uri => $record_xslt{$type}{namespace_uri},
469 docs => $record_xslt{$type}{docs},
470 schema_location => $record_xslt{$type}{schema_location},
477 __PACKAGE__->register_method(
478 method => 'list_record_formats',
479 api_name => 'open-ils.supercat.record.formats',
484 Returns the list of valid record formats that supercat understands.
487 { desc => 'The format list',
498 throw OpenSRF::EX::InvalidArg ('I need an ISBN please')
499 unless (length($isbn) >= 10);
501 my $_storage = OpenSRF::AppSession->create( 'open-ils.storage' );
503 # Create a storage session, since we'll be making muliple requests.
506 # Find the record that has that ISBN.
507 my $bibrec = $_storage->request(
508 'open-ils.storage.direct.metabib.full_rec.search_where.atomic',
509 { tag => '020', subfield => 'a', value => { ilike => $isbn.'%'} }
512 # Go away if we don't have one.
513 return {} unless (@$bibrec);
515 # Find the metarecord for that bib record.
516 my $mr = $_storage->request(
517 'open-ils.storage.direct.metabib.metarecord_source_map.search.source.atomic',
521 # Find the other records for that metarecord.
522 my $records = $_storage->request(
523 'open-ils.storage.direct.metabib.metarecord_source_map.search.metarecord.atomic',
527 # Just to be safe. There's currently no unique constraint on sources...
528 my %unique_recs = map { ($_->source, 1) } @$records;
529 my @rec_list = sort keys %unique_recs;
531 # And now fetch the ISBNs for thos records.
532 my $recs = $_storage->request(
533 'open-ils.storage.direct.metabib.full_rec.search_where.atomic',
534 { tag => '020', subfield => 'a', record => \@rec_list }
537 # We're done with the storage server session.
538 $_storage->disconnect;
540 # Return the oISBN data structure. This will be XMLized at a higher layer.
542 { metarecord => $mr->[0]->metarecord,
543 record_list => { map { ($_->record, $_->value) } @$recs } };
546 __PACKAGE__->register_method(
548 api_name => 'open-ils.supercat.oisbn',
553 Returns the ISBN list for the metarecord of the requested isbn
558 desc => 'An ISBN. Duh.',
562 { desc => 'record to isbn map',