]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm
added general storage result caching where applicable; added actor::card to the field...
[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 (defined $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               => 'ac'    },
63                 'Fieldmapper::actor::user'                      => { hint => 'au'    },
64                 'Fieldmapper::actor::card'                      => { hint => 'ac'    },
65                 'Fieldmapper::actor::stat_cat'                  => { hint               => 'asc',
66                                                                      proto_fields       => { entries => 1 } },
67                 'Fieldmapper::actor::stat_cat_entry'            => { hint => 'asce'    },
68                 'Fieldmapper::actor::stat_cat_entry_user_map'   => { hint => 'ascecm'  },
69                 'Fieldmapper::actor::org_unit'                  => { hint               => 'aou',
70                                                                      proto_fields       => { children => 1 } },
71                 'Fieldmapper::actor::org_unit_type'             => { hint               => 'aout',
72                                                                      proto_fields       => { children => 1 } },
73                 
74                 'Fieldmapper::biblio::record_node'              => { hint               => 'brn',
75                                                                      virtual            => 1,
76                                                                      proto_fields       => { children           => 1,
77                                                                                              id                 => 1,
78                                                                                              owner_doc          => 1,
79                                                                                              intra_doc_id       => 1,
80                                                                                              parent_node        => 1,
81                                                                                              node_type          => 1,
82                                                                                              namepsace_uri      => 1,
83                                                                                              name               => 1,
84                                                                                              value              => 1,
85                                                                                            } },
86                 'Fieldmapper::biblio::record_entry'             => { hint               => 'bre',
87                                                                      proto_fields       => { call_numbers => 1 } },
88                 #'Fieldmapper::biblio::record_marc'             => { hint => 'brx'  }, # now it's inside record_entry
89
90                 'Fieldmapper::money::cash_payment'              => { hint => 'mcp'  },
91                 'Fieldmapper::money::billing'                   => { hint => 'mb'  },
92
93                 'Fieldmapper::config::identification_type'      => { hint => 'cit'  },
94                 'Fieldmapper::config::bib_source'               => { hint => 'cbs'  },
95                 'Fieldmapper::config::metabib_field'            => { hint => 'cmf'  },
96
97                 'Fieldmapper::metabib::metarecord'              => { hint => 'mmr'  },
98                 'Fieldmapper::metabib::title_field_entry'       => { hint => 'mtfe' },
99                 'Fieldmapper::metabib::author_field_entry'      => { hint => 'mafe' },
100                 'Fieldmapper::metabib::subject_field_entry'     => { hint => 'msfe' },
101                 'Fieldmapper::metabib::keyword_field_entry'     => { hint => 'mkfe' },
102                 'Fieldmapper::metabib::full_rec'                => { hint => 'mfr'  },
103                 'Fieldmapper::metabib::record_descriptor'       => { hint => 'mrd'  },
104                 'Fieldmapper::metabib::metarecord_source_map'   => { hint => 'mmrsm'},
105
106                 'Fieldmapper::asset::copy'                      => { hint               => 'acp',
107                                                                      proto_fields       => { stat_cat_entries => 1 } },
108                 'Fieldmapper::asset::stat_cat'                  => { hint               => 'asc',
109                                                                      proto_fields       => { entries => 1 } },
110                 'Fieldmapper::asset::stat_cat_entry'            => { hint => 'asce'    },
111                 'Fieldmapper::asset::stat_cat_entry_copy_map'   => { hint => 'ascecm'  },
112                 'Fieldmapper::asset::copy_note'                 => { hint => 'acpn'    },
113                 'Fieldmapper::asset::call_number'               => { hint               => 'acn',
114                                                                      proto_fields       => { copies => 1 } },
115                 'Fieldmapper::asset::call_number_note'          => { hint => 'acnn'    },
116         };
117
118         #-------------------------------------------------------------------------------
119         # Now comes the evil!  Generate classes
120
121         for my $pkg ( keys %$fieldmap ) {
122                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
123
124                 eval <<"                PERL";
125                         package $pkg;
126                         use base 'Fieldmapper';
127                 PERL
128
129                 $$fieldmap{$pkg}{cdbi} = $cdbi;
130
131                 my $pos = 0;
132                 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
133                         $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
134                         $pos++;
135                 }
136
137                 if (exists $$fieldmap{$pkg}{proto_fields}) {
138                         for my $pfield ( keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
139                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
140                                 $pos++;
141                         }
142                 }
143
144                 unless ( $$fieldmap{$pkg}{virtual} ) {
145                         for my $col ( $cdbi->columns('All') ) {
146                                 $$fieldmap{$pkg}{fields}{$col} = { position => $pos, virtual => 0 };
147                                 $pos++;
148                         }
149                 }
150
151                 JSON->register_class_hint(
152                         hint => $pkg->json_hint,
153                         name => $pkg,
154                         type => 'array',
155                 );
156
157         }
158 }
159
160 sub new {
161         my $self = shift;
162         my $value = shift;
163         $value = [] unless (defined $value);
164         return bless $value => $self->class_name;
165 }
166
167 sub DESTROY {}
168
169 sub AUTOLOAD {
170         my $obj = shift;
171         my $value = shift;
172         (my $field = $AUTOLOAD) =~ s/^.*://o;
173         my $class_name = $obj->class_name;
174
175         my $pos = $$fieldmap{$class_name}{fields}{$field}{position};
176
177         if ($field =~ /^clear_/o) {
178                 {       no strict 'subs';
179                         *{$obj->class_name."::$field"} = sub {
180                                 my $self = shift;
181                                 $self->[$pos] = undef;
182                                 return 1;
183                         };
184                 }
185                 return $obj->$field();
186         }
187
188         die "No field by the name $field in $class_name!"
189                 unless (exists $$fieldmap{$class_name}{fields}{$field});
190
191
192         {       no strict 'subs';
193                 *{$obj->class_name."::$field"} = sub {
194                         my $self = shift;
195                         my $new_val = shift;
196                         $self->[$pos] = $new_val if (defined $new_val);
197                         return $self->[$pos];
198                 };
199         }
200         return $obj->$field($value);
201 }
202
203 sub class_name {
204         my $class_name = shift;
205         return ref($class_name) || $class_name;
206 }
207
208 sub real_fields {
209         my $self = shift;
210         my $class_name = $self->class_name;
211         my $fields = $$fieldmap{$class_name}{fields};
212
213         my @f = grep {
214                         !$$fields{$_}{virtual}
215                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
216
217         return @f;
218 }
219
220 sub api_level {
221         my $self = shift;
222         return $fieldmap->{$self->class_name}->{api_level};
223 }
224
225 sub json_hint {
226         my $self = shift;
227         return $fieldmap->{$self->class_name}->{hint};
228 }
229
230
231 1;