]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm
adding org_unit_setting code
[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::biblio::record_entry'             => { hint               => 'bre',
128                                                                      proto_fields       => { call_numbers => 1,
129                                                                                              fixed_fields => 1 } },
130                 #'Fieldmapper::biblio::record_marc'             => { hint => 'brx'  }, # now it's inside record_entry
131
132                 'Fieldmapper::money::payment'                   => { hint => 'mp',
133                                                                      readonly   => 1 },
134
135                 'Fieldmapper::money::cash_payment'              => { hint => 'mcp'  },
136                 'Fieldmapper::money::check_payment'             => { hint => 'mckp'  },
137                 'Fieldmapper::money::credit_payment'            => { hint => 'mcrp'  },
138                 'Fieldmapper::money::credit_card_payment'       => { hint => 'mccp'  },
139                 'Fieldmapper::money::forgive_payment'           => { hint => 'mfp'  },
140                 'Fieldmapper::money::work_payment'              => { hint => 'mwp'  },
141
142                 'Fieldmapper::money::billing'                   => { hint => 'mb'  },
143                 'Fieldmapper::money::billable_transaction'      => { hint => 'mbt'  },
144
145                 'Fieldmapper::money::user_summary'              => { hint       => 'mus',
146                                                                      readonly   => 1 },
147
148                 'Fieldmapper::money::user_circulation_summary'  => { hint       => 'mucs',
149                                                                      readonly   => 1 },
150
151                 'Fieldmapper::money::billable_transaction_summary'      => { hint       => 'mbts',
152                                                                              readonly   => 1 },
153
154                 'Fieldmapper::config::identification_type'      => { hint => 'cit'  },
155                 'Fieldmapper::config::bib_source'               => { hint => 'cbs'  },
156                 'Fieldmapper::config::metabib_field'            => { hint => 'cmf'  },
157                 'Fieldmapper::config::rules::recuring_fine'     => { hint => 'crrf'  },
158                 'Fieldmapper::config::rules::circ_duration'     => { hint => 'crcd'  },
159                 'Fieldmapper::config::rules::max_fine'          => { hint => 'crmf'  },
160
161                 'Fieldmapper::metabib::metarecord'              => { hint => 'mmr'  },
162                 'Fieldmapper::metabib::title_field_entry'       => { hint => 'mtfe' },
163                 'Fieldmapper::metabib::author_field_entry'      => { hint => 'mafe' },
164                 'Fieldmapper::metabib::subject_field_entry'     => { hint => 'msfe' },
165                 'Fieldmapper::metabib::keyword_field_entry'     => { hint => 'mkfe' },
166                 'Fieldmapper::metabib::series_field_entry'      => { hint => 'msefe' },
167                 'Fieldmapper::metabib::full_rec'                => { hint => 'mfr'  },
168                 'Fieldmapper::metabib::record_descriptor'       => { hint => 'mrd'  },
169                 'Fieldmapper::metabib::metarecord_source_map'   => { hint => 'mmrsm'},
170
171                 'Fieldmapper::asset::copy'                      => { hint               => 'acp',
172                                                                      proto_fields       => { stat_cat_entries => 1 } },
173                 'Fieldmapper::asset::stat_cat'                  => { hint               => 'asc',
174                                                                      proto_fields       => { entries => 1 } },
175                 'Fieldmapper::asset::stat_cat_entry'            => { hint => 'asce'    },
176                 'Fieldmapper::asset::stat_cat_entry_copy_map'   => { hint => 'ascecm'  },
177                 'Fieldmapper::asset::copy_note'                 => { hint => 'acpn'    },
178                 'Fieldmapper::asset::copy_location'             => { hint => 'acpl'    },
179                 'Fieldmapper::asset::call_number'               => { hint               => 'acn',
180                                                                      proto_fields       => { copies => 1 } },
181                 'Fieldmapper::asset::call_number_note'          => { hint => 'acnn'    },
182
183                 'Fieldmapper::permission::perm_list'            => { hint => 'ppl'    },
184                 'Fieldmapper::permission::grp_tree'             => { hint => 'pgt'    },
185                 'Fieldmapper::permission::usr_grp_map'          => { hint => 'pugm'   },
186                 'Fieldmapper::permission::usr_perm_map'         => { hint => 'pupm'   },
187                 'Fieldmapper::permission::grp_perm_map'         => { hint => 'pgpm'   },
188                 'Fieldmapper::action::hold_request'             => { hint => 'ahr', proto_fields => { status => 1 } },
189                 'Fieldmapper::action::hold_notification'        => { hint => 'ahn'    },
190                 'Fieldmapper::action::hold_copy_map'            => { hint => 'ahcm'   },
191                 'Fieldmapper::action::hold_transit_copy'        => { hint => 'ahtc'   },
192                 'Fieldmapper::action::transit_copy'             => { hint => 'atc'    },
193                 'Fieldmapper::action::unfulfilled_hold_list'    => { hint => 'aufh'   },
194
195
196                 'Fieldmapper::ex'                               => { hint           => 'ex',
197                                                                      virtual        => 1,
198                                                                      proto_fields   => {
199                                                                         err_msg  => 1,
200                                                                         type     => 1,
201                                                                      } },
202
203
204                 'Fieldmapper::perm_ex'                          => { hint           => 'perm_ex',
205                                                                      virtual        => 1,
206                                                                      proto_fields   => {
207                                                                         err_msg => 1,
208                                                                         type    => 1,
209                                                                      } },
210
211
212       
213         };
214
215         #-------------------------------------------------------------------------------
216         # Now comes the evil!  Generate classes
217
218         for my $pkg ( __PACKAGE__->classes ) {
219                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
220
221                 eval <<"                PERL";
222                         package $pkg;
223                         use base 'Fieldmapper';
224                 PERL
225
226                 my $pos = 0;
227                 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
228                         $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
229                         $pos++;
230                 }
231
232                 if (exists $$fieldmap{$pkg}{proto_fields}) {
233                         for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
234                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
235                                 $pos++;
236                         }
237                 }
238
239                 unless ( $$fieldmap{$pkg}{virtual} ) {
240                         $$fieldmap{$pkg}{cdbi} = $cdbi;
241                         for my $col ( sort $cdbi->columns('All') ) {
242                                 $$fieldmap{$pkg}{fields}{$col} = { position => $pos, virtual => 0 };
243                                 $pos++;
244                         }
245                 }
246
247                 JSON->register_class_hint(
248                         hint => $pkg->json_hint,
249                         name => $pkg,
250                         type => 'array',
251                 );
252
253         }
254 }
255
256 sub new {
257         my $self = shift;
258         my $value = shift;
259         $value = [] unless (defined $value);
260         return bless $value => $self->class_name;
261 }
262
263 sub decast {
264         my $self = shift;
265         return [ @$self ];
266 }
267
268 sub DESTROY {}
269
270 sub AUTOLOAD {
271         my $obj = shift;
272         my $value = shift;
273         (my $field = $AUTOLOAD) =~ s/^.*://o;
274         my $class_name = $obj->class_name;
275
276         my $fpos = $field;
277         $fpos  =~ s/^clear_//og ;
278
279         my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
280
281         if ($field =~ /^clear_/o) {
282                 {       no strict 'subs';
283                         *{$obj->class_name."::$field"} = sub {
284                                 my $self = shift;
285                                 $self->[$pos] = undef;
286                                 return 1;
287                         };
288                 }
289                 return $obj->$field();
290         }
291
292         die "No field by the name $field in $class_name!"
293                 unless (exists $$fieldmap{$class_name}{fields}{$field} && defined($pos));
294
295
296         {       no strict 'subs';
297                 *{$obj->class_name."::$field"} = sub {
298                         my $self = shift;
299                         my $new_val = shift;
300                         $self->[$pos] = $new_val if (defined $new_val);
301                         return $self->[$pos];
302                 };
303         }
304         return $obj->$field($value);
305 }
306
307 sub class_name {
308         my $class_name = shift;
309         return ref($class_name) || $class_name;
310 }
311
312 sub real_fields {
313         my $self = shift;
314         my $class_name = $self->class_name;
315         my $fields = $$fieldmap{$class_name}{fields};
316
317         my @f = grep {
318                         !$$fields{$_}{virtual}
319                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
320
321         return @f;
322 }
323
324 sub properties {
325         my $self = shift;
326         my $class_name = $self->class_name;
327         return keys %{$$fieldmap{$class_name}{fields}};
328 }
329
330 sub clone {
331         my $self = shift;
332         return $self->new( [@$self] );
333 }
334
335 sub api_level {
336         my $self = shift;
337         return $fieldmap->{$self->class_name}->{api_level};
338 }
339
340 sub cdbi {
341         my $self = shift;
342         return $fieldmap->{$self->class_name}->{cdbi};
343 }
344
345 sub is_virtual {
346         my $self = shift;
347         my $field = shift;
348         return $fieldmap->{$self->class_name}->{proto_fields}->{$field} if ($field);
349         return $fieldmap->{$self->class_name}->{virtual};
350 }
351
352 sub is_readonly {
353         my $self = shift;
354         my $field = shift;
355         return $fieldmap->{$self->class_name}->{readonly};
356 }
357
358 sub json_hint {
359         my $self = shift;
360         return $fieldmap->{$self->class_name}->{hint};
361 }
362
363
364 1;