]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Utils/Fieldmapper.pm
Merge branch 'master' of git+ssh://yeti.esilibrary.com/home/evergreen/evergreen-equin...
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Utils / Fieldmapper.pm
1 package Fieldmapper;
2 use OpenSRF::Utils::JSON;
3 use Data::Dumper;
4 use base 'OpenSRF::Application';
5 use OpenSRF::Utils::Logger;
6 use OpenSRF::Utils::SettingsClient;
7 use OpenSRF::System;
8 use XML::LibXML;
9
10 my $log = 'OpenSRF::Utils::Logger';
11
12 use vars qw/$fieldmap $VERSION/;
13
14 sub publish_fieldmapper {
15         my ($self,$client,$class) = @_;
16
17         return $fieldmap unless (defined $class);
18         return undef unless (exists($$fieldmap{$class}));
19         return {$class => $$fieldmap{$class}};
20 }
21 __PACKAGE__->register_method(
22         api_name        => 'opensrf.open-ils.system.fieldmapper',
23         api_level       => 1,
24         method          => 'publish_fieldmapper',
25 );
26
27 #
28 # To dump the Javascript version of the fieldmapper struct use the command:
29 #
30 #       PERL5LIB=:~/vcs/ILS/Open-ILS/src/perlmods/lib/ GEN_JS=1 perl -MOpenILS::Utils::Fieldmapper -e 'print "\n";'
31 #
32 # ... adjusted for your VCS sandbox of choice, of course.
33 #
34
35 sub classes {
36         return () unless (defined $fieldmap);
37         return keys %$fieldmap;
38 }
39
40 sub get_attribute {
41         my $attr_list = shift;
42         my $attr_name = shift;
43
44         my $attr = $attr_list->getNamedItem( $attr_name );
45         if( defined( $attr ) ) {
46                 return $attr->getValue();
47         }
48         return undef;
49 }
50
51 sub load_fields {
52         my $field_list = shift;
53         my $fm = shift;
54
55         # Get attributes of the field list.  Since there is only one
56         # <field> per class, these attributes logically belong to the
57         # enclosing class, and that's where we load them.
58
59         my $field_attr_list = $field_list->attributes();
60
61         my $sequence  = get_attribute( $field_attr_list, 'oils_persist:sequence' );
62         if( ! defined( $sequence ) ) {
63                 $sequence = '';
64         }
65         my $primary   = get_attribute( $field_attr_list, 'oils_persist:primary' );
66
67         # Load attributes into the Fieldmapper ----------------------
68
69         $$fieldmap{$fm}{ sequence } = $sequence;
70         $$fieldmap{$fm}{ identity } = $primary;
71
72         # Load each field -------------------------------------------
73
74         my $array_position = 0;
75         for my $field ( $field_list->childNodes() ) {    # For each <field>
76                 if( $field->nodeName eq 'field' ) {
77         
78                         my $attribute_list = $field->attributes();
79                         
80                         my $name     = get_attribute( $attribute_list, 'name' );
81                         next if( $name eq 'isnew' || $name eq 'ischanged' || $name eq 'isdeleted' );
82                         my $required  = get_attribute( $attribute_list, 'oils_obj:required' );
83                         my $validate  = get_attribute( $attribute_list, 'oils_obj:validate' );
84                         my $virtual  = get_attribute( $attribute_list, 'oils_persist:virtual' );
85                         if( ! defined( $virtual ) ) {
86                                 $virtual = "false";
87                         }
88                         my $selector = get_attribute( $attribute_list, 'reporter:selector' );
89
90                         $$fieldmap{$fm}{fields}{ $name } =
91                                 { virtual => ( $virtual eq 'true' ) ? 1 : 0,
92                                   required => ( $required eq 'true' ) ? 1 : 0,
93                                   position => $array_position,
94                                 };
95
96                         $$fieldmap{$fm}{fields}{ $name }{validate} = qr/$validate/ if (defined($validate));
97
98                         # The selector attribute, if present at all, attaches to only one
99                         # of the fields in a given class.  So if we see it, we store it at
100                         # the level of the enclosing class.
101
102                         if( defined( $selector ) ) {
103                                 $$fieldmap{$fm}{selector} = $selector;
104                         }
105
106                         ++$array_position;
107                 }
108         }
109
110         # Load the standard 3 virtual fields ------------------------
111
112         for my $vfield ( qw/isnew ischanged isdeleted/ ) {
113                 $$fieldmap{$fm}{fields}{ $vfield } =
114                         { position => $array_position,
115                           virtual => 1
116                         };
117                 ++$array_position;
118         }
119 }
120
121 sub load_links {
122         my $link_list = shift;
123         my $fm = shift;
124
125         for my $link ( $link_list->childNodes() ) {    # For each <link>
126                 if( $link->nodeName eq 'link' ) {
127                         my $attribute_list = $link->attributes();
128                         
129                         my $field   = get_attribute( $attribute_list, 'field' );
130                         my $reltype = get_attribute( $attribute_list, 'reltype' );
131                         my $key     = get_attribute( $attribute_list, 'key' );
132                         my $class   = get_attribute( $attribute_list, 'class' );
133
134                         $$fieldmap{$fm}{links}{ $field } =
135                                 { class   => $class,
136                                   reltype => $reltype,
137                                   key     => $key,
138                                 };
139                 }
140         }
141 }
142
143 sub load_class {
144         my $class_node = shift;
145
146         # Get attributes ---------------------------------------------
147
148         my $attribute_list = $class_node->attributes();
149
150         my $fm               = get_attribute( $attribute_list, 'oils_obj:fieldmapper' );
151         $fm                  = 'Fieldmapper::' . $fm;
152         my $id               = get_attribute( $attribute_list, 'id' );
153         my $controller       = get_attribute( $attribute_list, 'controller' );
154         my $virtual          = get_attribute( $attribute_list, 'virtual' );
155         if( ! defined( $virtual ) ) {
156                 $virtual = 'false';
157         }
158         my $tablename        = get_attribute( $attribute_list, 'oils_persist:tablename' );
159         if( ! defined( $tablename ) ) {
160                 $tablename = '';
161         }
162         my $restrict_primary = get_attribute( $attribute_list, 'oils_persist:restrict_primary' );
163
164         # Load the attributes into the Fieldmapper --------------------
165
166         $log->debug("Building Fieldmapper class for [$fm] from IDL");
167
168         $$fieldmap{$fm}{ hint }             = $id;
169         $$fieldmap{$fm}{ virtual }          = ( $virtual eq 'true' ) ? 1 : 0;
170         $$fieldmap{$fm}{ table }            = $tablename;
171         $$fieldmap{$fm}{ controller }       = [ split ' ', $controller ];
172         $$fieldmap{$fm}{ restrict_primary } = $restrict_primary;
173
174         # Load fields and links
175
176         for my $child ( $class_node->childNodes() ) {
177                 my $nodeName = $child->nodeName;
178                 if( $nodeName eq 'fields' ) {
179                         load_fields( $child, $fm );
180                 } elsif( $nodeName eq 'links' ) {
181                         load_links( $child, $fm );
182                 }
183         }
184 }
185
186 import();
187 sub import {
188         my $class = shift;
189         my %args = @_;
190
191         return if (keys %$fieldmap);
192         return if (!OpenSRF::System->connected && !$args{IDL});
193
194         # parse the IDL ...
195         my $parser = XML::LibXML->new();
196         my $file = $args{IDL} || OpenSRF::Utils::SettingsClient->new->config_value( 'IDL' );
197         my $fmdoc = $parser->parse_file( $file );
198         my $rootnode = $fmdoc->documentElement();
199
200         for my $child ( $rootnode->childNodes() ) {    # For each <class>
201                 my $nodeName = $child->nodeName;
202                 if( $nodeName eq 'class' ) {
203                         load_class( $child );
204                 }
205         }
206
207         #-------------------------------------------------------------------------------
208         # Now comes the evil!  Generate classes
209
210         for my $pkg ( __PACKAGE__->classes ) {
211                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
212
213                 eval <<"                PERL";
214                         package $pkg;
215                         use base 'Fieldmapper';
216                 PERL
217
218                 if (exists $$fieldmap{$pkg}{proto_fields}) {
219                         for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
220                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
221                                 $pos++;
222                         }
223                 }
224
225                 OpenSRF::Utils::JSON->register_class_hint(
226                         hint => $pkg->json_hint,
227                         name => $pkg,
228                         type => 'array',
229                 );
230
231         }
232 }
233
234 sub new {
235         my $self = shift;
236         my $value = shift;
237         $value = [] unless (defined $value);
238         return bless $value => $self->class_name;
239 }
240
241 sub decast {
242         my $self = shift;
243         return [ @$self ];
244 }
245
246 sub DESTROY {}
247
248 sub AUTOLOAD {
249         my $obj = shift;
250         my $value = shift;
251         (my $field = $AUTOLOAD) =~ s/^.*://o;
252         my $class_name = $obj->class_name;
253
254         my $fpos = $field;
255         $fpos  =~ s/^clear_//og ;
256
257         my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
258
259         if ($field =~ /^clear_/o) {
260                 {       no strict 'subs';
261                         *{$obj->class_name."::$field"} = sub {
262                                 my $self = shift;
263                                 $self->[$pos] = undef;
264                                 return 1;
265                         };
266                 }
267                 return $obj->$field();
268         }
269
270         die "No field by the name $field in $class_name!"
271                 unless (exists $$fieldmap{$class_name}{fields}{$field} && defined($pos));
272
273
274         {       no strict 'subs';
275                 *{$obj->class_name."::$field"} = sub {
276                         my $self = shift;
277                         my $new_val = shift;
278                         $self->[$pos] = $new_val if (defined $new_val);
279                         return $self->[$pos];
280                 };
281         }
282         return $obj->$field($value);
283 }
284
285 sub Selector {
286         my $self = shift;
287         return $$fieldmap{$self->class_name}{selector};
288 }
289
290 sub Identity {
291         my $self = shift;
292         return $$fieldmap{$self->class_name}{identity};
293 }
294
295 sub RestrictPrimary {
296         my $self = shift;
297         return $$fieldmap{$self->class_name}{restrict_primary};
298 }
299
300 sub Sequence {
301         my $self = shift;
302         return $$fieldmap{$self->class_name}{sequence};
303 }
304
305 sub Table {
306         my $self = shift;
307         return $$fieldmap{$self->class_name}{table};
308 }
309
310 sub Controller {
311         my $self = shift;
312         return $$fieldmap{$self->class_name}{controller};
313 }
314
315 sub RequiredField {
316         my $self = shift;
317         my $f = shift;
318     return undef unless ($f);
319         return $$fieldmap{$self->class_name}{fields}{$f}{required};
320 }
321
322 sub ValidateField {
323         my $self = shift;
324         my $f = shift;
325     return undef unless ($f);
326         return 1 if (!exists($$fieldmap{$self->class_name}{fields}{$f}{validate}));
327         return $self->$f =~ $$fieldmap{$self->class_name}{fields}{$f}{validate};
328 }
329
330 sub class_name {
331         my $class_name = shift;
332         return ref($class_name) || $class_name;
333 }
334
335 sub real_fields {
336         my $self = shift;
337         my $class_name = $self->class_name;
338         my $fields = $$fieldmap{$class_name}{fields};
339
340         my @f = grep {
341                         !$$fields{$_}{virtual}
342                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
343
344         return @f;
345 }
346
347 sub has_field {
348         my $self = shift;
349         my $field = shift;
350         my $class_name = $self->class_name;
351         return 1 if grep { $_ eq $field } keys %{$$fieldmap{$class_name}{fields}};
352         return 0;
353 }
354
355 sub properties {
356         my $self = shift;
357         my $class_name = $self->class_name;
358         return keys %{$$fieldmap{$class_name}{fields}};
359 }
360
361 sub to_bare_hash {
362         my $self = shift;
363
364         my %hash = ();
365         for my $f ($self->properties) {
366                 my $val = $self->$f;
367                 $hash{$f} = $val;
368         }
369
370         return \%hash;
371 }
372
373 sub clone {
374         my $self = shift;
375         return $self->new( [@$self] );
376 }
377
378 sub api_level {
379         my $self = shift;
380         return $fieldmap->{$self->class_name}->{api_level};
381 }
382
383 sub cdbi {
384         my $self = shift;
385         return $fieldmap->{$self->class_name}->{cdbi};
386 }
387
388 sub is_virtual {
389         my $self = shift;
390         my $field = shift;
391         return $fieldmap->{$self->class_name}->{proto_fields}->{$field} if ($field);
392         return $fieldmap->{$self->class_name}->{virtual};
393 }
394
395 sub is_readonly {
396         my $self = shift;
397         my $field = shift;
398         return $fieldmap->{$self->class_name}->{readonly};
399 }
400
401 sub json_hint {
402         my $self = shift;
403         return $fieldmap->{$self->class_name}->{hint};
404 }
405
406
407 1;