]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/sql/Pg/upgrade/XXXX.schema.authority-propage-edit-date.sql
LP#1588948: Only attempt a bib update if the heading changes
[Evergreen.git] / Open-ILS / src / sql / Pg / upgrade / XXXX.schema.authority-propage-edit-date.sql
1
2 BEGIN;
3
4 CREATE OR REPLACE FUNCTION authority.propagate_changes 
5     (aid BIGINT, bid BIGINT) RETURNS BIGINT AS $func$
6 DECLARE
7     bib_rec biblio.record_entry%ROWTYPE;
8     new_marc TEXT;
9 BEGIN
10
11     SELECT INTO bib_rec * FROM biblio.record_entry WHERE id = bid;
12
13     new_marc := vandelay.merge_record_xml(
14         bib_rec.marc, authority.generate_overlay_template(aid));
15
16     IF new_marc = bib_rec.marc THEN
17         -- Authority record change had no impact on this bib record.
18         -- Nothing left to do.
19         RETURN aid;
20     END IF;
21
22     PERFORM 1 FROM config.global_flag 
23         WHERE name = 'ingest.disable_authority_auto_update_bib_meta' 
24             AND enabled;
25
26     IF NOT FOUND THEN 
27         -- update the bib record editor and edit_date
28         bib_rec.editor := (
29             SELECT editor FROM authority.record_entry WHERE id = aid);
30         bib_rec.edit_date = NOW();
31     END IF;
32
33     UPDATE biblio.record_entry SET
34         marc = new_marc,
35         editor = bib_rec.editor,
36         edit_date = bib_rec.edit_date
37     WHERE id = bid;
38
39     RETURN aid;
40
41 END;
42 $func$ LANGUAGE PLPGSQL;
43
44
45 -- DATA
46 -- Disabled by default
47 INSERT INTO config.global_flag (name, enabled, label) VALUES (
48     'ingest.disable_authority_auto_update_bib_meta',  FALSE, 
49     oils_i18n_gettext(
50         'ingest.disable_authority_auto_update_bib_meta',
51         'Authority Automation: Disable automatic authority updates ' ||
52             'from modifying bib record editor and edit_date',
53         'cgf',
54         'label'
55     )
56 );
57
58
59 CREATE OR REPLACE FUNCTION authority.indexing_ingest_or_delete () RETURNS TRIGGER AS $func$
60 DECLARE
61     ashs    authority.simple_heading%ROWTYPE;
62     mbe_row metabib.browse_entry%ROWTYPE;
63     mbe_id  BIGINT;
64     ash_id  BIGINT;
65 BEGIN
66
67     IF NEW.deleted IS TRUE THEN -- If this authority is deleted
68         DELETE FROM authority.bib_linking WHERE authority = NEW.id; -- Avoid updating fields in bibs that are no longer visible
69         DELETE FROM authority.full_rec WHERE record = NEW.id; -- Avoid validating fields against deleted authority records
70         DELETE FROM authority.simple_heading WHERE record = NEW.id;
71           -- Should remove matching $0 from controlled fields at the same time?
72
73         -- XXX What do we about the actual linking subfields present in
74         -- authority records that target this one when this happens?
75         DELETE FROM authority.authority_linking
76             WHERE source = NEW.id OR target = NEW.id;
77
78         RETURN NEW; -- and we're done
79     END IF;
80
81     IF TG_OP = 'UPDATE' THEN -- re-ingest?
82         PERFORM * FROM config.internal_flag WHERE name = 'ingest.reingest.force_on_same_marc' AND enabled;
83
84         IF NOT FOUND AND OLD.marc = NEW.marc THEN -- don't do anything if the MARC didn't change
85             RETURN NEW;
86         END IF;
87
88         -- Unless there's a setting stopping us, propagate these updates to any linked bib records when the heading changes
89         PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_auto_update' AND enabled;
90
91         IF NOT FOUND AND NEW.heading <> OLD.heading THEN
92             PERFORM authority.propagate_changes(NEW.id);
93         END IF;
94         
95         DELETE FROM authority.simple_heading WHERE record = NEW.id;
96         DELETE FROM authority.authority_linking WHERE source = NEW.id;
97     END IF;
98
99     INSERT INTO authority.authority_linking (source, target, field)
100         SELECT source, target, field FROM authority.calculate_authority_linking(
101             NEW.id, NEW.control_set, NEW.marc::XML
102         );
103
104     FOR ashs IN SELECT * FROM authority.simple_heading_set(NEW.marc) LOOP
105
106         INSERT INTO authority.simple_heading (record,atag,value,sort_value,thesaurus)
107             VALUES (ashs.record, ashs.atag, ashs.value, ashs.sort_value, ashs.thesaurus);
108             ash_id := CURRVAL('authority.simple_heading_id_seq'::REGCLASS);
109
110         SELECT INTO mbe_row * FROM metabib.browse_entry
111             WHERE value = ashs.value AND sort_value = ashs.sort_value;
112
113         IF FOUND THEN
114             mbe_id := mbe_row.id;
115         ELSE
116             INSERT INTO metabib.browse_entry
117                 ( value, sort_value ) VALUES
118                 ( ashs.value, ashs.sort_value );
119
120             mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
121         END IF;
122
123         INSERT INTO metabib.browse_entry_simple_heading_map (entry,simple_heading) VALUES (mbe_id,ash_id);
124
125     END LOOP;
126
127     -- Flatten and insert the afr data
128     PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_full_rec' AND enabled;
129     IF NOT FOUND THEN
130         PERFORM authority.reingest_authority_full_rec(NEW.id);
131         PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_rec_descriptor' AND enabled;
132         IF NOT FOUND THEN
133             PERFORM authority.reingest_authority_rec_descriptor(NEW.id);
134         END IF;
135     END IF;
136
137     RETURN NEW;
138 END;
139 $func$ LANGUAGE PLPGSQL;
140
141 COMMIT;
142