From cd216c1754bacced35647b38d091fd8ce8663c66 Mon Sep 17 00:00:00 2001 From: Amit Gupta Date: Tue, 8 Oct 2013 22:01:17 +0530 Subject: [PATCH] Bug-10477 Increased flexibility for upload of Staged When uploading bulk MARC records against existing records need further option to allow the process to just add/merge extra MARC tags rather than having to completely overwrite an existing record or just leave the existing record unchanged. Test Plan: 1. Click on Tools->Stage MARC records for import. 2. Upload the MARC file for ex: first.mrc 3. Click on upload button. 4. Choose Record matching rule: 020$a ISBN. 5. Choose Action if matching record found: Append or update MARC21/UNIMARC tag a text box will appear after choosing enter MARC tag for ex: 700a which you want to merge. 6. Click on Stage for import button. 7. On the next screen click on Manage staged records. 8. You can also edit the record matching rule in manage record screen. 9. Click on Import this batch on catalog button. 10. Next screen will give no of record updated. 11. It will check duplicate field record for ex: 700$aamit is already in record it will skip the tag and move to next. Signed-off-by: Amit Gupta --- C4/ImportBatch.pm | 113 +++++++++++++++++++- .../prog/en/includes/tools-overlay-action.inc | 21 +++- .../prog/en/modules/tools/manage-marc-import.tt | 1 + tools/manage-marc-import.pl | 12 ++- tools/stage-marc-import.pl | 3 +- 5 files changed, 145 insertions(+), 5 deletions(-) diff --git a/C4/ImportBatch.pm b/C4/ImportBatch.pm index c045878..42e2b2e 100644 --- a/C4/ImportBatch.pm +++ b/C4/ImportBatch.pm @@ -77,6 +77,8 @@ BEGIN { SetImportRecordStatus GetImportRecordMatches SetImportRecordMatches + GetImportMarcAction + SetImportMarcAction ); } @@ -206,7 +208,8 @@ sub AddImportBatch { my (@fields, @vals); foreach (qw( matcher_id template_id branchcode overlay_action nomatch_action item_action - import_status batch_type file_name comments record_type )) { + import_status batch_type file_name comments record_type marctag )) { + if (exists $params->{$_}) { push @fields, $_; push @vals, $params->{$_}; @@ -220,6 +223,34 @@ sub AddImportBatch { return $dbh->{'mysql_insertid'}; } +=head2 GetImportMarcAction + + my $marctag = GetImportMarcAction($batch_id); + +=cut + +sub GetImportMarcAction { + my ($batch_id) = @_; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("SELECT * FROM import_batches WHERE import_batch_id = ?"); + $sth->execute($batch_id); + return $sth->fetchrow_hashref; +} + +=head2 SetImportMarcAction + + SetImportMarcAction($batch_id, $marctag); + +=cut + +sub SetImportMarcAction { + my ($batch_id, $marctag) = @_; + my $dbh = C4::Context->dbh; + my $sth = $dbh->prepare("UPDATE import_batches SET marctag = ? WHERE import_batch_id = ?"); + $sth->execute($marctag, $batch_id); + return; +} + =head2 GetImportBatch my $row = GetImportBatch($batch_id); @@ -334,6 +365,7 @@ sub BatchStageMarcRecords { my $branch_code = shift; my $parse_items = shift; my $leave_as_staging = shift; + my $marctag = shift; # optional callback to monitor status # of job @@ -353,6 +385,7 @@ sub BatchStageMarcRecords { file_name => $file_name, comments => $comments, record_type => $record_type, + marctag => $marctag, } ); if ($parse_items) { SetImportBatchItemAction($batch_id, 'always_add'); @@ -617,6 +650,82 @@ sub BatchCommitRecords { $sth->execute($recordid, $rowref->{'import_record_id'}); $sth->finish(); SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); + } elsif ($record_result eq 'appendmarctag') { + $num_updated++; + $recordid = $record_match; + my $tag = GetImportMarcAction($batch_id); + my $marctag = $tag->{'marctag'}; + my $mtag = substr($marctag,0,3); + my $sub = substr($marctag,3); + my $oldxml; + if ($record_type eq 'biblio') { + my ($count, $oldbiblio) = GetBiblio($recordid); + $oldxml = GetXmlBiblio($recordid); + my $old_marc = MARC::Record->new_from_xml(StripNonXmlChars($oldxml), 'UTF-8', $rowref->{'encoding'}, $marc_type); + foreach my $item_field ($old_marc->field($item_tag)) { + $old_marc->delete_field($item_field); + } + $oldxml = $old_marc->as_xml($marc_type); + + my $sth=$dbh->prepare("SELECT * FROM marc_tag_structure WHERE tagfield=? AND frameworkcode=?"); + if ($oldbiblio->{'frameworkcode'} eq '') { + $sth->execute($mtag,''); + } else { + $sth->execute($mtag,$oldbiblio->{'frameworkcode'}); + } + my $data=$sth->fetchrow_hashref; + + if ($data->{'repeatable'} == 0) { + my $new_marc = $old_marc->field($mtag); + my $newvalue = $marc_record->field($mtag)->subfield($sub); + my $oldvalue = $old_marc->field($mtag)->subfield($sub); + ##checking value if value is not duplicate update the marc tag. + if ($oldvalue ne $newvalue) { + $new_marc->delete_subfield(code => $sub); + my $new_field = $marc_record->field($mtag); + $new_field->update( $sub => $newvalue ); + $old_marc->insert_fields_ordered($new_field); + } + } elsif($data->{'repeatable'} == 1 ) { + my @value; + my $i =0; + my @oldvalue; + my $j=0; + foreach my $field_marc ($marc_record->field($mtag)) { + @value[$i] = $field_marc->subfield($sub); + $i = $i + 1; + } + foreach my $oldm ($old_marc->field($mtag)) { + @oldvalue[$j] = $oldm->subfield($sub); + $j = $j + 1; + } + for my $index (0 .. $#value) { + #checking for duplicate values + if ($value[$index] ne $oldvalue[$index]) { + my $new = MARC::Field->new($mtag,'','',$sub => $value[$index]); + $old_marc->insert_fields_ordered($new); + } + } + } + ModBiblio($old_marc, $recordid, $oldbiblio->{'frameworkcode'}); + $query = "UPDATE import_biblios SET matched_biblionumber = ? WHERE import_record_id = ?"; + + if ($item_result eq 'create_new') { + my ($bib_items_added, $bib_items_errored) = BatchCommitItems($rowref->{'import_record_id'}, $recordid); + $num_items_added = $bib_items_added; + $num_items_errored = $bib_items_errored; + } + } else { + $oldxml = GetAuthorityXML($recordid); + ModAuthority($recordid, $marc_record, GuessAuthTypeCode($marc_record)); + $query = "UPDATE import_auths SET matched_authid = ? WHERE import_record_id = ?"; + } + my $sth = $dbh->prepare("UPDATE import_records SET marcxml_old = ? WHERE import_record_id = ?"); + $sth->execute($oldxml, $rowref->{'import_record_id'}); + $sth = $dbh->prepare($query); + $sth->execute($recordid, $rowref->{'import_record_id'}); + SetImportRecordOverlayStatus($rowref->{'import_record_id'}, 'match_applied'); + SetImportRecordStatus($rowref->{'import_record_id'}, 'imported'); } elsif ($record_result eq 'replace') { $num_updated++; $recordid = $record_match; @@ -1504,6 +1613,8 @@ sub _get_commit_action { $bib_result = 'create_new'; } elsif ($overlay_action eq 'ignore') { $bib_result = 'ignore'; + } elsif ($overlay_action eq 'appendmarctag') { + $bib_result = 'appendmarctag'; } if($item_action eq 'always_add' or $item_action eq 'add_only_for_matches'){ $item_result = 'create_new'; diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/tools-overlay-action.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/tools-overlay-action.inc index 9318814..8a2f93d 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/tools-overlay-action.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/tools-overlay-action.inc @@ -1,4 +1,16 @@ - [% IF ( overlay_action_replace ) %] + [% IF ( overlay_action_appendmarctag ) %] + + diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt index e8384c1..47dc939 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/tools/manage-marc-import.tt @@ -214,6 +214,7 @@ $(document).ready(function(){ [% END %] [% END %] + [% IF ( marctag ) %]
  • MarcTag:[% marctag %]
  • [% END %]
  • [% IF ( can_commit ) %] diff --git a/tools/manage-marc-import.pl b/tools/manage-marc-import.pl index f1a6c83..3a16488 100755 --- a/tools/manage-marc-import.pl +++ b/tools/manage-marc-import.pl @@ -128,9 +128,10 @@ if ($op eq "") { my $current_matcher_id = $input->param('current_matcher_id'); my $overlay_action = $input->param('overlay_action'); my $nomatch_action = $input->param('nomatch_action'); + my $marctag = $input->param('marctag'); my $item_action = $input->param('item_action'); redo_matching($template, $import_batch_id, $new_matcher_id, $current_matcher_id, - $overlay_action, $nomatch_action, $item_action); + $overlay_action, $nomatch_action, $item_action, $marctag); import_records_list($template, $import_batch_id, $offset, $results_per_page); } @@ -139,12 +140,14 @@ output_html_with_http_headers $input, $cookie, $template->output; exit 0; sub redo_matching { - my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id, $overlay_action, $nomatch_action, $item_action) = @_; + my ($template, $import_batch_id, $new_matcher_id, $current_matcher_id, $overlay_action, $nomatch_action, $item_action, $marctag) = @_; my $rematch_failed = 0; return if not defined $new_matcher_id and not defined $current_matcher_id; my $old_overlay_action = GetImportBatchOverlayAction($import_batch_id); my $old_nomatch_action = GetImportBatchNoMatchAction($import_batch_id); my $old_item_action = GetImportBatchItemAction($import_batch_id); + my $oldtag = GetImportMarcAction($import_batch_id); + my $oldmarctag = $oldtag->{'marctag'}; return if $new_matcher_id eq $current_matcher_id and $old_overlay_action eq $overlay_action and $old_nomatch_action eq $nomatch_action and @@ -162,6 +165,9 @@ sub redo_matching { SetImportBatchItemAction($import_batch_id, $item_action); $template->param('changed_item_action' => 1); } + if ($oldmarctag ne $marctag) { + SetImportMarcAction($import_batch_id, $marctag); + } my $num_with_matches = 0; if (defined $new_matcher_id and $new_matcher_id ne "") { @@ -218,6 +224,7 @@ sub import_batches_list { comments => $batch->{'comments'}, can_clean => ($batch->{'import_status'} ne 'cleaned') ? 1 : 0, record_type => $batch->{'record_type'}, + marctag => $batch->{'marctag'}, }; } $template->param(batch_list => \@list); @@ -420,6 +427,7 @@ sub batch_info { $template->param(batch_info => 1); $template->param(file_name => $batch->{'file_name'}); $template->param(comments => $batch->{'comments'}); + $template->param(marctag => $batch->{'marctag'}); $template->param(import_status => $batch->{'import_status'}); $template->param(upload_timestamp => $batch->{'upload_timestamp'}); $template->{VARS}->{'record_type'} = $batch->{'record_type'}; diff --git a/tools/stage-marc-import.pl b/tools/stage-marc-import.pl index 8d6ab1a..2583f21 100755 --- a/tools/stage-marc-import.pl +++ b/tools/stage-marc-import.pl @@ -59,6 +59,7 @@ my $comments = $input->param('comments'); my $record_type = $input->param('record_type'); my $encoding = $input->param('encoding'); my $marc_modification_template = $input->param('marc_modification_template_id'); +my $marctag = $input->param('marctag'); my ($template, $loggedinuser, $cookie) = get_template_and_user({template_name => "tools/stage-marc-import.tmpl", @@ -134,7 +135,7 @@ if ($completedJobID) { } # FIXME branch code - my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($record_type, $encoding, $marcrecord, $filename, $marc_modification_template, $comments, '', $parse_items, 0, 50, staging_progress_callback($job, $dbh)); + my ($batch_id, $num_valid, $num_items, @import_errors) = BatchStageMarcRecords($record_type, $encoding, $marcrecord, $filename, $marc_modification_template, $comments, '', $parse_items, 0, $marctag, 50, staging_progress_callback($job, $dbh)); $dbh->commit(); -- 1.7.10.4