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