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