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