]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0337.function.asset.merge_record_assets.sql
LP#1947173: Clean up bad cataloging pot hole
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0337.function.asset.merge_record_assets.sql
1 BEGIN;
2
3 INSERT INTO config.upgrade_log (version) VALUES ('0337'); -- dbs
4
5 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
6 DECLARE
7     moved_objects INT := 0;
8     source_cn     asset.call_number%ROWTYPE;
9     target_cn     asset.call_number%ROWTYPE;
10     metarec       metabib.metarecord%ROWTYPE;
11     hold          action.hold_request%ROWTYPE;
12     ser_rec       serial.record_entry%ROWTYPE;
13     auth_link     authority.bib_linking%ROWTYPE;
14     uri_count     INT := 0;
15     counter       INT := 0;
16     uri_datafield TEXT;
17     uri_text      TEXT := '';
18 BEGIN
19
20     -- move any 856 entries on records that have at least one MARC-mapped URI entry
21     SELECT  INTO uri_count COUNT(*)
22       FROM  asset.uri_call_number_map m
23             JOIN asset.call_number cn ON (m.call_number = cn.id)
24       WHERE cn.record = source_record;
25
26     IF uri_count > 0 THEN
27         
28         SELECT  COUNT(*) INTO counter
29           FROM  oils_xpath_table(
30                     'id',
31                     'marc',
32                     'biblio.record_entry',
33                     '//*[@tag="856"]',
34                     'id=' || lineitem
35                 ) as t(i int,c text);
36     
37         FOR i IN 1 .. counter LOOP
38             SELECT  '<datafield xmlns="http://www.loc.gov/MARC21/slim" tag="856">' ||
39                         array_to_string(
40                             array_accum(
41                                 '<subfield code="' || subfield || '">' ||
42                                 regexp_replace(
43                                     regexp_replace(
44                                         regexp_replace(data,'&','&amp;','g'),
45                                         '>', '&gt;', 'g'
46                                     ),
47                                     '<', '&lt;', 'g'
48                                 ) || '</subfield>'
49                             ), ''
50                         ) || '</datafield>' INTO uri_datafield
51               FROM  oils_xpath_table(
52                         'id',
53                         'marc',
54                         'biblio.record_entry',
55                         '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
56                         '//*[@tag="856"][position()=' || i || ']/*[@code]',
57                         'id=' || source_record
58                     ) as t(id int,subfield text,data text);
59
60             uri_text := uri_text || uri_datafield;
61         END LOOP;
62
63         IF uri_text <> '' THEN
64             UPDATE  biblio.record_entry
65               SET   marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
66               WHERE id = target_record;
67         END IF;
68
69     END IF;
70
71     -- Find and move metarecords to the target record
72     SELECT    INTO metarec *
73       FROM    metabib.metarecord
74       WHERE    master_record = source_record;
75
76     IF FOUND THEN
77         UPDATE    metabib.metarecord
78           SET    master_record = target_record,
79             mods = NULL
80           WHERE    id = metarec.id;
81
82         moved_objects := moved_objects + 1;
83     END IF;
84
85     -- Find call numbers attached to the source ...
86     FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
87
88         SELECT    INTO target_cn *
89           FROM    asset.call_number
90           WHERE    label = source_cn.label
91             AND owning_lib = source_cn.owning_lib
92             AND record = target_record;
93
94         -- ... and if there's a conflicting one on the target ...
95         IF FOUND THEN
96
97             -- ... move the copies to that, and ...
98             UPDATE    asset.copy
99               SET    call_number = target_cn.id
100               WHERE    call_number = source_cn.id;
101
102             -- ... move V holds to the move-target call number
103             FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
104         
105                 UPDATE    action.hold_request
106                   SET    target = target_cn.id
107                   WHERE    id = hold.id;
108         
109                 moved_objects := moved_objects + 1;
110             END LOOP;
111
112         -- ... if not ...
113         ELSE
114             -- ... just move the call number to the target record
115             UPDATE    asset.call_number
116               SET    record = target_record
117               WHERE    id = source_cn.id;
118         END IF;
119
120         moved_objects := moved_objects + 1;
121     END LOOP;
122
123     -- Find T holds targeting the source record ...
124     FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
125
126         -- ... and move them to the target record
127         UPDATE    action.hold_request
128           SET    target = target_record
129           WHERE    id = hold.id;
130
131         moved_objects := moved_objects + 1;
132     END LOOP;
133
134     -- Find serial records targeting the source record ...
135     FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
136         -- ... and move them to the target record
137         UPDATE    serial.record_entry
138           SET    record = target_record
139           WHERE    id = ser_rec.id;
140
141         moved_objects := moved_objects + 1;
142     END LOOP;
143
144     -- Delete authority->bib links to the source record to avoid
145     -- the overhead of updating controlled fields in deleted records
146     FOR auth_link IN SELECT * FROM authority.bib_linking WHERE bib = source_record LOOP
147         DELETE FROM authority.bib_linking
148           WHERE id = auth_link.id;
149
150         moved_objects := moved_objects + 1;
151     END LOOP;
152
153     -- Finally, "delete" the source record
154     DELETE FROM biblio.record_entry WHERE id = source_record;
155
156     -- That's all, folks!
157     RETURN moved_objects;
158 END;
159 $func$ LANGUAGE plpgsql;
160
161 COMMIT;
162