]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm
adding hours_of_operation and closed_date -- all must dance
[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::recuring_fine'     => { hint => 'crrf'  },
197                 'Fieldmapper::config::rules::circ_duration'     => { hint => 'crcd'  },
198                 'Fieldmapper::config::rules::max_fine'          => { hint => 'crmf'  },
199                 'Fieldmapper::config::non_cataloged_type'       => { hint => 'cnct'   },
200                 'Fieldmapper::config::standing'                 => { hint => 'cst'   },
201                 'Fieldmapper::config::copy_status'              => { hint => 'ccs'   },
202
203                 'Fieldmapper::config::audience_map'             => { hint       => 'cam',
204                                                                      readonly   => 1 },
205                 'Fieldmapper::config::language_map'             => { hint       => 'clm',
206                                                                      readonly   => 1 },
207                 'Fieldmapper::config::item_form_map'            => { hint       => 'cifm',
208                                                                      readonly   => 1 },
209                 'Fieldmapper::config::item_type_map'            => { hint       => 'citm',
210                                                                      readonly   => 1 },
211                 'Fieldmapper::config::lit_form_map'             => { hint       => 'clfm',
212                                                                      readonly   => 1 },
213
214                 'Fieldmapper::authority::full_rec'              => { hint => 'afr'  },
215                 'Fieldmapper::authority::record_descriptor'     => { hint => 'ard'  },
216
217                 'Fieldmapper::metabib::metarecord'              => { hint => 'mmr'  },
218                 'Fieldmapper::metabib::title_field_entry'       => { hint => 'mtfe' },
219                 'Fieldmapper::metabib::author_field_entry'      => { hint => 'mafe' },
220                 'Fieldmapper::metabib::subject_field_entry'     => { hint => 'msfe' },
221                 'Fieldmapper::metabib::keyword_field_entry'     => { hint => 'mkfe' },
222                 'Fieldmapper::metabib::series_field_entry'      => { hint => 'msefe' },
223                 'Fieldmapper::metabib::full_rec'                => { hint => 'mfr'  },
224                 'Fieldmapper::metabib::record_descriptor'       => { hint => 'mrd'  },
225                 'Fieldmapper::metabib::metarecord_source_map'   => { hint => 'mmrsm'},
226
227                 'Fieldmapper::asset::copy'                      => { hint               => 'acp',
228                                                                      proto_fields       => { stat_cat_entries => 1 } },
229                 'Fieldmapper::asset::stat_cat'                  => { hint               => 'asc',
230                                                                      proto_fields       => { entries => 1 } },
231                 'Fieldmapper::asset::stat_cat_entry'            => { hint => 'asce'    },
232                 'Fieldmapper::asset::stat_cat_entry_copy_map'   => { hint => 'ascecm'  },
233                 'Fieldmapper::asset::copy_note'                 => { hint => 'acpn'    },
234                 'Fieldmapper::asset::copy_location'             => { hint => 'acpl'    },
235                 'Fieldmapper::asset::call_number'               => { hint               => 'acn',
236                                                                      proto_fields       => { copies => 1 } },
237                 'Fieldmapper::asset::call_number_note'          => { hint => 'acnn'    },
238
239                 'Fieldmapper::permission::perm_list'            => { hint => 'ppl'    },
240                 'Fieldmapper::permission::grp_tree'             => { hint => 'pgt', proto_fields => { children => 1 } },
241                 'Fieldmapper::permission::usr_grp_map'          => { hint => 'pugm'   },
242                 'Fieldmapper::permission::usr_perm_map'         => { hint => 'pupm'   },
243                 'Fieldmapper::permission::grp_perm_map'         => { hint => 'pgpm'   },
244                 'Fieldmapper::action::hold_request'             => { hint => 'ahr', proto_fields => { status => 1, transit => 1 } },
245                 'Fieldmapper::action::hold_notification'        => { hint => 'ahn'    },
246                 'Fieldmapper::action::hold_copy_map'            => { hint => 'ahcm'   },
247                 'Fieldmapper::action::hold_transit_copy'        => { hint => 'ahtc'   },
248                 'Fieldmapper::action::transit_copy'             => { hint => 'atc'    },
249                 'Fieldmapper::action::unfulfilled_hold_list'    => { hint => 'aufh'   },
250
251
252                 'Fieldmapper::ex'                               => { hint           => 'ex',
253                                                                      virtual        => 1,
254                                                                      proto_fields   => {
255                                                                         err_msg  => 1,
256                                                                         type     => 1,
257                                                                      } },
258
259
260                 'Fieldmapper::perm_ex'                          => { hint           => 'perm_ex',
261                                                                      virtual        => 1,
262                                                                      proto_fields   => {
263                                                                         err_msg => 1,
264                                                                         type    => 1,
265                                                                      } },
266
267
268       
269         };
270
271         #-------------------------------------------------------------------------------
272         # Now comes the evil!  Generate classes
273
274         for my $pkg ( __PACKAGE__->classes ) {
275                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
276
277                 eval <<"                PERL";
278                         package $pkg;
279                         use base 'Fieldmapper';
280                 PERL
281
282                 my $pos = 0;
283                 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
284                         $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
285                         $pos++;
286                 }
287
288                 if (exists $$fieldmap{$pkg}{proto_fields}) {
289                         for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
290                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
291                                 $pos++;
292                         }
293                 }
294
295                 unless ( $$fieldmap{$pkg}{virtual} ) {
296                         $$fieldmap{$pkg}{cdbi} = $cdbi;
297                         for my $col ( sort $cdbi->columns('Essential') ) {
298                                 $$fieldmap{$pkg}{fields}{$col} = { position => $pos, virtual => 0 };
299                                 $pos++;
300                         }
301                 }
302
303                 JSON->register_class_hint(
304                         hint => $pkg->json_hint,
305                         name => $pkg,
306                         type => 'array',
307                 );
308
309         }
310 }
311
312 sub new {
313         my $self = shift;
314         my $value = shift;
315         $value = [] unless (defined $value);
316         return bless $value => $self->class_name;
317 }
318
319 sub decast {
320         my $self = shift;
321         return [ @$self ];
322 }
323
324 sub DESTROY {}
325
326 sub AUTOLOAD {
327         my $obj = shift;
328         my $value = shift;
329         (my $field = $AUTOLOAD) =~ s/^.*://o;
330         my $class_name = $obj->class_name;
331
332         my $fpos = $field;
333         $fpos  =~ s/^clear_//og ;
334
335         my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
336
337         if ($field =~ /^clear_/o) {
338                 {       no strict 'subs';
339                         *{$obj->class_name."::$field"} = sub {
340                                 my $self = shift;
341                                 $self->[$pos] = undef;
342                                 return 1;
343                         };
344                 }
345                 return $obj->$field();
346         }
347
348         die "No field by the name $field in $class_name!"
349                 unless (exists $$fieldmap{$class_name}{fields}{$field} && defined($pos));
350
351
352         {       no strict 'subs';
353                 *{$obj->class_name."::$field"} = sub {
354                         my $self = shift;
355                         my $new_val = shift;
356                         $self->[$pos] = $new_val if (defined $new_val);
357                         return $self->[$pos];
358                 };
359         }
360         return $obj->$field($value);
361 }
362
363 sub class_name {
364         my $class_name = shift;
365         return ref($class_name) || $class_name;
366 }
367
368 sub real_fields {
369         my $self = shift;
370         my $class_name = $self->class_name;
371         my $fields = $$fieldmap{$class_name}{fields};
372
373         my @f = grep {
374                         !$$fields{$_}{virtual}
375                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
376
377         return @f;
378 }
379
380 sub has_field {
381         my $self = shift;
382         my $field = shift;
383         my $class_name = $self->class_name;
384         return 1 if grep { $_ eq $field } keys %{$$fieldmap{$class_name}{fields}};
385         return 0;
386 }
387
388 sub properties {
389         my $self = shift;
390         my $class_name = $self->class_name;
391         return keys %{$$fieldmap{$class_name}{fields}};
392 }
393
394 sub clone {
395         my $self = shift;
396         return $self->new( [@$self] );
397 }
398
399 sub api_level {
400         my $self = shift;
401         return $fieldmap->{$self->class_name}->{api_level};
402 }
403
404 sub cdbi {
405         my $self = shift;
406         return $fieldmap->{$self->class_name}->{cdbi};
407 }
408
409 sub is_virtual {
410         my $self = shift;
411         my $field = shift;
412         return $fieldmap->{$self->class_name}->{proto_fields}->{$field} if ($field);
413         return $fieldmap->{$self->class_name}->{virtual};
414 }
415
416 sub is_readonly {
417         my $self = shift;
418         my $field = shift;
419         return $fieldmap->{$self->class_name}->{readonly};
420 }
421
422 sub json_hint {
423         my $self = shift;
424         return $fieldmap->{$self->class_name}->{hint};
425 }
426
427
428 1;