]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/0989.schema.authority-vandeley-edit-date.sql
LP#1117808: Stamping upgrade scripts for extend use of merge profiles
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / 0989.schema.authority-vandeley-edit-date.sql
1 BEGIN;
2
3 SELECT evergreen.upgrade_deps_block_check('0989', :eg_version); -- berick/miker/gmcharlt
4
5 CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
6 DECLARE
7     merge_profile   vandelay.merge_profile%ROWTYPE;
8     dyn_profile     vandelay.compile_profile%ROWTYPE;
9     editor_string   TEXT;
10     new_editor      INT;
11     new_edit_date   TIMESTAMPTZ;
12     source_marc     TEXT;
13     target_marc     TEXT;
14     eg_marc_row     authority.record_entry%ROWTYPE;
15     eg_marc         TEXT;
16     v_marc          TEXT;
17     replace_rule    TEXT;
18     match_count     INT;
19     update_query    TEXT;
20 BEGIN
21
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)
25       LIMIT 1;
26
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)
30       LIMIT 1;
31
32     eg_marc := eg_marc_row.marc;
33
34     IF eg_marc IS NULL OR v_marc IS NULL THEN
35         -- RAISE NOTICE 'no marc for vandelay or authority record';
36         RETURN FALSE;
37     END IF;
38
39     -- Extract the editor string before any modification to the vandelay
40     -- MARC occur.
41     editor_string := 
42         (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1];
43
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
47
48         -- Vandelay.pm sets the value to 'usrname' when needed.  
49         SELECT id INTO new_editor
50             FROM actor.usr WHERE usrname = editor_string;
51
52         IF new_editor IS NULL THEN
53             SELECT usr INTO new_editor
54                 FROM actor.card WHERE barcode = editor_string;
55         END IF;
56
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;
62         END IF;
63     ELSE
64         new_editor = eg_marc_row.editor;
65         new_edit_date = eg_marc_row.edit_date;
66     END IF;
67
68     dyn_profile := vandelay.compile_profile( v_marc );
69
70     IF merge_profile_id IS NOT NULL THEN
71         SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
72         IF FOUND THEN
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,''), ',');
77         END IF;
78     END IF;
79
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;
82         RETURN FALSE;
83     END IF;
84
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"
87         RETURN TRUE;
88     ELSIF dyn_profile.replace_rule <> '' THEN
89         source_marc = v_marc;
90         target_marc = eg_marc;
91         replace_rule = dyn_profile.replace_rule;
92     ELSE
93         source_marc = eg_marc;
94         target_marc = v_marc;
95         replace_rule = dyn_profile.preserve_rule;
96     END IF;
97
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 ),
100             editor = new_editor,
101             edit_date = new_edit_date
102       WHERE id = eg_id;
103
104     IF NOT FOUND THEN 
105         -- Import/merge failed.  Nothing left to do.
106         RETURN FALSE;
107     END IF;
108
109     -- Authority record successfully merged / imported.
110
111     -- Update the vandelay record to show the successful import.
112     UPDATE  vandelay.queued_authority_record
113       SET   imported_as = eg_id,
114             import_time = NOW()
115       WHERE id = import_id;
116
117     RETURN TRUE;
118
119 END;
120 $$ LANGUAGE PLPGSQL;
121
122
123 COMMIT;
124