updating money handling; adding readonly fieldmapper classes (views); allow fields...
[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
18 use vars qw/$fieldmap $VERSION/;
19
20 _init();
21
22 sub publish_fieldmapper {
23         my ($self,$client,$class) = @_;
24
25         return $fieldmap unless (defined $class);
26         return undef unless (exists($$fieldmap{$class}));
27         return {$class => $$fieldmap{$class}};
28 }
29 __PACKAGE__->register_method(
30         api_name        => 'opensrf.open-ils.system.fieldmapper',
31         api_level       => 1,
32         method          => 'publish_fieldmapper',
33 );
34
35 #
36 # To dump the Javascript version of the fieldmapper struct use the command:
37 #
38 #       PERL5LIB=~/cvs/ILS/OpenSRF/src/perlmods/:~/cvs/ILS/Open-ILS/src/perlmods/ GEN_JS=1 perl -MOpenILS::Utils::Fieldmapper -e 'print "\n";'
39 #
40 # ... adjusted for your CVS sandbox, of course.
41 #
42
43 sub classes {
44         return () unless (defined $fieldmap);
45         return keys %$fieldmap;
46 }
47
48 sub _init {
49         return if (keys %$fieldmap);
50
51         $fieldmap = 
52         {
53                 'Fieldmapper::action::survey'                   => { hint               => 'asv',
54                                                                      proto_fields       => { questions  => 1,
55                                                                                              responses  => 1 } },
56                 'Fieldmapper::action::survey_question'          => { hint               => 'asvq',
57                                                                      proto_fields       => { answers    => 1,
58                                                                                              responses  => 1 } },
59                 'Fieldmapper::action::survey_answer'            => { hint               => 'asva',
60                                                                      proto_fields       => { responses => 1 } },
61                 'Fieldmapper::action::survey_response'          => { hint               => 'asvr'  },
62                 'Fieldmapper::action::circulation'              => { hint               => 'circ',
63                                                                      proto_fields       => {} },
64                 'Fieldmapper::actor::user'                      => { hint               => 'au',
65                                                                      proto_fields       => { cards              => 1,
66                                                                                              survey_responses   => 1,
67                                                                                              stat_cat_entries   => 1,
68                                                                                              checkouts          => 1,
69                                                                                              hold_requests      => 1,
70                                                                                              addresses          => 1 } },
71                 'Fieldmapper::actor::user_address'              => { hint => 'aua'    },
72                 'Fieldmapper::actor::org_address'               => { hint => 'aoa'    },
73                 'Fieldmapper::actor::profile'                   => { hint => 'ap'    },
74                 'Fieldmapper::actor::card'                      => { hint => 'ac'    },
75                 'Fieldmapper::config::standing'                 => { hint => 'cst'   },
76                 'Fieldmapper::config::copy_status'              => { hint => 'ccs'   },
77                 'Fieldmapper::actor::stat_cat'                  => { hint               => 'actsc',
78                                                                      proto_fields       => { entries => 1 } },
79                 'Fieldmapper::actor::stat_cat_entry'            => { hint => 'actsce'    },
80                 'Fieldmapper::actor::stat_cat_entry_user_map'   => { hint => 'actscecm'  },
81                 'Fieldmapper::actor::org_unit'                  => { hint               => 'aou',
82                                                                      proto_fields       => { children => 1 } },
83                 'Fieldmapper::actor::org_unit_type'             => { hint               => 'aout',
84                                                                      proto_fields       => { children => 1 } },
85                 
86                 'Fieldmapper::biblio::record_node'              => { hint               => 'brn',
87                                                                      virtual            => 1,
88                                                                      proto_fields       => { children           => 1,
89                                                                                              id                 => 1,
90                                                                                              owner_doc          => 1,
91                                                                                              intra_doc_id       => 1,
92                                                                                              parent_node        => 1,
93                                                                                              node_type          => 1,
94                                                                                              namespace_uri      => 1,
95                                                                                              name               => 1,
96                                                                                              value              => 1,
97                                                                                            } },
98
99                 'Fieldmapper::metabib::virtual_record'          => { hint               => 'mvr',
100                                                                      virtual            => 1,
101                                                                      proto_fields       => { title              => 1,
102                                                                                              author             => 1,
103                                                                                              doc_id             => 1,
104                                                                                              doc_type           => 1,
105                                                                                              isbn               => 1,
106                                                                                              pubdate            => 1,
107                                                                                              publisher          => 1,
108                                                                                              tcn                => 1,
109                                                                                              subject            => 1,
110                                                                                              types_of_resource  => 1,
111                                                                                              call_numbers       => 1,
112                                                                                                           edition       => 1,
113                                                                                                           online_loc    => 1,
114                                                                                                           synopsis      => 1,
115                                                                                              copy_count         => 1,
116                                                                                              series             => 1,
117                                                                                              serials            => 1,
118                                                                                            } },
119
120                 'Fieldmapper::biblio::record_entry'             => { hint               => 'bre',
121                                                                      proto_fields       => { call_numbers => 1,
122                                                                                              fixed_fields => 1 } },
123                 #'Fieldmapper::biblio::record_marc'             => { hint => 'brx'  }, # now it's inside record_entry
124
125                 'Fieldmapper::money::payment'                   => { hint => 'mp'  },
126                 'Fieldmapper::money::cash_payment'              => { hint => 'mcp'  },
127                 'Fieldmapper::money::check_payment'             => { hint => 'mckp'  },
128                 'Fieldmapper::money::credit_payment'            => { hint => 'mcrp'  },
129                 'Fieldmapper::money::credit_card_payment'       => { hint => 'mccp'  },
130                 'Fieldmapper::money::forgive_payment'           => { hint => 'mfp'  },
131                 'Fieldmapper::money::work_payment'              => { hint => 'mwp'  },
132
133                 'Fieldmapper::money::billing'                   => { hint => 'mb'  },
134                 'Fieldmapper::money::billable_transaction'      => { hint => 'mbt'  },
135
136                 'Fieldmapper::money::user_summary'              => { hint       => 'mus',
137                                                                      readonly   => 1 },
138
139                 'Fieldmapper::money::billable_transaction_summary'      => { hint       => 'mbts',
140                                                                              readonly   => 1 },
141
142                 'Fieldmapper::config::identification_type'      => { hint => 'cit'  },
143                 'Fieldmapper::config::bib_source'               => { hint => 'cbs'  },
144                 'Fieldmapper::config::metabib_field'            => { hint => 'cmf'  },
145                 'Fieldmapper::config::rules::recuring_fine'     => { hint => 'crrf'  },
146                 'Fieldmapper::config::rules::circ_duration'     => { hint => 'crcd'  },
147                 'Fieldmapper::config::rules::max_fine'          => { hint => 'crmf'  },
148
149                 'Fieldmapper::metabib::metarecord'              => { hint => 'mmr'  },
150                 'Fieldmapper::metabib::title_field_entry'       => { hint => 'mtfe' },
151                 'Fieldmapper::metabib::author_field_entry'      => { hint => 'mafe' },
152                 'Fieldmapper::metabib::subject_field_entry'     => { hint => 'msfe' },
153                 'Fieldmapper::metabib::keyword_field_entry'     => { hint => 'mkfe' },
154                 'Fieldmapper::metabib::series_field_entry'      => { hint => 'msefe' },
155                 'Fieldmapper::metabib::full_rec'                => { hint => 'mfr'  },
156                 'Fieldmapper::metabib::record_descriptor'       => { hint => 'mrd'  },
157                 'Fieldmapper::metabib::metarecord_source_map'   => { hint => 'mmrsm'},
158
159                 'Fieldmapper::asset::copy'                      => { hint               => 'acp',
160                                                                      proto_fields       => { stat_cat_entries => 1 } },
161                 'Fieldmapper::asset::stat_cat'                  => { hint               => 'asc',
162                                                                      proto_fields       => { entries => 1 } },
163                 'Fieldmapper::asset::stat_cat_entry'            => { hint => 'asce'    },
164                 'Fieldmapper::asset::stat_cat_entry_copy_map'   => { hint => 'ascecm'  },
165                 'Fieldmapper::asset::copy_note'                 => { hint => 'acpn'    },
166                 'Fieldmapper::asset::copy_location'             => { hint => 'acpl'    },
167                 'Fieldmapper::asset::call_number'               => { hint               => 'acn',
168                                                                      proto_fields       => { copies => 1 } },
169                 'Fieldmapper::asset::call_number_note'          => { hint => 'acnn'    },
170
171                 'Fieldmapper::permission::perm_list'            => { hint => 'ppl'    },
172                 'Fieldmapper::permission::grp_tree'             => { hint => 'pgt'    },
173                 'Fieldmapper::permission::usr_grp_map'          => { hint => 'pugm'   },
174                 'Fieldmapper::permission::usr_perm_map'         => { hint => 'pupm'   },
175                 'Fieldmapper::permission::grp_perm_map'         => { hint => 'pgpm'   },
176                 'Fieldmapper::action::hold_request'             => { hint => 'ahr'   },
177                 'Fieldmapper::action::hold_notification'        => { hint => 'ahn'   },
178                 'Fieldmapper::action::hold_copy_map'            => { hint => 'ahcm'   },
179                 'Fieldmapper::action::hold_transit_copy'        => { hint => 'ahtc'   },
180
181
182                 'Fieldmapper::ex'                               => { hint           => 'ex',
183                                                                      virtual        => 1,
184                                                                      proto_fields   => {
185                                                                         err_msg  => 1,
186                                                                         type     => 1,
187                                                                      } },
188
189
190                 'Fieldmapper::perm_ex'                          => { hint           => 'perm_ex',
191                                                                      virtual        => 1,
192                                                                      proto_fields   => {
193                                                                         err_msg => 1,
194                                                                         type    => 1,
195                                                                      } },
196
197
198       
199         };
200
201         #-------------------------------------------------------------------------------
202         # Now comes the evil!  Generate classes
203
204         for my $pkg ( __PACKAGE__->classes ) {
205                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
206
207                 eval <<"                PERL";
208                         package $pkg;
209                         use base 'Fieldmapper';
210                 PERL
211
212                 my $pos = 0;
213                 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
214                         $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
215                         $pos++;
216                 }
217
218                 if (exists $$fieldmap{$pkg}{proto_fields}) {
219                         for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
220                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
221                                 $pos++;
222                         }
223                 }
224
225                 unless ( $$fieldmap{$pkg}{virtual} ) {
226                         $$fieldmap{$pkg}{cdbi} = $cdbi;
227                         for my $col ( sort $cdbi->columns('All') ) {
228                                 $$fieldmap{$pkg}{fields}{$col} = { position => $pos, virtual => 0 };
229                                 $pos++;
230                         }
231                 }
232
233                 JSON->register_class_hint(
234                         hint => $pkg->json_hint,
235                         name => $pkg,
236                         type => 'array',
237                 );
238
239         }
240 }
241
242 sub new {
243         my $self = shift;
244         my $value = shift;
245         $value = [] unless (defined $value);
246         return bless $value => $self->class_name;
247 }
248
249 sub decast {
250         my $self = shift;
251         return [ @$self ];
252 }
253
254 sub DESTROY {}
255
256 sub AUTOLOAD {
257         my $obj = shift;
258         my $value = shift;
259         (my $field = $AUTOLOAD) =~ s/^.*://o;
260         my $class_name = $obj->class_name;
261
262         my $fpos = $field;
263         $fpos  =~ s/^clear_//og ;
264
265         my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
266
267         if ($field =~ /^clear_/o) {
268                 {       no strict 'subs';
269                         *{$obj->class_name."::$field"} = sub {
270                                 my $self = shift;
271                                 $self->[$pos] = undef;
272                                 return 1;
273                         };
274                 }
275                 return $obj->$field();
276         }
277
278         die "No field by the name $field in $class_name!"
279                 unless (exists $$fieldmap{$class_name}{fields}{$field} && defined($pos));
280
281
282         {       no strict 'subs';
283                 *{$obj->class_name."::$field"} = sub {
284                         my $self = shift;
285                         my $new_val = shift;
286                         $self->[$pos] = $new_val if (defined $new_val);
287                         return $self->[$pos];
288                 };
289         }
290         return $obj->$field($value);
291 }
292
293 sub class_name {
294         my $class_name = shift;
295         return ref($class_name) || $class_name;
296 }
297
298 sub real_fields {
299         my $self = shift;
300         my $class_name = $self->class_name;
301         my $fields = $$fieldmap{$class_name}{fields};
302
303         my @f = grep {
304                         !$$fields{$_}{virtual}
305                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
306
307         return @f;
308 }
309
310 sub properties {
311         my $self = shift;
312         my $class_name = $self->class_name;
313         return keys %{$$fieldmap{$class_name}{fields}};
314 }
315
316 sub clone {
317         my $self = shift;
318         return $self->new( [@$self] );
319 }
320
321 sub api_level {
322         my $self = shift;
323         return $fieldmap->{$self->class_name}->{api_level};
324 }
325
326
327 sub is_virtual {
328         my $self = shift;
329         my $field = shift;
330         return $fieldmap->{$self->class_name}->{proto_fields}->{$field} if ($field);
331         return $fieldmap->{$self->class_name}->{virtual};
332 }
333
334 sub is_readonly {
335         my $self = shift;
336         my $field = shift;
337         return $fieldmap->{$self->class_name}->{readonly};
338 }
339
340 sub json_hint {
341         my $self = shift;
342         return $fieldmap->{$self->class_name}->{hint};
343 }
344
345
346 1;