JBAS-944 merging code for authority.indexing_ingest_or_delete
authorBill Erickson <berickxx@gmail.com>
Mon, 26 Oct 2015 15:32:09 +0000 (11:32 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Too many local changes to merge into stock.  Cross-porting
stock changes instead.

Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/sql/schema/deploy/2.5-to-2.7-upgrade.sql

index d1b4822..8abdd9c 100644 (file)
@@ -7400,22 +7400,50 @@ CREATE TRIGGER acp_location_fixer_trig
 
 SELECT evergreen.upgrade_deps_block_check('0897', :eg_version);
 
+
+/*
+-- KCLS has too many local changes to this function to port
+-- into the stock code.  Instead, upgrade the KCLS copy below.
+-- to support ingest.disable_authority_auto_update
+*/
 CREATE OR REPLACE FUNCTION authority.indexing_ingest_or_delete() RETURNS TRIGGER AS $BODY$
 DECLARE
     ashs    authority.simple_heading%ROWTYPE;
     mbe_row metabib.browse_entry%ROWTYPE;
     mbe_id  BIGINT;
     ash_id  BIGINT;
+    search_class text;
+    field_id integer;
+    are_row authority.record_entry%ROWTYPE;
+    bre_row biblio.record_entry%ROWTYPE;
 BEGIN
-
+    --ver 2.1 KMAIN-1119
     IF NEW.deleted IS TRUE THEN -- If this authority is deleted
+        -- Remove the actual linking subfields present in
+        -- marc bib records that is controlled by this one
+         FOR bre_row IN SELECT * FROM biblio.record_entry bre
+             INNER JOIN authority.bib_linking abl
+             ON bre.id = abl.bib AND abl.authority = NEW.id LOOP
+
+             UPDATE biblio.record_entry
+             SET marc = (SELECT regexp_replace(bre_row.marc,E'<subfield[^>]*?code="0">\\([A-Z]+\\)' || NEW.id || '</subfield>','','g'))
+             WHERE id = bre_row.id;
+
+         END LOOP;
         DELETE FROM authority.bib_linking WHERE authority = NEW.id; -- Avoid updating fields in bibs that are no longer visible
         DELETE FROM authority.full_rec WHERE record = NEW.id; -- Avoid validating fields against deleted authority records
         DELETE FROM authority.simple_heading WHERE record = NEW.id;
-          -- Should remove matching $0 from controlled fields at the same time?
+        -- Remove the actual linking subfields present in
+        -- authority records that target this one
+         FOR are_row IN SELECT * FROM authority.record_entry auth
+             INNER JOIN authority.authority_linking aal
+             ON auth.id = aal.source AND aal.target = NEW.id LOOP
+
+             UPDATE authority.record_entry
+             SET marc = (SELECT regexp_replace(are_row.marc,E'<subfield[^>]*?code="0">\\([A-Z]+\\)' || NEW.id || '</subfield>','','g'))
+             WHERE id = are_row.id;
 
-        -- XXX What do we about the actual linking subfields present in
-        -- authority records that target this one when this happens?
+         END LOOP;
         DELETE FROM authority.authority_linking
             WHERE source = NEW.id OR target = NEW.id;
 
@@ -7433,9 +7461,14 @@ BEGIN
         PERFORM * FROM config.internal_flag WHERE name = 'ingest.disable_authority_auto_update' AND enabled;
 
         IF NOT FOUND THEN
-            PERFORM authority.propagate_changes(NEW.id);
+            -- KMAIN-1521
+            -- Did the authority heading change? 
+            -- No need to propagate to bibs if the heading did not change
+            IF authority.heading_changed(OLD.marc, NEW.marc) THEN
+                PERFORM authority.propagate_changes(NEW.id);
+            END IF;
         END IF;
-
+        
         DELETE FROM authority.simple_heading WHERE record = NEW.id;
         DELETE FROM authority.authority_linking WHERE source = NEW.id;
     END IF;
@@ -7447,24 +7480,91 @@ BEGIN
 
     FOR ashs IN SELECT * FROM authority.simple_heading_set(NEW.marc) LOOP
 
+        -- Get the search_class
+        SELECT INTO search_class, field_id cmf.field_class, cmf.id
+            FROM authority.control_set_auth_field_metabib_field_map_refs AS acsafmfmr
+            JOIN config.metabib_field AS cmf
+            ON acsafmfmr.metabib_field = cmf.id
+            WHERE acsafmfmr.authority_field = ashs.atag;
+
         INSERT INTO authority.simple_heading (record,atag,value,sort_value)
             VALUES (ashs.record, ashs.atag, ashs.value, ashs.sort_value);
+
         ash_id := CURRVAL('authority.simple_heading_id_seq'::REGCLASS);
 
-        SELECT INTO mbe_row * FROM metabib.browse_entry
-            WHERE value = ashs.value AND sort_value = ashs.sort_value;
+        -- CASE statement switches on search_class to use the correct browse table (author, series, subject, title)
+        CASE search_class
+            WHEN 'author' THEN
+                SELECT INTO mbe_row * FROM metabib.browse_author_entry
+                    WHERE sort_value = ashs.sort_value
+                    ORDER BY id;
 
-        IF FOUND THEN
-            mbe_id := mbe_row.id;
-        ELSE
-            INSERT INTO metabib.browse_entry
-                ( value, sort_value ) VALUES
-                ( ashs.value, ashs.sort_value );
+                IF FOUND THEN
+                    mbe_id := mbe_row.id;
+                ELSE
+                    INSERT INTO metabib.browse_author_entry
+                        ( value, sort_value, truncated_sort_value ) VALUES
+                        ( ashs.value, ashs.sort_value, substr(ashs.sort_value, 1, 2700) );
 
-            mbe_id := CURRVAL('metabib.browse_entry_id_seq'::REGCLASS);
-        END IF;
+                    mbe_id := CURRVAL('metabib.browse_author_entry_id_seq'::REGCLASS);
+                END IF;
+
+                INSERT INTO metabib.browse_author_entry_simple_heading_map (entry,simple_heading) VALUES (mbe_id,ash_id);
+
+            WHEN 'series' THEN
+                SELECT INTO mbe_row * FROM metabib.browse_series_entry
+                    WHERE sort_value = ashs.sort_value
+                    ORDER BY id;
+
+                IF FOUND THEN
+                    mbe_id := mbe_row.id;
+                ELSE
+                    INSERT INTO metabib.browse_series_entry
+                        ( value, sort_value, truncated_sort_value ) VALUES
+                        ( ashs.value, ashs.sort_value, substr(ashs.sort_value, 1, 2700) );
+
+                    mbe_id := CURRVAL('metabib.browse_series_entry_id_seq'::REGCLASS);
+                END IF;
+
+                INSERT INTO metabib.browse_series_entry_simple_heading_map (entry,simple_heading) VALUES (mbe_id,ash_id);
+
+            WHEN 'subject' THEN
+                SELECT INTO mbe_row * FROM metabib.browse_subject_entry
+                    WHERE sort_value = ashs.sort_value
+                    ORDER BY id;
+
+                IF FOUND THEN
+                    mbe_id := mbe_row.id;
+                ELSE
+                    INSERT INTO metabib.browse_subject_entry
+                        ( value, sort_value, truncated_sort_value ) VALUES
+                        ( ashs.value, ashs.sort_value, substr(ashs.sort_value, 1, 2700) );
+
+                    mbe_id := CURRVAL('metabib.browse_subject_entry_id_seq'::REGCLASS);
+                END IF;
+
+                INSERT INTO metabib.browse_subject_entry_simple_heading_map (entry,simple_heading) VALUES (mbe_id,ash_id);
 
-        INSERT INTO metabib.browse_entry_simple_heading_map (entry,simple_heading) VALUES (mbe_id,ash_id);
+            WHEN 'title' THEN
+                SELECT INTO mbe_row * FROM metabib.browse_title_entry
+                    WHERE sort_value = ashs.sort_value
+                    ORDER BY id;
+
+                IF FOUND THEN
+                    mbe_id := mbe_row.id;
+                ELSE
+                    INSERT INTO metabib.browse_title_entry
+                        ( value, sort_value, truncated_sort_value ) VALUES
+                        ( ashs.value, ashs.sort_value, substr(ashs.sort_value, 1, 2700) );
+
+                    mbe_id := CURRVAL('metabib.browse_title_entry_id_seq'::REGCLASS);
+                END IF;
+
+                INSERT INTO metabib.browse_title_entry_simple_heading_map (entry,simple_heading) VALUES (mbe_id,ash_id);
+
+            ELSE
+                -- mainly to handle when search_class is 'keyword'
+        END CASE;
 
     END LOOP;
 
@@ -7482,8 +7582,6 @@ BEGIN
 END;
 $BODY$ LANGUAGE plpgsql;
 
-
-
 SELECT evergreen.upgrade_deps_block_check('0898', :eg_version);
 
 CREATE OR REPLACE FUNCTION unapi.mmr_mra (