]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Cat/Authority.pm
Replace erroneous calls to $e->retrieve_authority_record($rec_id).
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Cat / Authority.pm
1 package OpenILS::Application::Cat::Authority;
2 use strict; use warnings;
3 use base qw/OpenILS::Application/;
4 use OpenILS::Utils::CStoreEditor q/:funcs/;
5 use OpenILS::Application::Cat::AuthCommon;
6 use OpenSRF::Utils::Logger qw($logger);
7 use OpenILS::Application::AppUtils;
8 use OpenILS::Utils::Fieldmapper;
9 use OpenILS::Const qw/:const/;
10 use OpenILS::Event;
11 my $U = 'OpenILS::Application::AppUtils';
12 my $MARC_NAMESPACE = 'http://www.loc.gov/MARC21/slim';
13
14
15 # generate a MARC XML document from a MARC XML string
16 sub marc_xml_to_doc {
17     my $xml = shift;
18     my $marc_doc = XML::LibXML->new->parse_string($xml);
19     $marc_doc->documentElement->setNamespace($MARC_NAMESPACE, 'marc', 1);
20     $marc_doc->documentElement->setNamespace($MARC_NAMESPACE);
21     return $marc_doc;
22 }
23
24
25 __PACKAGE__->register_method(
26     method  => 'import_authority_record',
27     api_name    => 'open-ils.cat.authority.record.import',
28 );
29
30 sub import_authority_record {
31     my($self, $conn, $auth, $marc_xml, $source) = @_;
32     my $e = new_editor(authtoken=>$auth, xact=>1);
33     return $e->die_event unless $e->checkauth;
34     return $e->die_event unless $e->allowed('CREATE_AUTHORITY_RECORD');
35     my $rec = OpenILS::Application::Cat::AuthCommon->import_authority_record($marc_xml, $source);
36     $e->commit unless $U->event_code($rec);
37     return $rec;
38 }
39
40 __PACKAGE__->register_method(
41     method => 'create_authority_record_from_bib_field',
42     api_name => 'open-ils.cat.authority.record.create_from_bib',
43     signature => {
44         desc => q/Create an authority record entry from a field in a bibliographic record/,
45         params => q/
46             @param field A hash representing the field to control, consisting of: { tag: string, ind1: string, ind2: string, subfields: [ [code, value] ... ] }
47             @param identifier A MARC control number identifier
48             @param authtoken A valid authentication token
49             @returns The new record object 
50  /}
51 );
52
53 __PACKAGE__->register_method(
54     method => 'create_authority_record_from_bib_field',
55     api_name => 'open-ils.cat.authority.record.create_from_bib.readonly',
56     signature => {
57         desc => q/Creates MARCXML for an authority record entry from a field in a bibliographic record/,
58         params => q/
59             @param field A hash representing the field to control, consisting of: { tag: string, ind1: string, ind2: string, subfields: [ [code, value] ... ] }
60             @param identifier A MARC control number identifier
61             @returns The MARCXML for the authority record
62  /}
63 );
64
65 sub create_authority_record_from_bib_field {
66     my($self, $conn, $field, $cni, $auth) = @_;
67
68     # Control number identifier should have been passed in
69     if (!$cni) {
70         $cni = 'UNSET';
71     }
72
73     # Change the first character of the incoming bib field tag to a '1'
74     # for use in our authority record; close enough for now?
75     my $tag = $field->{'tag'};
76     $tag =~ s/^./1/;
77
78     my $ind1 = $field->{ind1} || ' ';
79     my $ind2 = $field->{ind2} || ' ';
80
81     my $control = qq{<datafield tag="$tag" ind1="$ind1" ind2="$ind2">};
82     foreach my $sf (@{$field->{subfields}}) {
83         my $code = $sf->[0];
84         my $val = $U->entityize($sf->[1]);
85         $control .= qq{<subfield code="$code">$val</subfield>};
86     }
87     $control .= '</datafield>';
88
89     # ARN, or "authority record number", used to need to be unique across the database.
90     # Of course, we have no idea what's in the database, and if the
91     # cat.maintain_control_numbers flag is set to "TRUE" then the 001 will
92     # be reset to the record ID anyway.
93     my $arn = 'AUTOGEN-' . time();
94
95     # Placeholder MARCXML; 
96     #   001/003 can be be properly filled in via database triggers
97     #   005 will be filled in automatically at creation time
98     #   008 needs to be set by a cataloguer (could be some OU settings, I suppose)
99     #   040 should come from OU settings / OU shortname
100     #   
101     my $marc_xml = <<MARCXML;
102 <record xmlns:marc="http://www.loc.gov/MARC21/slim" xmlns="http://www.loc.gov/MARC21/slim"><leader>     nz  a22     o  4500</leader>
103 <controlfield tag="001">$arn</controlfield>
104 <controlfield tag="008">      ||||||||||||||||||||||||||||||||||</controlfield>
105 <datafield tag="040" ind1=" " ind2=" "><subfield code="a">$cni</subfield><subfield code="c">$cni</subfield></datafield>
106 $control
107 </record>
108 MARCXML
109
110     if ($self->api_name =~ m/readonly$/) {
111         return $marc_xml;
112     } else {
113         my $e = new_editor(authtoken=>$auth, xact=>1);
114         return $e->die_event unless $e->checkauth;
115         return $e->die_event unless $e->allowed('CREATE_AUTHORITY_RECORD');
116         my $rec = OpenILS::Application::Cat::AuthCommon->import_authority_record($e, $marc_xml);
117         $e->commit unless $U->event_code($rec);
118         return $rec;
119     }
120 }
121
122 __PACKAGE__->register_method(
123     method  => 'overlay_authority_record',
124     api_name    => 'open-ils.cat.authority.record.overlay',
125 );
126
127 sub overlay_authority_record {
128     my($self, $conn, $auth, $rec_id, $marc_xml, $source) = @_;
129     my $e = new_editor(authtoken=>$auth, xact=>1);
130     return $e->die_event unless $e->checkauth;
131     return $e->die_event unless $e->allowed('UPDATE_AUTHORITY_RECORD');
132     my $rec = OpenILS::Application::Cat::AuthCommon->overlay_authority_record($rec_id, $marc_xml, $source);
133     $e->commit unless $U->event_code($rec);
134     return $rec;
135
136 }
137
138 __PACKAGE__->register_method(
139     method  => 'retrieve_authority_record',
140     api_name    => 'open-ils.cat.authority.record.retrieve',
141     signature => {
142         desc => q/Retrieve an authority record entry/,
143         params => [
144             {desc => q/hash of options.  Options include "clear_marc" which clears
145                 the MARC xml from the record before it is returned/}
146         ]
147     }
148 );
149 sub retrieve_authority_record {
150     my($self, $conn, $auth, $rec_id, $options) = @_;
151     my $e = new_editor(authtoken=>$auth);
152     return $e->die_event unless $e->checkauth;
153     my $rec = $e->retrieve_authority_record_entry($rec_id) or return $e->event;
154     $rec->clear_marc if $$options{clear_marc};
155     return $rec;
156 }
157
158 __PACKAGE__->register_method(
159     method  => 'batch_retrieve_authority_record',
160     api_name    => 'open-ils.cat.authority.record.batch.retrieve',
161     stream => 1,
162     signature => {
163         desc => q/Retrieve a set of authority record entry objects/,
164         params => [
165             {desc => q/hash of options.  Options include "clear_marc" which clears
166                 the MARC xml from the record before it is returned/}
167         ]
168     }
169 );
170 sub batch_retrieve_authority_record {
171     my($self, $conn, $auth, $rec_id_list, $options) = @_;
172     my $e = new_editor(authtoken=>$auth);
173     return $e->die_event unless $e->checkauth;
174     for my $rec_id (@$rec_id_list) {
175         my $rec = $e->retrieve_authority_record_entry($rec_id) or return $e->event;
176         $rec->clear_marc if $$options{clear_marc};
177         $conn->respond($rec);
178     }
179     return undef;
180 }
181
182 __PACKAGE__->register_method(
183     method    => 'count_linked_bibs',
184     api_name  => 'open-ils.cat.authority.records.count_linked_bibs',
185     signature => q/
186         Counts the number of bib records linked to each authority record in the input list
187         @param records Array of authority records to return counts
188         @return A list of hashes containing the authority record ID ("id") and linked bib count ("bibs")
189     /
190 );
191
192 sub count_linked_bibs {
193     my( $self, $conn, $records ) = @_;
194
195     my $editor = new_editor();
196
197     my $link_count = [];
198     my @clean_records;
199     for my $auth ( @$records ) {
200         # Protection against SQL injection? Might be overkill.
201         my $intauth = int($auth);
202         if ($intauth) {
203             push(@clean_records, $intauth);
204         }
205     }
206     return $link_count if !@clean_records;
207     
208     $link_count = $editor->json_query({
209         "select" => {
210             "abl" => [
211                 {
212                     "column" => "authority"
213                 },
214                 {
215                     "alias" => "bibs",
216                     "transform" => "count",
217                     "column" => "bib",
218                     "aggregate" => 1
219                 }
220             ]
221         },
222         "from" => "abl",
223         "where" => { "authority" => \@clean_records }
224     });
225
226     return $link_count;
227 }
228
229 __PACKAGE__->register_method(
230     "method" => "retrieve_acs",
231     "api_name" => "open-ils.cat.authority.control_set.retrieve",
232     "api_level" => 1,
233     "stream" => 1,
234     "argc" => 2,
235     "signature" => {
236         "desc" => q/Retrieve authority.control_set objects with fleshed
237         thesauri and authority fields/,
238         "params" => [
239             {"name" => "limit",  "desc" => "limit (optional; default 15)", "type" => "number"},
240             {"name" => "offset",  "desc" => "offset doptional; default 0)", "type" => "number"},
241             {"name" => "focus",  "desc" => "optionally make sure the acs object with ID matching this value comes at the top of the result set (only works with offset 0)", "type" => "number"}
242         ]
243     }
244 );
245
246 # XXX I don't think this really needs to be protected by perms, or does it?
247 sub retrieve_acs {
248     my $self = shift;
249     my $client = shift;
250
251     my ($limit, $offset, $focus) = map int, @_;
252
253     $limit ||= 15;
254     $offset ||= 0;
255     $focus ||= undef;
256
257     my $e = new_editor;
258     my $order_by = [
259         {"class" => "acs", "field" => "name"}
260     ];
261
262     # Here is the magic that let's us say that a given acsaf
263     # will be our first result.
264     unshift @$order_by, {
265         "class" => "acs", "field" => "id",
266         "transform" => "numeric_eq", "params" => [$focus],
267         "direction" => "desc"
268     } if $focus;
269
270     my $sets = $e->search_authority_control_set([
271         {"id" => {"!=" => undef}}, {
272             "flesh" => 1,
273             "flesh_fields" => {"acs" => [qw/thesauri authority_fields/]},
274             "order_by" => $order_by,
275             "limit" => $limit,
276             "offset" => $offset
277         }
278     ]) or return $e->die_event;
279
280     $e->disconnect;
281
282     $client->respond($_) foreach @$sets;
283     return undef;
284 }
285
286 __PACKAGE__->register_method(
287     "method" => "retrieve_acsaf",
288     "api_name" => "open-ils.cat.authority.control_set_authority_field.retrieve",
289     "api_level" => 1,
290     "stream" => 1,
291     "argc" => 2,
292     "signature" => {
293         "desc" => q/Retrieve authority.control_set_authority_field objects with
294         fleshed bib_fields and axes/,
295         "params" => [
296             {"name" => "limit",  "desc" => "limit (optional; default 15)", "type" => "number"},
297             {"name" => "offset",  "desc" => "offset (optional; default 0)", "type" => "number"},
298             {"name" => "control_set",  "desc" => "optionally constrain by value of acsaf.control_set field", "type" => "number"},
299             {"name" => "focus", "desc" => "optionally make sure the acsaf object with ID matching this value comes at the top of the result set (only works with offset 0)"}
300         ]
301     }
302 );
303
304 sub retrieve_acsaf {
305     my $self = shift;
306     my $client = shift;
307
308     my ($limit, $offset, $control_set, $focus) = map int, @_;
309
310     $limit ||= 15;
311     $offset ||= 0;
312     $control_set ||= undef;
313     $focus ||= undef;
314
315     my $e = new_editor;
316     my $where = {
317         "control_set" => ($control_set ? $control_set : {"!=" => undef})
318     };
319     my $order_by = [
320         {"class" => "acsaf", "field" => "main_entry", "direction" => "desc"},
321         {"class" => "acsaf", "field" => "id"}
322     ];
323
324     unshift @$order_by, {
325         "class" => "acsaf", "field" => "id",
326         "transform" => "numeric_eq", "params" => [$focus],
327         "direction" => "desc"
328     } if $focus;
329
330     my $fields = $e->search_authority_control_set_authority_field([
331         $where, {
332             "flesh" => 2,
333             "flesh_fields" => {
334                 "acsaf" => ["bib_fields", "axis_maps"],
335                 "abaafm" => ["axis"]
336             },
337             "order_by" => $order_by,
338             "limit" => $limit,
339             "offset" => $offset
340         }
341     ]) or return $e->die_event;
342
343     $e->disconnect;
344
345     $client->respond($_) foreach @$fields;
346     return undef;
347 }
348
349 1;