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