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