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;
13 use OpenSRF::Utils::Logger;
14 use OpenSRF::EX qw/:try/;
17 my $log = 'OpenSRF::Utils::Logger';
22 $log->debug("Creating ImaDBI Querys", DEBUG);
23 __PACKAGE__->set_sql( 'OILSFastSearch', <<" SQL", 'Main');
29 __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<" SQL", 'Main');
36 __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<" SQL", 'Main');
43 $log->debug("Calling Driver child_init", DEBUG);
44 $self->SUPER::child_init(@_);
50 $class = ref($class) || $class;
58 if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
59 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
63 $order = { order_by => $class->columns('Primary') }
67 unless (defined $value) {
69 $field = $class->primary_column;
72 unless (defined $field) {
73 $field = $class->primary_column;
76 unless ($order->{order_by}) {
77 $order = { order_by => $class->columns('Primary') }
80 my $fm_class = 'Fieldmapper::'.$class;
81 my $field_list = join ',', $class->columns('All');
85 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
87 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
89 $sth->execute($value);
95 return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
98 sub fast_fieldmapper {
103 my $class = ref($self) || $self;
104 my $fm_class = 'Fieldmapper::'.$class;
106 $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
107 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
108 my $fm = $fm_class->new;
109 for my $field ( $fm_class->real_fields ) {
110 $fm->$field( $$hash{$field} );
120 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
123 $log->debug("Retrieving $self with $arg", INTERNAL);
126 $rec = $self->SUPER::retrieve("$arg");
128 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
136 my $class = ref($obj) || $obj;
138 my $fm_class = 'Fieldmapper::'.$class;
139 my $fm = $fm_class->new;
142 for my $field ( $fm->real_fields ) {
143 $fm->$field( $obj->$field );
154 $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
156 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
157 return $self->create_from_fieldmapper($arg,@_);
160 return $self->SUPER::create($arg,@_);
163 sub create_from_fieldmapper {
168 $log->debug("Creating node of type ".ref($fm), DEBUG);
170 my $class = ref($obj) || $obj;
171 my $primary = $class->primary_column;
174 my %hash = map { defined $fm->$_ ?
177 } grep { $_ ne $primary } $class->columns('All');
179 if ($class->find_column( 'last_xact_id' )) {
180 my $xact_id = $class->current_xact_id;
181 throw Error unless ($xact_id);
182 $hash{last_xact_id} = $xact_id;
185 return $class->create( \%hash, @params );
195 my $class = ref($self) || $self;
197 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
198 $self = $self->retrieve($arg);
199 unless (defined $self) {
200 $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
201 throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
205 if ($class->find_column( 'last_xact_id' )) {
206 my $xact_id = $self->current_xact_id;
207 throw Error unless ($xact_id);
208 $self->last_xact_id( $class->current_xact_id );
209 $self->SUPER::update;
212 $self->SUPER::delete;
220 $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
222 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
223 $self = $self->modify_from_fieldmapper($arg);
224 $log->debug("Modification of $self seems to have failed....", DEBUG);
225 return undef unless (defined $self);
228 $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
229 return $self->SUPER::update if ($self->is_changed);
233 sub modify_from_fieldmapper {
237 $log->debug("Modifying object using fieldmapper", DEBUG);
239 my $class = ref($obj) || $obj;
242 $obj = $class->retrieve($fm);
244 $log->debug("Retrieve of $class using $fm (".$fm->id.") failed! -- ".shift(), ERROR);
245 throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
249 my %hash = map { defined $fm->$_ ?
254 my $au = $obj->autoupdate;
257 for my $field ( keys %hash ) {
258 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
259 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
262 if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
263 my $xact_id = $obj->current_xact_id;
264 throw Error unless ($xact_id);
265 $obj->last_xact_id( $xact_id );
267 $obj->autoupdate($au)
276 return if ($VERSION);
278 #-------------------------------------------------------------------------------
279 actor::user->has_a( home_ou => 'actor::org_unit' );
280 actor::user->has_many( survey_responses => 'action::survey_response' );
281 #-------------------------------------------------------------------------------
282 actor::org_unit->has_many( users => 'actor::user' );
283 actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
284 actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
285 #actor::org_unit->has_a( address => 'actor::address' );
286 #-------------------------------------------------------------------------------
288 #-------------------------------------------------------------------------------
289 action::survey_response->has_a( usr => 'actor::user' );
290 action::survey_response->has_a( survey => 'action::survey' );
291 action::survey_response->has_a( question => 'action::survey_question' );
292 action::survey_response->has_a( answer => 'action::survey_answer' );
293 #-------------------------------------------------------------------------------
294 action::survey->has_many( questions => 'action::survey_question' );
295 action::survey->has_many( responses => 'action::survey_response' );
296 #-------------------------------------------------------------------------------
297 action::survey_question->has_a( survey => 'action::survey' );
298 action::survey_question->has_many( answers => 'action::survey_answer' );
299 action::survey_question->has_many( responses => 'action::survey_response' );
300 #-------------------------------------------------------------------------------
301 action::survey_answer->has_a( question => 'action::survey' );
302 action::survey_answer->has_many( responses => 'action::survey_response' );
303 #-------------------------------------------------------------------------------
305 #-------------------------------------------------------------------------------
306 asset::copy_note->has_a( owning_copy => 'asset::copy' );
307 #-------------------------------------------------------------------------------
308 asset::copy->has_a( call_number => 'asset::call_number' );
309 asset::copy->has_many( notes => 'asset::copy_note' );
310 asset::copy->has_a( creator => 'actor::user' );
311 asset::copy->has_a( editor => 'actor::user' );
312 #-------------------------------------------------------------------------------
313 asset::call_number_note->has_a( owning_call_number => 'asset::call_number' );
314 #-------------------------------------------------------------------------------
315 asset::call_number->has_a( record => 'biblio::record_entry' );
316 asset::call_number->has_many( copies => 'asset::copy' );
317 asset::call_number->has_many( notes => 'asset::call_number_note' );
318 asset::call_number->has_a( creator => 'actor::user' );
319 asset::call_number->has_a( editor => 'actor::user' );
320 #-------------------------------------------------------------------------------
323 #-------------------------------------------------------------------------------
324 biblio::record_note->has_a( record => 'biblio::record_entry' );
325 #-------------------------------------------------------------------------------
326 biblio::record_entry->has_a( creator => 'actor::user' );
327 biblio::record_entry->has_a( editor => 'actor::user' );
328 biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
329 biblio::record_entry->has_many( notes => 'biblio::record_note' );
330 biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
332 # should we have just one field entry per class for each record???? (xslt vs xpath)
333 biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
334 biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
335 biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
336 biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
337 biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
338 #-------------------------------------------------------------------------------
341 #-------------------------------------------------------------------------------
342 metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
343 metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
344 #-------------------------------------------------------------------------------
345 metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
346 #-------------------------------------------------------------------------------
347 metabib::full_rec->has_a( record => 'biblio::record_entry' );
348 #-------------------------------------------------------------------------------
349 metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
350 metabib::title_field_entry->has_a( field => 'config::metabib_field' );
351 #-------------------------------------------------------------------------------
352 metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
353 metabib::author_field_entry->has_a( field => 'config::metabib_field' );
354 #-------------------------------------------------------------------------------
355 metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
356 metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
357 #-------------------------------------------------------------------------------
358 metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
359 metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
360 #-------------------------------------------------------------------------------
361 metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
362 metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
363 #-------------------------------------------------------------------------------
365 action::circulation->has_a( usr => 'actor::user' );
366 action::circulation->has_a( target_copy => 'asset::copy' );
368 #-------------------------------------------------------------------------------
370 money::billable_transaction->has_a( usr => 'actor::user' );
371 money::billable_transaction->has_many( billings => 'money::billing' );
372 money::billable_transaction->has_many( payments => 'money::payment' );
374 money::billing->has_a( transaction => 'money::billable_transaction' );
375 money::payment->has_a( transaction => 'money::billable_transaction' );
377 money::cash_payment->has_a( transaction => 'money::billable_transaction' );
378 money::cash_payment->has_a( accepting_usr => 'actor::user' );
380 money::check_payment->has_a( transaction => 'money::billable_transaction' );
381 money::check_payment->has_a( accepting_usr => 'actor::user' );
383 money::credit_card_payment->has_a( transaction => 'money::billable_transaction' );
384 money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
386 money::forgive_payment->has_a( transaction => 'money::billable_transaction' );
387 money::forgive_payment->has_a( accepting_usr => 'actor::user' );
389 money::work_payment->has_a( transaction => 'money::billable_transaction' );
390 money::work_payment->has_a( accepting_usr => 'actor::user' );
392 money::credit_payment->has_a( transaction => 'money::billable_transaction' );
393 money::credit_payment->has_a( accepting_usr => 'actor::user' );
396 # should we have just one field entry per class for each record???? (xslt vs xpath)
397 #metabib::title_field_entry_source_map->has_a( field_entry => 'metabib::title_field_entry' );
398 #metabib::title_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
399 #metabib::title_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
400 #-------------------------------------------------------------------------------
401 #metabib::subject_field_entry_source_map->has_a( field_entry => 'metabib::subject_field_entry' );
402 #metabib::subject_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
403 #metabib::subject_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
404 #-------------------------------------------------------------------------------
405 #metabib::author_field_entry_source_map->has_a( field_entry => 'metabib::author_field_entry' );
406 #metabib::author_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
407 #metabib::author_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
408 #-------------------------------------------------------------------------------
409 #metabib::keyword_field_entry_source_map->has_a( field_entry => 'metabib::keyword_field_entry' );
410 #metabib::keyword_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
411 #metabib::keyword_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
412 #-------------------------------------------------------------------------------