]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm
added series stuff and updated hint for actor::stat_cat*
[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       => { due_date => 1 } },
64                 'Fieldmapper::actor::user'                      => { hint               => 'au',
65                                                                      proto_fields       => { cards      => 1,
66                                                                                              addresses  => 1 } },
67                 'Fieldmapper::actor::user_address'              => { hint => 'aua'    },
68                 'Fieldmapper::actor::org_address'               => { hint => 'aoa'    },
69                 'Fieldmapper::actor::profile'                   => { hint => 'ap'    },
70                 'Fieldmapper::actor::card'                      => { hint => 'ac'    },
71                 'Fieldmapper::config::standing'                 => { hint => 'cst'   },
72                 'Fieldmapper::config::copy_status'              => { hint => 'ccs'   },
73                 'Fieldmapper::actor::stat_cat'                  => { hint               => 'actsc',
74                                                                      proto_fields       => { entries => 1 } },
75                 'Fieldmapper::actor::stat_cat_entry'            => { hint => 'actsce'    },
76                 'Fieldmapper::actor::stat_cat_entry_user_map'   => { hint => 'actscecm'  },
77                 'Fieldmapper::actor::org_unit'                  => { hint               => 'aou',
78                                                                      proto_fields       => { children => 1 } },
79                 'Fieldmapper::actor::org_unit_type'             => { hint               => 'aout',
80                                                                      proto_fields       => { children => 1 } },
81                 
82                 'Fieldmapper::biblio::record_node'              => { hint               => 'brn',
83                                                                      virtual            => 1,
84                                                                      proto_fields       => { children           => 1,
85                                                                                              id                 => 1,
86                                                                                              owner_doc          => 1,
87                                                                                              intra_doc_id       => 1,
88                                                                                              parent_node        => 1,
89                                                                                              node_type          => 1,
90                                                                                              namespace_uri      => 1,
91                                                                                              name               => 1,
92                                                                                              value              => 1,
93                                                                                            } },
94
95                 'Fieldmapper::metabib::virtual_record'          => { hint               => 'mvr',
96                                                                      virtual            => 1,
97                                                                      proto_fields       => { title              => 1,
98                                                                                              author             => 1,
99                                                                                              doc_id             => 1,
100                                                                                              doc_type           => 1,
101                                                                                              isbn               => 1,
102                                                                                              pubdate            => 1,
103                                                                                              publisher          => 1,
104                                                                                              tcn                => 1,
105                                                                                              subject            => 1,
106                                                                                              types_of_resource  => 1,
107                                                                                              call_numbers       => 1,
108                                                                                              copy_count         => 1,
109                                                                                            } },
110
111                 'Fieldmapper::biblio::record_entry'             => { hint               => 'bre',
112                                                                      proto_fields       => { call_numbers => 1,
113                                                                                              fixed_fields => 1 } },
114                 #'Fieldmapper::biblio::record_marc'             => { hint => 'brx'  }, # now it's inside record_entry
115
116                 'Fieldmapper::money::cash_payment'              => { hint => 'mcp'  },
117                 'Fieldmapper::money::billing'                   => { hint => 'mb'  },
118
119                 'Fieldmapper::config::identification_type'      => { hint => 'cit'  },
120                 'Fieldmapper::config::bib_source'               => { hint => 'cbs'  },
121                 'Fieldmapper::config::metabib_field'            => { hint => 'cmf'  },
122                 'Fieldmapper::config::rules::recuring_fine'     => { hint => 'crrf'  },
123                 'Fieldmapper::config::rules::circ_duration'     => { hint => 'crcd'  },
124                 'Fieldmapper::config::rules::max_fine'          => { hint => 'crmf'  },
125
126                 'Fieldmapper::metabib::metarecord'              => { hint => 'mmr'  },
127                 'Fieldmapper::metabib::title_field_entry'       => { hint => 'mtfe' },
128                 'Fieldmapper::metabib::author_field_entry'      => { hint => 'mafe' },
129                 'Fieldmapper::metabib::subject_field_entry'     => { hint => 'msfe' },
130                 'Fieldmapper::metabib::keyword_field_entry'     => { hint => 'mkfe' },
131                 'Fieldmapper::metabib::series_field_entry'      => { hint => 'msefe' },
132                 'Fieldmapper::metabib::full_rec'                => { hint => 'mfr'  },
133                 'Fieldmapper::metabib::record_descriptor'       => { hint => 'mrd'  },
134                 'Fieldmapper::metabib::metarecord_source_map'   => { hint => 'mmrsm'},
135
136                 'Fieldmapper::asset::copy'                      => { hint               => 'acp',
137                                                                      proto_fields       => { stat_cat_entries => 1 } },
138                 'Fieldmapper::asset::stat_cat'                  => { hint               => 'asc',
139                                                                      proto_fields       => { entries => 1 } },
140                 'Fieldmapper::asset::stat_cat_entry'            => { hint => 'asce'    },
141                 'Fieldmapper::asset::stat_cat_entry_copy_map'   => { hint => 'ascecm'  },
142                 'Fieldmapper::asset::copy_note'                 => { hint => 'acpn'    },
143                 'Fieldmapper::asset::copy_location'             => { hint => 'acpl'    },
144                 'Fieldmapper::asset::call_number'               => { hint               => 'acn',
145                                                                      proto_fields       => { copies => 1 } },
146                 'Fieldmapper::asset::call_number_note'          => { hint => 'acnn'    },
147
148                 'Fieldmapper::ex'                               => { hint => 'ex',
149                                                                      virtual => 1,
150                                                                      proto_fields => {
151                                                                         err_msg => 1,
152                                                                         type    => 1,
153                                                                      } },
154
155       
156         };
157
158         #-------------------------------------------------------------------------------
159         # Now comes the evil!  Generate classes
160
161         for my $pkg ( __PACKAGE__->classes ) {
162                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
163
164                 eval <<"                PERL";
165                         package $pkg;
166                         use base 'Fieldmapper';
167                 PERL
168
169                 my $pos = 0;
170                 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
171                         $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
172                         $pos++;
173                 }
174
175                 if (exists $$fieldmap{$pkg}{proto_fields}) {
176                         for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
177                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
178                                 $pos++;
179                         }
180                 }
181
182                 unless ( $$fieldmap{$pkg}{virtual} ) {
183                         $$fieldmap{$pkg}{cdbi} = $cdbi;
184                         for my $col ( sort $cdbi->columns('All') ) {
185                                 $$fieldmap{$pkg}{fields}{$col} = { position => $pos, virtual => 0 };
186                                 $pos++;
187                         }
188                 }
189
190                 JSON->register_class_hint(
191                         hint => $pkg->json_hint,
192                         name => $pkg,
193                         type => 'array',
194                 );
195
196         }
197 }
198
199 sub new {
200         my $self = shift;
201         my $value = shift;
202         $value = [] unless (defined $value);
203         return bless $value => $self->class_name;
204 }
205
206 sub decast {
207         my $self = shift;
208         return [ @$self ];
209 }
210
211 sub DESTROY {}
212
213 sub AUTOLOAD {
214         my $obj = shift;
215         my $value = shift;
216         (my $field = $AUTOLOAD) =~ s/^.*://o;
217         my $class_name = $obj->class_name;
218
219         my $fpos = $field;
220         $fpos  =~ s/^clear_//og ;
221
222         my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
223
224         if ($field =~ /^clear_/o) {
225                 {       no strict 'subs';
226                         *{$obj->class_name."::$field"} = sub {
227                                 my $self = shift;
228                                 $self->[$pos] = undef;
229                                 return 1;
230                         };
231                 }
232                 return $obj->$field();
233         }
234
235         die "No field by the name $field in $class_name!"
236                 unless (exists $$fieldmap{$class_name}{fields}{$field});
237
238
239         {       no strict 'subs';
240                 *{$obj->class_name."::$field"} = sub {
241                         my $self = shift;
242                         my $new_val = shift;
243                         $self->[$pos] = $new_val if (defined $new_val);
244                         return $self->[$pos];
245                 };
246         }
247         return $obj->$field($value);
248 }
249
250 sub class_name {
251         my $class_name = shift;
252         return ref($class_name) || $class_name;
253 }
254
255 sub real_fields {
256         my $self = shift;
257         my $class_name = $self->class_name;
258         my $fields = $$fieldmap{$class_name}{fields};
259
260         my @f = grep {
261                         !$$fields{$_}{virtual}
262                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
263
264         return @f;
265 }
266
267 sub api_level {
268         my $self = shift;
269         return $fieldmap->{$self->class_name}->{api_level};
270 }
271
272
273 sub is_virtual {
274         my $self = shift;
275         return $fieldmap->{$self->class_name}->{virtual};
276 }
277
278 sub json_hint {
279         my $self = shift;
280         return $fieldmap->{$self->class_name}->{hint};
281 }
282
283
284 1;