3 CREATE OR REPLACE FUNCTION biblio.extract_located_uris( bib_id BIGINT, marcxml TEXT, editor_id INT ) RETURNS VOID AS $func$
10 uri_owner_list TEXT[];
18 current_map_owner_list INT[];
19 orphaned_uri_list INT[];
22 uris := oils_xpath('//*[@tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]',marcxml);
23 IF ARRAY_UPPER(uris,1) > 0 THEN
24 FOR i IN 1 .. ARRAY_UPPER(uris, 1) LOOP
25 -- First we pull info out of the 856
28 uri_href := (oils_xpath('//*[@code="u"]/text()',uri_xml))[1];
29 uri_label := (oils_xpath('//*[@code="y"]/text()|//*[@code="3"]/text()',uri_xml))[1];
30 uri_use := (oils_xpath('//*[@code="z"]/text()|//*[@code="2"]/text()|//*[@code="n"]/text()',uri_xml))[1];
32 IF uri_label IS NULL THEN
33 uri_label := uri_href;
35 CONTINUE WHEN uri_href IS NULL;
37 -- Get the distinct list of libraries wanting to use
39 DISTINCT REGEXP_REPLACE(
41 $re$^.*?\((\w+)\).*$$re$,
47 '//*[@code="9"]/text()|//*[@code="w"]/text()|//*[@code="n"]/text()',
52 IF ARRAY_UPPER(uri_owner_list,1) > 0 THEN
54 -- look for a matching uri
55 IF uri_use IS NULL THEN
58 WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active
60 IF NOT FOUND THEN -- create one
61 INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
64 WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active;
69 WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active
71 IF NOT FOUND THEN -- create one
72 INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
75 WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
79 FOR j IN 1 .. ARRAY_UPPER(uri_owner_list, 1) LOOP
80 uri_owner := uri_owner_list[j];
82 SELECT id INTO uri_owner_id FROM actor.org_unit WHERE shortname = uri_owner;
83 CONTINUE WHEN NOT FOUND;
85 -- we need a call number to link through
86 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;
88 INSERT INTO asset.call_number (owning_lib, record, create_date, edit_date, creator, editor, label)
89 VALUES (uri_owner_id, bib_id, 'now', 'now', editor_id, editor_id, '##URI##');
90 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;
93 -- now, link them if they're not already
94 SELECT id INTO uri_map_id FROM asset.uri_call_number_map WHERE call_number = uri_cn_id AND uri = uri_id;
96 INSERT INTO asset.uri_call_number_map (call_number, uri) VALUES (uri_cn_id, uri_id);
99 current_map_owner_list := current_map_owner_list || uri_cn_id
108 -- Clear any orphaned URIs, URI mappings and call
109 -- numbers for this bib that weren't mapped above.
112 FROM asset.call_number
113 WHERE record = bib_id
114 AND label = '##URI##'
116 AND NOT (id = ANY (current_map_owner_list))
118 -- Check for URIs to-be-orphaned URIs
121 FROM asset.uri_call_number_map
122 WHERE call_number = uri_cn_id
124 SELECT COUNT(*) INTO uri_map_count FROM asset.uri_call_number_map WHERE uri = current_uri;
125 IF uri_map_count = 1 THEN -- only one means it's the last
126 orphaned_uri_list := orphaned_uri_list || current_uri;
130 DELETE FROM asset.uri_call_number_map WHERE call_number = uri_cn_id;
131 DELETE FROM asset.call_number WHERE id = uri_cn_id;
132 DELETE FROM asset.uri WHERE id = ANY (orphaned_uri_list);
137 $func$ LANGUAGE PLPGSQL;