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