]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0164.schema.in-db-ingest-fix-urls.sql
Stamping upgrade scripts for Vandelay default match set, with minor adjustments
[working/Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0164.schema.in-db-ingest-fix-urls.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0164'); -- Galen Charlton
4
5 CREATE OR REPLACE FUNCTION biblio.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$
6 DECLARE
7     ind_data        metabib.field_entry_template%ROWTYPE;
8     old_mr          INT;
9     tmp_mr          metabib.metarecord%ROWTYPE;
10     source_count    INT;
11     deleted_mrs     INT[];
12     uris            TEXT[];
13     uri_xml         TEXT;
14     uri_label       TEXT;
15     uri_href        TEXT;
16     uri_use         TEXT;
17     uri_owner       TEXT;
18     uri_owner_id    INT;
19     uri_id          INT;
20     uri_cn_id       INT;
21     uri_map_id      INT;
22 BEGIN
23
24     DELETE FROM metabib.metarecord_source_map WHERE source = NEW.id; -- Rid ourselves of the search-estimate-killing linkage
25
26     IF NEW.deleted IS TRUE THEN
27         RETURN NEW; -- and we're done
28     END IF;
29
30     IF TG_OP = 'UPDATE' THEN -- Clean out the cruft
31         DELETE FROM metabib.title_field_entry WHERE source = NEW.id;
32         DELETE FROM metabib.author_field_entry WHERE source = NEW.id;
33         DELETE FROM metabib.subject_field_entry WHERE source = NEW.id;
34         DELETE FROM metabib.keyword_field_entry WHERE source = NEW.id;
35         DELETE FROM metabib.series_field_entry WHERE source = NEW.id;
36         DELETE FROM metabib.full_rec WHERE record = NEW.id;
37         DELETE FROM metabib.rec_descriptor WHERE record = NEW.id;
38
39     END IF;
40
41     -- Shove the flattened MARC in
42     INSERT INTO metabib.full_rec (record, tag, ind1, ind2, subfield, value)
43         SELECT record, tag, ind1, ind2, subfield, value FROM biblio.flatten_marc( NEW.id );
44
45     -- And now the indexing data
46     FOR ind_data IN SELECT * FROM biblio.extract_metabib_field_entry( NEW.id ) LOOP
47         IF ind_data.field_class = 'title' THEN
48             INSERT INTO metabib.title_field_entry (field, source, value)
49                 VALUES (ind_data.field, ind_data.source, ind_data.value);
50         ELSIF ind_data.field_class = 'author' THEN
51             INSERT INTO metabib.author_field_entry (field, source, value)
52                 VALUES (ind_data.field, ind_data.source, ind_data.value);
53         ELSIF ind_data.field_class = 'subject' THEN
54             INSERT INTO metabib.subject_field_entry (field, source, value)
55                 VALUES (ind_data.field, ind_data.source, ind_data.value);
56         ELSIF ind_data.field_class = 'keyword' THEN
57             INSERT INTO metabib.keyword_field_entry (field, source, value)
58                 VALUES (ind_data.field, ind_data.source, ind_data.value);
59         ELSIF ind_data.field_class = 'series' THEN
60             INSERT INTO metabib.series_field_entry (field, source, value)
61                 VALUES (ind_data.field, ind_data.source, ind_data.value);
62         END IF;
63     END LOOP;
64
65     -- Then, the rec_descriptor
66     INSERT INTO metabib.rec_descriptor (record, item_type, item_form, bib_level, control_type, enc_level, audience, lit_form, type_mat, cat_form, pub_status, item_lang, vr_format, date1, date2)
67         SELECT  NEW.id,
68                 biblio.marc21_extract_fixed_field( NEW.id, 'Type' ),
69                 biblio.marc21_extract_fixed_field( NEW.id, 'Form' ),
70                 biblio.marc21_extract_fixed_field( NEW.id, 'BLvl' ),
71                 biblio.marc21_extract_fixed_field( NEW.id, 'Ctrl' ),
72                 biblio.marc21_extract_fixed_field( NEW.id, 'ELvl' ),
73                 biblio.marc21_extract_fixed_field( NEW.id, 'Audn' ),
74                 biblio.marc21_extract_fixed_field( NEW.id, 'LitF' ),
75                 biblio.marc21_extract_fixed_field( NEW.id, 'TMat' ),
76                 biblio.marc21_extract_fixed_field( NEW.id, 'Desc' ),
77                 biblio.marc21_extract_fixed_field( NEW.id, 'DtSt' ),
78                 biblio.marc21_extract_fixed_field( NEW.id, 'Lang' ),
79                 (   SELECT  v.value
80                       FROM  biblio.marc21_physical_characteristics( NEW.id) p
81                             JOIN config.marc21_physical_characteristic_subfield_map s ON (s.id = p.subfield)
82                             JOIN config.marc21_physical_characteristic_value_map v ON (v.id = p.value)
83                       WHERE p.ptype = 'v' AND s.subfield = 'e'    ),
84                 biblio.marc21_extract_fixed_field( NEW.id, 'Date1'),
85                 biblio.marc21_extract_fixed_field( NEW.id, 'Date2');
86
87     -- On to URIs ...
88     uris := oils_xpath('//*[@tag="856" and (@ind1="4" or @ind1="1") and (@ind2="0" or @ind2="1")]',NEW.marc);
89     IF ARRAY_UPPER(uris,1) > 0 THEN
90         FOR i IN 1 .. ARRAY_UPPER(uris, 1) LOOP
91             -- First we pull infot out of the 856
92             uri_xml     := uris[i];
93
94             uri_href    := (oils_xpath('//*[@code="u"]/text()',uri_xml))[1];
95             CONTINUE WHEN uri_href IS NULL;
96
97             uri_label   := (oils_xpath('//*[@code="y"]/text()|//*[@code="3"]/text()|//*[@code="u"]/text()',uri_xml))[1];
98             CONTINUE WHEN uri_label IS NULL;
99
100             uri_owner   := (oils_xpath('//*[@code="9"]/text()|//*[@code="w"]/text()|//*[@code="n"]/text()',uri_xml))[1];
101             CONTINUE WHEN uri_owner IS NULL;
102     
103             uri_use     := (oils_xpath('//*[@code="z"]/text()|//*[@code="2"]/text()|//*[@code="n"]/text()',uri_xml))[1];
104
105             uri_owner := REGEXP_REPLACE(uri_owner, $re$^.*?\((\w+)\).*$$re$, E'\\1');
106     
107             SELECT id INTO uri_owner_id FROM actor.org_unit WHERE shortname = uri_owner;
108             CONTINUE WHEN NOT FOUND;
109     
110             -- now we look for a matching uri
111             SELECT id INTO uri_id FROM asset.uri WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
112             IF NOT FOUND THEN -- create one
113                 INSERT INTO asset.uri (label, href, use_restriction) VALUES (uri_label, uri_href, uri_use);
114                 SELECT id INTO uri_id FROM asset.uri WHERE label = uri_label AND href = uri_href AND use_restriction = uri_use AND active;
115             END IF;
116     
117             -- we need a call number to link through
118             SELECT id INTO uri_cn_id FROM asset.call_number WHERE owning_lib = uri_owner_id AND record = NEW.id AND label = '##URI##' AND NOT deleted;
119             IF NOT FOUND THEN
120                 INSERT INTO asset.call_number (owning_lib, record, create_date, edit_date, creator, editor, label)
121                     VALUES (uri_owner_id, NEW.id, 'now', 'now', NEW.editor, NEW.editor, '##URI##');
122                 SELECT id INTO uri_cn_id FROM asset.call_number WHERE owning_lib = uri_owner_id AND record = NEW.id AND label = '##URI##' AND NOT deleted;
123             END IF;
124     
125             -- now, link them if they're not already
126             SELECT id INTO uri_map_id FROM asset.uri_call_number_map WHERE call_number = uri_cn_id AND uri = uri_id;
127             IF NOT FOUND THEN
128                 INSERT INTO asset.uri_call_number_map (call_number, uri) VALUES (uri_cn_id, uri_id);
129             END IF;
130     
131         END LOOP;
132     END IF;
133
134     -- And, finally, metarecord mapping!
135
136     FOR tmp_mr IN SELECT  m.* FROM  metabib.metarecord m JOIN metabib.metarecord_source_map s ON (s.metarecord = m.id) WHERE s.source = NEW.id LOOP
137
138         IF old_mr IS NULL AND NEW.fingerprint = tmp_mr.fingerprint THEN -- Find the first fingerprint-matching
139             old_mr := tmp_mr.id;
140         ELSE
141             SELECT COUNT(*) INTO source_count FROM metabib.metarecord_source_map WHERE metarecord = tmp_mr.id;
142             IF source_count = 0 THEN -- No other records
143                 deleted_mrs := ARRAY_APPEND(deleted_mrs, tmp_mr.id);
144                 DELETE FROM metabib.metarecord WHERE id = tmp_mr.id;
145             END IF;
146         END IF;
147
148     END LOOP;
149
150     IF old_mr IS NULL THEN -- we found no suitable, preexisting MR based on old source maps
151         SELECT id INTO old_mr FROM metabib.metarecord WHERE fingerprint = NEW.fingerprint; -- is there one for our current fingerprint?
152         IF old_mr IS NULL THEN -- nope, create one and grab its id
153             INSERT INTO metabib.metarecord ( fingerprint, master_record ) VALUES ( NEW.fingerprint, NEW.id );
154             SELECT id INTO old_mr FROM metabib.metarecord WHERE fingerprint = NEW.fingerprint;
155         ELSE -- indeed there is. update it with a null cache and recalcualated master record
156             UPDATE  metabib.metarecord
157               SET   mods = NULL,
158                     master_record = ( SELECT id FROM biblio.record_entry WHERE fingerprint = NEW.fingerprint ORDER BY quality DESC LIMIT 1)
159               WHERE id = old_mr;
160         END IF;
161     ELSE -- there was one we already attached to, update its mods cache and master_record
162         UPDATE  metabib.metarecord
163           SET   mods = NULL,
164                 master_record = ( SELECT id FROM biblio.record_entry WHERE fingerprint = NEW.fingerprint ORDER BY quality DESC LIMIT 1)
165           WHERE id = old_mr;
166     END IF;
167
168     INSERT INTO metabib.metarecord_source_map (metarecord, source) VALUES (old_mr, NEW.id); -- new source mapping
169
170     UPDATE action.hold_request SET target = old_mr WHERE target IN ( SELECT explode_array(deleted_mrs) ) AND hold_type = 'M'; -- if we had to delete any MRs above, make sure their holds are moved
171
172     RETURN NEW;
173
174 END;
175 $func$ LANGUAGE PLPGSQL;
176
177 COMMIT;