]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/1171.schema.trim_spaces_during_856_9_parsing.sql
LP2045292 Color contrast for AngularJS patron bills
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 1171.schema.trim_spaces_during_856_9_parsing.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('1171', :eg_version);
4
5 CREATE OR REPLACE FUNCTION biblio.extract_located_uris( bib_id BIGINT, marcxml TEXT, editor_id INT ) RETURNS VOID AS $func$
6 DECLARE
7     uris            TEXT[];
8     uri_xml         TEXT;
9     uri_label       TEXT;
10     uri_href        TEXT;
11     uri_use         TEXT;
12     uri_owner_list  TEXT[];
13     uri_owner       TEXT;
14     uri_owner_id    INT;
15     uri_id          INT;
16     uri_cn_id       INT;
17     uri_map_id      INT;
18 BEGIN
19
20     -- Clear any URI mappings and call numbers for this bib.
21     -- This leads to acn / auricnm inflation, but also enables
22     -- old acn/auricnm's to go away and for bibs to be deleted.
23     FOR uri_cn_id IN SELECT id FROM asset.call_number WHERE record = bib_id AND label = '##URI##' AND NOT deleted LOOP
24         DELETE FROM asset.uri_call_number_map WHERE call_number = uri_cn_id;
25         DELETE FROM asset.call_number WHERE id = uri_cn_id;
26     END LOOP;
27
28     uris := oils_xpath('//*[@tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]',marcxml);
29     IF ARRAY_UPPER(uris,1) > 0 THEN
30         FOR i IN 1 .. ARRAY_UPPER(uris, 1) LOOP
31             -- First we pull info out of the 856
32             uri_xml     := uris[i];
33
34             uri_href    := (oils_xpath('//*[@code="u"]/text()',uri_xml))[1];
35             uri_label   := (oils_xpath('//*[@code="y"]/text()|//*[@code="3"]/text()',uri_xml))[1];
36             uri_use     := (oils_xpath('//*[@code="z"]/text()|//*[@code="2"]/text()|//*[@code="n"]/text()',uri_xml))[1];
37
38             IF uri_label IS NULL THEN
39                 uri_label := uri_href;
40             END IF;
41             CONTINUE WHEN uri_href IS NULL;
42
43             -- Get the distinct list of libraries wanting to use 
44             SELECT  ARRAY_AGG(
45                         DISTINCT REGEXP_REPLACE(
46                             x,
47                             $re$^.*?\((\w+)\).*$$re$,
48                             E'\\1'
49                         )
50                     ) INTO uri_owner_list
51               FROM  UNNEST(
52                         oils_xpath(
53                             '//*[@code="9"]/text()|//*[@code="w"]/text()|//*[@code="n"]/text()',
54                             uri_xml
55                         )
56                     )x;
57
58             IF ARRAY_UPPER(uri_owner_list,1) > 0 THEN
59
60                 -- look for a matching uri
61                 IF uri_use IS NULL THEN
62                     SELECT id INTO uri_id
63                         FROM asset.uri
64                         WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active
65                         ORDER BY id LIMIT 1;
66                     IF NOT FOUND THEN -- create one
67                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
68                         SELECT id INTO uri_id
69                             FROM asset.uri
70                             WHERE label = uri_label AND href = uri_href AND use_restriction IS NULL AND active;
71                     END IF;
72                 ELSE
73                     SELECT id INTO uri_id
74                         FROM asset.uri
75                         WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active
76                         ORDER BY id LIMIT 1;
77                     IF NOT FOUND THEN -- create one
78                         INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
79                         SELECT id INTO uri_id
80                             FROM asset.uri
81                             WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
82                     END IF;
83                 END IF;
84
85                 FOR j IN 1 .. ARRAY_UPPER(uri_owner_list, 1) LOOP
86                     uri_owner := uri_owner_list[j];
87
88                     SELECT id INTO uri_owner_id FROM actor.org_unit WHERE shortname = BTRIM(REPLACE(uri_owner,chr(160),''));
89                     CONTINUE WHEN NOT FOUND;
90
91                     -- we need a call number to link through
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                     IF NOT FOUND THEN
94                         INSERT INTO asset.call_number (owning_lib, record, create_date, edit_date, creator, editor, label)
95                             VALUES (uri_owner_id, bib_id, 'now', 'now', editor_id, editor_id, '##URI##');
96                         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;
97                     END IF;
98
99                     -- now, link them if they're not already
100                     SELECT id INTO uri_map_id FROM asset.uri_call_number_map WHERE call_number = uri_cn_id AND uri = uri_id;
101                     IF NOT FOUND THEN
102                         INSERT INTO asset.uri_call_number_map (call_number, uri) VALUES (uri_cn_id, uri_id);
103                     END IF;
104
105                 END LOOP;
106
107             END IF;
108
109         END LOOP;
110     END IF;
111
112     RETURN;
113 END;
114 $func$ LANGUAGE PLPGSQL;
115
116 COMMIT;