updating money handling; adding readonly fieldmapper classes (views); allow fields...
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage / CDBI.pm
1 package OpenILS::Application::Storage::CDBI;
2 use base qw/Class::DBI/;
3 use Class::DBI;
4 use Class::DBI::AbstractSearch;
5
6 use OpenILS::Application::Storage::CDBI::actor;
7 use OpenILS::Application::Storage::CDBI::action;
8 use OpenILS::Application::Storage::CDBI::asset;
9 use OpenILS::Application::Storage::CDBI::biblio;
10 use OpenILS::Application::Storage::CDBI::config;
11 use OpenILS::Application::Storage::CDBI::metabib;
12 use OpenILS::Application::Storage::CDBI::money;
13 use OpenILS::Application::Storage::CDBI::permission;
14
15 use OpenSRF::Utils::Logger;
16 use OpenSRF::EX qw/:try/;
17
18 our $VERSION = undef;
19 my $log = 'OpenSRF::Utils::Logger';
20
21 sub child_init {
22         my $self = shift;
23
24         $log->debug("Creating ImaDBI Querys", DEBUG);
25         __PACKAGE__->set_sql( 'OILSFastSearch', <<"     SQL", 'Main');
26                 SELECT  %s
27                   FROM  %s
28                   WHERE %s = ?
29         SQL
30
31         __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<"  SQL", 'Main');
32                 SELECT  %s
33                   FROM  %s
34                   WHERE %s LIKE ?
35                   ORDER BY %s
36         SQL
37
38         __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<"      SQL", 'Main');
39                 SELECT  %s
40                   FROM  %s
41                   WHERE %s = ?
42                   ORDER BY %s
43         SQL
44
45         $log->debug("Calling Driver child_init", DEBUG);
46         $self->SUPER::child_init(@_);
47
48 }
49
50 sub fast_flesh_sth {
51         my $class = shift;
52         $class = ref($class) || $class;
53
54         my $field = shift;
55         my $value = shift;
56         my $order = shift;
57         my $like = shift;
58
59
60         if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
61                 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
62                         $order = $value;
63                         $value = undef;
64                 } else {
65                         $order = { order_by => $class->columns('Primary') }
66                 }
67         }
68
69         unless (defined $value) {
70                 $value = $field;
71                 $field = $class->primary_column;
72         }
73
74         unless (defined $field) {
75                 $field = $class->primary_column;
76         }
77
78         unless ($order->{order_by}) {
79                 $order = { order_by => $class->columns('Primary') }
80         }
81
82         my $fm_class = 'Fieldmapper::'.$class;
83         my $field_list = join ',', $class->columns('All');
84         
85         my $sth;
86         if (!$like) {
87                 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
88         } else {
89                 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
90         }
91         $sth->execute($value);
92         return $sth;
93 }
94
95 sub fast_flesh {
96         my $self = shift;
97         return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
98 }
99
100 sub fast_fieldmapper {
101         my $self = shift;
102         my $id = shift;
103         my $col = shift;
104         my $like = shift;
105         my $options = shift;
106         my $class = ref($self) || $self;
107         my $fm_class = 'Fieldmapper::'.$class;
108         my @fms;
109         $log->debug("fast_fieldmapper() ==> Retrieving $fm_class", INTERNAL);
110         if ($like < 2) {
111                 for my $hash ($self->fast_flesh_sth( $col, "$id", { order_by => $col }, $like )->fetchall_hash) {
112                         my $fm = $fm_class->new;
113                         for my $field ( $fm_class->real_fields ) {
114                                 $fm->$field( $$hash{$field} );
115                         }
116                         push @fms, $fm;
117                 }
118         } else {
119                 my $search_type = 'search';
120                 if ($like == 2) {
121                         $search_type = 'search_fts'
122                 } elsif ($like == 3) {
123                         $search_type = 'search_regex'
124                 }
125
126                 for my $obj ($class->$search_type({ $col => $id}, $options)) {
127                         push @fms, $obj->to_fieldmapper;
128                 }
129         }
130         return @fms;
131 }
132
133 sub retrieve {
134         my $self = shift;
135         my $arg = shift;
136         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
137                 my $col = $arg->primary_column;
138                 $arg = $arg->$col;
139         }
140         $log->debug("Retrieving $self with $arg", INTERNAL);
141         my $rec;
142         try {
143                 $rec = $self->SUPER::retrieve("$arg");
144         } catch Error with {
145                 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
146                 return undef;
147         };
148         return $rec;
149 }
150
151 sub to_fieldmapper {
152         my $obj = shift;
153         my $class = ref($obj) || $obj;
154
155         my $fm_class = 'Fieldmapper::'.$class;
156         my $fm = $fm_class->new;
157
158         if (ref($obj)) {
159                 for my $field ( $fm->real_fields ) {
160                         $fm->$field( $obj->$field );
161                 }
162         }
163
164         return $fm;
165 }
166
167 sub create {
168         my $self = shift;
169         my $arg = shift;
170
171         $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
172
173         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
174                 return $self->create_from_fieldmapper($arg,@_);
175         }
176
177         return $self->SUPER::create($arg,@_);
178 }
179
180 sub create_from_fieldmapper {
181         my $obj = shift;
182         my $fm = shift;
183         my @params = @_;
184
185         $log->debug("Creating node of type ".ref($fm), DEBUG);
186
187         my $class = ref($obj) || $obj;
188         my $primary = $class->primary_column;
189
190         if (ref $fm) {
191                 my %hash = map { defined $fm->$_ ?
192                                         ($_ => $fm->$_) :
193                                         ()
194                                 } grep { $_ ne $primary } $class->columns('All');
195
196                 if ($class->find_column( 'last_xact_id' )) {
197                         my $xact_id = $class->current_xact_id;
198                         throw Error unless ($xact_id);
199                         $hash{last_xact_id} = $xact_id;
200                 }
201
202                 return $class->create( \%hash, @params );
203         } else {
204                 return undef;
205         }
206 }
207
208 sub delete {
209         my $self = shift;
210         my $arg = shift;
211
212         my $class = ref($self) || $self;
213
214         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
215                 $self = $self->retrieve($arg);
216                 unless (defined $self) {
217                         $log->debug("ARG! Couldn't retrieve record ".$arg->id, DEBUG);
218                         throw OpenSRF::EX::WARN ("ARG! Couldn't retrieve record ");
219                 }
220         }
221
222         if ($class->find_column( 'last_xact_id' )) {
223                 my $xact_id = $self->current_xact_id;
224                 throw Error unless ($xact_id);
225                 $self->last_xact_id( $class->current_xact_id );
226                 $self->SUPER::update;
227         }
228
229         $self->SUPER::delete;
230         return 1;
231 }
232
233 sub update {
234         my $self = shift;
235         my $arg = shift;
236
237         $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
238
239         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
240                 $self = $self->modify_from_fieldmapper($arg);
241                 $log->debug("Modification of $self seems to have failed....", DEBUG);
242                 return undef unless (defined $self);
243         }
244
245         $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
246         return $self->SUPER::update if ($self->is_changed);
247         return 0;
248 }
249
250 sub modify_from_fieldmapper {
251         my $obj = shift;
252         my $fm = shift;
253
254         $log->debug("Modifying object using fieldmapper", DEBUG);
255
256         my $class = ref($obj) || $obj;
257
258         if (!ref($obj)) {
259                 $obj = $class->retrieve($fm);
260                 unless ($obj) {
261                         $log->debug("Retrieve of $class using $fm (".$fm->id.") failed! -- ".shift(), ERROR);
262                         throw OpenSRF::EX::WARN ("No $class with id of ".$fm->id."!!");
263                 }
264         }
265
266         my %hash = map { defined $fm->$_ ?
267                                 ($_ => ''.$fm->$_) :
268                                 ()
269                         } $fm->real_fields;
270
271         my $au = $obj->autoupdate;
272         $obj->autoupdate(0);
273         
274         for my $field ( keys %hash ) {
275                 $obj->$field( $hash{$field} ) if ($obj->$field ne $hash{$field});
276                 $log->debug("Setting field $field on $obj to $hash{$field}",INTERNAL);
277         }
278
279         if ($class->find_column( 'last_xact_id' ) and $obj->is_changed) {
280                 my $xact_id = $obj->current_xact_id;
281                 throw Error unless ($xact_id);
282                 $obj->last_xact_id( $xact_id );
283         } else {
284                 $obj->autoupdate($au)
285         }
286
287         return $obj;
288 }
289
290
291
292         #-------------------------------------------------------------------------------
293         actor::user->has_a( home_ou => 'actor::org_unit' );
294         actor::user->has_a( card => 'actor::card' );
295         actor::user->has_a( standing => 'config::standing' );
296         actor::user->has_a( profile => 'actor::profile' );
297         actor::user->has_a( mailing_address => 'actor::user_address' );
298         actor::user->has_a( billing_address => 'actor::user_address' );
299         actor::user->has_a( ident_type => 'config::identification_type' );
300         actor::user->has_a( ident_type2 => 'config::identification_type' );
301         actor::user->has_a( net_access_level => 'config::net_access_level' );
302
303         actor::user_address->has_a( usr => 'actor::user' );
304         
305         actor::card->has_a( usr => 'actor::user' );
306         
307         actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
308         actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
309         #actor::org_unit->has_a( address => 'actor::org_address' );
310
311         actor::stat_cat_entry->has_a( stat_cat => 'actor::stat_cat' );
312         actor::stat_cat->has_many( entries => 'actor::stat_cat_entry' );
313         actor::stat_cat_entry_user_map->has_a( stat_cat => 'actor::stat_cat' );
314         actor::stat_cat_entry_user_map->has_a( stat_cat_entry => 'actor::stat_cat_entry' );
315         actor::stat_cat_entry_user_map->has_a( target_usr => 'actor::user' );
316
317         asset::stat_cat_entry->has_a( stat_cat => 'asset::stat_cat' );
318         asset::stat_cat->has_many( entries => 'asset::stat_cat_entry' );
319         asset::stat_cat_entry_copy_map->has_a( stat_cat => 'asset::stat_cat' );
320         asset::stat_cat_entry_copy_map->has_a( stat_cat_entry => 'asset::stat_cat_entry' );
321         asset::stat_cat_entry_copy_map->has_a( owning_copy => 'asset::copy' );
322
323         action::survey_response->has_a( usr => 'actor::user' );
324         action::survey_response->has_a( survey => 'action::survey' );
325         action::survey_response->has_a( question => 'action::survey_question' );
326         action::survey_response->has_a( answer => 'action::survey_answer' );
327
328         action::survey_question->has_a( survey => 'action::survey' );
329
330         action::survey_answer->has_a( question => 'action::survey' );
331
332         asset::copy_note->has_a( owning_copy => 'asset::copy' );
333
334         actor::user->has_many( stat_cat_entries => [ 'actor::stat_cat_entry_user_map' => 'stat_cat_entry' ] );
335         actor::user->has_many( stat_cat_entry_user_maps => 'actor::stat_cat_entry_user_map' );
336
337         asset::copy->has_many( stat_cat_entries => [ 'asset::stat_cat_entry_copy_map' => 'stat_cat_entry' ] );
338         asset::copy->has_many( stat_cat_entry_copy_maps => 'asset::stat_cat_entry_copy_map' );
339
340         asset::copy->has_a( call_number => 'asset::call_number' );
341         asset::copy->has_a( creator => 'actor::user' );
342         asset::copy->has_a( editor => 'actor::user' );
343         asset::copy->has_a( status => 'config::copy_status' );
344         asset::copy->has_a( location => 'asset::copy_location' );
345         asset::copy->has_a( circ_lib => 'actor::org_unit' );
346
347         asset::call_number_note->has_a( call_number => 'asset::call_number' );
348
349         asset::call_number->has_a( record => 'biblio::record_entry' );
350         asset::call_number->has_a( creator => 'actor::user' );
351         asset::call_number->has_a( editor => 'actor::user' );
352
353         biblio::record_note->has_a( record => 'biblio::record_entry' );
354         
355         biblio::record_entry->has_a( creator => 'actor::user' );
356         biblio::record_entry->has_a( editor => 'actor::user' );
357         
358         metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
359         
360         metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
361         
362         metabib::full_rec->has_a( record => 'biblio::record_entry' );
363         
364         metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
365         metabib::title_field_entry->has_a( field => 'config::metabib_field' );
366         
367         metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
368         metabib::author_field_entry->has_a( field => 'config::metabib_field' );
369         
370         metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
371         metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
372         
373         metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
374         metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
375         
376         metabib::series_field_entry->has_a( source => 'biblio::record_entry' );
377         metabib::series_field_entry->has_a( field => 'config::metabib_field' );
378         
379         metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
380         metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
381
382         action::circulation->has_a( usr => 'actor::user' );
383         action::circulation->has_a( target_copy => 'asset::copy' );
384         action::circulation->has_a( circ_lib => 'actor::org_unit' );
385
386         money::billable_transaction->has_a( usr => 'actor::user' );
387         
388         
389         #-------------------------------------------------------------------------------
390         actor::user->has_many( survey_responses => 'action::survey_response' );
391         actor::user->has_many( addresses => 'actor::user_address' );
392         actor::user->has_many( cards => 'actor::card' );
393
394         actor::org_unit->has_many( users => 'actor::user' );
395         actor::profile->has_many( users => 'actor::user' );
396
397         action::survey->has_many( questions => 'action::survey_question' );
398         action::survey->has_many( responses => 'action::survey_response' );
399         
400         action::survey_question->has_many( answers => 'action::survey_answer' );
401         action::survey_question->has_many( responses => 'action::survey_response' );
402
403         action::survey_answer->has_many( responses => 'action::survey_response' );
404
405         asset::copy->has_many( notes => 'asset::copy_note' );
406         asset::call_number->has_many( copies => 'asset::copy' );
407         asset::call_number->has_many( notes => 'asset::call_number_note' );
408
409         biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
410         biblio::record_entry->has_many( notes => 'biblio::record_note' );
411         biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
412         biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
413         biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
414         biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
415         biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
416         biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
417         biblio::record_entry->has_many( series_field_entries => 'metabib::series_field_entry' );
418
419         metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
420
421         money::billable_transaction->has_many( billings => 'money::billing' );
422         money::billable_transaction->has_many( payments => 'money::payment' );
423
424         money::billing->has_a( xact => 'money::billable_transaction' );
425         money::payment->has_a( xact => 'money::billable_transaction' );
426
427         money::cash_payment->has_a( xact => 'money::billable_transaction' );
428         money::cash_payment->has_a( accepting_usr => 'actor::user' );
429
430         money::check_payment->has_a( xact => 'money::billable_transaction' );
431         money::check_payment->has_a( accepting_usr => 'actor::user' );
432
433         money::credit_card_payment->has_a( xact => 'money::billable_transaction' );
434         money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
435
436         money::forgive_payment->has_a( xact => 'money::billable_transaction' );
437         money::forgive_payment->has_a( accepting_usr => 'actor::user' );
438
439         money::work_payment->has_a( xact => 'money::billable_transaction' );
440         money::work_payment->has_a( accepting_usr => 'actor::user' );
441
442         money::credit_payment->has_a( xact => 'money::billable_transaction' );
443         money::credit_payment->has_a( accepting_usr => 'actor::user' );
444
445         permission::grp_tree->has_a( parent => 'permission::grp_tree' );
446
447         permission::grp_perm_map->has_a( grp => 'permission::grp_tree' );
448         permission::grp_perm_map->has_a(  perm => 'permission::perm_list' );
449         permission::grp_perm_map->has_a(  depth => 'actor::org_unit_type' );
450         
451         permission::usr_perm_map->has_a( usr => 'actor::user' );
452         permission::usr_perm_map->has_a(  perm => 'permission::perm_list' );
453         permission::usr_perm_map->has_a(  depth => 'actor::org_unit_type' );
454         
455         permission::usr_grp_map->has_a(  usr => 'actor::user' );
456         permission::usr_grp_map->has_a(  grp => 'permission::grp_tree' );
457
458         action::hold_notification->has_a(  hold => 'action::hold_request' );
459         
460         action::hold_copy_map->has_a(  hold => 'action::hold_request' );
461         action::hold_copy_map->has_a(  target_copy => 'asset::copy' );
462
463         action::hold_request->has_a(  current_copy => 'asset::copy' );
464         action::hold_request->has_a(  requestor => 'actor::user' );
465         action::hold_request->has_a(  usr => 'actor::user' );
466         action::hold_request->has_a(  pickup_lib => 'actor::org_unit' );
467
468         action::hold_request->has_many(  notifications => 'action::hold_notification' );
469         action::hold_request->has_many(  copy_maps => 'action::hold_copy_map' );
470
471         asset::copy->has_many(  hold_maps => 'action::hold_copy_map' );
472
473 1;