]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Cat/AuthCommon.pm
1c55b8642bd3f40a524a7f9fd118cb701d8d356d
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Cat / AuthCommon.pm
1 package OpenILS::Application::Cat::AuthCommon;
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/;
8 use OpenSRF::AppSession;
9 use OpenILS::Event;
10 my $U = 'OpenILS::Application::AppUtils';
11 my $MARC_NAMESPACE = 'http://www.loc.gov/MARC21/slim';
12
13
14 # ---------------------------------------------------------------------------
15 # Shared authority mangling code.  Do not publish methods from here.
16 # ---------------------------------------------------------------------------
17
18 # generate a MARC XML document from a MARC XML string
19 sub marc_xml_to_doc {
20         my $xml = shift;
21         my $marc_doc = XML::LibXML->new->parse_string($xml);
22         $marc_doc->documentElement->setNamespace($MARC_NAMESPACE, 'marc', 1);
23         $marc_doc->documentElement->setNamespace($MARC_NAMESPACE);
24         return $marc_doc;
25 }
26
27
28 sub import_authority_record {
29     my($class, $e, $marc_xml, $source) = @_;
30     
31     my $marc_doc = marc_xml_to_doc($marc_xml);
32     my $rec = Fieldmapper::authority::record_entry->new;
33         $rec->creator($e->requestor->id);
34         $rec->editor($e->requestor->id);
35         $rec->create_date('now');
36         $rec->edit_date('now');
37         $rec->marc($U->entityize($marc_doc->documentElement->toString));
38
39     my ($arn, $evt) = find_arn($e, $marc_doc);
40     return $evt if $evt;
41     $rec->arn_value($arn);
42
43     $rec = $e->create_authority_record_entry($rec) or return $e->die_event;
44
45     # we don't care about the result, just fire off the request
46     #my $ses = OpenSRF::AppSession->create('open-ils.ingest');
47     #$ses->request('open-ils.ingest.full.authority.record', $recid);
48
49         return $rec;
50 }
51
52
53 sub overlay_authority_record {
54     my($class, $e, $rec_id, $marc_xml, $source) = @_;
55     
56     my $marc_doc = marc_xml_to_doc($marc_xml);
57     my $rec = $e->retrieve_authority_record_entry($rec_id) or return $e->die_event;
58         $rec->editor($e->requestor->id);
59         $rec->edit_date('now');
60         $rec->marc($U->entityize($marc_doc->documentElement->toString));
61
62     $rec = $e->update_authority_record_entry($rec) or return $e->die_event;
63
64     # we don't care about the result, just fire off the request
65     #my $ses = OpenSRF::AppSession->create('open-ils.ingest');
66     #$ses->request('open-ils.ingest.full.authority.record', $recid);
67
68         return $rec;
69 }
70
71 sub find_arn {
72     my($e, $marc_doc) = @_;
73
74         my $xpath = '//marc:controlfield[@tag="001"]';
75         my ($arn) = $marc_doc->documentElement->findvalue($xpath);
76
77     if(my $existing_rec = $e->search_authority_record_entry({arn_value => $arn, deleted => 'f'})->[0]) {
78         # this arn is taken
79         return (
80             undef, 
81             OpenILS::Event->new(
82                 'AUTHORITY_RECORD_NUMBER_EXISTS', 
83                 payload => {existing_record => $existing_rec, arn => $arn}
84             )
85         );
86     }
87
88     return ($arn);
89 }
90
91 1;