final updates
[sitka/sitka-tools.git] / marc_import_overdrive / overdrive-ingest-db-func.sql
CommitLineData
4856f9b1 1CREATE OR REPLACE FUNCTION sitka.overdrive_bc_delete_all_uri_fields( TEXT ) RETURNS TEXT AS $func$
38891e1e
JF
2 use MARC::Record;
3 use MARC::File::XML (BinaryEncoding => 'UTF-8');
4
5 my $xml = shift;
6
7 my $r = MARC::Record->new_from_xml( $xml );
8
9 # check all 856s
10 URIFIELD: foreach my $uri ($r->field('856')){
11
12 # Overdrive uses a $3 for Excerpts, we want to keep this intact so carry on then
13 next URIFIELD if ($uri->subfield('3'));
14
15 # otherwise, delete away
4856f9b1 16 $r->delete_field($uri);
38891e1e
JF
17 }
18
19 $xml = $r->as_xml_record();
20 $xml =~ s/^<\?.+?\?>$//mo;
21 $xml =~ s/\n//sgo;
22 $xml =~ s/>\s+</></sgo;
23 return $xml;
24$func$ LANGUAGE PLPERLU;
25
4856f9b1 26CREATE OR REPLACE FUNCTION sitka.overdrive_bc_add_uri_field( TEXT, TEXT, TEXT, TEXT, TEXT ) RETURNS TEXT AS $func$
38891e1e
JF
27 use MARC::Record;
28 use MARC::File::XML (BinaryEncoding => 'UTF-8');
29
30 my $xml = shift;
31 my $tcn_prefix = shift;
32 my $url_prefix = shift;
33 my $uri_caption = shift;
34 my $shortname = shift;
35
36 my $r = MARC::Record->new_from_xml( $xml );
37
38 my $tcn = $r->subfield('901','a');
39 $tcn =~ s/^$tcn_prefix//g;
40
41 foreach my $uri ($r->field('856')){
42 if($uri->subfield('9')){
43 if($uri->subfield('9') eq $shortname){
44 $r->delete_field($uri);
45 }
46 }
47 }
48 my $field856 = MARC::Field->new(
49 '856' => ('4', '0'),
50 u => $url_prefix . $tcn,
1cfb727d 51 y => $uri_caption,
38891e1e
JF
52 9 => $shortname
53 );
54
55 $r->insert_fields_ordered($field856);
56
57 $xml = $r->as_xml_record();
58 $xml =~ s/^<\?.+?\?>$//mo;
59 $xml =~ s/\n//sgo;
60 $xml =~ s/>\s+</></sgo;
61 return $xml;
62
63$func$ LANGUAGE PLPERLU;
64
65-- #u => "http://downloads.bclibrary.ca/ContentDetails.htm?ID=" . $tcn,
66-- "Click to access online (library card required)",
67
68
69CREATE OR REPLACE FUNCTION sitka.overdrive_bc_refresh_uris ( rid BIGINT ) RETURNS BOOLEAN AS $$
70DECLARE
71 marcxml TEXT;
72 tcnprefix TEXT := 'Overdrive_';
73 urlprefix TEXT := 'http://downloads.bclibrary.ca/ContentDetails.htm?ID=';
74 urlcaption TEXT := 'Click to access online (library card required)';
75BEGIN
4856f9b1 76 SELECT sitka.overdrive_bc_delete_all_uri_fields(marc) FROM biblio.record_entry where id = rid INTO marcxml;
38891e1e
JF
77
78 -- clean out asset.call_number and asset.uri and asset.uri_call_number_map
79
80 -- could be modified to pull shortnames based on an ou setting
81 -- select sitka.overdrive_MARCXML_add_uri_field(rid,tcnprefix,urlprefix,urlcaption,a.shortname)
82 -- from actor.org_unit_setting b join actor.org_unit a on (a.id = b.org_unit) where b.name = 'sitka.overdrive_setting' and b.value = true
83 -- something like that
84
4856f9b1
JF
85 SELECT sitka.overdrive_bc_add_uri_field(marcxml::TEXT, tcnprefix::TEXT, urlprefix::TEXT, urlcaption::TEXT, 'BBGVL'::TEXT) INTO marcxml;
86 SELECT sitka.overdrive_bc_add_uri_field(marcxml::TEXT, tcnprefix::TEXT, urlprefix::TEXT, urlcaption::TEXT, 'BBNCLF'::TEXT) INTO marcxml;
87 SELECT sitka.overdrive_bc_add_uri_field(marcxml::TEXT, tcnprefix::TEXT, urlprefix::TEXT, urlcaption::TEXT, 'BCK'::TEXT) INTO marcxml;
88 SELECT sitka.overdrive_bc_add_uri_field(marcxml::TEXT, tcnprefix::TEXT, urlprefix::TEXT, urlcaption::TEXT, 'BNCLF'::TEXT) INTO marcxml;
89 SELECT sitka.overdrive_bc_add_uri_field(marcxml::TEXT, tcnprefix::TEXT, urlprefix::TEXT, urlcaption::TEXT, 'BNELF'::TEXT) INTO marcxml;
90 SELECT sitka.overdrive_bc_add_uri_field(marcxml::TEXT, tcnprefix::TEXT, urlprefix::TEXT, urlcaption::TEXT, 'ISLANDLINK'::TEXT) INTO marcxml;
91 SELECT sitka.overdrive_bc_add_uri_field(marcxml::TEXT, tcnprefix::TEXT, urlprefix::TEXT, urlcaption::TEXT, 'UNFEDERATED'::TEXT) INTO marcxml;
38891e1e 92
4856f9b1 93 UPDATE biblio.record_entry set marc = marcxml WHERE id = rid;
38891e1e
JF
94 return true;
95END;
4856f9b1 96$$ LANGUAGE PLPGSQL;