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