updates to support corner cases in circ
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / Fieldmapper.pm
1 package Fieldmapper;
2 use JSON;
3 use Data::Dumper;
4 use base 'OpenSRF::Application';
5
6 use OpenSRF::Utils::Logger;
7 my $log = 'OpenSRF::Utils::Logger';
8
9 use OpenILS::Application::Storage::CDBI;
10 use OpenILS::Application::Storage::CDBI::actor;
11 use OpenILS::Application::Storage::CDBI::action;
12 use OpenILS::Application::Storage::CDBI::asset;
13 use OpenILS::Application::Storage::CDBI::biblio;
14 use OpenILS::Application::Storage::CDBI::config;
15 use OpenILS::Application::Storage::CDBI::metabib;
16 use OpenILS::Application::Storage::CDBI::money;
17 use OpenILS::Application::Storage::CDBI::container;
18
19 use vars qw/$fieldmap $VERSION/;
20
21 _init();
22
23 sub publish_fieldmapper {
24         my ($self,$client,$class) = @_;
25
26         return $fieldmap unless (defined $class);
27         return undef unless (exists($$fieldmap{$class}));
28         return {$class => $$fieldmap{$class}};
29 }
30 __PACKAGE__->register_method(
31         api_name        => 'opensrf.open-ils.system.fieldmapper',
32         api_level       => 1,
33         method          => 'publish_fieldmapper',
34 );
35
36 #
37 # To dump the Javascript version of the fieldmapper struct use the command:
38 #
39 #       PERL5LIB=~/cvs/ILS/OpenSRF/src/perlmods/:~/cvs/ILS/Open-ILS/src/perlmods/ GEN_JS=1 perl -MOpenILS::Utils::Fieldmapper -e 'print "\n";'
40 #
41 # ... adjusted for your CVS sandbox, of course.
42 #
43
44 sub classes {
45         return () unless (defined $fieldmap);
46         return keys %$fieldmap;
47 }
48
49 sub _init {
50         return if (keys %$fieldmap);
51
52         $fieldmap = 
53         {
54                 'Fieldmapper::container::user_bucket'           => { hint => 'cub',
55                                                                      proto_fields       => { items => 1 } },
56                 'Fieldmapper::container::copy_bucket'           => { hint => 'ccb',
57                                                                      proto_fields       => { items => 1 } },
58                 'Fieldmapper::container::call_number_bucket'    => { hint => 'ccnb',
59                                                                      proto_fields       => { items => 1 } },
60                 'Fieldmapper::container::biblio_record_entry_bucket'            => { hint => 'cbreb',
61                                                                                      proto_fields       => { items => 1 } },
62
63                 'Fieldmapper::container::user_bucket_item'              => { hint => 'cubi'   },
64                 'Fieldmapper::container::copy_bucket_item'              => { hint => 'ccbi'   },
65                 'Fieldmapper::container::call_number_bucket_item'       => { hint => 'ccnbi'   },
66                 'Fieldmapper::container::biblio_record_entry_bucket_item'               => { hint => 'cbrebi'   },
67
68                 'Fieldmapper::action::in_house_use'             => { hint => 'aihu'   },
69                 'Fieldmapper::action::non_cataloged_circulation'=> { hint => 'ancc'   },
70
71                 'Fieldmapper::action::survey'                   => { hint               => 'asv',
72                                                                      proto_fields       => { questions  => 1,
73                                                                                              responses  => 1 } },
74                 'Fieldmapper::action::survey_question'          => { hint               => 'asvq',
75                                                                      proto_fields       => { answers    => 1,
76                                                                                              responses  => 1 } },
77                 'Fieldmapper::action::survey_answer'            => { hint               => 'asva',
78                                                                      proto_fields       => { responses => 1 } },
79                 'Fieldmapper::action::survey_response'          => { hint               => 'asvr'  },
80                 'Fieldmapper::action::circulation'              => { hint               => 'circ',
81                                                                      proto_fields       => {} },
82
83                 'Fieldmapper::action::open_circulation'         => { hint       => 'aoc',
84                                                                      readonly   => 1 },
85
86                 'Fieldmapper::actor::user'                      => { hint               => 'au',
87                                                                      proto_fields       => { cards              => 1,
88                                                                                              survey_responses   => 1,
89                                                                                              stat_cat_entries   => 1,
90                                                                                              checkouts          => 1,
91                                                                                              hold_requests      => 1,
92                                                                                              settings           => 1,
93                                                                                              addresses          => 1 } },
94                 'Fieldmapper::actor::user_setting'              => { hint => 'aus'    },
95                 'Fieldmapper::actor::org_unit_setting'          => { hint => 'aous'    },
96                 'Fieldmapper::actor::user_address'              => { hint => 'aua'    },
97                 'Fieldmapper::actor::org_address'               => { hint => 'aoa'    },
98                 'Fieldmapper::actor::profile'                   => { hint => 'ap'    },
99                 'Fieldmapper::actor::card'                      => { hint => 'ac'    },
100                 'Fieldmapper::actor::stat_cat'                  => { hint               => 'actsc',
101                                                                      proto_fields       => { entries => 1 } },
102                 'Fieldmapper::actor::stat_cat_entry'            => { hint => 'actsce'    },
103                 'Fieldmapper::actor::stat_cat_entry_user_map'   => { hint => 'actscecm'  },
104                 'Fieldmapper::actor::org_unit'                  => { hint               => 'aou',
105                                                                      proto_fields       => { children => 1 } },
106                 'Fieldmapper::actor::org_unit_type'             => { hint               => 'aout',
107                                                                      proto_fields       => { children => 1 } },
108                 
109                 'Fieldmapper::biblio::record_node'              => { hint               => 'brn',
110                                                                      virtual            => 1,
111                                                                      proto_fields       => { children           => 1,
112                                                                                              id                 => 1,
113                                                                                              owner_doc          => 1,
114                                                                                              intra_doc_id       => 1,
115                                                                                              parent_node        => 1,
116                                                                                              node_type          => 1,
117                                                                                              namespace_uri      => 1,
118                                                                                              name               => 1,
119                                                                                              value              => 1,
120                                                                                            } },
121
122                 'Fieldmapper::metabib::virtual_record'          => { hint               => 'mvr',
123                                                                      virtual            => 1,
124                                                                      proto_fields       => { title              => 1,
125                                                                                              author             => 1,
126                                                                                              doc_id             => 1,
127                                                                                              doc_type           => 1,
128                                                                                              isbn               => 1,
129                                                                                              pubdate            => 1,
130                                                                                              publisher          => 1,
131                                                                                              tcn                => 1,
132                                                                                              subject            => 1,
133                                                                                              types_of_resource  => 1,
134                                                                                              call_numbers       => 1,
135                                                                                                           edition       => 1,
136                                                                                                           online_loc    => 1,
137                                                                                                           synopsis      => 1,
138                                                                                              copy_count         => 1,
139                                                                                              series             => 1,
140                                                                                              serials            => 1,
141                                                                                            } },
142
143                 'Fieldmapper::authority::record_entry'          => { hint               => 'are', },
144                 'Fieldmapper::authority::record_note'           => { hint               => 'arn', },
145                 'Fieldmapper::biblio::record_entry'             => { hint               => 'bre',
146                                                                      proto_fields       => { call_numbers => 1,
147                                                                                              fixed_fields => 1 } },
148                 #'Fieldmapper::biblio::record_marc'             => { hint => 'brx'  }, # now it's inside record_entry
149
150                 'Fieldmapper::money::payment'                   => { hint => 'mp',
151                                                                      readonly   => 1 },
152
153                 'Fieldmapper::money::cash_payment'              => { hint => 'mcp'  },
154                 'Fieldmapper::money::check_payment'             => { hint => 'mckp'  },
155                 'Fieldmapper::money::credit_payment'            => { hint => 'mcrp'  },
156                 'Fieldmapper::money::credit_card_payment'       => { hint => 'mccp'  },
157                 'Fieldmapper::money::forgive_payment'           => { hint => 'mfp'  },
158                 'Fieldmapper::money::work_payment'              => { hint => 'mwp'  },
159
160                 'Fieldmapper::money::billing'                   => { hint => 'mb'  },
161                 'Fieldmapper::money::billable_transaction'      => { hint => 'mbt'  },
162                 'Fieldmapper::money::grocery'                   => { hint => 'mg'  },
163
164                 'Fieldmapper::money::open_user_summary'         => { hint       => 'mous',
165                                                                      readonly   => 1 },
166                 'Fieldmapper::money::user_summary'              => { hint       => 'mus',
167                                                                      readonly   => 1 },
168
169                 'Fieldmapper::money::open_user_circulation_summary'     => { hint       => 'moucs',
170                                                                              readonly   => 1 },
171                 'Fieldmapper::money::user_circulation_summary'  => { hint       => 'mucs',
172                                                                      readonly   => 1 },
173
174                 'Fieldmapper::money::open_billable_transaction_summary' => { hint       => 'mobts',
175                                                                              readonly   => 1 },
176                 'Fieldmapper::money::billable_transaction_summary'      => { hint       => 'mbts',
177                                                                              readonly   => 1 },
178
179                 'Fieldmapper::config::identification_type'      => { hint => 'cit'  },
180                 'Fieldmapper::config::bib_source'               => { hint => 'cbs'  },
181                 'Fieldmapper::config::metabib_field'            => { hint => 'cmf'  },
182                 'Fieldmapper::config::rules::recuring_fine'     => { hint => 'crrf'  },
183                 'Fieldmapper::config::rules::circ_duration'     => { hint => 'crcd'  },
184                 'Fieldmapper::config::rules::max_fine'          => { hint => 'crmf'  },
185                 'Fieldmapper::config::non_cataloged_type'       => { hint => 'cnct'   },
186                 'Fieldmapper::config::standing'                 => { hint => 'cst'   },
187                 'Fieldmapper::config::copy_status'              => { hint => 'ccs'   },
188
189                 'Fieldmapper::authority::full_rec'              => { hint => 'afr'  },
190                 'Fieldmapper::authority::record_descriptor'     => { hint => 'ard'  },
191
192                 'Fieldmapper::metabib::metarecord'              => { hint => 'mmr'  },
193                 'Fieldmapper::metabib::title_field_entry'       => { hint => 'mtfe' },
194                 'Fieldmapper::metabib::author_field_entry'      => { hint => 'mafe' },
195                 'Fieldmapper::metabib::subject_field_entry'     => { hint => 'msfe' },
196                 'Fieldmapper::metabib::keyword_field_entry'     => { hint => 'mkfe' },
197                 'Fieldmapper::metabib::series_field_entry'      => { hint => 'msefe' },
198                 'Fieldmapper::metabib::full_rec'                => { hint => 'mfr'  },
199                 'Fieldmapper::metabib::record_descriptor'       => { hint => 'mrd'  },
200                 'Fieldmapper::metabib::metarecord_source_map'   => { hint => 'mmrsm'},
201
202                 'Fieldmapper::asset::copy'                      => { hint               => 'acp',
203                                                                      proto_fields       => { stat_cat_entries => 1 } },
204                 'Fieldmapper::asset::stat_cat'                  => { hint               => 'asc',
205                                                                      proto_fields       => { entries => 1 } },
206                 'Fieldmapper::asset::stat_cat_entry'            => { hint => 'asce'    },
207                 'Fieldmapper::asset::stat_cat_entry_copy_map'   => { hint => 'ascecm'  },
208                 'Fieldmapper::asset::copy_note'                 => { hint => 'acpn'    },
209                 'Fieldmapper::asset::copy_location'             => { hint => 'acpl'    },
210                 'Fieldmapper::asset::call_number'               => { hint               => 'acn',
211                                                                      proto_fields       => { copies => 1 } },
212                 'Fieldmapper::asset::call_number_note'          => { hint => 'acnn'    },
213
214                 'Fieldmapper::permission::perm_list'            => { hint => 'ppl'    },
215                 'Fieldmapper::permission::grp_tree'             => { hint => 'pgt', proto_fields => { children => 1 } },
216                 'Fieldmapper::permission::usr_grp_map'          => { hint => 'pugm'   },
217                 'Fieldmapper::permission::usr_perm_map'         => { hint => 'pupm'   },
218                 'Fieldmapper::permission::grp_perm_map'         => { hint => 'pgpm'   },
219                 'Fieldmapper::action::hold_request'             => { hint => 'ahr', proto_fields => { status => 1 } },
220                 'Fieldmapper::action::hold_notification'        => { hint => 'ahn'    },
221                 'Fieldmapper::action::hold_copy_map'            => { hint => 'ahcm'   },
222                 'Fieldmapper::action::hold_transit_copy'        => { hint => 'ahtc'   },
223                 'Fieldmapper::action::transit_copy'             => { hint => 'atc'    },
224                 'Fieldmapper::action::unfulfilled_hold_list'    => { hint => 'aufh'   },
225
226
227                 'Fieldmapper::ex'                               => { hint           => 'ex',
228                                                                      virtual        => 1,
229                                                                      proto_fields   => {
230                                                                         err_msg  => 1,
231                                                                         type     => 1,
232                                                                      } },
233
234
235                 'Fieldmapper::perm_ex'                          => { hint           => 'perm_ex',
236                                                                      virtual        => 1,
237                                                                      proto_fields   => {
238                                                                         err_msg => 1,
239                                                                         type    => 1,
240                                                                      } },
241
242
243       
244         };
245
246         #-------------------------------------------------------------------------------
247         # Now comes the evil!  Generate classes
248
249         for my $pkg ( __PACKAGE__->classes ) {
250                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
251
252                 eval <<"                PERL";
253                         package $pkg;
254                         use base 'Fieldmapper';
255                 PERL
256
257                 my $pos = 0;
258                 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
259                         $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
260                         $pos++;
261                 }
262
263                 if (exists $$fieldmap{$pkg}{proto_fields}) {
264                         for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
265                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
266                                 $pos++;
267                         }
268                 }
269
270                 unless ( $$fieldmap{$pkg}{virtual} ) {
271                         $$fieldmap{$pkg}{cdbi} = $cdbi;
272                         for my $col ( sort $cdbi->columns('All') ) {
273                                 $$fieldmap{$pkg}{fields}{$col} = { position => $pos, virtual => 0 };
274                                 $pos++;
275                         }
276                 }
277
278                 JSON->register_class_hint(
279                         hint => $pkg->json_hint,
280                         name => $pkg,
281                         type => 'array',
282                 );
283
284         }
285 }
286
287 sub new {
288         my $self = shift;
289         my $value = shift;
290         $value = [] unless (defined $value);
291         return bless $value => $self->class_name;
292 }
293
294 sub decast {
295         my $self = shift;
296         return [ @$self ];
297 }
298
299 sub DESTROY {}
300
301 sub AUTOLOAD {
302         my $obj = shift;
303         my $value = shift;
304         (my $field = $AUTOLOAD) =~ s/^.*://o;
305         my $class_name = $obj->class_name;
306
307         my $fpos = $field;
308         $fpos  =~ s/^clear_//og ;
309
310         my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
311
312         if ($field =~ /^clear_/o) {
313                 {       no strict 'subs';
314                         *{$obj->class_name."::$field"} = sub {
315                                 my $self = shift;
316                                 $self->[$pos] = undef;
317                                 return 1;
318                         };
319                 }
320                 return $obj->$field();
321         }
322
323         die "No field by the name $field in $class_name!"
324                 unless (exists $$fieldmap{$class_name}{fields}{$field} && defined($pos));
325
326
327         {       no strict 'subs';
328                 *{$obj->class_name."::$field"} = sub {
329                         my $self = shift;
330                         my $new_val = shift;
331                         $self->[$pos] = $new_val if (defined $new_val);
332                         return $self->[$pos];
333                 };
334         }
335         return $obj->$field($value);
336 }
337
338 sub class_name {
339         my $class_name = shift;
340         return ref($class_name) || $class_name;
341 }
342
343 sub real_fields {
344         my $self = shift;
345         my $class_name = $self->class_name;
346         my $fields = $$fieldmap{$class_name}{fields};
347
348         my @f = grep {
349                         !$$fields{$_}{virtual}
350                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
351
352         return @f;
353 }
354
355 sub has_field {
356         my $self = shift;
357         my $field = shift;
358         my $class_name = $self->class_name;
359         return 1 if grep { $_ eq $field } keys %{$$fieldmap{$class_name}{fields}};
360         return 0;
361 }
362
363 sub properties {
364         my $self = shift;
365         my $class_name = $self->class_name;
366         return keys %{$$fieldmap{$class_name}{fields}};
367 }
368
369 sub clone {
370         my $self = shift;
371         return $self->new( [@$self] );
372 }
373
374 sub api_level {
375         my $self = shift;
376         return $fieldmap->{$self->class_name}->{api_level};
377 }
378
379 sub cdbi {
380         my $self = shift;
381         return $fieldmap->{$self->class_name}->{cdbi};
382 }
383
384 sub is_virtual {
385         my $self = shift;
386         my $field = shift;
387         return $fieldmap->{$self->class_name}->{proto_fields}->{$field} if ($field);
388         return $fieldmap->{$self->class_name}->{virtual};
389 }
390
391 sub is_readonly {
392         my $self = shift;
393         my $field = shift;
394         return $fieldmap->{$self->class_name}->{readonly};
395 }
396
397 sub json_hint {
398         my $self = shift;
399         return $fieldmap->{$self->class_name}->{hint};
400 }
401
402
403 1;