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