1 package OpenILS::Application::Storage::CDBI;
2 use base qw/Class::DBI/;
5 use OpenILS::Application::Storage::CDBI::actor;
6 use OpenILS::Application::Storage::CDBI::action;
7 use OpenILS::Application::Storage::CDBI::asset;
8 use OpenILS::Application::Storage::CDBI::biblio;
9 use OpenILS::Application::Storage::CDBI::config;
10 use OpenILS::Application::Storage::CDBI::metabib;
11 use OpenILS::Application::Storage::CDBI::money;
12 use OpenILS::Application::Storage::CDBI::permission;
14 use OpenSRF::Utils::Logger;
15 use OpenSRF::EX qw/:try/;
18 my $log = 'OpenSRF::Utils::Logger';
23 $log->debug("Creating ImaDBI Querys", DEBUG);
24 __PACKAGE__->set_sql( 'OILSFastSearch', <<" SQL", 'Main');
30 __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<" SQL", 'Main');
37 __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<" SQL", 'Main');
44 $log->debug("Calling Driver child_init", DEBUG);
45 $self->SUPER::child_init(@_);
51 $class = ref($class) || $class;
59 if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
60 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
64 $order = { order_by => $class->columns('Primary') }
68 unless (defined $value) {
70 $field = $class->primary_column;
73 unless (defined $field) {
74 $field = $class->primary_column;
77 unless ($order->{order_by}) {
78 $order = { order_by => $class->columns('Primary') }
81 my $fm_class = 'Fieldmapper::'.$class;
82 my $field_list = join ',', $class->columns('All');
86 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
88 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
90 $sth->execute($value);
96 return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
99 sub fast_fieldmapper {
105 my $class = ref($self) || $self;
106 my $fm_class = 'Fieldmapper::'.$class;
108 $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
110 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
111 my $fm = $fm_class->new;
112 for my $field ( $fm_class->real_fields ) {
113 $fm->$field( $$hash{$field} );
118 my $search_type = 'search';
120 $search_type = 'search_fts'
121 } elsif ($like == 3) {
122 $search_type = 'search_regex'
125 for my $obj ($class->$search_type({ $col => $id}, $options)) {
126 push @fms, $obj->to_fieldmapper;
135 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
138 $log->debug("Retrieving $self with $arg", INTERNAL);
141 $rec = $self->SUPER::retrieve("$arg");
143 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
151 my $class = ref($obj) || $obj;
153 my $fm_class = 'Fieldmapper::'.$class;
154 my $fm = $fm_class->new;
157 for my $field ( $fm->real_fields ) {
158 $fm->$field( $obj->$field );
169 $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
171 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
172 return $self->create_from_fieldmapper($arg,@_);
175 return $self->SUPER::create($arg,@_);
178 sub create_from_fieldmapper {
183 $log->debug("Creating node of type ".ref($fm), DEBUG);
185 my $class = ref($obj) || $obj;
186 my $primary = $class->primary_column;
189 my %hash = map { defined $fm->$_ ?
192 } grep { $_ ne $primary } $class->columns('All');
194 if ($class->find_column( 'last_xact_id' )) {
195 my $xact_id = $class->current_xact_id;
196 throw Error unless ($xact_id);
197 $hash{last_xact_id} = $xact_id;
200 return $class->create( \%hash, @params );
210 my $class = ref($self) || $self;
212 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
213 $self = $self->retrieve($arg);
214 unless (defined $self) {
215 $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
216 throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
220 if ($class->find_column( 'last_xact_id' )) {
221 my $xact_id = $self->current_xact_id;
222 throw Error unless ($xact_id);
223 $self->last_xact_id( $class->current_xact_id );
224 $self->SUPER::update;
227 $self->SUPER::delete;
235 $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
237 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
238 $self = $self->modify_from_fieldmapper($arg);
239 $log->debug("Modification of $self seems to have failed....", DEBUG);
240 return undef unless (defined $self);
243 $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
244 return $self->SUPER::update if ($self->is_changed);
248 sub modify_from_fieldmapper {
252 $log->debug("Modifying object using fieldmapper", DEBUG);
254 my $class = ref($obj) || $obj;
257 $obj = $class->retrieve($fm);
259 $log->debug("Retrieve of $class using $fm (".$fm->id.") failed! -- ".shift(), ERROR);
260 throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
264 my %hash = map { defined $fm->$_ ?
269 my $au = $obj->autoupdate;
272 for my $field ( keys %hash ) {
273 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
274 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
277 if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
278 my $xact_id = $obj->current_xact_id;
279 throw Error unless ($xact_id);
280 $obj->last_xact_id( $xact_id );
282 $obj->autoupdate($au)
290 #-------------------------------------------------------------------------------
291 actor::user->has_a( home_ou => 'actor::org_unit' );
292 actor::user->has_a( card => 'actor::card' );
293 actor::user->has_a( standing => 'config::standing' );
294 actor::user->has_a( profile => 'actor::profile' );
295 actor::user->has_a( mailing_address => 'actor::user_address' );
296 actor::user->has_a( billing_address => 'actor::user_address' );
297 actor::user->has_a( ident_type => 'config::identification_type' );
298 actor::user->has_a( ident_type2 => 'config::identification_type' );
299 actor::user->has_a( net_access_level => 'config::net_access_level' );
301 actor::user_address->has_a( usr => 'actor::user' );
303 actor::card->has_a( usr => 'actor::user' );
305 actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
306 actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
307 #actor::org_unit->has_a( address => 'actor::org_address' );
309 actor::stat_cat_entry->has_a( stat_cat => 'actor::stat_cat' );
310 actor::stat_cat->has_many( entries => 'actor::stat_cat_entry' );
311 actor::stat_cat_entry_user_map->has_a( stat_cat => 'actor::stat_cat' );
312 actor::stat_cat_entry_user_map->has_a( stat_cat_entry => 'actor::stat_cat_entry' );
313 actor::stat_cat_entry_user_map->has_a( target_usr => 'actor::user' );
315 asset::stat_cat_entry->has_a( stat_cat => 'asset::stat_cat' );
316 asset::stat_cat->has_many( entries => 'asset::stat_cat_entry' );
317 asset::stat_cat_entry_copy_map->has_a( stat_cat => 'asset::stat_cat' );
318 asset::stat_cat_entry_copy_map->has_a( stat_cat_entry => 'asset::stat_cat_entry' );
319 asset::stat_cat_entry_copy_map->has_a( owning_copy => 'asset::copy' );
321 action::survey_response->has_a( usr => 'actor::user' );
322 action::survey_response->has_a( survey => 'action::survey' );
323 action::survey_response->has_a( question => 'action::survey_question' );
324 action::survey_response->has_a( answer => 'action::survey_answer' );
326 action::survey_question->has_a( survey => 'action::survey' );
328 action::survey_answer->has_a( question => 'action::survey' );
330 asset::copy_note->has_a( owning_copy => 'asset::copy' );
332 actor::user->has_many( stat_cat_entries => [ 'actor::stat_cat_entry_user_map' => 'stat_cat_entry' ] );
333 actor::user->has_many( stat_cat_entry_user_maps => 'actor::stat_cat_entry_user_map' );
335 asset::copy->has_many( stat_cat_entries => [ 'asset::stat_cat_entry_copy_map' => 'stat_cat_entry' ] );
336 asset::copy->has_many( stat_cat_entry_copy_maps => 'asset::stat_cat_entry_copy_map' );
338 asset::copy->has_a( call_number => 'asset::call_number' );
339 asset::copy->has_a( creator => 'actor::user' );
340 asset::copy->has_a( editor => 'actor::user' );
341 asset::copy->has_a( status => 'config::copy_status' );
342 asset::copy->has_a( location => 'asset::copy_location' );
343 asset::copy->has_a( circ_lib => 'actor::org_unit' );
345 asset::call_number_note->has_a( call_number => 'asset::call_number' );
347 asset::call_number->has_a( record => 'biblio::record_entry' );
348 asset::call_number->has_a( creator => 'actor::user' );
349 asset::call_number->has_a( editor => 'actor::user' );
351 biblio::record_note->has_a( record => 'biblio::record_entry' );
353 biblio::record_entry->has_a( creator => 'actor::user' );
354 biblio::record_entry->has_a( editor => 'actor::user' );
356 metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
358 metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
360 metabib::full_rec->has_a( record => 'biblio::record_entry' );
362 metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
363 metabib::title_field_entry->has_a( field => 'config::metabib_field' );
365 metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
366 metabib::author_field_entry->has_a( field => 'config::metabib_field' );
368 metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
369 metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
371 metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
372 metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
374 metabib::series_field_entry->has_a( source => 'biblio::record_entry' );
375 metabib::series_field_entry->has_a( field => 'config::metabib_field' );
377 metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
378 metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
380 action::circulation->has_a( usr => 'actor::user' );
381 action::circulation->has_a( target_copy => 'asset::copy' );
382 action::circulation->has_a( circ_lib => 'actor::org_unit' );
384 money::billable_transaction->has_a( usr => 'actor::user' );
387 #-------------------------------------------------------------------------------
388 actor::user->has_many( survey_responses => 'action::survey_response' );
389 actor::user->has_many( addresses => 'actor::user_address' );
390 actor::user->has_many( cards => 'actor::card' );
392 actor::org_unit->has_many( users => 'actor::user' );
393 actor::profile->has_many( users => 'actor::user' );
395 action::survey->has_many( questions => 'action::survey_question' );
396 action::survey->has_many( responses => 'action::survey_response' );
398 action::survey_question->has_many( answers => 'action::survey_answer' );
399 action::survey_question->has_many( responses => 'action::survey_response' );
401 action::survey_answer->has_many( responses => 'action::survey_response' );
403 asset::copy->has_many( notes => 'asset::copy_note' );
404 asset::call_number->has_many( copies => 'asset::copy' );
405 asset::call_number->has_many( notes => 'asset::call_number_note' );
407 biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
408 biblio::record_entry->has_many( notes => 'biblio::record_note' );
409 biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
410 biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
411 biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
412 biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
413 biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
414 biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
415 biblio::record_entry->has_many( series_field_entries => 'metabib::series_field_entry' );
417 metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
419 money::billable_transaction->has_many( billings => 'money::billing' );
420 money::billable_transaction->has_many( payments => 'money::payment' );
422 money::billing->has_a( xact => 'money::billable_transaction' );
423 money::payment->has_a( xact => 'money::billable_transaction' );
425 money::cash_payment->has_a( xact => 'money::billable_transaction' );
426 money::cash_payment->has_a( accepting_usr => 'actor::user' );
428 money::check_payment->has_a( xact => 'money::billable_transaction' );
429 money::check_payment->has_a( accepting_usr => 'actor::user' );
431 money::credit_card_payment->has_a( xact => 'money::billable_transaction' );
432 money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
434 money::forgive_payment->has_a( xact => 'money::billable_transaction' );
435 money::forgive_payment->has_a( accepting_usr => 'actor::user' );
437 money::work_payment->has_a( xact => 'money::billable_transaction' );
438 money::work_payment->has_a( accepting_usr => 'actor::user' );
440 money::credit_payment->has_a( xact => 'money::billable_transaction' );
441 money::credit_payment->has_a( accepting_usr => 'actor::user' );
443 permission::grp_tree->has_a( parent => 'permission::grp_tree' );
445 permission::grp_perm_map->has_a( grp => 'permission::grp_tree' );
446 permission::grp_perm_map->has_a( perm => 'permission::perm_list' );
447 permission::grp_perm_map->has_a( depth => 'actor::org_unit_type' );
449 permission::usr_perm_map->has_a( usr => 'actor::user' );
450 permission::usr_perm_map->has_a( perm => 'permission::perm_list' );
451 permission::usr_perm_map->has_a( depth => 'actor::org_unit_type' );
453 permission::usr_grp_map->has_a( usr => 'actor::user' );
454 permission::usr_grp_map->has_a( grp => 'permission::grp_tree' );
456 action::hold_notification->has_a( hold => 'action::hold_request' );
458 action::hold_copy_map->has_a( hold => 'action::hold_request' );
459 action::hold_copy_map->has_a( target_copy => 'asset::copy' );
461 action::hold_request->has_a( current_copy => 'asset::copy' );
462 action::hold_request->has_a( requestor => 'actor::user' );
463 action::hold_request->has_a( usr => 'actor::user' );
464 action::hold_request->has_a( pickup_lib => 'actor::org_unit' );
466 action::hold_request->has_many( notifications => 'action::hold_notification' );
467 action::hold_request->has_many( copy_maps => 'action::hold_copy_map' );
469 asset::copy->has_many( hold_maps => 'action::hold_copy_map' );