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