From cf2aeda73ea429c26ff6a224320104d01816ac46 Mon Sep 17 00:00:00 2001 From: Remington Steed Date: Mon, 13 Oct 2014 12:22:32 -0400 Subject: [PATCH] LP#957466: Update editor/edit_date/source on overlay This commit does three things. 1. It removes some declared variables that are never used in this function. 2. It updates the bib record edit_date field (along with the editor) if an editor is found in the MARC. If an editor is not found (or doesn't match an Evergreen user), it seems best to leave the edit_date unchanged so as not to imply that the previous editor is responsible for the newest edit. 3. If a bib source is chosen in the vandelay importer UI, it updates the bib record with the source. To access this field, the reference to table "queued_record" is replaced by its child table "queued_bib_record". Since the new table is a child of the other, all of the other needed values are still available. Signed-off-by: Remington Steed Signed-off-by: Jason Stephenson Signed-off-by: Martha Driscoll Signed-off-by: Dan Wells --- Open-ILS/src/sql/Pg/012.schema.vandelay.sql | 26 +++++--- ...X.function.vandelay-overlay_bib_record.sql | 66 +++++++++++++++++++ 2 files changed, 83 insertions(+), 9 deletions(-) create mode 100644 Open-ILS/src/sql/Pg/upgrade/XXXX.function.vandelay-overlay_bib_record.sql diff --git a/Open-ILS/src/sql/Pg/012.schema.vandelay.sql b/Open-ILS/src/sql/Pg/012.schema.vandelay.sql index 2ca0fcc074..67aab22b85 100644 --- a/Open-ILS/src/sql/Pg/012.schema.vandelay.sql +++ b/Open-ILS/src/sql/Pg/012.schema.vandelay.sql @@ -1415,19 +1415,16 @@ $$ LANGUAGE SQL; CREATE OR REPLACE FUNCTION vandelay.overlay_bib_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; + v_bib_source INT; + update_fields TEXT[]; + update_query TEXT; BEGIN - SELECT q.marc INTO v_marc - FROM vandelay.queued_record q + SELECT q.marc, q.bib_source INTO v_marc, v_bib_source + FROM vandelay.queued_bib_record q JOIN vandelay.bib_match m ON (m.queued_record = q.id AND q.id = import_id) LIMIT 1; @@ -1452,10 +1449,21 @@ BEGIN END IF; IF editor_id IS NOT NULL THEN - UPDATE biblio.record_entry SET editor = editor_id WHERE id = eg_id; + --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 v_bib_source IS NOT NULL THEN + update_fields := ARRAY_APPEND(update_fields, 'source = ' || v_bib_source); + END IF; + + IF ARRAY_LENGTH(update_fields, 1) > 0 THEN + update_query := 'UPDATE biblio.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 IF; diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.function.vandelay-overlay_bib_record.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.function.vandelay-overlay_bib_record.sql new file mode 100644 index 0000000000..82dda298ee --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.function.vandelay-overlay_bib_record.sql @@ -0,0 +1,66 @@ +BEGIN; + +--SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version); + +CREATE OR REPLACE FUNCTION vandelay.overlay_bib_record ( import_id BIGINT, eg_id BIGINT, merge_profile_id INT ) RETURNS BOOL AS $$ +DECLARE + editor_string TEXT; + editor_id INT; + v_marc TEXT; + v_bib_source INT; + update_fields TEXT[]; + update_query TEXT; +BEGIN + + SELECT q.marc, q.bib_source INTO v_marc, v_bib_source + FROM vandelay.queued_bib_record q + JOIN vandelay.bib_match m ON (m.queued_record = q.id AND q.id = import_id) + LIMIT 1; + + IF v_marc IS NULL THEN + -- RAISE NOTICE 'no marc for vandelay or bib record'; + RETURN FALSE; + END IF; + + IF vandelay.template_overlay_bib_record( v_marc, eg_id, merge_profile_id) THEN + UPDATE vandelay.queued_bib_record + SET imported_as = eg_id, + import_time = NOW() + WHERE id = import_id; + + editor_string := (oils_xpath('//*[@tag="905"]/*[@code="u"]/text()',v_marc))[1]; + + IF editor_string IS NOT NULL AND editor_string <> '' THEN + SELECT usr INTO editor_id FROM actor.card WHERE barcode = editor_string; + + IF editor_id IS NULL THEN + SELECT id INTO editor_id FROM actor.usr WHERE usrname = 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 v_bib_source IS NOT NULL THEN + update_fields := ARRAY_APPEND(update_fields, 'source = ' || v_bib_source); + END IF; + + IF ARRAY_LENGTH(update_fields, 1) > 0 THEN + update_query := 'UPDATE biblio.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 IF; + + -- RAISE NOTICE 'update of biblio.record_entry failed'; + + RETURN FALSE; + +END; +$$ LANGUAGE PLPGSQL; + +COMMIT; -- 2.43.2