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::biblio;
10 use OpenILS::Application::Storage::CDBI::config;
11 use OpenILS::Application::Storage::CDBI::metabib;
12 use OpenILS::Application::Storage::CDBI::money;
13 use OpenILS::Application::Storage::CDBI::permission;
15 use OpenSRF::Utils::Logger;
16 use OpenSRF::EX qw/:try/;
19 my $log = 'OpenSRF::Utils::Logger';
24 $log->debug("Creating ImaDBI Querys", DEBUG);
25 __PACKAGE__->set_sql( 'OILSFastSearch', <<" SQL", 'Main');
31 __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<" SQL", 'Main');
38 __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<" SQL", 'Main');
45 $log->debug("Calling Driver child_init", DEBUG);
46 $self->SUPER::child_init(@_);
52 $class = ref($class) || $class;
60 if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
61 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
65 $order = { order_by => $class->columns('Primary') }
69 unless (defined $value) {
71 ($field) = $class->columns('Primary');
74 unless (defined $field) {
75 ($field) = $class->columns('Primary');
78 unless ($order->{order_by}) {
79 $order = { order_by => $class->columns('Primary') }
82 my $fm_class = 'Fieldmapper::'.$class;
83 my $field_list = join ',', $class->columns('All');
87 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
89 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
91 $sth->execute($value);
97 return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
100 sub fast_fieldmapper {
106 my $class = ref($self) || $self;
107 my $fm_class = 'Fieldmapper::'.$class;
109 $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
111 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
112 my $fm = $fm_class->new;
113 for my $field ( $fm_class->real_fields ) {
114 $fm->$field( $$hash{$field} );
119 my $search_type = 'search';
121 $search_type = 'search_fts'
122 } elsif ($like == 3) {
123 $search_type = 'search_regex'
126 for my $obj ($class->$search_type({ $col => $id}, $options)) {
127 push @fms, $obj->to_fieldmapper;
136 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
137 my ($col) = $self->primary_column;
138 $log->debug("Using field $col as the primary key", INTERNAL);
141 $log->debug("Retrieving $self with $arg", INTERNAL);
144 $rec = $self->SUPER::retrieve("$arg");
146 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
154 my $class = ref($obj) || $obj;
156 my $fm_class = 'Fieldmapper::'.$class;
157 my $fm = $fm_class->new;
160 for my $field ( $fm->real_fields ) {
161 $fm->$field( $obj->$field );
172 $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
174 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
175 return $self->create_from_fieldmapper($arg,@_);
178 return $self->SUPER::create($arg,@_);
181 sub create_from_fieldmapper {
186 $log->debug("Creating node of type ".ref($fm), DEBUG);
188 my $class = ref($obj) || $obj;
189 my ($primary) = $class->columns('Primary');
192 my %hash = map { defined $fm->$_ ?
195 } grep { $_ ne $primary } $class->columns('All');
197 if ($class->find_column( 'last_xact_id' )) {
198 my $xact_id = $class->current_xact_id;
199 throw Error unless ($xact_id);
200 $hash{last_xact_id} = $xact_id;
203 return $class->create( \%hash, @params );
213 my $class = ref($self) || $self;
215 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
216 $self = $self->retrieve($arg);
217 unless (defined $self) {
218 $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
219 throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
223 if ($class->find_column( 'last_xact_id' )) {
224 my $xact_id = $self->current_xact_id;
225 throw Error unless ($xact_id);
226 $self->last_xact_id( $class->current_xact_id );
227 $self->SUPER::update;
230 $self->SUPER::delete;
238 $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
240 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
241 $self = $self->modify_from_fieldmapper($arg);
242 $log->debug("Modification of $self seems to have failed....", DEBUG);
243 return undef unless (defined $self);
246 $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
247 return $self->SUPER::update if ($self->is_changed);
251 sub modify_from_fieldmapper {
255 $log->debug("Modifying object using fieldmapper", DEBUG);
257 my $class = ref($obj) || $obj;
260 $obj = $class->retrieve($fm);
262 $log->debug("Retrieve of $class using $fm (".$fm->id.") failed! -- ".shift(), ERROR);
263 throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
267 my %hash = map { defined $fm->$_ ?
272 my $au = $obj->autoupdate;
275 for my $field ( keys %hash ) {
276 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
277 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
280 if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
281 my $xact_id = $obj->current_xact_id;
282 throw Error unless ($xact_id);
283 $obj->last_xact_id( $xact_id );
285 $obj->autoupdate($au)
293 #-------------------------------------------------------------------------------
294 actor::user->has_a( home_ou => 'actor::org_unit' );
295 actor::user->has_a( card => 'actor::card' );
296 actor::user->has_a( standing => 'config::standing' );
297 actor::user->has_a( profile => 'actor::profile' );
298 actor::user->has_a( mailing_address => 'actor::user_address' );
299 actor::user->has_a( billing_address => 'actor::user_address' );
300 actor::user->has_a( ident_type => 'config::identification_type' );
301 actor::user->has_a( ident_type2 => 'config::identification_type' );
302 actor::user->has_a( net_access_level => 'config::net_access_level' );
304 actor::user_address->has_a( usr => 'actor::user' );
306 actor::card->has_a( usr => 'actor::user' );
308 actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
309 actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
310 #actor::org_unit->has_a( address => 'actor::org_address' );
312 actor::stat_cat_entry->has_a( stat_cat => 'actor::stat_cat' );
313 actor::stat_cat->has_many( entries => 'actor::stat_cat_entry' );
314 actor::stat_cat_entry_user_map->has_a( stat_cat => 'actor::stat_cat' );
315 actor::stat_cat_entry_user_map->has_a( stat_cat_entry => 'actor::stat_cat_entry' );
316 actor::stat_cat_entry_user_map->has_a( target_usr => 'actor::user' );
318 asset::stat_cat_entry->has_a( stat_cat => 'asset::stat_cat' );
319 asset::stat_cat->has_many( entries => 'asset::stat_cat_entry' );
320 asset::stat_cat_entry_copy_map->has_a( stat_cat => 'asset::stat_cat' );
321 asset::stat_cat_entry_copy_map->has_a( stat_cat_entry => 'asset::stat_cat_entry' );
322 asset::stat_cat_entry_copy_map->has_a( owning_copy => 'asset::copy' );
324 action::survey_response->has_a( usr => 'actor::user' );
325 action::survey_response->has_a( survey => 'action::survey' );
326 action::survey_response->has_a( question => 'action::survey_question' );
327 action::survey_response->has_a( answer => 'action::survey_answer' );
329 action::survey_question->has_a( survey => 'action::survey' );
331 action::survey_answer->has_a( question => 'action::survey' );
333 asset::copy_note->has_a( owning_copy => 'asset::copy' );
335 actor::user->has_many( stat_cat_entries => [ 'actor::stat_cat_entry_user_map' => 'stat_cat_entry' ] );
336 actor::user->has_many( stat_cat_entry_user_maps => 'actor::stat_cat_entry_user_map' );
338 asset::copy->has_many( stat_cat_entries => [ 'asset::stat_cat_entry_copy_map' => 'stat_cat_entry' ] );
339 asset::copy->has_many( stat_cat_entry_copy_maps => 'asset::stat_cat_entry_copy_map' );
341 asset::copy->has_a( call_number => 'asset::call_number' );
342 asset::copy->has_a( creator => 'actor::user' );
343 asset::copy->has_a( editor => 'actor::user' );
344 asset::copy->has_a( status => 'config::copy_status' );
345 asset::copy->has_a( location => 'asset::copy_location' );
346 asset::copy->has_a( circ_lib => 'actor::org_unit' );
348 asset::call_number_note->has_a( call_number => 'asset::call_number' );
350 asset::call_number->has_a( record => 'biblio::record_entry' );
351 asset::call_number->has_a( creator => 'actor::user' );
352 asset::call_number->has_a( editor => 'actor::user' );
354 biblio::record_note->has_a( record => 'biblio::record_entry' );
356 biblio::record_entry->has_a( creator => 'actor::user' );
357 biblio::record_entry->has_a( editor => 'actor::user' );
359 metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
361 metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
363 metabib::full_rec->has_a( record => 'biblio::record_entry' );
365 metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
366 metabib::title_field_entry->has_a( field => 'config::metabib_field' );
368 metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
369 metabib::author_field_entry->has_a( field => 'config::metabib_field' );
371 metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
372 metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
374 metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
375 metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
377 metabib::series_field_entry->has_a( source => 'biblio::record_entry' );
378 metabib::series_field_entry->has_a( field => 'config::metabib_field' );
380 metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
381 metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
383 action::circulation->has_a( usr => 'actor::user' );
384 action::circulation->has_a( target_copy => 'asset::copy' );
385 action::circulation->has_a( circ_lib => 'actor::org_unit' );
387 money::billable_transaction->has_a( usr => 'actor::user' );
390 #-------------------------------------------------------------------------------
391 actor::user->has_many( survey_responses => 'action::survey_response' );
392 actor::user->has_many( addresses => 'actor::user_address' );
393 actor::user->has_many( cards => 'actor::card' );
395 actor::org_unit->has_many( users => 'actor::user' );
396 actor::profile->has_many( users => 'actor::user' );
398 action::survey->has_many( questions => 'action::survey_question' );
399 action::survey->has_many( responses => 'action::survey_response' );
401 action::survey_question->has_many( answers => 'action::survey_answer' );
402 action::survey_question->has_many( responses => 'action::survey_response' );
404 action::survey_answer->has_many( responses => 'action::survey_response' );
406 asset::copy->has_many( notes => 'asset::copy_note' );
407 asset::call_number->has_many( copies => 'asset::copy' );
408 asset::call_number->has_many( notes => 'asset::call_number_note' );
410 biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
411 biblio::record_entry->has_many( notes => 'biblio::record_note' );
412 biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
413 biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
414 biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
415 biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
416 biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
417 biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
418 biblio::record_entry->has_many( series_field_entries => 'metabib::series_field_entry' );
420 metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
422 money::billable_transaction->has_many( billings => 'money::billing' );
423 money::billable_transaction->has_many( payments => 'money::payment' );
425 money::billing->has_a( xact => 'money::billable_transaction' );
426 money::payment->has_a( xact => 'money::billable_transaction' );
428 money::cash_payment->has_a( xact => 'money::billable_transaction' );
429 money::cash_payment->has_a( accepting_usr => 'actor::user' );
431 money::check_payment->has_a( xact => 'money::billable_transaction' );
432 money::check_payment->has_a( accepting_usr => 'actor::user' );
434 money::credit_card_payment->has_a( xact => 'money::billable_transaction' );
435 money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
437 money::forgive_payment->has_a( xact => 'money::billable_transaction' );
438 money::forgive_payment->has_a( accepting_usr => 'actor::user' );
440 money::work_payment->has_a( xact => 'money::billable_transaction' );
441 money::work_payment->has_a( accepting_usr => 'actor::user' );
443 money::credit_payment->has_a( xact => 'money::billable_transaction' );
444 money::credit_payment->has_a( accepting_usr => 'actor::user' );
446 permission::grp_tree->has_a( parent => 'permission::grp_tree' );
448 permission::grp_perm_map->has_a( grp => 'permission::grp_tree' );
449 permission::grp_perm_map->has_a( perm => 'permission::perm_list' );
450 permission::grp_perm_map->has_a( depth => 'actor::org_unit_type' );
452 permission::usr_perm_map->has_a( usr => 'actor::user' );
453 permission::usr_perm_map->has_a( perm => 'permission::perm_list' );
454 permission::usr_perm_map->has_a( depth => 'actor::org_unit_type' );
456 permission::usr_grp_map->has_a( usr => 'actor::user' );
457 permission::usr_grp_map->has_a( grp => 'permission::grp_tree' );
459 action::hold_notification->has_a( hold => 'action::hold_request' );
461 action::hold_copy_map->has_a( hold => 'action::hold_request' );
462 action::hold_copy_map->has_a( target_copy => 'asset::copy' );
464 action::hold_request->has_a( current_copy => 'asset::copy' );
465 action::hold_request->has_a( requestor => 'actor::user' );
466 action::hold_request->has_a( usr => 'actor::user' );
467 action::hold_request->has_a( pickup_lib => 'actor::org_unit' );
469 action::hold_request->has_many( notifications => 'action::hold_notification' );
470 action::hold_request->has_many( copy_maps => 'action::hold_copy_map' );
472 asset::copy->has_many( hold_maps => 'action::hold_copy_map' );