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