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