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