]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage/CDBI.pm
big changes... big
[working/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
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
13 use OpenSRF::Utils::Logger;
14 use OpenSRF::EX qw/:try/;
15
16 our $VERSION = undef;
17 my $log = 'OpenSRF::Utils::Logger';
18
19 sub child_init {
20         my $self = shift;
21
22         $log->debug("Creating ImaDBI Querys", DEBUG);
23         __PACKAGE__->set_sql( 'OILSFastSearch', <<"     SQL", 'Main');
24                 SELECT  %s
25                   FROM  %s
26                   WHERE %s = ?
27         SQL
28
29         __PACKAGE__->set_sql( 'OILSFastOrderedSearchLike', <<"  SQL", 'Main');
30                 SELECT  %s
31                   FROM  %s
32                   WHERE %s ~ ?
33                   ORDER BY %s
34         SQL
35
36         __PACKAGE__->set_sql( 'OILSFastOrderedSearch', <<"      SQL", 'Main');
37                 SELECT  %s
38                   FROM  %s
39                   WHERE %s = ?
40                   ORDER BY %s
41         SQL
42
43         $log->debug("Calling Driver child_init", DEBUG);
44         $self->SUPER::child_init(@_);
45
46 }
47
48 sub fast_flesh_sth {
49         my $class = shift;
50         $class = ref($class) || $class;
51
52         my $field = shift;
53         my $value = shift;
54         my $order = shift;
55         my $like = shift;
56
57
58         if (!(defined($order) and ref($order) and ref($order) eq 'HASH')) {
59                 if (defined($value) and ref($value) and ref($value) eq 'HASH') {
60                         $order = $value;
61                         $value = undef;
62                 } else {
63                         $order = { order_by => $class->columns('Primary') }
64                 }
65         }
66
67         unless (defined $value) {
68                 $value = $field;
69                 $field = $class->primary_column;
70         }
71
72         unless (defined $field) {
73                 $field = $class->primary_column;
74         }
75
76         unless ($order->{order_by}) {
77                 $order = { order_by => $class->columns('Primary') }
78         }
79
80         my $fm_class = 'Fieldmapper::'.$class;
81         my $field_list = join ',', $class->columns('All');
82         
83         my $sth;
84         if (!$like) {
85                 $sth = $class->sql_OILSFastOrderedSearch( $field_list, $class->table, $field, $order->{order_by});
86         } else {
87                 $sth = $class->sql_OILSFastOrderedSearchLike( $field_list, $class->table, $field, $order->{order_by});
88         }
89         $sth->execute($value);
90         return $sth;
91 }
92
93 sub fast_flesh {
94         my $self = shift;
95         return map $class->construct($_), $self->fast_flesh_sth(@_)->fetchall_hash;
96 }
97
98 sub fast_fieldmapper {
99         my $self = shift;
100         my $id = shift;
101         my $col = shift;
102         my $like = shift;
103         my $class = ref($self) || $self;
104         my $fm_class = 'Fieldmapper::'.$class;
105         my @fms;
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} );
111                 }
112                 push @fms, $fm;
113         }
114         return @fms;
115 }
116
117 sub retrieve {
118         my $self = shift;
119         my $arg = shift;
120         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
121                 $arg = $arg->id;
122         }
123         $log->debug("Retrieving $self with $arg", INTERNAL);
124         my $rec;
125         try {
126                 $rec = $self->SUPER::retrieve("$arg");
127         } catch Error with {
128                 $log->debug("Could not retrieve $self with $arg! -- ".shift(), DEBUG);
129                 return undef;
130         };
131         return $rec;
132 }
133
134 sub to_fieldmapper {
135         my $obj = shift;
136         my $class = ref($obj) || $obj;
137
138         my $fm_class = 'Fieldmapper::'.$class;
139         my $fm = $fm_class->new;
140
141         if (ref($obj)) {
142                 for my $field ( $fm->real_fields ) {
143                         $fm->$field( $obj->$field );
144                 }
145         }
146
147         return $fm;
148 }
149
150 sub create {
151         my $self = shift;
152         my $arg = shift;
153
154         $log->debug("\$arg is $arg (".ref($arg).")",DEBUG);
155
156         if (ref($arg) and UNIVERSAL::isa($arg => 'Fieldmapper')) {
157                 return $self->create_from_fieldmapper($arg,@_);
158         }
159
160         return $self->SUPER::create($arg,@_);
161 }
162
163 sub create_from_fieldmapper {
164         my $obj = shift;
165         my $fm = shift;
166         my @params = @_;
167
168         $log->debug("Creating node of type ".ref($fm), DEBUG);
169
170         my $class = ref($obj) || $obj;
171         my $primary = $class->primary_column;
172
173         if (ref $fm) {
174                 my %hash = map { defined $fm->$_ ?
175                                         ($_ => $fm->$_) :
176                                         ()
177                                 } grep { $_ ne $primary } $class->columns('All');
178
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;
183                 }
184
185                 return $class->create( \%hash, @params );
186         } else {
187                 return undef;
188         }
189 }
190
191 sub delete {
192         my $self = shift;
193         my $arg = shift;
194
195         my $class = ref($self) || $self;
196
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 ");
202                 }
203         }
204
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;
210         }
211
212         $self->SUPER::delete;
213         return 1;
214 }
215
216 sub update {
217         my $self = shift;
218         my $arg = shift;
219
220         $log->debug("Attempting to update using $arg", DEBUG) if ($arg);
221
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);
226         }
227
228         $log->debug("Calling Class::DBI->update on modified object $self", DEBUG);
229         return $self->SUPER::update if ($self->is_changed);
230         return 0;
231 }
232
233 sub modify_from_fieldmapper {
234         my $obj = shift;
235         my $fm = shift;
236
237         $log->debug("Modifying object using fieldmapper", DEBUG);
238
239         my $class = ref($obj) || $obj;
240
241         if (!ref($obj)) {
242                 $obj = $class->retrieve($fm);
243                 unless ($obj) {
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."!!");
246                 }
247         }
248
249         my %hash = map { defined $fm->$_ ?
250                                 ($_ => $fm->$_) :
251                                 ()
252                         } $fm->real_fields;
253
254         my $au = $obj->autoupdate;
255         $obj->autoupdate(0);
256         
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);
260         }
261
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 );
266         } else {
267                 $obj->autoupdate($au)
268         }
269
270         return $obj;
271 }
272
273
274
275         #-------------------------------------------------------------------------------
276         actor::user->has_a( home_ou => 'actor::org_unit' );
277         actor::user->has_a( card => 'actor::card' );
278         actor::user->has_a( standing => 'config::standing' );
279         actor::user->has_a( profile => 'actor::profile' );
280         actor::user->has_a( mailing_address => 'actor::user_address' );
281         actor::user->has_a( billing_address => 'actor::user_address' );
282         actor::user->has_a( ident_type => 'config::identification_type' );
283         actor::user->has_a( ident_type2 => 'config::identification_type' );
284         actor::user->has_a( net_access_level => 'config::net_access_level' );
285
286         actor::user_address->has_a( usr => 'actor::user' );
287         
288         actor::card->has_a( usr => 'actor::user' );
289         
290         actor::org_unit->has_a( parent_ou => 'actor::org_unit' );
291         actor::org_unit->has_a( ou_type => 'actor::org_unit_type' );
292         #actor::org_unit->has_a( address => 'actor::org_address' );
293
294         action::survey_response->has_a( usr => 'actor::user' );
295         action::survey_response->has_a( survey => 'action::survey' );
296         action::survey_response->has_a( question => 'action::survey_question' );
297         action::survey_response->has_a( answer => 'action::survey_answer' );
298
299         action::survey_question->has_a( survey => 'action::survey' );
300
301         action::survey_answer->has_a( question => 'action::survey' );
302
303         asset::copy_note->has_a( owning_copy => 'asset::copy' );
304
305         asset::copy->has_a( call_number => 'asset::call_number' );
306         asset::copy->has_a( creator => 'actor::user' );
307         asset::copy->has_a( editor => 'actor::user' );
308         asset::copy->has_a( status => 'config::copy_status' );
309         asset::copy->has_a( location => 'asset::copy_location' );
310
311         asset::call_number_note->has_a( owning_call_number => 'asset::call_number' );
312
313         asset::call_number->has_a( record => 'biblio::record_entry' );
314         asset::call_number->has_a( creator => 'actor::user' );
315         asset::call_number->has_a( editor => 'actor::user' );
316
317         biblio::record_note->has_a( record => 'biblio::record_entry' );
318         
319         biblio::record_entry->has_a( creator => 'actor::user' );
320         biblio::record_entry->has_a( editor => 'actor::user' );
321         
322         metabib::metarecord->has_a( master_record => 'biblio::record_entry' );
323         
324         metabib::record_descriptor->has_a( record => 'biblio::record_entry' );
325         
326         metabib::full_rec->has_a( record => 'biblio::record_entry' );
327         
328         metabib::title_field_entry->has_a( source => 'biblio::record_entry' );
329         metabib::title_field_entry->has_a( field => 'config::metabib_field' );
330         
331         metabib::author_field_entry->has_a( source => 'biblio::record_entry' );
332         metabib::author_field_entry->has_a( field => 'config::metabib_field' );
333         
334         metabib::subject_field_entry->has_a( source => 'biblio::record_entry' );
335         metabib::subject_field_entry->has_a( field => 'config::metabib_field' );
336         
337         metabib::keyword_field_entry->has_a( source => 'biblio::record_entry' );
338         metabib::keyword_field_entry->has_a( field => 'config::metabib_field' );
339         
340         metabib::metarecord_source_map->has_a( metarecord => 'metabib::metarecord' );
341         metabib::metarecord_source_map->has_a( source => 'biblio::record_entry' );
342
343         action::circulation->has_a( usr => 'actor::user' );
344         action::circulation->has_a( target_copy => 'asset::copy' );
345         action::circulation->has_a( circ_lib => 'actor::org_unit' );
346
347         money::billable_transaction->has_a( usr => 'actor::user' );
348         
349         
350         #-------------------------------------------------------------------------------
351         actor::user->has_many( survey_responses => 'action::survey_response' );
352         actor::user->has_many( addresses => 'actor::user_address' );
353         actor::user->has_many( cards => 'actor::card' );
354
355         actor::org_unit->has_many( users => 'actor::user' );
356         actor::profile->has_many( users => 'actor::user' );
357
358         action::survey->has_many( questions => 'action::survey_question' );
359         action::survey->has_many( responses => 'action::survey_response' );
360         
361         action::survey_question->has_many( answers => 'action::survey_answer' );
362         action::survey_question->has_many( responses => 'action::survey_response' );
363
364         action::survey_answer->has_many( responses => 'action::survey_response' );
365
366         asset::copy->has_many( notes => 'asset::copy_note' );
367         asset::call_number->has_many( copies => 'asset::copy' );
368         asset::call_number->has_many( notes => 'asset::call_number_note' );
369
370         biblio::record_entry->has_many( record_descriptor => 'metabib::record_descriptor' );
371         biblio::record_entry->has_many( notes => 'biblio::record_note' );
372         biblio::record_entry->has_many( call_numbers => 'asset::call_number' );
373         biblio::record_entry->has_many( full_record_entries => 'metabib::full_rec' );
374         biblio::record_entry->has_many( title_field_entries => 'metabib::title_field_entry' );
375         biblio::record_entry->has_many( author_field_entries => 'metabib::author_field_entry' );
376         biblio::record_entry->has_many( subject_field_entries => 'metabib::subject_field_entry' );
377         biblio::record_entry->has_many( keyword_field_entries => 'metabib::keyword_field_entry' );
378
379         metabib::metarecord->has_many( source_records => [ 'metabib::metarecord_source_map' => 'source'] );
380
381         money::billable_transaction->has_many( billings => 'money::billing' );
382         money::billable_transaction->has_many( payments => 'money::payment' );
383
384         money::billing->has_a( xact => 'money::billable_transaction' );
385         money::payment->has_a( xact => 'money::billable_transaction' );
386
387         money::cash_payment->has_a( xact => 'money::billable_transaction' );
388         money::cash_payment->has_a( accepting_usr => 'actor::user' );
389
390         money::check_payment->has_a( xact => 'money::billable_transaction' );
391         money::check_payment->has_a( accepting_usr => 'actor::user' );
392
393         money::credit_card_payment->has_a( xact => 'money::billable_transaction' );
394         money::credit_card_payment->has_a( accepting_usr => 'actor::user' );
395
396         money::forgive_payment->has_a( xact => 'money::billable_transaction' );
397         money::forgive_payment->has_a( accepting_usr => 'actor::user' );
398
399         money::work_payment->has_a( xact => 'money::billable_transaction' );
400         money::work_payment->has_a( accepting_usr => 'actor::user' );
401
402         money::credit_payment->has_a( xact => 'money::billable_transaction' );
403         money::credit_payment->has_a( accepting_usr => 'actor::user' );
404
405 1;