1 package C4::ImportBatch;
3 # Copyright (C) 2007 LibLime
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA 02111-1307 USA
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
29 # set the version for version checking
34 C4::ImportBatch - manage batches of imported MARC records
57 =head2 GetZ3950BatchId
61 my $batchid = GetZ3950BatchId($z3950server);
65 Retrieves the ID of the import batch for the Z39.50
66 reservoir for the given target. If necessary,
67 creates the import batch.
72 my ($z3950server) = @_;
74 my $dbh = C4::Context->dbh;
75 my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
76 WHERE batch_type = 'z3950'
78 $sth->execute($z3950server);
79 my $rowref = $sth->fetchrow_arrayref();
81 if (defined $rowref) {
84 my $batch_id = AddImportBatch('create_new', 'staged', 'z3950', $z3950server, '');
90 =head2 GetImportRecordMarc
94 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
100 sub GetImportRecordMarc {
101 my ($import_record_id) = @_;
103 my $dbh = C4::Context->dbh;
104 my $sth = $dbh->prepare("SELECT marc, encoding FROM import_records WHERE import_record_id = ?");
105 $sth->execute($import_record_id);
106 my ($marc, $encoding) = $sth->fetchrow();
112 =head2 AddImportBatch
116 my $batch_id = AddImportBatch($overlay_action, $import_status, $type, $file_name, $comments);
123 my ($overlay_action, $import_status, $type, $file_name, $comments) = @_;
125 my $dbh = C4::Context->dbh;
126 my $sth = $dbh->prepare("INSERT INTO import_batches (overlay_action, import_status, batch_type,
128 VALUES (?, ?, ?, ?, ?)");
129 $sth->execute($overlay_action, $import_status, $type, $file_name, $comments);
130 my $batch_id = $dbh->{'mysql_insertid'};
137 =head2 AddBiblioToBatch
141 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence, $marc_record, $encoding, $z3950random);
145 sub AddBiblioToBatch {
146 my ($batch_id, $record_sequence, $marc_record, $encoding, $z3950random) = @_;
148 my $import_record_id = _create_import_record($batch_id, $record_sequence, $marc_record, 'bib', $encoding, $z3950random);
149 _add_biblio_fields($import_record_id, $marc_record);
150 return $import_record_id;
153 =head2 ModBiblioInBatch
157 ModBiblioInBatch($import_record_id, $marc_record);
161 sub ModBiblioInBatch {
162 my ($import_record_id, $marc_record) = @_;
164 _update_import_record_marc($import_record_id, $marc_record);
165 _update_biblio_fields($import_record_id, $marc_record);
171 sub _create_import_record {
172 my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
174 my $dbh = C4::Context->dbh;
175 my $sth = $dbh->prepare("INSERT INTO import_records (import_batch_id, record_sequence, marc, marcxml,
176 record_type, encoding, z3950random)
177 VALUES (?, ?, ?, ?, ?, ?, ?)");
178 $sth->execute($batch_id, $record_sequence, $marc_record->as_usmarc(), $marc_record->as_xml(),
179 $record_type, $encoding, $z3950random);
180 my $import_record_id = $dbh->{'mysql_insertid'};
182 return $import_record_id;
185 sub _update_import_record_marc {
186 my ($import_record_id, $marc_record) = @_;
188 my $dbh = C4::Context->dbh;
189 my $sth = $dbh->prepare("UPDATE import_records SET marc = ?, marcxml = ?
190 WHERE import_record_id = ?");
191 $sth->execute($marc_record->as_usmarc(), $marc_record->as_xml(), $import_record_id);
195 sub _add_biblio_fields {
196 my ($import_record_id, $marc_record) = @_;
198 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
199 my $dbh = C4::Context->dbh;
200 # FIXME no controlnumber, originalsource
201 # FIXME 2 - should regularize normalization of ISBN wherever it is done
205 my $sth = $dbh->prepare("INSERT INTO import_biblios (import_record_id, title, author, isbn, issn) VALUES (?, ?, ?, ?, ?)");
206 $sth->execute($import_record_id, $title, $author, $isbn, $issn);
211 sub _update_biblio_fields {
212 my ($import_record_id, $marc_record) = @_;
214 my ($title, $author, $isbn, $issn) = _parse_biblio_fields($marc_record);
215 my $dbh = C4::Context->dbh;
216 # FIXME no controlnumber, originalsource
217 # FIXME 2 - should regularize normalization of ISBN wherever it is done
221 my $sth = $dbh->prepare("UPDATE import_biblios SET title = ?, author = ?, isbn = ?, issn = ?
222 WHERE import_record_id = ?");
223 $sth->execute($title, $author, $isbn, $issn, $import_record_id);
227 sub _parse_biblio_fields {
228 my ($marc_record) = @_;
230 my $dbh = C4::Context->dbh;
231 my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
232 return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
240 Koha Development Team <info@koha.org>
242 Galen Charlton <galen.charlton@liblime.com>