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