1 package OpenILS::Application::Storage::CDBI;
2 use base qw/Class::DBI/;
4 use Class::DBI::AbstractSearch;
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;
16 use OpenSRF::Utils::Logger;
17 use OpenSRF::EX qw/:try/;
20 my $log = 'OpenSRF::Utils::Logger';
25 $log->debug("Creating ImaDBI Querys", DEBUG);
26 __PACKAGE__->set_sql( 'OILSFastSearch', <<" SQL", 'Main');
32 __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<" SQL", 'Main');
39 __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<" SQL", 'Main');
46 $log->debug("Calling Driver child_init", DEBUG);
47 $self->SUPER::child_init(@_);
53 $class = ref($class) || $class;
61 if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
62 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
66 $order = { order_by => $class->columns('Primary') }
70 unless (defined $value) {
72 ($field) = $class->columns('Primary');
75 unless (defined $field) {
76 ($field) = $class->columns('Primary');
79 unless ($order->{order_by}) {
80 $order = { order_by => $class->columns('Primary') }
83 my $fm_class = 'Fieldmapper::'.$class;
84 my $field_list = join ',', $class->columns('All');
88 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
90 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
92 $sth->execute($value);
98 return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
101 sub fast_fieldmapper {
107 my $class = ref($self) || $self;
108 my $fm_class = 'Fieldmapper::'.$class;
110 $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
112 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
113 my $fm = $fm_class->new;
114 for my $field ( $fm_class->real_fields ) {
115 $fm->$field( $$hash{$field} );
120 my $search_type = 'search';
122 $search_type = 'search_fts'
123 } elsif ($like == 3) {
124 $search_type = 'search_regex'
127 for my $obj ($class->$search_type({ $col => $id}, $options)) {
128 push @fms, $obj->to_fieldmapper;
137 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
138 my ($col) = $self->primary_column;
139 $log->debug("Using field $col as the primary key", INTERNAL);
142 $log->debug("Retrieving $self with $arg", INTERNAL);
145 $rec = $self->SUPER::retrieve("$arg");
147 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
155 my $class = ref($obj) || $obj;
157 my $fm_class = 'Fieldmapper::'.$class;
158 my $fm = $fm_class->new;
161 for my $field ( $fm->real_fields ) {
162 $fm->$field( $obj->$field );
174 delete $$arg{$_} for (keys %$search);
177 @objs = $self->search_where($search) unless (ref $self);
180 return $objs[0]->update($arg);
181 } elsif (@objs == 0) {
182 return $self->create({%$search,%$arg});
184 throw OpenSRF::EX::WARN ("Non-unique search key for merge. Perhaps you meant to use remote_update?");
193 delete $$arg{$_} for (keys %$search);
195 my @objs = $self->search_where($search);
197 throw OpenSRF::EX::WARN ("No objects found for remote_update. Perhaps you meant to use merge?");
199 $_->update($arg) for (@objs);
200 return scalar(@objs);
208 $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
210 if (ref($arg) && UNIVERSAL::isa($arg => 'Fieldmapper')) {
211 return $self->create_from_fieldmapper($arg,@_);
214 return $self->SUPER::create($arg,@_);
217 sub create_from_fieldmapper {
222 $log->debug("Creating node of type ".ref($fm), DEBUG);
224 my $class = ref($obj) || $obj;
225 my ($primary) = $class->columns('Primary');
227 if (ref($fm) &&UNIVERSAL::isa($fm => 'Fieldmapper')) {
228 my %hash = map { defined $fm->$_ ?
231 } grep { $_ ne $primary } $class->columns('All');
233 if ($class->find_column( 'last_xact_id' )) {
234 my $xact_id = $class->current_xact_id;
235 throw Error unless ($xact_id);
236 $hash{last_xact_id} = $xact_id;
239 return $class->create( \%hash, @params );
249 my $class = ref($self) || $self;
251 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
252 $self = $self->retrieve($arg);
253 unless (defined $self) {
254 $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
255 throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
259 if ($class->find_column( 'last_xact_id' )) {
260 my $xact_id = $self->current_xact_id;
261 throw Error unless ($xact_id);
262 $self->last_xact_id( $class->current_xact_id );
263 $self->SUPER::update;
266 $self->SUPER::delete;
274 $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
277 $self = $self->modify_from_fieldmapper($arg);
278 $log->debug("Modification of $self seems to have failed....", DEBUG);
279 return undef unless (defined $self);
282 $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
283 return $self->SUPER::update if ($self->is_changed);
287 sub modify_from_fieldmapper {
291 $log->debug("Modifying object using fieldmapper", DEBUG);
293 my $class = ref($obj) || $obj;
294 my ($primary) = $class->columns('Primary');
297 $obj = $class->retrieve($fm);
299 $log->debug("Retrieve of $class using $fm (".$fm->id.") failed! -- ".shift(), ERROR);
300 throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
306 if (ref($fm) and UNIVERSAL::isa($fm => 'Fieldmapper')) {
307 %hash = map { defined $fm->$_ ?
310 } grep { $_ ne $primary } $class->columns('All');
315 my $au = $obj->autoupdate;
318 for my $field ( keys %hash ) {
319 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
320 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
323 if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
324 my $xact_id = $obj->current_xact_id;
325 throw Error unless ($xact_id);
326 $obj->last_xact_id( $xact_id );
328 $obj->autoupdate($au)
336 #-------------------------------------------------------------------------------
337 actor::user->has_a( home_ou => 'actor::org_unit' );
338 actor::user->has_a( card => 'actor::card' );
339 actor::user->has_a( standing => 'config::standing' );
340 actor::user->has_a( profile => 'actor::profile' );
341 actor::user->has_a( mailing_address => 'actor::user_address' );
342 actor::user->has_a( billing_address => 'actor::user_address' );
343 actor::user->has_a( ident_type => 'config::identification_type' );
344 actor::user->has_a( ident_type2 => 'config::identification_type' );
345 actor::user->has_a( net_access_level => 'config::net_access_level' );
347 actor::user_address->has_a( usr => 'actor::user' );
349 actor::card->has_a( usr => 'actor::user' );
351 actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
352 actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
353 #actor::org_unit->has_a( address => 'actor::org_address' );
355 actor::stat_cat_entry->has_a( stat_cat => 'actor::stat_cat' );
356 actor::stat_cat->has_many( entries => 'actor::stat_cat_entry' );
357 actor::stat_cat_entry_user_map->has_a( stat_cat => 'actor::stat_cat' );
358 actor::stat_cat_entry_user_map->has_a( stat_cat_entry => 'actor::stat_cat_entry' );
359 actor::stat_cat_entry_user_map->has_a( target_usr => 'actor::user' );
361 asset::stat_cat_entry->has_a( stat_cat => 'asset::stat_cat' );
362 asset::stat_cat->has_many( entries => 'asset::stat_cat_entry' );
363 asset::stat_cat_entry_copy_map->has_a( stat_cat => 'asset::stat_cat' );
364 asset::stat_cat_entry_copy_map->has_a( stat_cat_entry => 'asset::stat_cat_entry' );
365 asset::stat_cat_entry_copy_map->has_a( owning_copy => 'asset::copy' );
367 action::survey_response->has_a( usr => 'actor::user' );
368 action::survey_response->has_a( survey => 'action::survey' );
369 action::survey_response->has_a( question => 'action::survey_question' );
370 action::survey_response->has_a( answer => 'action::survey_answer' );
372 action::survey_question->has_a( survey => 'action::survey' );
374 action::survey_answer->has_a( question => 'action::survey' );
376 asset::copy_note->has_a( owning_copy => 'asset::copy' );
378 actor::user->has_many( stat_cat_entries => [ 'actor::stat_cat_entry_user_map' => 'stat_cat_entry' ] );
379 actor::user->has_many( stat_cat_entry_user_maps => 'actor::stat_cat_entry_user_map' );
381 asset::copy->has_many( stat_cat_entries => [ 'asset::stat_cat_entry_copy_map' => 'stat_cat_entry' ] );
382 asset::copy->has_many( stat_cat_entry_copy_maps => 'asset::stat_cat_entry_copy_map' );
384 asset::copy->has_a( call_number => 'asset::call_number' );
385 asset::copy->has_a( creator => 'actor::user' );
386 asset::copy->has_a( editor => 'actor::user' );
387 asset::copy->has_a( status => 'config::copy_status' );
388 asset::copy->has_a( location => 'asset::copy_location' );
389 asset::copy->has_a( circ_lib => 'actor::org_unit' );
391 asset::call_number_note->has_a( call_number => 'asset::call_number' );
393 asset::call_number->has_a( record => 'biblio::record_entry' );
394 asset::call_number->has_a( creator => 'actor::user' );
395 asset::call_number->has_a( editor => 'actor::user' );
397 authority::record_note->has_a( record => 'authority::record_entry' );
398 biblio::record_note->has_a( record => 'biblio::record_entry' );
400 authority::record_entry->has_a( creator => 'actor::user' );
401 authority::record_entry->has_a( editor => 'actor::user' );
402 biblio::record_entry->has_a( creator => 'actor::user' );
403 biblio::record_entry->has_a( editor => 'actor::user' );
405 metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
407 authority::record_descriptor->has_a( record => 'authority::record_entry' );
408 metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
410 authority::full_rec->has_a( record => 'authority::record_entry' );
411 metabib::full_rec->has_a( record => 'biblio::record_entry' );
413 metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
414 metabib::title_field_entry->has_a( field => 'config::metabib_field' );
416 metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
417 metabib::author_field_entry->has_a( field => 'config::metabib_field' );
419 metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
420 metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
422 metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
423 metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
425 metabib::series_field_entry->has_a( source => 'biblio::record_entry' );
426 metabib::series_field_entry->has_a( field => 'config::metabib_field' );
428 metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
429 metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
431 action::circulation->has_a( usr => 'actor::user' );
432 action::circulation->has_a( target_copy => 'asset::copy' );
433 action::circulation->has_a( circ_lib => 'actor::org_unit' );
435 money::billable_transaction->has_a( usr => 'actor::user' );
438 #-------------------------------------------------------------------------------
439 actor::user->has_many( survey_responses => 'action::survey_response' );
440 actor::user->has_many( addresses => 'actor::user_address' );
441 actor::user->has_many( cards => 'actor::card' );
443 actor::org_unit->has_many( users => 'actor::user' );
444 actor::profile->has_many( users => 'actor::user' );
446 action::survey->has_many( questions => 'action::survey_question' );
447 action::survey->has_many( responses => 'action::survey_response' );
449 action::survey_question->has_many( answers => 'action::survey_answer' );
450 action::survey_question->has_many( responses => 'action::survey_response' );
452 action::survey_answer->has_many( responses => 'action::survey_response' );
454 asset::copy->has_many( notes => 'asset::copy_note' );
455 asset::call_number->has_many( copies => 'asset::copy' );
456 asset::call_number->has_many( notes => 'asset::call_number_note' );
458 authority::record_entry->has_many( record_descriptor => 'authority::record_descriptor' );
459 authority::record_entry->has_many( notes => 'authority::record_note' );
461 biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
462 biblio::record_entry->has_many( notes => 'biblio::record_note' );
463 biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
464 biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
465 biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
466 biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
467 biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
468 biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
469 biblio::record_entry->has_many( series_field_entries => 'metabib::series_field_entry' );
471 metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
473 money::billable_transaction->has_many( billings => 'money::billing' );
474 money::billable_transaction->has_many( payments => 'money::payment' );
476 money::billing->has_a( xact => 'money::billable_transaction' );
477 money::payment->has_a( xact => 'money::billable_transaction' );
479 money::cash_payment->has_a( xact => 'money::billable_transaction' );
480 money::cash_payment->has_a( accepting_usr => 'actor::user' );
482 money::check_payment->has_a( xact => 'money::billable_transaction' );
483 money::check_payment->has_a( accepting_usr => 'actor::user' );
485 money::credit_card_payment->has_a( xact => 'money::billable_transaction' );
486 money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
488 money::forgive_payment->has_a( xact => 'money::billable_transaction' );
489 money::forgive_payment->has_a( accepting_usr => 'actor::user' );
491 money::work_payment->has_a( xact => 'money::billable_transaction' );
492 money::work_payment->has_a( accepting_usr => 'actor::user' );
494 money::credit_payment->has_a( xact => 'money::billable_transaction' );
495 money::credit_payment->has_a( accepting_usr => 'actor::user' );
497 permission::grp_tree->has_a( parent => 'permission::grp_tree' );
499 permission::grp_perm_map->has_a( grp => 'permission::grp_tree' );
500 permission::grp_perm_map->has_a( perm => 'permission::perm_list' );
501 permission::grp_perm_map->has_a( depth => 'actor::org_unit_type' );
503 permission::usr_perm_map->has_a( usr => 'actor::user' );
504 permission::usr_perm_map->has_a( perm => 'permission::perm_list' );
505 permission::usr_perm_map->has_a( depth => 'actor::org_unit_type' );
507 permission::usr_grp_map->has_a( usr => 'actor::user' );
508 permission::usr_grp_map->has_a( grp => 'permission::grp_tree' );
510 action::hold_notification->has_a( hold => 'action::hold_request' );
512 action::hold_copy_map->has_a( hold => 'action::hold_request' );
513 action::hold_copy_map->has_a( target_copy => 'asset::copy' );
515 action::hold_request->has_a( current_copy => 'asset::copy' );
516 action::hold_request->has_a( requestor => 'actor::user' );
517 action::hold_request->has_a( usr => 'actor::user' );
518 action::hold_request->has_a( pickup_lib => 'actor::org_unit' );
520 action::hold_request->has_many( notifications => 'action::hold_notification' );
521 action::hold_request->has_many( copy_maps => 'action::hold_copy_map' );
523 asset::copy->has_many( hold_maps => 'action::hold_copy_map' );