1 CREATE OR REPLACE FUNCTION asset.merge_record_assets( target_record BIGINT, source_record BIGINT ) RETURNS INT AS $func$
3 moved_objects INT := 0;
4 source_cn asset.call_number%ROWTYPE;
5 target_cn asset.call_number%ROWTYPE;
6 metarec metabib.metarecord%ROWTYPE;
7 hold action.hold_request%ROWTYPE;
8 ser_rec serial.record_entry%ROWTYPE;
9 ser_sub serial.subscription%ROWTYPE;
10 acq_lineitem acq.lineitem%ROWTYPE;
11 acq_request acq.user_request%ROWTYPE;
12 booking booking.resource_type%ROWTYPE;
13 source_part biblio.monograph_part%ROWTYPE;
14 target_part biblio.monograph_part%ROWTYPE;
15 multi_home biblio.peer_bib_copy_map%ROWTYPE;
22 -- move any 856 entries on records that have at least one MARC-mapped URI entry
23 SELECT INTO uri_count COUNT(*)
24 FROM asset.uri_call_number_map m
25 JOIN asset.call_number cn ON (m.call_number = cn.id)
26 WHERE cn.record = source_record;
30 SELECT COUNT(*) INTO counter
31 FROM oils_xpath_table(
34 'biblio.record_entry',
36 'id=' || source_record
39 FOR i IN 1 .. counter LOOP
40 SELECT '<datafield xmlns="http://www.loc.gov/MARC21/slim"' ||
42 ' ind1="' || FIRST(ind1) || '"' ||
43 ' ind2="' || FIRST(ind2) || '">' ||
46 '<subfield code="' || subfield || '">' ||
49 regexp_replace(data,'&','&','g'),
55 ) || '</datafield>' INTO uri_datafield
56 FROM oils_xpath_table(
59 'biblio.record_entry',
60 '//*[@tag="856"][position()=' || i || ']/@ind1|' ||
61 '//*[@tag="856"][position()=' || i || ']/@ind2|' ||
62 '//*[@tag="856"][position()=' || i || ']/*/@code|' ||
63 '//*[@tag="856"][position()=' || i || ']/*[@code]',
64 'id=' || source_record
65 ) as t(id int,ind1 text, ind2 text,subfield text,data text);
67 uri_text := uri_text || uri_datafield;
70 IF uri_text <> '' THEN
71 UPDATE biblio.record_entry
72 SET marc = regexp_replace(marc,'(</[^>]*record>)', uri_text || E'\\1')
73 WHERE id = target_record;
78 -- Find and move metarecords to the target record
80 FROM metabib.metarecord
81 WHERE master_record = source_record;
84 UPDATE metabib.metarecord
85 SET master_record = target_record,
87 WHERE id = metarec.id;
89 moved_objects := moved_objects + 1;
92 -- Find call numbers attached to the source ...
93 FOR source_cn IN SELECT * FROM asset.call_number WHERE record = source_record LOOP
95 SELECT INTO target_cn *
96 FROM asset.call_number
97 WHERE label = source_cn.label
98 AND owning_lib = source_cn.owning_lib
99 AND record = target_record;
101 -- ... and if there's a conflicting one on the target ...
104 -- ... move the copies to that, and ...
106 SET call_number = target_cn.id
107 WHERE call_number = source_cn.id;
109 -- ... move V holds to the move-target call number
110 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_cn.id AND hold_type = 'V' LOOP
112 UPDATE action.hold_request
113 SET target = target_cn.id
116 moved_objects := moved_objects + 1;
121 -- ... just move the call number to the target record
122 UPDATE asset.call_number
123 SET record = target_record
124 WHERE id = source_cn.id;
127 moved_objects := moved_objects + 1;
130 -- Find T holds targeting the source record ...
131 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_record AND hold_type = 'T' LOOP
133 -- ... and move them to the target record
134 UPDATE action.hold_request
135 SET target = target_record
138 moved_objects := moved_objects + 1;
141 -- Find serial records targeting the source record ...
142 FOR ser_rec IN SELECT * FROM serial.record_entry WHERE record = source_record LOOP
143 -- ... and move them to the target record
144 UPDATE serial.record_entry
145 SET record = target_record
146 WHERE id = ser_rec.id;
148 moved_objects := moved_objects + 1;
151 -- Find serial subscriptions targeting the source record ...
152 FOR ser_sub IN SELECT * FROM serial.subscription WHERE record_entry = source_record LOOP
153 -- ... and move them to the target record
154 UPDATE serial.subscription
155 SET record_entry = target_record
156 WHERE id = ser_sub.id;
158 moved_objects := moved_objects + 1;
161 -- Find booking resource types targeting the source record ...
162 FOR booking IN SELECT * FROM booking.resource_type WHERE record = source_record LOOP
163 -- ... and move them to the target record
164 UPDATE booking.resource_type
165 SET record = target_record
166 WHERE id = booking.id;
168 moved_objects := moved_objects + 1;
171 -- Find acq lineitems targeting the source record ...
172 FOR acq_lineitem IN SELECT * FROM acq.lineitem WHERE eg_bib_id = source_record LOOP
173 -- ... and move them to the target record
175 SET eg_bib_id = target_record
176 WHERE id = acq_lineitem.id;
178 moved_objects := moved_objects + 1;
181 -- Find acq user purchase requests targeting the source record ...
182 FOR acq_request IN SELECT * FROM acq.user_request WHERE eg_bib = source_record LOOP
183 -- ... and move them to the target record
184 UPDATE acq.user_request
185 SET eg_bib = target_record
186 WHERE id = acq_request.id;
188 moved_objects := moved_objects + 1;
191 -- Find parts attached to the source ...
192 FOR source_part IN SELECT * FROM biblio.monograph_part WHERE record = source_record LOOP
194 SELECT INTO target_part *
195 FROM biblio.monograph_part
196 WHERE label = source_part.label
197 AND record = target_record;
199 -- ... and if there's a conflicting one on the target ...
202 -- ... move the copy-part maps to that, and ...
203 UPDATE asset.copy_part_map
204 SET part = target_part.id
205 WHERE part = source_part.id;
207 -- ... move P holds to the move-target part
208 FOR hold IN SELECT * FROM action.hold_request WHERE target = source_part.id AND hold_type = 'P' LOOP
210 UPDATE action.hold_request
211 SET target = target_part.id
214 moved_objects := moved_objects + 1;
219 -- ... just move the part to the target record
220 UPDATE biblio.monograph_part
221 SET record = target_record
222 WHERE id = source_part.id;
225 moved_objects := moved_objects + 1;
228 -- Find multi_home items attached to the source ...
229 FOR multi_home IN SELECT * FROM biblio.peer_bib_copy_map WHERE peer_record = source_record LOOP
230 -- ... and move them to the target record
231 UPDATE biblio.peer_bib_copy_map
232 SET peer_record = target_record
233 WHERE id = multi_home.id;
235 moved_objects := moved_objects + 1;
238 -- And delete mappings where the item's home bib was merged with the peer bib
239 DELETE FROM biblio.peer_bib_copy_map WHERE peer_record = (
240 SELECT (SELECT record FROM asset.call_number WHERE id = call_number)
241 FROM asset.copy WHERE id = target_copy
244 -- Finally, "delete" the source record
245 DELETE FROM biblio.record_entry WHERE id = source_record;
247 -- That's all, folks!
248 RETURN moved_objects;
250 $func$ LANGUAGE plpgsql;