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