]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm
06bcd07e259f247ced35908f7d4392b167a8bbd0
[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::workstation'               => { hint       => 'aws' },
87
88                 'Fieldmapper::actor::user'                      => { hint               => 'au',
89                                                                      proto_fields       => { cards              => 1,
90                                                                                              survey_responses   => 1,
91                                                                                              stat_cat_entries   => 1,
92                                                                                              checkouts          => 1,
93                                                                                              hold_requests      => 1,
94                                                                                              settings           => 1,
95                                                                                              permissions        => 1,
96                                                                                              standing_penalties => 1,
97                                                                                              addresses          => 1 } },
98                 'Fieldmapper::actor::user_setting'              => { hint => 'aus'    },
99                 'Fieldmapper::actor::user_standing_penalty'     => { hint => 'ausp'    },
100                 'Fieldmapper::actor::org_unit_setting'          => { hint => 'aous'    },
101                 'Fieldmapper::actor::user_address'              => { hint => 'aua'    },
102                 'Fieldmapper::actor::org_address'               => { hint => 'aoa'    },
103                 'Fieldmapper::actor::profile'                   => { hint => 'ap'    },
104                 'Fieldmapper::actor::card'                      => { hint => 'ac'    },
105                 'Fieldmapper::actor::stat_cat'                  => { hint               => 'actsc',
106                                                                      proto_fields       => { entries => 1 } },
107                 'Fieldmapper::actor::stat_cat_entry'            => { hint => 'actsce'    },
108                 'Fieldmapper::actor::stat_cat_entry_user_map'   => { hint => 'actscecm'  },
109                 'Fieldmapper::actor::org_unit'                  => { hint               => 'aou',
110                                                                      proto_fields       => { children => 1 } },
111                 'Fieldmapper::actor::org_unit_type'             => { hint               => 'aout',
112                                                                      proto_fields       => { children => 1 } },
113                 
114                 'Fieldmapper::biblio::record_node'              => { hint               => 'brn',
115                                                                      virtual            => 1,
116                                                                      proto_fields       => { children           => 1,
117                                                                                              id                 => 1,
118                                                                                              owner_doc          => 1,
119                                                                                              intra_doc_id       => 1,
120                                                                                              parent_node        => 1,
121                                                                                              node_type          => 1,
122                                                                                              namespace_uri      => 1,
123                                                                                              name               => 1,
124                                                                                              value              => 1,
125                                                                                            } },
126
127                 'Fieldmapper::metabib::virtual_record'          => { hint               => 'mvr',
128                                                                      virtual            => 1,
129                                                                      proto_fields       => { title              => 1,
130                                                                                              author             => 1,
131                                                                                              doc_id             => 1,
132                                                                                              doc_type           => 1,
133                                                                                              isbn               => 1,
134                                                                                              pubdate            => 1,
135                                                                                              publisher          => 1,
136                                                                                              tcn                => 1,
137                                                                                              subject            => 1,
138                                                                                              types_of_resource  => 1,
139                                                                                              call_numbers       => 1,
140                                                                                                           edition       => 1,
141                                                                                                           online_loc    => 1,
142                                                                                                           synopsis      => 1,
143                                                                                              copy_count         => 1,
144                                                                                              series             => 1,
145                                                                                              serials            => 1,
146                                                                                            } },
147
148                 'Fieldmapper::authority::record_entry'          => { hint               => 'are', },
149                 'Fieldmapper::authority::record_note'           => { hint               => 'arn', },
150                 'Fieldmapper::biblio::record_entry'             => { hint               => 'bre',
151                                                                      proto_fields       => { call_numbers => 1,
152                                                                                              fixed_fields => 1 } },
153                 #'Fieldmapper::biblio::record_marc'             => { hint => 'brx'  }, # now it's inside record_entry
154
155                 'Fieldmapper::money::payment'                   => { hint => 'mp',
156                                                                      readonly   => 1 },
157
158                 'Fieldmapper::money::cash_payment'              => { hint => 'mcp'  },
159                 'Fieldmapper::money::check_payment'             => { hint => 'mckp'  },
160                 'Fieldmapper::money::credit_payment'            => { hint => 'mcrp'  },
161                 'Fieldmapper::money::credit_card_payment'       => { hint => 'mccp'  },
162                 'Fieldmapper::money::forgive_payment'           => { hint => 'mfp'  },
163                 'Fieldmapper::money::work_payment'              => { hint => 'mwp'  },
164
165                 'Fieldmapper::money::billing'                   => { hint => 'mb'  },
166                 'Fieldmapper::money::billable_transaction'      => { hint => 'mbt'  },
167                 'Fieldmapper::money::grocery'                   => { hint => 'mg'  },
168
169                 'Fieldmapper::money::open_user_summary'         => { hint       => 'mous',
170                                                                      readonly   => 1 },
171                 'Fieldmapper::money::user_summary'              => { hint       => 'mus',
172                                                                      readonly   => 1 },
173
174                 'Fieldmapper::money::open_user_circulation_summary'     => { hint       => 'moucs',
175                                                                              readonly   => 1 },
176                 'Fieldmapper::money::user_circulation_summary'  => { hint       => 'mucs',
177                                                                      readonly   => 1 },
178
179                 'Fieldmapper::money::open_billable_transaction_summary' => { hint       => 'mobts',
180                                                                              readonly   => 1 },
181                 'Fieldmapper::money::billable_transaction_summary'      => { hint       => 'mbts',
182                                                                              readonly   => 1 },
183
184                 'Fieldmapper::config::identification_type'      => { hint => 'cit'  },
185                 'Fieldmapper::config::bib_source'               => { hint => 'cbs'  },
186                 'Fieldmapper::config::metabib_field'            => { hint => 'cmf'  },
187                 'Fieldmapper::config::rules::recuring_fine'     => { hint => 'crrf'  },
188                 'Fieldmapper::config::rules::circ_duration'     => { hint => 'crcd'  },
189                 'Fieldmapper::config::rules::max_fine'          => { hint => 'crmf'  },
190                 'Fieldmapper::config::non_cataloged_type'       => { hint => 'cnct'   },
191                 'Fieldmapper::config::standing'                 => { hint => 'cst'   },
192                 'Fieldmapper::config::copy_status'              => { hint => 'ccs'   },
193
194                 'Fieldmapper::authority::full_rec'              => { hint => 'afr'  },
195                 'Fieldmapper::authority::record_descriptor'     => { hint => 'ard'  },
196
197                 'Fieldmapper::metabib::metarecord'              => { hint => 'mmr'  },
198                 'Fieldmapper::metabib::title_field_entry'       => { hint => 'mtfe' },
199                 'Fieldmapper::metabib::author_field_entry'      => { hint => 'mafe' },
200                 'Fieldmapper::metabib::subject_field_entry'     => { hint => 'msfe' },
201                 'Fieldmapper::metabib::keyword_field_entry'     => { hint => 'mkfe' },
202                 'Fieldmapper::metabib::series_field_entry'      => { hint => 'msefe' },
203                 'Fieldmapper::metabib::full_rec'                => { hint => 'mfr'  },
204                 'Fieldmapper::metabib::record_descriptor'       => { hint => 'mrd'  },
205                 'Fieldmapper::metabib::metarecord_source_map'   => { hint => 'mmrsm'},
206
207                 'Fieldmapper::asset::copy'                      => { hint               => 'acp',
208                                                                      proto_fields       => { stat_cat_entries => 1 } },
209                 'Fieldmapper::asset::stat_cat'                  => { hint               => 'asc',
210                                                                      proto_fields       => { entries => 1 } },
211                 'Fieldmapper::asset::stat_cat_entry'            => { hint => 'asce'    },
212                 'Fieldmapper::asset::stat_cat_entry_copy_map'   => { hint => 'ascecm'  },
213                 'Fieldmapper::asset::copy_note'                 => { hint => 'acpn'    },
214                 'Fieldmapper::asset::copy_location'             => { hint => 'acpl'    },
215                 'Fieldmapper::asset::call_number'               => { hint               => 'acn',
216                                                                      proto_fields       => { copies => 1 } },
217                 'Fieldmapper::asset::call_number_note'          => { hint => 'acnn'    },
218
219                 'Fieldmapper::permission::perm_list'            => { hint => 'ppl'    },
220                 'Fieldmapper::permission::grp_tree'             => { hint => 'pgt', proto_fields => { children => 1 } },
221                 'Fieldmapper::permission::usr_grp_map'          => { hint => 'pugm'   },
222                 'Fieldmapper::permission::usr_perm_map'         => { hint => 'pupm'   },
223                 'Fieldmapper::permission::grp_perm_map'         => { hint => 'pgpm'   },
224                 'Fieldmapper::action::hold_request'             => { hint => 'ahr', proto_fields => { status => 1 } },
225                 'Fieldmapper::action::hold_notification'        => { hint => 'ahn'    },
226                 'Fieldmapper::action::hold_copy_map'            => { hint => 'ahcm'   },
227                 'Fieldmapper::action::hold_transit_copy'        => { hint => 'ahtc'   },
228                 'Fieldmapper::action::transit_copy'             => { hint => 'atc'    },
229                 'Fieldmapper::action::unfulfilled_hold_list'    => { hint => 'aufh'   },
230
231
232                 'Fieldmapper::ex'                               => { hint           => 'ex',
233                                                                      virtual        => 1,
234                                                                      proto_fields   => {
235                                                                         err_msg  => 1,
236                                                                         type     => 1,
237                                                                      } },
238
239
240                 'Fieldmapper::perm_ex'                          => { hint           => 'perm_ex',
241                                                                      virtual        => 1,
242                                                                      proto_fields   => {
243                                                                         err_msg => 1,
244                                                                         type    => 1,
245                                                                      } },
246
247
248       
249         };
250
251         #-------------------------------------------------------------------------------
252         # Now comes the evil!  Generate classes
253
254         for my $pkg ( __PACKAGE__->classes ) {
255                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
256
257                 eval <<"                PERL";
258                         package $pkg;
259                         use base 'Fieldmapper';
260                 PERL
261
262                 my $pos = 0;
263                 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
264                         $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
265                         $pos++;
266                 }
267
268                 if (exists $$fieldmap{$pkg}{proto_fields}) {
269                         for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
270                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
271                                 $pos++;
272                         }
273                 }
274
275                 unless ( $$fieldmap{$pkg}{virtual} ) {
276                         $$fieldmap{$pkg}{cdbi} = $cdbi;
277                         for my $col ( sort $cdbi->columns('Essential') ) {
278                                 $$fieldmap{$pkg}{fields}{$col} = { position => $pos, virtual => 0 };
279                                 $pos++;
280                         }
281                 }
282
283                 JSON->register_class_hint(
284                         hint => $pkg->json_hint,
285                         name => $pkg,
286                         type => 'array',
287                 );
288
289         }
290 }
291
292 sub new {
293         my $self = shift;
294         my $value = shift;
295         $value = [] unless (defined $value);
296         return bless $value => $self->class_name;
297 }
298
299 sub decast {
300         my $self = shift;
301         return [ @$self ];
302 }
303
304 sub DESTROY {}
305
306 sub AUTOLOAD {
307         my $obj = shift;
308         my $value = shift;
309         (my $field = $AUTOLOAD) =~ s/^.*://o;
310         my $class_name = $obj->class_name;
311
312         my $fpos = $field;
313         $fpos  =~ s/^clear_//og ;
314
315         my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
316
317         if ($field =~ /^clear_/o) {
318                 {       no strict 'subs';
319                         *{$obj->class_name."::$field"} = sub {
320                                 my $self = shift;
321                                 $self->[$pos] = undef;
322                                 return 1;
323                         };
324                 }
325                 return $obj->$field();
326         }
327
328         die "No field by the name $field in $class_name!"
329                 unless (exists $$fieldmap{$class_name}{fields}{$field} && defined($pos));
330
331
332         {       no strict 'subs';
333                 *{$obj->class_name."::$field"} = sub {
334                         my $self = shift;
335                         my $new_val = shift;
336                         $self->[$pos] = $new_val if (defined $new_val);
337                         return $self->[$pos];
338                 };
339         }
340         return $obj->$field($value);
341 }
342
343 sub class_name {
344         my $class_name = shift;
345         return ref($class_name) || $class_name;
346 }
347
348 sub real_fields {
349         my $self = shift;
350         my $class_name = $self->class_name;
351         my $fields = $$fieldmap{$class_name}{fields};
352
353         my @f = grep {
354                         !$$fields{$_}{virtual}
355                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
356
357         return @f;
358 }
359
360 sub has_field {
361         my $self = shift;
362         my $field = shift;
363         my $class_name = $self->class_name;
364         return 1 if grep { $_ eq $field } keys %{$$fieldmap{$class_name}{fields}};
365         return 0;
366 }
367
368 sub properties {
369         my $self = shift;
370         my $class_name = $self->class_name;
371         return keys %{$$fieldmap{$class_name}{fields}};
372 }
373
374 sub clone {
375         my $self = shift;
376         return $self->new( [@$self] );
377 }
378
379 sub api_level {
380         my $self = shift;
381         return $fieldmap->{$self->class_name}->{api_level};
382 }
383
384 sub cdbi {
385         my $self = shift;
386         return $fieldmap->{$self->class_name}->{cdbi};
387 }
388
389 sub is_virtual {
390         my $self = shift;
391         my $field = shift;
392         return $fieldmap->{$self->class_name}->{proto_fields}->{$field} if ($field);
393         return $fieldmap->{$self->class_name}->{virtual};
394 }
395
396 sub is_readonly {
397         my $self = shift;
398         my $field = shift;
399         return $fieldmap->{$self->class_name}->{readonly};
400 }
401
402 sub json_hint {
403         my $self = shift;
404         return $fieldmap->{$self->class_name}->{hint};
405 }
406
407
408 1;