]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm
tada! the new worm
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / CDBI.pm
1 package OpenILS::Application::Storage::CDBI;
2 use base qw/Class::DBI/;
3 use Class::DBI;
4 use Class::DBI::AbstractSearch;
5
6 use OpenILS::Application::Storage::CDBI::actor;
7 use OpenILS::Application::Storage::CDBI::action;
8 use OpenILS::Application::Storage::CDBI::asset;
9 use OpenILS::Application::Storage::CDBI::authority;
10 use OpenILS::Application::Storage::CDBI::biblio;
11 use OpenILS::Application::Storage::CDBI::config;
12 use OpenILS::Application::Storage::CDBI::metabib;
13 use OpenILS::Application::Storage::CDBI::money;
14 use OpenILS::Application::Storage::CDBI::permission;
15
16 use JSON;
17 use OpenSRF::Utils::Logger;
18 use OpenSRF::EX qw/:try/;
19
20 our $VERSION = undef;
21 my $log = 'OpenSRF::Utils::Logger';
22
23 sub child_init {
24         my $self = shift;
25
26         $log->debug("Creating ImaDBI Querys", DEBUG);
27         __PACKAGE__->set_sql( 'OILSFastSearch', <<"     SQL", 'Main');
28                 SELECT  %s
29                   FROM  %s
30                   WHERE %s = ?
31         SQL
32
33         __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<"  SQL", 'Main');
34                 SELECT  %s
35                   FROM  %s
36                   WHERE %s LIKE ?
37                   ORDER BY %s
38         SQL
39
40         __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<"      SQL", 'Main');
41                 SELECT  %s
42                   FROM  %s
43                   WHERE %s = ?
44                   ORDER BY %s
45         SQL
46
47         $log->debug("Calling Driver child_init", DEBUG);
48         $self->SUPER::child_init(@_);
49
50 }
51
52 sub fast_flesh_sth {
53         my $class = shift;
54         $class = ref($class) || $class;
55
56         my $field = shift;
57         my $value = shift;
58         my $order = shift;
59         my $like = shift;
60
61
62         if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
63                 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
64                         $order = $value;
65                         $value = undef;
66                 } else {
67                         $order = { order_by => $class->columns('Primary') }
68                 }
69         }
70
71         unless (defined $value) {
72                 $value = $field;
73                 ($field) = $class->columns('Primary');
74         }
75
76         unless (defined $field) {
77                 ($field) = $class->columns('Primary');
78         }
79
80         unless ($order->{order_by}) {
81                 $order = { order_by => $class->columns('Primary') }
82         }
83
84         my $fm_class = 'Fieldmapper::'.$class;
85         my $field_list = join ',', $class->columns('All');
86         
87         my $sth;
88         if (!$like) {
89                 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
90         } else {
91                 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
92         }
93         $sth->execute($value);
94         return $sth;
95 }
96
97 sub fast_flesh {
98         my $self = shift;
99         return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
100 }
101
102 sub fast_fieldmapper {
103         my $self = shift;
104         my $id = shift;
105         my $col = shift;
106         my $like = shift;
107         my $options = shift;
108         my $class = ref($self) || $self;
109         my $fm_class = 'Fieldmapper::'.$class;
110         my @fms;
111         $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
112         if ($like < 2) {
113                 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
114                         my $fm = $fm_class->new;
115                         for my $field ( $fm_class->real_fields ) {
116                                 $fm->$field( $$hash{$field} );
117                         }
118                         push @fms, $fm;
119                 }
120         } else {
121                 my $search_type = 'search';
122                 if ($like == 2) {
123                         $search_type = 'search_fts'
124                 } elsif ($like == 3) {
125                         $search_type = 'search_regex'
126                 }
127
128                 for my $obj ($class->$search_type({ $col => $id}, $options)) {
129                         push @fms, $obj->to_fieldmapper;
130                 }
131         }
132         return @fms;
133 }
134
135 sub retrieve {
136         my $self = shift;
137         my $arg = shift;
138         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
139                 my ($col) = $self->primary_column;
140                 $log->debug("Using field $col as the primary key", INTERNAL);
141                 $arg = $arg->$col;
142         }
143         $log->debug("Retrieving $self with $arg", INTERNAL);
144         my $rec;
145         try {
146                 $rec = $self->SUPER::retrieve("$arg");
147         } catch Error with {
148                 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
149                 return undef;
150         };
151         return $rec;
152 }
153
154 sub to_fieldmapper {
155         my $obj = shift;
156         my $class = ref($obj) || $obj;
157
158         my $fm_class = 'Fieldmapper::'.$class;
159         my $fm = $fm_class->new;
160
161         if (ref($obj)) {
162                 for my $field ( $fm->real_fields ) {
163                         $fm->$field( $obj->$field );
164                 }
165         }
166
167         return $fm;
168 }
169
170 sub merge {
171         my $self = shift;
172         my $search = shift;
173         my $arg = shift;
174
175         delete $$arg{$_} for (keys %$search);
176
177         $log->debug("CDBI->merge: \$search is $search (".ref($search)." : ".join(',',map{"$_ => $$search{$_}"}keys(%$search)).")",DEBUG);
178         $log->debug("CDBI->merge: \$arg is $arg (".ref($arg)." : ".join(',',map{"$_ => $$arg{$_}"}keys(%$arg)).")",DEBUG);
179
180         my @objs = ($self);
181         @objs = $self->search_where($search) unless (ref $self);
182
183         if (@objs == 1) {
184                 return $objs[0]->update($arg);
185         } elsif (@objs == 0) {
186                 return $self->create({%$search,%$arg});
187         } else {
188                 throw OpenSRF::EX::WARN ("Non-unique search key for merge.  Perhaps you meant to use remote_update?");
189         }
190 }
191
192 sub remote_update {
193         my $self = shift;
194         my $search = shift;
195         my $arg = shift;
196
197         delete $$arg{$_} for (keys %$search);
198
199         $log->debug("CDBI->remote_update: \$search is $search (".ref($search)." : ".join(',',map{"$_ => $$search{$_}"}keys(%$search)).")",DEBUG);
200         $log->debug("CDBI->remote_update: \$arg is $arg (".ref($arg)." : ".join(',',map{"$_ => $$arg{$_}"}keys(%$arg)).")",DEBUG);
201
202         my @finds = sort keys %$search;
203         my @sets = sort keys %$arg;
204
205         my @find_vals = @$search{@finds};
206         my @set_vals = @$arg{@sets};
207
208         my $sql = 'UPDATE %s SET %s WHERE %s';
209
210         my $table = $self->table;
211         my $set = join(', ', map { "$_=?" } @sets);
212         my $where = join(', ', map { "$_=?" } @finds);
213
214         my $sth = $self->db_Main->prepare(sprintf($sql, $table, $set, $where));
215         $sth->execute(@set_vals,@find_vals);
216         return $sth->rows;
217
218 }
219
220 sub create {
221         my $self = shift;
222         my $arg = shift;
223
224         $log->debug("CDBI->create: \$arg is $arg (".ref($arg)." : ".JSON->perl2JSON($arg).")",DEBUG);
225
226         if (ref($arg) && UNIVERSAL::isa($arg => 'Fieldmapper')) {
227                 return $self->create_from_fieldmapper($arg,@_);
228         }
229
230         return $self->SUPER::create($arg,@_);
231 }
232
233 sub create_from_fieldmapper {
234         my $obj = shift;
235         my $fm = shift;
236         my @params = @_;
237
238         $log->debug("Creating node of type ".ref($fm), DEBUG);
239
240         my $class = ref($obj) || $obj;
241         my ($primary) = $class->columns('Primary');
242
243         if (ref($fm) &&UNIVERSAL::isa($fm => 'Fieldmapper')) {
244                 my %hash = map { defined $fm->$_ ?
245                                         ($_ => $fm->$_) :
246                                         ()
247                                 } grep { $_ ne $primary } $class->columns('All');
248
249                 if ($class->find_column( 'last_xact_id' )) {
250                         my $xact_id = $class->current_xact_id;
251                         throw Error unless ($xact_id);
252                         $hash{last_xact_id} = $xact_id;
253                 }
254
255                 return $class->create( \%hash, @params );
256         } else {
257                 return undef;
258         }
259 }
260
261 sub delete {
262         my $self = shift;
263         my $arg = shift;
264         my $orig = $self;
265
266         my $class = ref($self) || $self;
267
268         warn "HERE 1 --";
269         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
270                 $arg = $arg->id;
271         }
272
273         $self = $self->retrieve($arg);
274         unless (defined $self) {
275                 $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
276                 throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
277         }
278
279         warn "HERE 2 --";
280         if ($class->find_column( 'last_xact_id' )) {
281                 my $xact_id = $self->current_xact_id;
282                 
283                 throw Error ("Deleting from $class requires a transaction be established")
284                         unless ($xact_id);
285                 
286                 throw Error ("The row you are attempting to delete has been changed since you read it")
287                         unless ( $orig->last_xact_id eq $self->last_xact_id);
288
289                 $self->last_xact_id( $class->current_xact_id );
290                 $self->SUPER::update;
291         }
292
293         warn "HERE 3 --";
294         $self->SUPER::delete;
295         warn "HERE 4 --";
296
297         return 1;
298 }
299
300 sub update {
301         my $self = shift;
302         my $arg = shift;
303
304         $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
305
306         if (ref($arg)) {
307                 $self = $self->modify_from_fieldmapper($arg);
308                 $log->debug("Modification of $self seems to have failed....", DEBUG);
309                 return undef unless (defined $self);
310         }
311
312         $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
313         return $self->SUPER::update if ($self->is_changed);
314         return 0;
315 }
316
317 sub modify_from_fieldmapper {
318         my $obj = shift;
319         my $fm = shift;
320         my $orig = $obj;
321
322         $log->debug("Modifying object using fieldmapper", DEBUG);
323
324         my $class = ref($obj) || $obj;
325         my ($primary) = $class->columns('Primary');
326
327         if (!ref($obj)) {
328                 $obj = $class->retrieve($fm);
329                 unless ($obj) {
330                         $log->debug("Retrieve of $class using $fm (".$fm->id.") failed! -- ".shift(), ERROR);
331                         throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
332                 }
333         }
334
335         my %hash;
336         
337         if (ref($fm) and UNIVERSAL::isa($fm => 'Fieldmapper')) {
338                 %hash = map { defined $fm->$_ ?
339                                 ($_ => ''.$fm->$_) :
340                                 ()
341                         } grep { $_ ne $primary } $class->columns('All');
342         } else {
343                 %hash = %{$fm};
344         }
345
346         my $au = $obj->autoupdate;
347         $obj->autoupdate(0);
348         
349         for my $field ( keys %hash ) {
350                 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
351                 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
352         }
353
354         if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
355                 my $xact_id = $obj->current_xact_id;
356                 throw Error ("Updating $class requires a transaction be established")
357                         unless ($xact_id);
358                 throw Error ("The row you are attempting to delete has been changed since you read it")
359                         unless ( $orig->last_xact_id eq $self->last_xact_id);
360                 $obj->last_xact_id( $xact_id );
361         } else {
362                 $obj->autoupdate($au)
363         }
364
365         return $obj;
366 }
367
368
369
370         #-------------------------------------------------------------------------------
371         actor::user->has_a( home_ou => 'actor::org_unit' );
372         actor::user->has_a( card => 'actor::card' );
373         actor::user->has_a( standing => 'config::standing' );
374         actor::user->has_a( profile => 'actor::profile' );
375         actor::user->has_a( mailing_address => 'actor::user_address' );
376         actor::user->has_a( billing_address => 'actor::user_address' );
377         actor::user->has_a( ident_type => 'config::identification_type' );
378         actor::user->has_a( ident_type2 => 'config::identification_type' );
379         actor::user->has_a( net_access_level => 'config::net_access_level' );
380
381         actor::user_address->has_a( usr => 'actor::user' );
382         
383         actor::card->has_a( usr => 'actor::user' );
384         
385         actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
386         actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
387         #actor::org_unit->has_a( address => 'actor::org_address' );
388
389         actor::stat_cat_entry->has_a( stat_cat => 'actor::stat_cat' );
390         actor::stat_cat->has_many( entries => 'actor::stat_cat_entry' );
391         actor::stat_cat_entry_user_map->has_a( stat_cat => 'actor::stat_cat' );
392         actor::stat_cat_entry_user_map->has_a( stat_cat_entry => 'actor::stat_cat_entry' );
393         actor::stat_cat_entry_user_map->has_a( target_usr => 'actor::user' );
394
395         asset::stat_cat_entry->has_a( stat_cat => 'asset::stat_cat' );
396         asset::stat_cat->has_many( entries => 'asset::stat_cat_entry' );
397         asset::stat_cat_entry_copy_map->has_a( stat_cat => 'asset::stat_cat' );
398         asset::stat_cat_entry_copy_map->has_a( stat_cat_entry => 'asset::stat_cat_entry' );
399         asset::stat_cat_entry_copy_map->has_a( owning_copy => 'asset::copy' );
400
401         action::survey_response->has_a( usr => 'actor::user' );
402         action::survey_response->has_a( survey => 'action::survey' );
403         action::survey_response->has_a( question => 'action::survey_question' );
404         action::survey_response->has_a( answer => 'action::survey_answer' );
405
406         action::survey_question->has_a( survey => 'action::survey' );
407
408         action::survey_answer->has_a( question => 'action::survey' );
409
410         asset::copy_note->has_a( owning_copy => 'asset::copy' );
411
412         actor::user->has_many( stat_cat_entries => [ 'actor::stat_cat_entry_user_map' => 'stat_cat_entry' ] );
413         actor::user->has_many( stat_cat_entry_user_maps => 'actor::stat_cat_entry_user_map' );
414
415         asset::copy->has_many( stat_cat_entries => [ 'asset::stat_cat_entry_copy_map' => 'stat_cat_entry' ] );
416         asset::copy->has_many( stat_cat_entry_copy_maps => 'asset::stat_cat_entry_copy_map' );
417
418         asset::copy->has_a( call_number => 'asset::call_number' );
419         asset::copy->has_a( creator => 'actor::user' );
420         asset::copy->has_a( editor => 'actor::user' );
421         asset::copy->has_a( status => 'config::copy_status' );
422         asset::copy->has_a( location => 'asset::copy_location' );
423         asset::copy->has_a( circ_lib => 'actor::org_unit' );
424
425         asset::call_number_note->has_a( call_number => 'asset::call_number' );
426
427         asset::call_number->has_a( record => 'biblio::record_entry' );
428         asset::call_number->has_a( creator => 'actor::user' );
429         asset::call_number->has_a( editor => 'actor::user' );
430
431         authority::record_note->has_a( record => 'authority::record_entry' );
432         biblio::record_note->has_a( record => 'biblio::record_entry' );
433         
434         authority::record_entry->has_a( creator => 'actor::user' );
435         authority::record_entry->has_a( editor => 'actor::user' );
436         biblio::record_entry->has_a( creator => 'actor::user' );
437         biblio::record_entry->has_a( editor => 'actor::user' );
438         
439         metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
440         
441         authority::record_descriptor->has_a( record => 'authority::record_entry' );
442         metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
443         
444         authority::full_rec->has_a( record => 'authority::record_entry' );
445         metabib::full_rec->has_a( record => 'biblio::record_entry' );
446         
447         metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
448         metabib::title_field_entry->has_a( field => 'config::metabib_field' );
449         
450         metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
451         metabib::author_field_entry->has_a( field => 'config::metabib_field' );
452         
453         metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
454         metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
455         
456         metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
457         metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
458         
459         metabib::series_field_entry->has_a( source => 'biblio::record_entry' );
460         metabib::series_field_entry->has_a( field => 'config::metabib_field' );
461         
462         metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
463         metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
464
465         action::circulation->has_a( usr => 'actor::user' );
466         action::circulation->has_a( target_copy => 'asset::copy' );
467         action::circulation->has_a( circ_lib => 'actor::org_unit' );
468
469         money::billable_transaction->has_a( usr => 'actor::user' );
470         
471         
472         #-------------------------------------------------------------------------------
473         actor::user->has_many( survey_responses => 'action::survey_response' );
474         actor::user->has_many( addresses => 'actor::user_address' );
475         actor::user->has_many( cards => 'actor::card' );
476
477         actor::org_unit->has_many( users => 'actor::user' );
478         actor::profile->has_many( users => 'actor::user' );
479
480         action::survey->has_many( questions => 'action::survey_question' );
481         action::survey->has_many( responses => 'action::survey_response' );
482         
483         action::survey_question->has_many( answers => 'action::survey_answer' );
484         action::survey_question->has_many( responses => 'action::survey_response' );
485
486         action::survey_answer->has_many( responses => 'action::survey_response' );
487
488         asset::copy->has_many( notes => 'asset::copy_note' );
489         asset::call_number->has_many( copies => 'asset::copy' );
490         asset::call_number->has_many( notes => 'asset::call_number_note' );
491
492         authority::record_entry->has_many( record_descriptor => 'authority::record_descriptor' );
493         authority::record_entry->has_many( notes => 'authority::record_note' );
494
495         biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
496         biblio::record_entry->has_many( notes => 'biblio::record_note' );
497         biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
498         biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
499         biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
500         biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
501         biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
502         biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
503         biblio::record_entry->has_many( series_field_entries => 'metabib::series_field_entry' );
504
505         metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
506
507         money::billable_transaction->has_many( billings => 'money::billing' );
508         money::billable_transaction->has_many( payments => 'money::payment' );
509
510         money::billing->has_a( xact => 'money::billable_transaction' );
511         money::payment->has_a( xact => 'money::billable_transaction' );
512
513         money::cash_payment->has_a( xact => 'money::billable_transaction' );
514         money::cash_payment->has_a( accepting_usr => 'actor::user' );
515
516         money::check_payment->has_a( xact => 'money::billable_transaction' );
517         money::check_payment->has_a( accepting_usr => 'actor::user' );
518
519         money::credit_card_payment->has_a( xact => 'money::billable_transaction' );
520         money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
521
522         money::forgive_payment->has_a( xact => 'money::billable_transaction' );
523         money::forgive_payment->has_a( accepting_usr => 'actor::user' );
524
525         money::work_payment->has_a( xact => 'money::billable_transaction' );
526         money::work_payment->has_a( accepting_usr => 'actor::user' );
527
528         money::credit_payment->has_a( xact => 'money::billable_transaction' );
529         money::credit_payment->has_a( accepting_usr => 'actor::user' );
530
531         permission::grp_tree->has_a( parent => 'permission::grp_tree' );
532
533         permission::grp_perm_map->has_a( grp => 'permission::grp_tree' );
534         permission::grp_perm_map->has_a(  perm => 'permission::perm_list' );
535         permission::grp_perm_map->has_a(  depth => 'actor::org_unit_type' );
536         
537         permission::usr_perm_map->has_a( usr => 'actor::user' );
538         permission::usr_perm_map->has_a(  perm => 'permission::perm_list' );
539         permission::usr_perm_map->has_a(  depth => 'actor::org_unit_type' );
540         
541         permission::usr_grp_map->has_a(  usr => 'actor::user' );
542         permission::usr_grp_map->has_a(  grp => 'permission::grp_tree' );
543
544         action::hold_notification->has_a(  hold => 'action::hold_request' );
545         
546         action::hold_copy_map->has_a(  hold => 'action::hold_request' );
547         action::hold_copy_map->has_a(  target_copy => 'asset::copy' );
548
549         action::hold_request->has_a(  current_copy => 'asset::copy' );
550         action::hold_request->has_a(  requestor => 'actor::user' );
551         action::hold_request->has_a(  usr => 'actor::user' );
552         action::hold_request->has_a(  pickup_lib => 'actor::org_unit' );
553
554         action::hold_request->has_many(  notifications => 'action::hold_notification' );
555         action::hold_request->has_many(  copy_maps => 'action::hold_copy_map' );
556
557         asset::copy->has_many(  hold_maps => 'action::hold_copy_map' );
558
559 1;