3 SELECT evergreen.upgrade_deps_block_check('0989', :eg_version); -- berick/miker/gmcharlt
5 CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
7 merge_profile vandelay.merge_profile%ROWTYPE;
8 dyn_profile vandelay.compile_profile%ROWTYPE;
11 new_edit_date TIMESTAMPTZ;
14 eg_marc_row authority.record_entry%ROWTYPE;
22 SELECT * INTO eg_marc_row
23 FROM authority.record_entry b
24 JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id)
27 SELECT q.marc INTO v_marc
28 FROM vandelay.queued_record q
29 JOIN vandelay.authority_match m ON (m.queued_record = q.id AND q.id = import_id)
32 eg_marc := eg_marc_row.marc;
34 IF eg_marc IS NULL OR v_marc IS NULL THEN
35 -- RAISE NOTICE 'no marc for vandelay or authority record';
39 -- Extract the editor string before any modification to the vandelay
42 (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1];
44 -- If an editor value can be found, update the authority record
45 -- editor and edit_date values.
46 IF editor_string IS NOT NULL AND editor_string <> '' THEN
48 -- Vandelay.pm sets the value to 'usrname' when needed.
49 SELECT id INTO new_editor
50 FROM actor.usr WHERE usrname = editor_string;
52 IF new_editor IS NULL THEN
53 SELECT usr INTO new_editor
54 FROM actor.card WHERE barcode = editor_string;
57 IF new_editor IS NOT NULL THEN
58 new_edit_date := NOW();
59 ELSE -- No valid editor, use current values
60 new_editor = eg_marc_row.editor;
61 new_edit_date = eg_marc_row.edit_date;
64 new_editor = eg_marc_row.editor;
65 new_edit_date = eg_marc_row.edit_date;
68 dyn_profile := vandelay.compile_profile( v_marc );
70 IF merge_profile_id IS NOT NULL THEN
71 SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
73 dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
74 dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
75 dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
76 dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
80 IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
81 -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
85 IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
86 --Since we have nothing to do, just return a NOOP "we did it"
88 ELSIF dyn_profile.replace_rule <> '' THEN
90 target_marc = eg_marc;
91 replace_rule = dyn_profile.replace_rule;
93 source_marc = eg_marc;
95 replace_rule = dyn_profile.preserve_rule;
98 UPDATE authority.record_entry
99 SET marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule ),
101 edit_date = new_edit_date
105 -- Import/merge failed. Nothing left to do.
109 -- Authority record successfully merged / imported.
111 -- Update the vandelay record to show the successful import.
112 UPDATE vandelay.queued_authority_record
113 SET imported_as = eg_id,
115 WHERE id = import_id;