From: Bill Erickson Date: Wed, 1 Jun 2016 14:40:45 +0000 (-0400) Subject: LP#1587639 Vandelay authority update editor/edit_date X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=43f06ef45bde83713b4b2700692dcc5606fb2a91;p=evergreen%2Fmasslnc.git LP#1587639 Vandelay authority update editor/edit_date Update the editor and edit_date of authority records when modified by Vandelay merge/overlay. Signed-off-by: Bill Erickson Signed-off-by: Mike Rylander --- diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm index e53729dcc1..39c857f209 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Vandelay.pm @@ -990,17 +990,17 @@ sub import_record_list_impl { my $marcdoc = XML::LibXML->new->parse_string($rec->marc); $rec->marc($U->strip_marc_fields($e, $marcdoc, $strip_grps)); + } - # Set the imported record's 905$u, so - # editor/creator/edit_date are set correctly. - $marcdoc = XML::LibXML->new->parse_string($rec->marc); - $rec->marc($U->set_marc_905u($marcdoc, $requestor->usrname)); + # Set the imported record's 905$u, so + # editor/edit_date are set correctly. + my $marcdoc = XML::LibXML->new->parse_string($rec->marc); + $rec->marc($U->set_marc_905u($marcdoc, $requestor->usrname)); - unless ($e->$update_func($rec)) { - $$report_args{evt} = $e->die_event; - finish_rec_import_attempt($report_args); - next; - } + unless ($e->$update_func($rec)) { + $$report_args{evt} = $e->die_event; + finish_rec_import_attempt($report_args); + next; } if(defined $overlay_target) { diff --git a/Open-ILS/src/sql/Pg/012.schema.vandelay.sql b/Open-ILS/src/sql/Pg/012.schema.vandelay.sql index 6c9325d96b..2e5c248038 100644 --- a/Open-ILS/src/sql/Pg/012.schema.vandelay.sql +++ b/Open-ILS/src/sql/Pg/012.schema.vandelay.sql @@ -1785,12 +1785,16 @@ CREATE OR REPLACE FUNCTION vandelay.overlay_authority_record ( import_id BIGINT, 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 @@ -1808,6 +1812,11 @@ BEGIN 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 @@ -1842,17 +1851,48 @@ BEGIN 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; + IF NOT FOUND THEN + -- Import/merge failed. Nothing left to do. + RETURN FALSE; END IF; - -- RAISE NOTICE 'update of authority.record_entry failed'; + -- Authority record successfully merged / imported. - RETURN FALSE; + -- 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; diff --git a/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.authority-vandeley-edit-date.sql b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.authority-vandeley-edit-date.sql new file mode 100644 index 0000000000..a8b3a91ca9 --- /dev/null +++ b/Open-ILS/src/sql/Pg/upgrade/XXXX.schema.authority-vandeley-edit-date.sql @@ -0,0 +1,123 @@ +BEGIN; + +-- SELECT evergreen.upgrade_deps_block_check('XXXX', :eg_version); + +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; +