]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.schema.vandelay-update-edit-date.sql
LP1928258 Vandelay separate bib edit update option
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.schema.vandelay-update-edit-date.sql
1 BEGIN;
2
3 -- SELECT evergreen.upgrade_deps_block_check('TODO', :eg_version);
4
5 ALTER TABLE vandelay.merge_profile
6     ADD COLUMN update_bib_editor BOOLEAN NOT NULL DEFAULT FALSE;
7
8 -- By default, updating bib source means updating the editor.
9 UPDATE vandelay.merge_profile SET update_bib_editor = update_bib_source;
10
11 CREATE OR REPLACE FUNCTION vandelay.overlay_bib_record 
12     ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
13 DECLARE
14     editor_string   TEXT;
15     editor_id       INT;
16     v_marc          TEXT;
17     v_bib_source    INT;
18     update_fields   TEXT[];
19     update_query    TEXT;
20     update_bib_source BOOL;
21     update_bib_editor BOOL;
22 BEGIN
23
24     SELECT  q.marc, q.bib_source INTO v_marc, v_bib_source
25       FROM  vandelay.queued_bib_record q
26             JOIN vandelay.bib_match m ON (m.queued_record = q.id AND q.id = import_id)
27       LIMIT 1;
28
29     IF v_marc IS NULL THEN
30         -- RAISE NOTICE 'no marc for vandelay or bib record';
31         RETURN FALSE;
32     END IF;
33
34     IF NOT vandelay.template_overlay_bib_record( v_marc, eg_id, merge_profile_id) THEN
35         -- no update happened, get outta here.
36         RETURN FALSE;
37     END IF;
38
39     UPDATE  vandelay.queued_bib_record
40       SET   imported_as = eg_id,
41             import_time = NOW()
42       WHERE id = import_id;
43
44     SELECT q.update_bib_source INTO update_bib_source 
45         FROM vandelay.merge_profile q where q.id = merge_profile_Id;
46
47     IF update_bib_source AND v_bib_source IS NOT NULL THEN
48         update_fields := ARRAY_APPEND(update_fields, 'source = ' || v_bib_source);
49     END IF;
50
51     SELECT q.update_bib_editor INTO update_bib_editor 
52         FROM vandelay.merge_profile q where q.id = merge_profile_Id;
53
54     IF update_bib_editor THEN
55
56         editor_string := (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1];
57
58         IF editor_string IS NOT NULL AND editor_string <> '' THEN
59             SELECT usr INTO editor_id FROM actor.card WHERE barcode = editor_string;
60
61             IF editor_id IS NULL THEN
62                 SELECT id INTO editor_id FROM actor.usr WHERE usrname = editor_string;
63             END IF;
64
65             IF editor_id IS NOT NULL THEN
66                 --only update the edit date if we have a valid editor
67                 update_fields := ARRAY_APPEND(
68                     update_fields, 'editor = ' || editor_id || ', edit_date = NOW()');
69             END IF;
70         END IF;
71     END IF;
72
73     IF ARRAY_LENGTH(update_fields, 1) > 0 THEN
74         update_query := 'UPDATE biblio.record_entry SET ' || 
75             ARRAY_TO_STRING(update_fields, ',') || ' WHERE id = ' || eg_id || ';';
76         EXECUTE update_query;
77     END IF;
78
79     RETURN TRUE;
80 END;
81 $$ LANGUAGE PLPGSQL;
82
83 COMMIT;
84