more work on batch import
[koha.git] / C4 / Breeding.pm
1 package C4::Breeding;
2
3 # Copyright 2000-2002 Katipo Communications
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::Biblio;
22 use C4::Koha;
23 use MARC::File::USMARC;
24 use C4::ImportBatch;
25 require Exporter;
26
27 use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
28
29 # set the version for version checking
30 $VERSION = 0.01;
31
32 =head1 NAME
33
34 C4::Breeding : module to add biblios to import_records via
35                the breeding/reservoir API.
36
37 =head1 SYNOPSIS
38
39     use C4::Scan;
40     &ImportBreeding($marcrecords,$overwrite_biblio,$filename,$z3950random,$batch_type);
41
42     C<$marcrecord> => the MARC::Record
43     C<$overwrite_biblio> => if set to 1 a biblio with the same ISBN will be overwritted.
44                                 if set to 0 a biblio with the same isbn will be ignored (the previous will be kept)
45                                 if set to -1 the biblio will be added anyway (more than 1 biblio with the same ISBN 
46                                 possible in the breeding
47     C<$encoding> => USMARC
48                         or UNIMARC. used for char_decoding.
49                         If not present, the parameter marcflavour is used instead
50     C<$z3950random> => the random value created during a z3950 search result.
51
52 =head1 DESCRIPTION
53
54     ImportBreeding import MARC records in the reservoir (import_records/import_batches tables).
55     the records can be properly encoded or not, we try to reencode them in utf-8 if needed.
56     works perfectly with BNF server, that sends UNIMARC latin1 records. Should work with other servers too.
57     the FixEncoding sub is in Koha.pm, as it's a general usage sub.
58
59 =cut
60
61 @ISA = qw(Exporter);
62 @EXPORT = qw(&ImportBreeding &BreedingSearch);
63
64 =head2 ImportBreeding
65
66         ImportBreeding($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type);
67
68         TODO description
69
70 =cut
71
72 sub ImportBreeding {
73     my ($marcrecords,$overwrite_biblio,$filename,$encoding,$z3950random,$batch_type) = @_;
74     my @marcarray = split /\x1D/, $marcrecords;
75     
76     my $dbh = C4::Context->dbh;
77     
78     my $batch_id = 0;
79     if ($batch_type eq 'z3950') {
80         $batch_id = GetZ3950BatchId($filename);
81     } else {
82         # create a new one
83         # FIXME - handle comments
84         $batch_id = AddImportBatch('create_new', 'staging', 'batch', $filename, '');
85     }
86     my $searchisbn = $dbh->prepare("select biblioitemnumber from biblioitems where isbn=?");
87     my $searchissn = $dbh->prepare("select biblioitemnumber from biblioitems where issn=?");
88     # FIXME -- not sure that this kind of checking is actually needed
89     my $searchbreeding = $dbh->prepare("select import_record_id from import_biblios where isbn=? and title=?");
90     
91     $encoding = C4::Context->preference("marcflavour") unless $encoding;
92     # fields used for import results
93     my $imported=0;
94     my $alreadyindb = 0;
95     my $alreadyinfarm = 0;
96     my $notmarcrecord = 0;
97     my $breedingid;
98     for (my $i=0;$i<=$#marcarray;$i++) {
99         my $marcrecord = FixEncoding($marcarray[$i]."\x1D");
100        
101         # FIXME - currently this does nothing 
102         my @warnings = $marcrecord->warnings();
103         
104         if (scalar($marcrecord->fields()) == 0) {
105             $notmarcrecord++;
106         } else {
107             my $oldbiblio = TransformMarcToKoha($dbh,$marcrecord,'');
108             # if isbn found and biblio does not exist, add it. If isbn found and biblio exists, 
109             # overwrite or ignore depending on user choice
110             # drop every "special" char : spaces, - ...
111             $oldbiblio->{isbn} =~ s/\(.*$//;
112             $oldbiblio->{isbn} =~ tr/ -_//;
113             $oldbiblio->{isbn} = uc $oldbiblio->{isbn}; 
114             # search if biblio exists
115             my $biblioitemnumber;
116             if ($oldbiblio->{isbn}) {
117                 $searchisbn->execute($oldbiblio->{isbn});
118                 ($biblioitemnumber) = $searchisbn->fetchrow;
119             } else {
120                 if ($oldbiblio->{issn}) {
121                     $searchissn->execute($oldbiblio->{issn});
122                         ($biblioitemnumber) = $searchissn->fetchrow;
123                 }
124             }
125             if ($biblioitemnumber) {
126                 $alreadyindb++;
127             } else {
128                 # FIXME - in context of batch load,
129                 # rejecting records because already present in the reservoir
130                 # not correct in every case.
131                 # search in breeding farm
132                 if ($oldbiblio->{isbn}) {
133                     $searchbreeding->execute($oldbiblio->{isbn},$oldbiblio->{title});
134                     ($breedingid) = $searchbreeding->fetchrow;
135                 } elsif ($oldbiblio->{issn}){
136                     $searchbreeding->execute($oldbiblio->{issn},$oldbiblio->{title});
137                     ($breedingid) = $searchbreeding->fetchrow;
138                 }
139                 if ($breedingid && $overwrite_biblio eq '0') {
140                     $alreadyinfarm++;
141                 } else {
142                     if ($breedingid && $overwrite_biblio eq '1') {
143                         ModBiblioInBatch($breedingid, $marcrecord);
144                     } else {
145                         my $import_id = AddBiblioToBatch($batch_id, $imported, $marcrecord, $encoding, $z3950random);
146                         $breedingid = $import_id;
147                     }
148                     $imported++;
149                 }
150             }
151         }
152     }
153     return ($notmarcrecord,$alreadyindb,$alreadyinfarm,$imported,$breedingid);
154 }
155
156
157 =head2 BreedingSearch
158
159 ($count, @results) = &BreedingSearch($title,$isbn,$random);
160 C<$title> contains the title,
161 C<$isbn> contains isbn or issn,
162 C<$random> contains the random seed from a z3950 search.
163
164 C<$count> is the number of items in C<@results>. C<@results> is an
165 array of references-to-hash; the keys are the items from the C<import_records> and
166 C<import_biblios> tables of the Koha database.
167
168 =cut
169
170 sub BreedingSearch {
171     my ($title,$isbn,$z3950random) = @_;
172     my $dbh   = C4::Context->dbh;
173     my $count = 0;
174     my ($query,@bind);
175     my $sth;
176     my @results;
177
178     $query = "SELECT import_record_id, file_name, isbn, title, author
179               FROM  import_biblios 
180               JOIN import_records USING (import_record_id)
181               JOIN import_batches USING (import_batch_id)
182               WHERE ";
183     if ($z3950random) {
184         $query .= "z3950random = ?";
185         @bind=($z3950random);
186     } else {
187         @bind=();
188         if ($title) {
189             $query .= "title like ?";
190             push(@bind,"$title%");
191         }
192         if ($title && $isbn) {
193             $query .= " and ";
194         }
195         if ($isbn) {
196             $query .= "isbn like ?";
197             push(@bind,"$isbn%");
198         }
199     }
200     $sth   = $dbh->prepare($query);
201     $sth->execute(@bind);
202     while (my $data = $sth->fetchrow_hashref) {
203             $results[$count] = $data;
204             # FIXME - hack to reflect difference in name 
205             # of columns in old marc_breeding and import_records
206             # There needs to be more separation between column names and 
207             # field names used in the templates </soapbox>
208             $data->{'file'} = $data->{'file_name'};
209             $data->{'id'} = $data->{'import_record_id'};
210             $count++;
211     } # while
212
213     $sth->finish;
214     return($count, @results);
215 } # sub breedingsearch
216
217
218 END { }       # module clean-up code here (global destructor)