1 package OpenILS::Application::Storage::CDBI;
2 use base qw/Class::DBI/;
5 use OpenILS::Application::Storage::CDBI::config;
6 use OpenILS::Application::Storage::CDBI::actor;
7 use OpenILS::Application::Storage::CDBI::asset;
8 use OpenILS::Application::Storage::CDBI::biblio;
9 use OpenILS::Application::Storage::CDBI::metabib;
11 use OpenILS::Utils::Fieldmapper;
12 use OpenSRF::Utils::Logger;
15 my $log = 'OpenSRF::Utils::Logger';
20 $log->debug("Creating ImaDBI Querys", DEBUG);
21 __PACKAGE__->set_sql( 'OILSFastSearch', <<" SQL", 'Main');
27 __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<" SQL", 'Main');
34 __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<" SQL", 'Main');
41 $log->debug("Calling Driver child_init", DEBUG);
42 $self->SUPER::child_init(@_);
48 $class = ref($class) || $class;
56 if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
57 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
61 $order = { order_by => $class->columns('Primary') }
65 unless (defined $value) {
67 $field = $class->primary_column;
70 unless (defined $field) {
71 $field = $class->primary_column;
74 unless ($order->{order_by}) {
75 $order = { order_by => $class->columns('Primary') }
78 my $fm_class = 'Fieldmapper::'.$class;
79 my $field_list = join ',', $class->columns('All');
83 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
85 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
87 $sth->execute($value);
93 return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
96 sub fast_fieldmapper {
101 my $class = ref($self) || $self;
102 my $fm_class = 'Fieldmapper::'.$class;
104 $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
105 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
106 my $fm = $fm_class->new;
107 for my $field ( $fm_class->real_fields ) {
108 $fm->$field( $$hash{$field} );
118 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
121 $log->debug("Retrieving $self with $arg", INTERNAL);
122 my $rec = $self->SUPER::retrieve("$arg");
124 $log->debug("Could not retrieve $self with $arg!", DEBUG);
132 my $class = ref($obj) || $obj;
134 my $fm_class = 'Fieldmapper::'.$class;
135 my $fm = $fm_class->new;
138 for my $field ( $fm->real_fields ) {
139 $fm->$field( $obj->$field );
150 $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
152 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
153 return $self->create_from_fieldmapper($arg,@_);
156 return $self->SUPER::create($arg,@_);
159 sub create_from_fieldmapper {
164 $log->debug("Creating node of type ".ref($fm), DEBUG);
166 my $class = ref($obj) || $obj;
169 my %hash = map { defined $fm->$_ ?
174 if ($class->find_column( 'last_xact_id' )) {
175 my $xact_id = $class->current_xact_id;
176 throw Error unless ($xact_id);
177 $hash{last_xact_id} = $xact_id;
180 return $class->create( \%hash, @params );
190 my $class = ref($self) || $self;
192 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
193 $self = $self->retrieve($arg);
194 unless (defined $self) {
195 $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
196 throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
200 if ($class->find_column( 'last_xact_id' )) {
201 my $xact_id = $self->current_xact_id;
202 throw Error unless ($xact_id);
203 $self->last_xact_id( $class->current_xact_id );
204 $self->SUPER::update;
207 $self->SUPER::delete;
215 $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
217 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
218 $self = $self->modify_from_fieldmapper($arg);
219 $log->debug("Modification of $self seems to have failed....", DEBUG);
220 return undef unless (defined $self);
223 $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
224 return $self->SUPER::update if ($self->is_changed);
228 sub modify_from_fieldmapper {
232 $log->debug("Modifying object using fieldmapper", DEBUG);
234 my $class = ref($obj) || $obj;
237 $obj = $class->retrieve($fm);
239 $log->debug("Rretrieve using $fm (".$fm->id.") failed!", ERROR);
240 throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
245 my %hash = map { defined $fm->$_ ?
250 my $au = $obj->autoupdate;
253 for my $field ( keys %hash ) {
254 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
255 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
258 if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
259 my $xact_id = $obj->current_xact_id;
260 throw Error unless ($xact_id);
261 $obj->last_xact_id( $xact_id );
263 $obj->autoupdate($au)
272 return if ($VERSION);
273 #-------------------------------------------------------------------------------
274 actor::user->has_a( home_ou => 'actor::org_unit' );
275 #actor::org_unit->has_a( address => 'actor::address' );
276 #-------------------------------------------------------------------------------
277 actor::org_unit->has_many( users => 'actor::user' );
278 actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
279 actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
280 #actor::org_unit->has_a( address => 'actor::address' );
281 #-------------------------------------------------------------------------------
283 #-------------------------------------------------------------------------------
284 asset::copy_note->has_a( owning_copy => 'asset::copy' );
285 #-------------------------------------------------------------------------------
286 asset::copy->has_a( call_number => 'asset::call_number' );
287 asset::copy->has_many( notes => 'asset::copy_note' );
288 asset::copy->has_a( creator => 'actor::user' );
289 asset::copy->has_a( editor => 'actor::user' );
290 #asset::copy->might_have( metadata => 'asset::copy_metadata' );
291 #-------------------------------------------------------------------------------
292 asset::call_number_note->has_a( owning_call_number => 'asset::call_number' );
293 #-------------------------------------------------------------------------------
294 asset::call_number->has_a( record => 'biblio::record_entry' );
295 asset::call_number->has_many( copies => 'asset::copy' );
296 asset::call_number->has_many( notes => 'asset::call_number_note' );
297 asset::call_number->has_a( creator => 'actor::user' );
298 asset::call_number->has_a( editor => 'actor::user' );
299 #-------------------------------------------------------------------------------
302 #-------------------------------------------------------------------------------
303 biblio::record_note->has_a( record => 'biblio::record_entry' );
304 #-------------------------------------------------------------------------------
305 biblio::record_mods->is_a( id => 'biblio::record_entry' );
306 #-------------------------------------------------------------------------------
307 biblio::record_marc->is_a( id => 'biblio::record_entry' );
308 #-------------------------------------------------------------------------------
309 biblio::record_entry->has_a( creator => 'actor::user' );
310 biblio::record_entry->has_a( editor => 'actor::user' );
311 biblio::record_entry->might_have( mods_entry => 'biblio::record_mods' => qw/mods/ );
312 biblio::record_entry->might_have( marc_entry => 'biblio::record_marc' => qw/marc/ );
313 biblio::record_entry->has_many( notes => 'biblio::record_note' );
314 biblio::record_entry->has_many( nodes => 'biblio::record_node', { order_by => 'intra_doc_id' } );
315 biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
317 # should we have just one field entry per class for each record???? (xslt vs xpath)
318 #biblio::record_entry->has_a( title_field_entries => 'metabib::title_field_entry' );
319 #biblio::record_entry->has_a( author_field_entries => 'metabib::author_field_entry' );
320 #biblio::record_entry->has_a( subject_field_entries => 'metabib::subject_field_entry' );
321 #biblio::record_entry->has_a( keyword_field_entries => 'metabib::keyword_field_entry' );
322 #-------------------------------------------------------------------------------
323 biblio::record_node->has_a( owner_doc => 'biblio::record_entry' );
324 #biblio::record_node->has_a(
325 # parent_node => 'biblio::record_node::subnode',
326 # inflate => sub { return biblio::record_node::subnode::_load(@_) }
328 #-------------------------------------------------------------------------------
330 #-------------------------------------------------------------------------------
331 metabib::full_rec->has_a( record => 'biblio::record_entry' );
332 #-------------------------------------------------------------------------------
333 metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
334 metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source_record'] );
335 #-------------------------------------------------------------------------------
336 metabib::title_field_entry->has_many( source_records => [ 'metabib::title_field_entry_source_map' => 'source_record'] );
337 metabib::title_field_entry->has_a( field => 'config::metabib_field' );
338 #-------------------------------------------------------------------------------
339 metabib::author_field_entry->has_many( source_records => [ 'metabib::author_field_entry_source_map' => 'source_record'] );
340 metabib::author_field_entry->has_a( field => 'config::metabib_field' );
341 #-------------------------------------------------------------------------------
342 metabib::subject_field_entry->has_many( source_records => [ 'metabib::title_field_entry_source_map' => 'source_record'] );
343 metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
344 #-------------------------------------------------------------------------------
345 metabib::keyword_field_entry->has_many( source_records => [ 'metabib::keyword_field_entry_source_map' => 'source_record'] );
346 metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
347 #-------------------------------------------------------------------------------
348 metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
349 metabib::metarecord_source_map->has_a( source_record => 'biblio::record_entry' );
350 #-------------------------------------------------------------------------------
353 # should we have just one field entry per class for each record???? (xslt vs xpath)
354 metabib::title_field_entry_source_map->has_a( field_entry => 'metabib::title_field_entry' );
355 metabib::title_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
356 metabib::title_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
357 #-------------------------------------------------------------------------------
358 metabib::subject_field_entry_source_map->has_a( field_entry => 'metabib::subject_field_entry' );
359 metabib::subject_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
360 metabib::subject_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
361 #-------------------------------------------------------------------------------
362 metabib::author_field_entry_source_map->has_a( field_entry => 'metabib::author_field_entry' );
363 metabib::author_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
364 metabib::author_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
365 #-------------------------------------------------------------------------------
366 metabib::keyword_field_entry_source_map->has_a( field_entry => 'metabib::keyword_field_entry' );
367 metabib::keyword_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
368 metabib::keyword_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
369 #-------------------------------------------------------------------------------