improved import batches part 2 -- replace use of marc_breeding
[koha.git] / C4 / ImportBatch.pm
1 package C4::ImportBatch;
2
3 # Copyright (C) 2007 LibLime
4 #
5 # This file is part of Koha.
6 #
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
10 # version.
11 #
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.
15 #
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
19
20 use strict;
21 use C4::Context;
22 use C4::Koha;
23 use C4::Biblio;
24 require Exporter;
25
26
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29 # set the version for version checking
30 $VERSION = 3.00;
31
32 =head1 NAME
33
34 C4::ImportBatch - manage batches of imported MARC records
35
36 =head1 SYNOPSIS
37
38 =over 4
39
40 use C4::ImportBatch;
41
42 =back
43
44 =head1 FUNCTIONS
45
46 =cut
47
48 @ISA    = qw(Exporter);
49 @EXPORT = qw(
50     GetZ3950BatchId
51     GetImportRecordMarc
52     AddImportBatch
53     AddBiblioToBatch
54     ModBiblioInBatch
55 );
56
57 =head2 GetZ3950BatchId
58
59 =over 4
60
61 my $batchid = GetZ3950BatchId($z3950server);
62
63 =back
64
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.
68
69 =cut
70
71 sub GetZ3950BatchId {
72     my ($z3950server) = @_;
73
74     my $dbh = C4::Context->dbh;
75     my $sth = $dbh->prepare("SELECT import_batch_id FROM import_batches
76                              WHERE  batch_type = 'z3950'
77                              AND    file_name = ?");
78     $sth->execute($z3950server);
79     my $rowref = $sth->fetchrow_arrayref();
80     $sth->finish();
81     if (defined $rowref) {
82         return $rowref->[0];
83     } else {
84         my $batch_id = AddImportBatch('create_new', 'staged', 'z3950', $z3950server, '');
85         return $batch_id;
86     }
87     
88 }
89
90 =head2 GetImportRecordMarc
91
92 =over4
93
94 my ($marcblob, $encoding) = GetImportRecordMarc($import_record_id);
95
96 =back
97
98 =cut
99
100 sub GetImportRecordMarc {
101     my ($import_record_id) = @_;
102
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();
107     $sth->finish();
108     return $marc;
109
110 }
111
112 =head2 AddImportBatch
113
114 =over 4
115
116 my $batch_id = AddImportBatch($overlay_action, $import_status, $type, $file_name, $comments);
117
118 =back
119
120 =cut
121
122 sub AddImportBatch {
123     my ($overlay_action, $import_status, $type, $file_name, $comments) = @_;
124
125     my $dbh = C4::Context->dbh;
126     my $sth = $dbh->prepare("INSERT INTO import_batches (overlay_action, import_status, batch_type,
127                                                          file_name, comments)
128                                     VALUES (?, ?, ?, ?, ?)");
129     $sth->execute($overlay_action, $import_status, $type, $file_name, $comments);
130     my $batch_id = $dbh->{'mysql_insertid'};
131     $sth->finish();
132
133     return $batch_id;
134
135 }
136
137 =head2 AddBiblioToBatch 
138
139 =over 4
140
141 my $import_record_id = AddBiblioToBatch($batch_id, $record_sequence, $marc_record, $encoding, $z3950random);
142
143 =cut
144
145 sub AddBiblioToBatch {
146     my ($batch_id, $record_sequence, $marc_record, $encoding, $z3950random) = @_;
147
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;
151 }
152
153 =head2 ModBiblioInBatch
154
155 =over 4
156
157 ModBiblioInBatch($import_record_id, $marc_record);
158
159 =cut
160
161 sub ModBiblioInBatch {
162     my ($import_record_id, $marc_record) = @_;
163
164     _update_import_record_marc($import_record_id, $marc_record);
165     _update_biblio_fields($import_record_id, $marc_record);
166
167 }
168
169 # internal functions
170
171 sub _create_import_record {
172     my ($batch_id, $record_sequence, $marc_record, $record_type, $encoding, $z3950random) = @_;
173     
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'};
181     $sth->finish();
182     return $import_record_id;
183 }
184
185 sub _update_import_record_marc {
186     my ($import_record_id, $marc_record) = @_;
187
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);
192     $sth->finish();
193 }
194
195 sub _add_biblio_fields {
196     my ($import_record_id, $marc_record) = @_;
197
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
202     $isbn =~ s/\(.*$//;
203     $isbn =~ tr/ -_//;  
204     $isbn = uc $isbn;
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);
207     $sth->finish();
208                 
209 }
210
211 sub _update_biblio_fields {
212     my ($import_record_id, $marc_record) = @_;
213
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
218     $isbn =~ s/\(.*$//;
219     $isbn =~ tr/ -_//;
220     $isbn = uc $isbn;
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);
224     $sth->finish();
225 }
226
227 sub _parse_biblio_fields {
228     my ($marc_record) = @_;
229
230     my $dbh = C4::Context->dbh;
231     my $bibliofields = TransformMarcToKoha($dbh, $marc_record, '');
232     return ($bibliofields->{'title'}, $bibliofields->{'author'}, $bibliofields->{'isbn'}, $bibliofields->{'issn'});
233
234 }
235
236 1;
237
238 =head1 AUTHOR
239
240 Koha Development Team <info@koha.org>
241
242 Galen Charlton <galen.charlton@liblime.com>
243
244 =cut