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 {
104 my $class = ref($self) || $self;
105 my $fm_class = 'Fieldmapper::'.$class;
107 $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
108 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
109 my $fm = $fm_class->new;
110 for my $field ( $fm_class->real_fields ) {
111 $fm->$field( $$hash{$field} );
121 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
124 $log->debug("Retrieving $self with $arg", INTERNAL);
127 $rec = $self->SUPER::retrieve("$arg");
129 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
137 my $class = ref($obj) || $obj;
139 my $fm_class = 'Fieldmapper::'.$class;
140 my $fm = $fm_class->new;
143 for my $field ( $fm->real_fields ) {
144 $fm->$field( $obj->$field );
155 $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
157 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
158 return $self->create_from_fieldmapper($arg,@_);
161 return $self->SUPER::create($arg,@_);
164 sub create_from_fieldmapper {
169 $log->debug("Creating node of type ".ref($fm), DEBUG);
171 my $class = ref($obj) || $obj;
172 my $primary = $class->primary_column;
175 my %hash = map { defined $fm->$_ ?
178 } grep { $_ ne $primary } $class->columns('All');
180 if ($class->find_column( 'last_xact_id' )) {
181 my $xact_id = $class->current_xact_id;
182 throw Error unless ($xact_id);
183 $hash{last_xact_id} = $xact_id;
186 return $class->create( \%hash, @params );
196 my $class = ref($self) || $self;
198 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
199 $self = $self->retrieve($arg);
200 unless (defined $self) {
201 $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
202 throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
206 if ($class->find_column( 'last_xact_id' )) {
207 my $xact_id = $self->current_xact_id;
208 throw Error unless ($xact_id);
209 $self->last_xact_id( $class->current_xact_id );
210 $self->SUPER::update;
213 $self->SUPER::delete;
221 $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
223 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
224 $self = $self->modify_from_fieldmapper($arg);
225 $log->debug("Modification of $self seems to have failed....", DEBUG);
226 return undef unless (defined $self);
229 $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
230 return $self->SUPER::update if ($self->is_changed);
234 sub modify_from_fieldmapper {
238 $log->debug("Modifying object using fieldmapper", DEBUG);
240 my $class = ref($obj) || $obj;
243 $obj = $class->retrieve($fm);
245 $log->debug("Retrieve of $class using $fm (".$fm->id.") failed! -- ".shift(), ERROR);
246 throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
250 my %hash = map { defined $fm->$_ ?
255 my $au = $obj->autoupdate;
258 for my $field ( keys %hash ) {
259 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
260 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
263 if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
264 my $xact_id = $obj->current_xact_id;
265 throw Error unless ($xact_id);
266 $obj->last_xact_id( $xact_id );
268 $obj->autoupdate($au)
276 #-------------------------------------------------------------------------------
277 actor::user->has_a( home_ou => 'actor::org_unit' );
278 actor::user->has_a( card => 'actor::card' );
279 actor::user->has_a( standing => 'config::standing' );
280 actor::user->has_a( profile => 'actor::profile' );
281 actor::user->has_a( mailing_address => 'actor::user_address' );
282 actor::user->has_a( billing_address => 'actor::user_address' );
283 actor::user->has_a( ident_type => 'config::identification_type' );
284 actor::user->has_a( ident_type2 => 'config::identification_type' );
285 actor::user->has_a( net_access_level => 'config::net_access_level' );
287 actor::user_address->has_a( usr => 'actor::user' );
289 actor::card->has_a( usr => 'actor::user' );
291 config::rules::age_hold_protect->has_a( radius => 'actor::org_unit_type' );
293 actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
294 actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
295 #actor::org_unit->has_a( address => 'actor::org_address' );
297 actor::stat_cat_entry->has_a( stat_cat => 'actor::stat_cat' );
298 actor::stat_cat->has_many( entries => 'actor::stat_cat_entry' );
299 actor::stat_cat_entry_user_map->has_a( stat_cat => 'actor::stat_cat' );
300 actor::stat_cat_entry_user_map->has_a( stat_cat_entry => 'actor::stat_cat_entry' );
301 actor::stat_cat_entry_user_map->has_a( target_usr => 'actor::user' );
303 asset::stat_cat_entry->has_a( stat_cat => 'asset::stat_cat' );
304 asset::stat_cat->has_many( entries => 'asset::stat_cat_entry' );
305 asset::stat_cat_entry_copy_map->has_a( stat_cat => 'asset::stat_cat' );
306 asset::stat_cat_entry_copy_map->has_a( stat_cat_entry => 'asset::stat_cat_entry' );
307 asset::stat_cat_entry_copy_map->has_a( owning_copy => 'asset::copy' );
309 action::survey_response->has_a( usr => 'actor::user' );
310 action::survey_response->has_a( survey => 'action::survey' );
311 action::survey_response->has_a( question => 'action::survey_question' );
312 action::survey_response->has_a( answer => 'action::survey_answer' );
314 action::survey_question->has_a( survey => 'action::survey' );
316 action::survey_answer->has_a( question => 'action::survey' );
318 asset::copy_note->has_a( owning_copy => 'asset::copy' );
320 actor::user->has_many( stat_cat_entries => [ 'actor::stat_cat_entry_user_map' => 'stat_cat_entry' ] );
321 actor::user->has_many( stat_cat_entry_user_maps => 'actor::stat_cat_entry_user_map' );
323 asset::copy->has_many( stat_cat_entries => [ 'asset::stat_cat_entry_copy_map' => 'stat_cat_entry' ] );
324 asset::copy->has_many( stat_cat_entry_copy_maps => 'asset::stat_cat_entry_copy_map' );
326 asset::copy->has_a( call_number => 'asset::call_number' );
327 asset::copy->has_a( creator => 'actor::user' );
328 asset::copy->has_a( editor => 'actor::user' );
329 asset::copy->has_a( status => 'config::copy_status' );
330 asset::copy->has_a( location => 'asset::copy_location' );
331 asset::copy->has_a( circ_lib => 'actor::org_unit' );
333 asset::call_number_note->has_a( call_number => 'asset::call_number' );
335 asset::call_number->has_a( record => 'biblio::record_entry' );
336 asset::call_number->has_a( creator => 'actor::user' );
337 asset::call_number->has_a( editor => 'actor::user' );
339 biblio::record_note->has_a( record => 'biblio::record_entry' );
341 biblio::record_entry->has_a( creator => 'actor::user' );
342 biblio::record_entry->has_a( editor => 'actor::user' );
344 metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
346 metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
348 metabib::full_rec->has_a( record => 'biblio::record_entry' );
350 metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
351 metabib::title_field_entry->has_a( field => 'config::metabib_field' );
353 metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
354 metabib::author_field_entry->has_a( field => 'config::metabib_field' );
356 metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
357 metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
359 metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
360 metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
362 metabib::series_field_entry->has_a( source => 'biblio::record_entry' );
363 metabib::series_field_entry->has_a( field => 'config::metabib_field' );
365 metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
366 metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
368 action::circulation->has_a( usr => 'actor::user' );
369 action::circulation->has_a( target_copy => 'asset::copy' );
370 action::circulation->has_a( circ_lib => 'actor::org_unit' );
372 money::billable_transaction->has_a( usr => 'actor::user' );
375 #-------------------------------------------------------------------------------
376 actor::user->has_many( survey_responses => 'action::survey_response' );
377 actor::user->has_many( addresses => 'actor::user_address' );
378 actor::user->has_many( cards => 'actor::card' );
380 actor::org_unit->has_many( users => 'actor::user' );
381 actor::profile->has_many( users => 'actor::user' );
383 action::survey->has_many( questions => 'action::survey_question' );
384 action::survey->has_many( responses => 'action::survey_response' );
386 action::survey_question->has_many( answers => 'action::survey_answer' );
387 action::survey_question->has_many( responses => 'action::survey_response' );
389 action::survey_answer->has_many( responses => 'action::survey_response' );
391 asset::copy->has_many( notes => 'asset::copy_note' );
392 asset::call_number->has_many( copies => 'asset::copy' );
393 asset::call_number->has_many( notes => 'asset::call_number_note' );
395 biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
396 biblio::record_entry->has_many( notes => 'biblio::record_note' );
397 biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
398 biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
399 biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
400 biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
401 biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
402 biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
403 biblio::record_entry->has_many( series_field_entries => 'metabib::series_field_entry' );
405 metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
407 money::billable_transaction->has_many( billings => 'money::billing' );
408 money::billable_transaction->has_many( payments => 'money::payment' );
410 money::billing->has_a( xact => 'money::billable_transaction' );
411 money::payment->has_a( xact => 'money::billable_transaction' );
413 money::cash_payment->has_a( xact => 'money::billable_transaction' );
414 money::cash_payment->has_a( accepting_usr => 'actor::user' );
416 money::check_payment->has_a( xact => 'money::billable_transaction' );
417 money::check_payment->has_a( accepting_usr => 'actor::user' );
419 money::credit_card_payment->has_a( xact => 'money::billable_transaction' );
420 money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
422 money::forgive_payment->has_a( xact => 'money::billable_transaction' );
423 money::forgive_payment->has_a( accepting_usr => 'actor::user' );
425 money::work_payment->has_a( xact => 'money::billable_transaction' );
426 money::work_payment->has_a( accepting_usr => 'actor::user' );
428 money::credit_payment->has_a( xact => 'money::billable_transaction' );
429 money::credit_payment->has_a( accepting_usr => 'actor::user' );
431 permission::grp_tree->has_a( parent => 'permission::grp_tree' );
433 permission::grp_perm_map->has_a( grp => 'permission::grp_tree' );
434 permission::grp_perm_map->has_a( perm => 'permission::perm_list' );
435 permission::grp_perm_map->has_a( depth => 'actor::org_unit_type' );
437 permission::usr_perm_map->has_a( usr => 'actor::user' );
438 permission::usr_perm_map->has_a( perm => 'permission::perm_list' );
439 permission::usr_perm_map->has_a( depth => 'actor::org_unit_type' );
441 permission::usr_grp_map->has_a( usr => 'actor::user' );
442 permission::usr_grp_map->has_a( grp => 'permission::grp_tree' );
444 action::hold_notification->has_a( hold => 'action::hold_request' );
446 action::hold_request->has_a( current_copy => 'asset::copy' );
447 action::hold_request->has_a( requestor => 'actor::user' );
448 action::hold_request->has_a( usr => 'actor::user' );
449 action::hold_request->has_a( pickup_lib => 'actor::org_unit' );
451 action::hold_request->has_many( notifications => 'action::hold_notification' );