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( 'OILSFastOrderedSearch', <<" SQL", 'Main');
34 $log->debug("Calling Driver child_init", DEBUG);
35 $self->SUPER::child_init(@_);
41 $class = ref($class) || $class;
47 if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
48 if (defined($value) and ref($order) and ref($order) eq 'HASH') {
51 $field = $class->primary_column;
53 $order = { order_by => 'id' }
57 unless (defined $value) {
59 $field = $class->primary_column;
62 my $fm_class = 'Fieldmapper::'.$class;
63 my $field_list = join ',', $class->columns('All');
65 my $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
66 $sth->execute($value);
72 return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
75 sub fast_fieldmapper {
77 my $class = ref($self) || $self;
78 my $fm_class = 'Fieldmapper::'.$class;
80 $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
81 for my $hash ($self->fast_flesh_sth(map {"$_"} @_)->fetchall_hash) {
82 my $fm = $fm_class->new;
83 for my $field ( keys %$hash ) {
84 $fm->$field( $$hash{$field} );
94 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
97 $log->debug("Retrieving $self with $arg", INTERNAL);
98 my $rec = $self->SUPER::retrieve("$arg");
100 $log->debug("Could not retrieve $self with $arg!", DEBUG);
108 my $class = ref($obj) || $obj;
110 my $fm_class = 'Fieldmapper::'.$class;
111 my $fm = $fm_class->new;
114 for my $field ( $fm->real_fields ) {
115 $fm->$field( $obj->$field );
126 $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
128 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
129 return $self->create_from_fieldmapper($arg,@_);
132 return $self->SUPER::create($arg,@_);
135 sub create_from_fieldmapper {
140 $log->debug("Creating node of type ".ref($fm), DEBUG);
142 my $class = ref($obj) || $obj;
145 my %hash = map { defined $fm->$_ ?
150 if ($class->find_column( 'last_xact_id' )) {
151 my $xact_id = $class->current_xact_id;
152 throw Error unless ($xact_id);
153 $hash{last_xact_id} = $xact_id;
156 return $class->create( \%hash, @params );
166 my $class = ref($self) || $self;
168 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
169 $self = $self->retrieve($arg);
170 unless (defined $self) {
171 $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
172 throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
176 if ($class->find_column( 'last_xact_id' )) {
177 my $xact_id = $self->current_xact_id;
178 throw Error unless ($xact_id);
179 $self->last_xact_id( $class->current_xact_id );
180 $self->SUPER::update;
183 $self->SUPER::delete;
191 $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
193 if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
194 $self = $self->modify_from_fieldmapper($arg);
195 $log->debug("Modification of $self seems to have failed....", DEBUG);
196 return undef unless (defined $self);
199 $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
200 return $self->SUPER::update if ($self->is_changed);
204 sub modify_from_fieldmapper {
208 $log->debug("Modifying object using fieldmapper", DEBUG);
210 my $class = ref($obj) || $obj;
213 $obj = $class->retrieve($fm);
215 $log->debug("Rretrieve using $fm (".$fm->id.") failed!", ERROR);
216 throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
221 my %hash = map { defined $fm->$_ ?
226 my $au = $obj->autoupdate;
229 for my $field ( keys %hash ) {
230 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
231 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
234 if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
235 my $xact_id = $obj->current_xact_id;
236 throw Error unless ($xact_id);
237 $obj->last_xact_id( $xact_id );
239 $obj->autoupdate($au)
248 return if ($VERSION);
249 #-------------------------------------------------------------------------------
250 actor::user->has_a( home_ou => 'actor::org_unit' );
251 #actor::org_unit->has_a( address => 'actor::address' );
252 #-------------------------------------------------------------------------------
253 actor::org_unit->has_many( users => 'actor::user' );
254 actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
255 actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
256 #actor::org_unit->has_a( address => 'actor::address' );
257 #-------------------------------------------------------------------------------
259 #-------------------------------------------------------------------------------
260 asset::copy->has_a( call_number => 'asset::call_number' );
261 #asset::copy->might_have( metadata => 'asset::copy_metadata' );
262 #-------------------------------------------------------------------------------
263 #asset::copy_metadata->might_have( copy => 'asset::copy' );
264 asset::copy_metadata->has_a( circulating_location => 'actor::org_unit');
265 asset::copy_metadata->has_a( hold_radius => 'actor::org_unit_type');
266 #-------------------------------------------------------------------------------
267 asset::call_number->has_a( record => 'biblio::record_entry' );
268 asset::call_number->has_many( copies => 'asset::copy' );
269 #-------------------------------------------------------------------------------
272 #-------------------------------------------------------------------------------
273 biblio::record_note->has_a( record => 'biblio::record_entry' );
274 #-------------------------------------------------------------------------------
275 biblio::record_mods->is_a( id => 'biblio::record_entry' );
276 #-------------------------------------------------------------------------------
277 biblio::record_marc->is_a( id => 'biblio::record_entry' );
278 #-------------------------------------------------------------------------------
279 biblio::record_entry->has_a( creator => 'actor::user' );
280 biblio::record_entry->has_a( editor => 'actor::user' );
281 biblio::record_entry->might_have( mods_entry => 'biblio::record_mods' => qw/mods/ );
282 biblio::record_entry->might_have( marc_entry => 'biblio::record_marc' => qw/marc/ );
283 biblio::record_entry->has_many( notes => 'biblio::record_note' );
284 biblio::record_entry->has_many( nodes => 'biblio::record_node', { order_by => 'intra_doc_id' } );
285 biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
287 # should we have just one field entry per class for each record???? (xslt vs xpath)
288 #biblio::record_entry->has_a( title_field_entries => 'metabib::title_field_entry' );
289 #biblio::record_entry->has_a( author_field_entries => 'metabib::author_field_entry' );
290 #biblio::record_entry->has_a( subject_field_entries => 'metabib::subject_field_entry' );
291 #biblio::record_entry->has_a( keyword_field_entries => 'metabib::keyword_field_entry' );
292 #-------------------------------------------------------------------------------
293 biblio::record_node->has_a( owner_doc => 'biblio::record_entry' );
294 #biblio::record_node->has_a(
295 # parent_node => 'biblio::record_node::subnode',
296 # inflate => sub { return biblio::record_node::subnode::_load(@_) }
298 #-------------------------------------------------------------------------------
300 #-------------------------------------------------------------------------------
301 metabib::full_rec->has_a( record => 'biblio::record_entry' );
302 #-------------------------------------------------------------------------------
303 metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
304 metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source_record'] );
305 #-------------------------------------------------------------------------------
306 metabib::title_field_entry->has_many( source_records => [ 'metabib::title_field_entry_source_map' => 'source_record'] );
307 metabib::title_field_entry->has_a( field => 'config::metabib_field' );
308 #-------------------------------------------------------------------------------
309 metabib::author_field_entry->has_many( source_records => [ 'metabib::author_field_entry_source_map' => 'source_record'] );
310 metabib::author_field_entry->has_a( field => 'config::metabib_field' );
311 #-------------------------------------------------------------------------------
312 metabib::subject_field_entry->has_many( source_records => [ 'metabib::title_field_entry_source_map' => 'source_record'] );
313 metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
314 #-------------------------------------------------------------------------------
315 metabib::keyword_field_entry->has_many( source_records => [ 'metabib::keyword_field_entry_source_map' => 'source_record'] );
316 metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
317 #-------------------------------------------------------------------------------
318 metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
319 metabib::metarecord_source_map->has_a( source_record => 'biblio::record_entry' );
320 #-------------------------------------------------------------------------------
323 # should we have just one field entry per class for each record???? (xslt vs xpath)
324 metabib::title_field_entry_source_map->has_a( field_entry => 'metabib::title_field_entry' );
325 metabib::title_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
326 metabib::title_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
327 #-------------------------------------------------------------------------------
328 metabib::subject_field_entry_source_map->has_a( field_entry => 'metabib::subject_field_entry' );
329 metabib::subject_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
330 metabib::subject_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
331 #-------------------------------------------------------------------------------
332 metabib::author_field_entry_source_map->has_a( field_entry => 'metabib::author_field_entry' );
333 metabib::author_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
334 metabib::author_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
335 #-------------------------------------------------------------------------------
336 metabib::keyword_field_entry_source_map->has_a( field_entry => 'metabib::keyword_field_entry' );
337 metabib::keyword_field_entry_source_map->has_a( source_record => 'biblio::record_entry' );
338 metabib::keyword_field_entry_source_map->has_a( metarecord => 'metabib::metarecord' );
339 #-------------------------------------------------------------------------------