1 package OpenILS::Application::Cat::BibCommon;
2 use strict; use warnings;
3 use OpenILS::Utils::CStoreEditor q/:funcs/;
4 use OpenSRF::Utils::Logger qw($logger);
5 use OpenILS::Application::AppUtils;
6 use OpenILS::Utils::Fieldmapper;
7 use OpenILS::Const qw/:const/;
9 my $U = 'OpenILS::Application::AppUtils';
10 my $MARC_NAMESPACE = 'http://www.loc.gov/MARC21/slim';
13 # ---------------------------------------------------------------------------
14 # Shared bib mangling code. Do not publish methods from here.
15 # ---------------------------------------------------------------------------
18 sub biblio_record_replace_marc {
19 my($class, $e, $recid, $newxml, $source, $fixtcn, $override) = @_;
21 my $rec = $e->retrieve_biblio_record_entry($recid)
22 or return $e->die_event;
24 # See if there is a different record in the database that has our TCN value
25 # If we're not updating the TCN, all we care about it the marcdoc
26 # XXX should .update even bother with the tcn_info if it's not going to replace it?
27 # there is the potential for returning a TCN_EXISTS event, even though no replacement happens
29 my( $tcn, $tsource, $marcdoc, $evt);
31 if($fixtcn or $override) {
33 ($tcn, $tsource, $marcdoc, $evt) =
34 _find_tcn_info($e, $newxml, $override, $recid);
38 $rec->tcn_value($tcn) if ($tcn);
39 $rec->tcn_source($tsource);
43 $marcdoc = __make_marc_doc($newxml);
47 $rec->source(bib_source_from_name($source)) if $source;
48 $rec->editor($e->requestor->id);
49 $rec->edit_date('now');
50 $rec->marc( $U->entityize( $marcdoc->documentElement->toString ) );
51 $e->update_biblio_record_entry($rec) or return $e->die_event;
53 # we don't care about the result, just fire off the request
54 my $ses = OpenSRF::AppSession->create('open-ils.ingest');
55 $ses->request('open-ils.ingest.full.biblio.record', $recid);
60 sub biblio_record_xml_import {
61 my($class, $e, $xml, $source, $auto_tcn, $override) = @_;
63 my( $evt, $tcn, $tcn_source, $marcdoc );
66 # auto_tcn forces a blank TCN value so the DB will have to generate one for us
67 $marcdoc = __make_marc_doc($xml);
69 ( $tcn, $tcn_source, $marcdoc, $evt ) = _find_tcn_info($e, $xml, $override);
73 $logger->info("user ".$e->requestor->id.
74 " creating new biblio entry with tcn=$tcn and tcn_source $tcn_source");
76 my $record = Fieldmapper::biblio::record_entry->new;
78 $record->source(bib_source_from_name($source)) if $source;
79 $record->tcn_source($tcn_source);
80 $record->tcn_value($tcn) if ($tcn);
81 $record->creator($e->requestor->id);
82 $record->editor($e->requestor->id);
83 $record->create_date('now');
84 $record->edit_date('now');
85 $record->marc($U->entityize($marcdoc->documentElement->toString));
87 $record = $e->create_biblio_record_entry($record) or return $e->die_event;
88 $logger->info("marc create/import created new record ".$record->id);
90 # we don't care about the result, just fire off the request
91 my $ses = OpenSRF::AppSession->create('open-ils.ingest');
92 $ses->request('open-ils.ingest.full.biblio.record', $record->id);
99 my $marcxml = XML::LibXML->new->parse_string($xml);
100 $marcxml->documentElement->setNamespace($MARC_NAMESPACE, "marc", 1 );
101 $marcxml->documentElement->setNamespace($MARC_NAMESPACE);
109 my $override = shift;
110 my $existing_rec = shift || 0;
113 my $marcxml = __make_marc_doc($xml);
115 my $xpath = '//marc:controlfield[@tag="001"]';
116 my $tcn = $marcxml->documentElement->findvalue($xpath);
117 $logger->info("biblio import located 001 (tcn) value of $tcn");
119 $xpath = '//marc:controlfield[@tag="003"]';
120 my $tcn_source = $marcxml->documentElement->findvalue($xpath) || "System Local";
122 if(my $rec = _tcn_exists($editor, $tcn, $tcn_source, $existing_rec) ) {
125 $tcn = find_free_tcn( $marcxml, $editor, $existing_rec );
127 # if we're overriding, try to find a different TCN to use
130 # XXX Create ALLOW_ALT_TCN permission check support
132 $logger->info("tcn value $tcn already exists, attempting to override");
140 'OPEN_TCN_NOT_FOUND',
141 payload => $marcxml->toString())
147 $logger->warn("tcn value $origtcn already exists in import/create");
149 # otherwise, return event
155 'TCN_EXISTS', payload => {
165 return ($tcn, $tcn_source, $marcxml);
172 my $existing_rec = shift;
176 my $xpath = '//marc:datafield[@tag="039"]/subfield[@code="a"]';
177 my ($tcn) = $marcxml->documentElement->findvalue($xpath) =~ /(\w+)\s*$/o;
178 $xpath = '//marc:datafield[@tag="039"]/subfield[@code="b"]';
179 my $tcn_source = $marcxml->documentElement->findvalue($xpath) || "System Local";
181 if(_tcn_exists($editor, $tcn, $tcn_source, $existing_rec)) {
189 $xpath = '//marc:datafield[@tag="020"]/subfield[@code="a"]';
190 ($tcn) = $marcxml->documentElement->findvalue($xpath) =~ /(\w+)\s*$/o;
191 $tcn_source = "ISBN";
192 if(_tcn_exists($editor, $tcn, $tcn_source, $existing_rec)) {$tcn = undef;}
196 $xpath = '//marc:datafield[@tag="022"]/subfield[@code="a"]';
197 ($tcn) = $marcxml->documentElement->findvalue($xpath) =~ /(\w+)\s*$/o;
198 $tcn_source = "ISSN";
199 if(_tcn_exists($editor, $tcn, $tcn_source, $existing_rec)) {$tcn = undef;}
203 $xpath = '//marc:datafield[@tag="010"]';
204 ($tcn) = $marcxml->documentElement->findvalue($xpath) =~ /(\w+)\s*$/o;
205 $tcn_source = "LCCN";
206 if(_tcn_exists($editor, $tcn, $tcn_source, $existing_rec)) {$tcn = undef;}
210 $xpath = '//marc:datafield[@tag="035"]/subfield[@code="a"]';
211 ($tcn) = $marcxml->documentElement->findvalue($xpath) =~ /(\w+)\s*$/o;
212 $tcn_source = "System Legacy";
213 if(_tcn_exists($editor, $tcn, $tcn_source, $existing_rec)) {$tcn = undef;}
216 $marcxml->documentElement->removeChild(
217 $marcxml->documentElement->findnodes( '//datafield[@tag="035"]' )
222 return undef unless $tcn;
225 my $df = $marcxml->createElementNS( 'http://www.loc.gov/MARC21/slim', 'datafield');
226 $df->setAttribute( tag => '039' );
227 $df->setAttribute( ind1 => ' ' );
228 $df->setAttribute( ind2 => ' ' );
229 $marcxml->documentElement->appendChild( $df );
231 my $sfa = $marcxml->createElementNS( 'http://www.loc.gov/MARC21/slim', 'subfield');
232 $sfa->setAttribute( code => 'a' );
233 $sfa->appendChild( $marcxml->createTextNode( $tcn ) );
234 $df->appendChild( $sfa );
236 my $sfb = $marcxml->createElementNS( 'http://www.loc.gov/MARC21/slim', 'subfield');
237 $sfb->setAttribute( code => 'b' );
238 $sfb->appendChild( $marcxml->createTextNode( $tcn_source ) );
239 $df->appendChild( $sfb );
251 my $existing_rec = shift || 0;
253 if(!$tcn) {return 0;}
255 $logger->debug("tcn_exists search for tcn $tcn and source $source and id $existing_rec");
257 # XXX why does the source matter?
258 # my $req = $session->request(
259 # { tcn_value => $tcn, tcn_source => $source, deleted => 'f' } );
261 my $recs = $editor->search_biblio_record_entry(
262 {tcn_value => $tcn, deleted => 'f', id => {'!=' => $existing_rec}}, {idlist =>1});
265 $logger->debug("_tcn_exists is true for tcn : $tcn ($source)");
269 $logger->debug("_tcn_exists is false for tcn : $tcn ($source)");