JBAS-1417 Vand. authority import set edit date SQL
authorBill Erickson <berickxx@gmail.com>
Wed, 1 Jun 2016 18:37:10 +0000 (14:37 -0400)
committerBill Erickson <berickxx@gmail.com>
Thu, 21 Mar 2019 19:46:23 +0000 (15:46 -0400)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
KCLS/sql/schema/deploy/vand-auth-edit-date.sql [new file with mode: 0644]
KCLS/sql/schema/revert/vand-auth-edit-date.sql [new file with mode: 0644]
KCLS/sql/schema/sqitch.plan
KCLS/sql/schema/verify/vand-auth-edit-date.sql [new file with mode: 0644]

diff --git a/KCLS/sql/schema/deploy/vand-auth-edit-date.sql b/KCLS/sql/schema/deploy/vand-auth-edit-date.sql
new file mode 100644 (file)
index 0000000..8eaafff
--- /dev/null
@@ -0,0 +1,122 @@
+-- Deploy kcls-evergreen:vand-auth-edit-date to pg
+-- requires: purge-user-activity
+
+BEGIN;
+
+CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$
+DECLARE
+    merge_profile   vandelay.merge_profile%ROWTYPE;
+    dyn_profile     vandelay.compile_profile%ROWTYPE;
+    editor_string   TEXT;
+    editor_id       INT;
+    source_marc     TEXT;
+    target_marc     TEXT;
+    eg_marc         TEXT;
+    v_marc          TEXT;
+    replace_rule    TEXT;
+    match_count     INT;
+    update_fields   TEXT[];
+    update_query    TEXT;
+BEGIN
+
+    SELECT  b.marc INTO eg_marc
+      FROM  authority.record_entry b
+            JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id)
+      LIMIT 1;
+
+    SELECT  q.marc INTO v_marc
+      FROM  vandelay.queued_record q
+            JOIN vandelay.authority_match m ON (m.queued_record = q.id AND q.id = import_id)
+      LIMIT 1;
+
+    IF eg_marc IS NULL OR v_marc IS NULL THEN
+        -- RAISE NOTICE 'no marc for vandelay or authority record';
+        RETURN FALSE;
+    END IF;
+
+    -- Extract the editor string before any modification to the vandelay
+    -- MARC occur.
+    editor_string := 
+        (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1];
+
+    dyn_profile := vandelay.compile_profile( v_marc );
+
+    IF merge_profile_id IS NOT NULL THEN
+        SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
+        IF FOUND THEN
+            dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
+            dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
+            dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
+            dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
+        END IF;
+    END IF;
+
+    IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
+        -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
+        RETURN FALSE;
+    END IF;
+
+    IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
+        --Since we have nothing to do, just return a NOOP "we did it"
+        RETURN TRUE;
+    ELSIF dyn_profile.replace_rule <> '' THEN
+        source_marc = v_marc;
+        target_marc = eg_marc;
+        replace_rule = dyn_profile.replace_rule;
+    ELSE
+        source_marc = eg_marc;
+        target_marc = v_marc;
+        replace_rule = dyn_profile.preserve_rule;
+    END IF;
+
+    UPDATE  authority.record_entry
+      SET   marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
+      WHERE id = eg_id;
+
+    IF NOT FOUND THEN 
+        -- Import/merge failed.  Nothing left to do.
+        RETURN FALSE;
+    END IF;
+
+    -- Authority record successfully merged / imported.
+
+    -- Update the vandelay record to show the successful import.
+    UPDATE  vandelay.queued_authority_record
+      SET   imported_as = eg_id,
+            import_time = NOW()
+      WHERE id = import_id;
+
+    -- If an editor value can be found, update the authority record
+    -- editor and edit_date values.
+    IF editor_string IS NOT NULL AND editor_string <> '' THEN
+
+        -- Vandelay.pm sets the value to 'usrname' when needed.  
+        SELECT id INTO editor_id 
+            FROM actor.usr WHERE usrname = editor_string;
+
+        IF editor_id IS NULL THEN
+            SELECT usr INTO editor_id 
+                FROM actor.card WHERE barcode = editor_string;
+        END IF;
+
+        IF editor_id IS NOT NULL THEN
+            --only update the edit date if we have a valid editor
+            update_fields := ARRAY_APPEND(update_fields, 
+                'editor = ' || editor_id || ', edit_date = NOW()');
+        END IF;
+    END IF;
+
+    IF ARRAY_LENGTH(update_fields, 1) > 0 THEN
+        update_query := 'UPDATE authority.record_entry SET ' || 
+            ARRAY_TO_STRING(update_fields, ',') || 
+            ' WHERE id = ' || eg_id || ';';
+        --RAISE NOTICE 'query: %', update_query;
+        EXECUTE update_query;
+    END IF;
+
+    RETURN TRUE;
+
+END;
+$$ LANGUAGE PLPGSQL;
+
+COMMIT;
diff --git a/KCLS/sql/schema/revert/vand-auth-edit-date.sql b/KCLS/sql/schema/revert/vand-auth-edit-date.sql
new file mode 100644 (file)
index 0000000..ea13eef
--- /dev/null
@@ -0,0 +1,84 @@
+-- Revert kcls-evergreen:vand-auth-edit-date from pg
+
+BEGIN;
+
+CREATE FUNCTION overlay_authority_record(import_id bigint, eg_id bigint, merge_profile_id integer) RETURNS boolean
+    LANGUAGE plpgsql
+    AS $$
+ DECLARE
+     merge_profile   vandelay.merge_profile%ROWTYPE;
+     dyn_profile     vandelay.compile_profile%ROWTYPE;
+     source_marc     TEXT;
+     target_marc     TEXT;
+     eg_marc         TEXT;
+     v_marc          TEXT;
+     replace_rule    TEXT;
+     match_count     INT;
+ BEGIN
+     SELECT  b.marc INTO eg_marc
+       FROM  authority.record_entry b
+             JOIN vandelay.authority_match m ON (m.eg_record = b.id AND m.queued_record = import_id)
+       LIMIT 1;
+     SELECT  q.marc INTO v_marc
+       FROM  vandelay.queued_record q
+             JOIN vandelay.authority_match m ON (m.queued_record = q.id AND q.id = import_id)
+       LIMIT 1;
+     IF eg_marc IS NULL OR v_marc IS NULL THEN
+         -- RAISE NOTICE 'no marc for vandelay or authority record';
+         RETURN FALSE;
+     END IF;
+     dyn_profile := vandelay.compile_profile( v_marc );
+     IF merge_profile_id IS NOT NULL THEN
+         SELECT * INTO merge_profile FROM vandelay.merge_profile WHERE id = merge_profile_id;
+         IF FOUND THEN
+             dyn_profile.add_rule := BTRIM( dyn_profile.add_rule || ',' || COALESCE(merge_profile.add_spec,''), ',');
+             dyn_profile.strip_rule := BTRIM( dyn_profile.strip_rule || ',' || COALESCE(merge_profile.strip_spec,''), ',');
+             dyn_profile.replace_rule := BTRIM( dyn_profile.replace_rule || ',' || COALESCE(merge_profile.replace_spec,''), ',');
+             dyn_profile.preserve_rule := BTRIM( dyn_profile.preserve_rule || ',' || COALESCE(merge_profile.preserve_spec,''), ',');
+         END IF;
+     END IF;
+     IF dyn_profile.replace_rule <> '' AND dyn_profile.preserve_rule <> '' THEN
+         -- RAISE NOTICE 'both replace [%] and preserve [%] specified', dyn_profile.replace_rule, dyn_profile.preserve_rule;
+         RETURN FALSE;
+     END IF;
+     IF dyn_profile.replace_rule = '' AND dyn_profile.preserve_rule = '' AND dyn_profile.add_rule = '' AND dyn_profile.strip_rule = '' THEN
+         --Since we have nothing to do, just return a NOOP "we did it"
+         RETURN TRUE;
+     ELSIF dyn_profile.replace_rule <> '' THEN
+         source_marc = v_marc;
+         target_marc = eg_marc;
+         replace_rule = dyn_profile.replace_rule;
+     ELSE
+         source_marc = eg_marc;
+         target_marc = v_marc;
+         replace_rule = dyn_profile.preserve_rule;
+     END IF;
+     UPDATE  authority.record_entry
+       SET   marc = vandelay.merge_record_xml( target_marc, source_marc, dyn_profile.add_rule, replace_rule, dyn_profile.strip_rule )
+       WHERE id = eg_id;
+     IF FOUND THEN
+         UPDATE  vandelay.queued_authority_record
+           SET   imported_as = eg_id,
+                 import_time = NOW()
+           WHERE id = import_id;
+         RETURN TRUE;
+     END IF;
+     -- RAISE NOTICE 'update of authority.record_entry failed';
+     RETURN FALSE;
+ END;
+$$ LANGUAGE PLPGSQL;
+
+COMMIT;
index cf3e77d..a17cdbc 100644 (file)
@@ -26,3 +26,4 @@ org-unit-addrs-copy [sip-activity-types] 2016-05-03T14:17:59Z Bill Erickson <ber
 drop-cc-cols [sip-activity-types] 2016-05-03T15:26:50Z Bill Erickson <berickxx@gmail.com> # Drop unneeded CC payment columns
 connexion-auth-imports [sip-activity-types] 2016-05-11T15:10:49Z Bill Erickson,,, <berick@teapot> # OCLC Connexion Authority Record Imports Data
 purge-user-activity [sip-activity-types] 2016-04-29T17:07:46Z Bill Erickson <berickxx@gmail.com> # Clean up actor.usr_activity
+vand-auth-edit-date [purge-user-activity] 2016-06-01T18:24:54Z Bill Erickson <berickxx@gmail.com> # Vandelay authority import sets edit[or|_date]
diff --git a/KCLS/sql/schema/verify/vand-auth-edit-date.sql b/KCLS/sql/schema/verify/vand-auth-edit-date.sql
new file mode 100644 (file)
index 0000000..7535d38
--- /dev/null
@@ -0,0 +1,7 @@
+-- Verify kcls-evergreen:vand-auth-edit-date on pg
+
+BEGIN;
+
+-- XXX Add verifications here.
+
+ROLLBACK;