]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/xxxx.metabib.trim_spaces_from_located_uris.sql
8eb227b6aca4a4959d76ebf71c74081176a194dd
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / xxxx.metabib.trim_spaces_from_located_uris.sql
1 CREATE OR REPLACE FUNCTION biblio.extract_located_uris( bib_id BIGINT, marcxml TEXT, editor_id INT ) RETURNS VOID AS $func$
2 DECLARE
3     uris            TEXT[];
4     uri_xml         TEXT;
5     uri_label       TEXT;
6     uri_href        TEXT;
7     uri_use         TEXT;
8     uri_owner_list  TEXT[];
9     uri_owner       TEXT;
10     uri_owner_id    INT;
11     uri_id          INT;
12     uri_cn_id       INT;
13     uri_map_id      INT;
14 BEGIN
15
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;
22     END LOOP;
23
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
28             uri_xml     := uris[i];
29
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];
33
34             IF uri_label IS NULL THEN
35                 uri_label := uri_href;
36             END IF;
37             CONTINUE WHEN uri_href IS NULL;
38
39             -- Get the distinct list of libraries wanting to use 
40             SELECT  ARRAY_AGG(
41                         DISTINCT REGEXP_REPLACE(
42                             x,
43                             $re$^.*?\((\w+)\).*$$re$,
44                             E'\\1'
45                         )
46                     ) INTO uri_owner_list
47               FROM  UNNEST(
48                         oils_xpath(
49                             '//*[@code="9"]/text()|//*[@code="w"]/text()|//*[@code="n"]/text()',
50                             uri_xml
51                         )
52                     )x;
53
54             IF ARRAY_UPPER(uri_owner_list,1) > 0 THEN
55
56                 -- look for a matching uri
57                 IF uri_use IS NULL THEN
58                     SELECT id INTO uri_id
59                         FROM asset.uri
60                         WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active
61                         ORDER BY id LIMIT 1;
62                     IF NOT FOUND THEN -- create one
63                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
64                         SELECT id INTO uri_id
65                             FROM asset.uri
66                             WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active;
67                     END IF;
68                 ELSE
69                     SELECT id INTO uri_id
70                         FROM asset.uri
71                         WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active
72                         ORDER BY id LIMIT 1;
73                     IF NOT FOUND THEN -- create one
74                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
75                         SELECT id INTO uri_id
76                             FROM asset.uri
77                             WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
78                     END IF;
79                 END IF;
80
81                 FOR j IN 1 .. ARRAY_UPPER(uri_owner_list, 1) LOOP
82                     uri_owner := uri_owner_list[j];
83
84                     SELECT id INTO uri_owner_id FROM actor.org_unit WHERE shortname = BTRIM(REPLACE(uri_owner,chr(160),''));
85                     CONTINUE WHEN NOT FOUND;
86
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;
89                     IF NOT FOUND THEN
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;
93                     END IF;
94
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;
97                     IF NOT FOUND THEN
98                         INSERT INTO asset.uri_call_number_map (call_number, uri) VALUES (uri_cn_id, uri_id);
99                     END IF;
100
101                 END LOOP;
102
103             END IF;
104
105         END LOOP;
106     END IF;
107
108     RETURN;
109 END;
110 $func$ LANGUAGE PLPGSQL;
111