]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm
added series and serials to virtual record
[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
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                                                                                              series             => 1,
110                                                                                              serials            => 1,
111                                                                                            } },
112
113                 'Fieldmapper::biblio::record_entry'             => { hint               => 'bre',
114                                                                      proto_fields       => { call_numbers => 1,
115                                                                                              fixed_fields => 1 } },
116                 #'Fieldmapper::biblio::record_marc'             => { hint => 'brx'  }, # now it's inside record_entry
117
118                 'Fieldmapper::money::cash_payment'              => { hint => 'mcp'  },
119                 'Fieldmapper::money::billing'                   => { hint => 'mb'  },
120
121                 'Fieldmapper::config::identification_type'      => { hint => 'cit'  },
122                 'Fieldmapper::config::bib_source'               => { hint => 'cbs'  },
123                 'Fieldmapper::config::metabib_field'            => { hint => 'cmf'  },
124                 'Fieldmapper::config::rules::recuring_fine'     => { hint => 'crrf'  },
125                 'Fieldmapper::config::rules::circ_duration'     => { hint => 'crcd'  },
126                 'Fieldmapper::config::rules::max_fine'          => { hint => 'crmf'  },
127
128                 'Fieldmapper::metabib::metarecord'              => { hint => 'mmr'  },
129                 'Fieldmapper::metabib::title_field_entry'       => { hint => 'mtfe' },
130                 'Fieldmapper::metabib::author_field_entry'      => { hint => 'mafe' },
131                 'Fieldmapper::metabib::subject_field_entry'     => { hint => 'msfe' },
132                 'Fieldmapper::metabib::keyword_field_entry'     => { hint => 'mkfe' },
133                 'Fieldmapper::metabib::series_field_entry'      => { hint => 'msefe' },
134                 'Fieldmapper::metabib::full_rec'                => { hint => 'mfr'  },
135                 'Fieldmapper::metabib::record_descriptor'       => { hint => 'mrd'  },
136                 'Fieldmapper::metabib::metarecord_source_map'   => { hint => 'mmrsm'},
137
138                 'Fieldmapper::asset::copy'                      => { hint               => 'acp',
139                                                                      proto_fields       => { stat_cat_entries => 1 } },
140                 'Fieldmapper::asset::stat_cat'                  => { hint               => 'asc',
141                                                                      proto_fields       => { entries => 1 } },
142                 'Fieldmapper::asset::stat_cat_entry'            => { hint => 'asce'    },
143                 'Fieldmapper::asset::stat_cat_entry_copy_map'   => { hint => 'ascecm'  },
144                 'Fieldmapper::asset::copy_note'                 => { hint => 'acpn'    },
145                 'Fieldmapper::asset::copy_location'             => { hint => 'acpl'    },
146                 'Fieldmapper::asset::call_number'               => { hint               => 'acn',
147                                                                      proto_fields       => { copies => 1 } },
148                 'Fieldmapper::asset::call_number_note'          => { hint => 'acnn'    },
149
150                 'Fieldmapper::ex'                               => { hint => 'ex',
151                                                                      virtual => 1,
152                                                                      proto_fields => {
153                                                                         err_msg => 1,
154                                                                         type    => 1,
155                                                                      } },
156
157       
158         };
159
160         #-------------------------------------------------------------------------------
161         # Now comes the evil!  Generate classes
162
163         for my $pkg ( __PACKAGE__->classes ) {
164                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
165
166                 eval <<"                PERL";
167                         package $pkg;
168                         use base 'Fieldmapper';
169                 PERL
170
171                 my $pos = 0;
172                 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
173                         $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
174                         $pos++;
175                 }
176
177                 if (exists $$fieldmap{$pkg}{proto_fields}) {
178                         for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
179                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
180                                 $pos++;
181                         }
182                 }
183
184                 unless ( $$fieldmap{$pkg}{virtual} ) {
185                         $$fieldmap{$pkg}{cdbi} = $cdbi;
186                         for my $col ( sort $cdbi->columns('All') ) {
187                                 $$fieldmap{$pkg}{fields}{$col} = { position => $pos, virtual => 0 };
188                                 $pos++;
189                         }
190                 }
191
192                 JSON->register_class_hint(
193                         hint => $pkg->json_hint,
194                         name => $pkg,
195                         type => 'array',
196                 );
197
198         }
199 }
200
201 sub new {
202         my $self = shift;
203         my $value = shift;
204         $value = [] unless (defined $value);
205         return bless $value => $self->class_name;
206 }
207
208 sub decast {
209         my $self = shift;
210         return [ @$self ];
211 }
212
213 sub DESTROY {}
214
215 sub AUTOLOAD {
216         my $obj = shift;
217         my $value = shift;
218         (my $field = $AUTOLOAD) =~ s/^.*://o;
219         my $class_name = $obj->class_name;
220
221         my $fpos = $field;
222         $fpos  =~ s/^clear_//og ;
223
224         my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
225
226         if ($field =~ /^clear_/o) {
227                 {       no strict 'subs';
228                         *{$obj->class_name."::$field"} = sub {
229                                 my $self = shift;
230                                 $self->[$pos] = undef;
231                                 return 1;
232                         };
233                 }
234                 return $obj->$field();
235         }
236
237         die "No field by the name $field in $class_name!"
238                 unless (exists $$fieldmap{$class_name}{fields}{$field});
239
240
241         {       no strict 'subs';
242                 *{$obj->class_name."::$field"} = sub {
243                         my $self = shift;
244                         my $new_val = shift;
245                         $self->[$pos] = $new_val if (defined $new_val);
246                         return $self->[$pos];
247                 };
248         }
249         return $obj->$field($value);
250 }
251
252 sub class_name {
253         my $class_name = shift;
254         return ref($class_name) || $class_name;
255 }
256
257 sub real_fields {
258         my $self = shift;
259         my $class_name = $self->class_name;
260         my $fields = $$fieldmap{$class_name}{fields};
261
262         my @f = grep {
263                         !$$fields{$_}{virtual}
264                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
265
266         return @f;
267 }
268
269 sub api_level {
270         my $self = shift;
271         return $fieldmap->{$self->class_name}->{api_level};
272 }
273
274
275 sub is_virtual {
276         my $self = shift;
277         return $fieldmap->{$self->class_name}->{virtual};
278 }
279
280 sub json_hint {
281         my $self = shift;
282         return $fieldmap->{$self->class_name}->{hint};
283 }
284
285
286 1;