asset setup
[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::biblio;
12 use OpenILS::Application::Storage::CDBI::config;
13 use OpenILS::Application::Storage::CDBI::metabib;
14
15 use vars qw/$fieldmap $VERSION/;
16
17 _init();
18
19 sub publish_fieldmapper {
20         my ($self,$client,$class) = @_;
21
22         return $fieldmap unless (defined $class);
23         return undef unless (exists($$fieldmap{$class}));
24         return {$class => $$fieldmap{$class}};
25 }
26 __PACKAGE__->register_method(
27         api_name        => 'opensrf.open-ils.system.fieldmapper',
28         api_level       => 1,
29         method          => 'publish_fieldmapper',
30 );
31
32 #
33 # To dump the Javascript version of the fieldmapper struct use the command:
34 #
35 #       PERL5LIB=~/cvs/ILS/OpenSRF/src/perlmods/:~/cvs/ILS/Open-ILS/src/perlmods/ GEN_JS=1 perl -MOpenILS::Utils::Fieldmapper -e 'print "\n";'
36 #
37 # ... adjusted for your CVS sandbox, of course.
38 #
39
40 sub classes {
41         return () unless (defined $fieldmap);
42         return keys %$fieldmap;
43 }
44
45 sub _init {
46         return if (defined $fieldmap);
47
48         $fieldmap = 
49         {
50                 'Fieldmapper::actor::user'                      => { hint => 'au'   },
51                 'Fieldmapper::actor::org_unit'                  => { hint => 'aou'  },
52                 'Fieldmapper::actor::org_unit_type'             => { hint => 'aout' },
53                 
54                 'Fieldmapper::biblio::record_node'              => { hint               => 'brn',
55                                                                      proto_fields       => { children => 1 } },
56                 'Fieldmapper::biblio::record_entry'             => { hint => 'bre'  },
57                 'Fieldmapper::biblio::record_mods'              => { hint => 'brm'  },
58                 'Fieldmapper::biblio::record_marc'              => { hint => 'brx'  },
59
60                 'Fieldmapper::config::bib_source'               => { hint => 'cbs'  },
61                 'Fieldmapper::config::metabib_field'            => { hint => 'cmf'  },
62
63                 'Fieldmapper::metabib::metarecord'              => { hint => 'mmr'  },
64                 'Fieldmapper::metabib::title_field_entry'       => { hint => 'mtfe' },
65                 'Fieldmapper::metabib::author_field_entry'      => { hint => 'mafe' },
66                 'Fieldmapper::metabib::subject_field_entry'     => { hint => 'msfe' },
67                 'Fieldmapper::metabib::keyword_field_entry'     => { hint => 'mkfe' },
68                 'Fieldmapper::metabib::full_rec'                => { hint => 'mfr'  },
69
70                 'Fieldmapper::asset::copy'                      => { hint => 'acp'  },
71                 'Fieldmapper::asset::copy_note'                 => { hint => 'acpn' },
72                 'Fieldmapper::asset::call_number'               => { hint => 'acn'  },
73                 'Fieldmapper::asset::call_number_note'          => { hint => 'acnn' },
74         };
75
76         #-------------------------------------------------------------------------------
77         # Now comes the evil!  Generate classes
78
79         for my $pkg ( keys %$fieldmap ) {
80                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
81
82                 eval <<"                PERL";
83                         package $pkg;
84                         use base 'Fieldmapper';
85                 PERL
86
87                 $$fieldmap{$pkg}{cdbi} = $cdbi;
88
89                 my $pos = 0;
90                 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
91                         $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
92                         $pos++;
93                 }
94
95                 if (exists $$fieldmap{$pkg}{proto_fields}) {
96                         for my $pfield ( keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
97                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
98                                 $pos++;
99                         }
100                 }
101
102                 for my $col ( $cdbi->columns('All') ) {
103                         $$fieldmap{$pkg}{fields}{$col} = { position => $pos, virtual => 0 };
104                         $pos++;
105                 }
106
107                 JSON->register_class_hint(
108                         hint => $pkg->json_hint,
109                         name => $pkg,
110                         type => 'array',
111                 );
112
113         }
114
115         print Fieldmapper->javascript() if ($ENV{GEN_JS});
116 }
117
118 sub new {
119         my $self = shift;
120         my $value = shift;
121         $value = [] unless (defined $value);
122         return bless $value => $self->class_name;
123 }
124
125 sub javascript {
126         my $class_name = shift;
127         return 'var fieldmap = ' . JSON->perl2JSON($fieldmap) . ';'
128 }
129
130 sub DESTROY {}
131
132 sub AUTOLOAD {
133         my $obj = shift;
134         my $value = shift;
135         (my $field = $AUTOLOAD) =~ s/^.*://o;
136         my $class_name = $obj->class_name;
137
138
139         if ($field =~ /^clear_/o) {
140                 {       no strict 'subs';
141                         *{$obj->class_name."::$field"} = sub {
142                                 my $self = shift;
143                                 $self->[$pos] = undef;
144                                 return 1;
145                         };
146                 }
147                 return $obj->$field();
148         }
149
150         die "No field by the name $field in $class_name!"
151                 unless (exists $$fieldmap{$class_name}{fields}{$field});
152
153         my $pos = $$fieldmap{$class_name}{fields}{$field}{position};
154
155         {       no strict 'subs';
156                 *{$obj->class_name."::$field"} = sub {
157                         my $self = shift;
158                         my $new_val = shift;
159                         $self->[$pos] = $new_val if (defined $new_val);
160                         return $self->[$pos];
161                 };
162         }
163         return $obj->$field($value);
164 }
165
166 sub class_name {
167         my $class_name = shift;
168         return ref($class_name) || $class_name;
169 }
170
171 sub real_fields {
172         my $self = shift;
173         my $class_name = $self->class_name;
174         my $fields = $$fieldmap{$class_name}{fields};
175
176         my @f = grep {
177                         !$$fields{$_}{virtual}
178                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
179
180         return @f;
181 }
182
183 sub api_level {
184         my $self = shift;
185         return $fieldmap->{$self->class_name}->{api_level};
186 }
187
188 sub json_hint {
189         my $self = shift;
190         return $fieldmap->{$self->class_name}->{hint};
191 }
192
193
194 1;