1 CREATE OR REPLACE FUNCTION biblio.extract_located_uris( bib_id BIGINT, marcxml TEXT, editor_id INT ) RETURNS VOID AS $func$
16 -- Clear any URI mappings and call numbers for this bib.
17 -- This leads to acn / auricnm inflation, but also enables
18 -- old acn/auricnm's to go away and for bibs to be deleted.
19 FOR uri_cn_id IN SELECT id FROM asset.call_number WHERE record = bib_id AND label = '##URI##' AND NOT deleted LOOP
20 DELETE FROM asset.uri_call_number_map WHERE call_number = uri_cn_id;
21 DELETE FROM asset.call_number WHERE id = uri_cn_id;
24 uris := oils_xpath('//*[@tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]',marcxml);
25 IF ARRAY_UPPER(uris,1) > 0 THEN
26 FOR i IN 1 .. ARRAY_UPPER(uris, 1) LOOP
27 -- First we pull info out of the 856
30 uri_href := (oils_xpath('//*[@code="u"]/text()',uri_xml))[1];
31 uri_label := (oils_xpath('//*[@code="y"]/text()|//*[@code="3"]/text()',uri_xml))[1];
32 uri_use := (oils_xpath('//*[@code="z"]/text()|//*[@code="2"]/text()|//*[@code="n"]/text()',uri_xml))[1];
34 IF uri_label IS NULL THEN
35 uri_label := uri_href;
37 CONTINUE WHEN uri_href IS NULL;
39 -- Get the distinct list of libraries wanting to use
41 DISTINCT REGEXP_REPLACE(
43 $re$^.*?\((\w+)\).*$$re$,
49 '//*[@code="9"]/text()|//*[@code="w"]/text()|//*[@code="n"]/text()',
54 IF ARRAY_UPPER(uri_owner_list,1) > 0 THEN
56 -- look for a matching uri
57 IF uri_use IS NULL THEN
60 WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active
62 IF NOT FOUND THEN -- create one
63 INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
66 WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active;
71 WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active
73 IF NOT FOUND THEN -- create one
74 INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
77 WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
81 FOR j IN 1 .. ARRAY_UPPER(uri_owner_list, 1) LOOP
82 uri_owner := uri_owner_list[j];
84 SELECT id INTO uri_owner_id FROM actor.org_unit WHERE shortname = BTRIM(REPLACE(uri_owner,chr(160),''));
85 CONTINUE WHEN NOT FOUND;
87 -- we need a call number to link through
88 SELECT id INTO uri_cn_id FROM asset.call_number WHERE owning_lib = uri_owner_id AND record = bib_id AND label = '##URI##' AND NOT deleted;
90 INSERT INTO asset.call_number (owning_lib, record, create_date, edit_date, creator, editor, label)
91 VALUES (uri_owner_id, bib_id, 'now', 'now', editor_id, editor_id, '##URI##');
92 SELECT id INTO uri_cn_id FROM asset.call_number WHERE owning_lib = uri_owner_id AND record = bib_id AND label = '##URI##' AND NOT deleted;
95 -- now, link them if they're not already
96 SELECT id INTO uri_map_id FROM asset.uri_call_number_map WHERE call_number = uri_cn_id AND uri = uri_id;
98 INSERT INTO asset.uri_call_number_map (call_number, uri) VALUES (uri_cn_id, uri_id);
110 $func$ LANGUAGE PLPGSQL;