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