]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/Fieldmapper.pm
field array position is determined by position in the IDL. dynamically generate...
[working/Evergreen.git] / Open-ILS / src / perlmods / 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=~/cvs/ILS/OpenSRF/src/perlmods/:~/cvs/ILS/Open-ILS/src/perlmods/ GEN_JS=1 perl -MOpenILS::Utils::Fieldmapper -e 'print "\n";'
31 #
32 # ... adjusted for your CVS sandbox, 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 $virtual  = get_attribute( $attribute_list, 'oils_persist:virtual' );
83                         if( ! defined( $virtual ) ) {
84                                 $virtual = "false";
85                         }
86                         my $selector = get_attribute( $attribute_list, 'reporter:selector' );
87
88                         $$fieldmap{$fm}{fields}{ $name } =
89                                 { virtual => ( $virtual eq 'true' ) ? 1 : 0,
90                                   position => $array_position,
91                                 };
92
93                         # The selector attribute, if present at all, attaches to only one
94                         # of the fields in a given class.  So if we see it, we store it at
95                         # the level of the enclosing class.
96
97                         if( defined( $selector ) ) {
98                                 $$fieldmap{$fm}{selector} = $selector;
99                         }
100
101                         ++$array_position;
102                 }
103         }
104
105         # Load the standard 3 virtual fields ------------------------
106
107         for my $vfield ( qw/isnew ischanged isdeleted/ ) {
108                 $$fieldmap{$fm}{fields}{ $vfield } =
109                         { position => $array_position,
110                           virtual => 1
111                         };
112                 ++$array_position;
113         }
114 }
115
116 sub load_links {
117         my $link_list = shift;
118         my $fm = shift;
119
120         for my $link ( $link_list->childNodes() ) {    # For each <link>
121                 if( $link->nodeName eq 'link' ) {
122                         my $attribute_list = $link->attributes();
123                         
124                         my $field   = get_attribute( $attribute_list, 'field' );
125                         my $reltype = get_attribute( $attribute_list, 'reltype' );
126                         my $key     = get_attribute( $attribute_list, 'key' );
127                         my $class   = get_attribute( $attribute_list, 'class' );
128
129                         $$fieldmap{$fm}{links}{ $field } =
130                                 { class   => $class,
131                                   reltype => $reltype,
132                                   key     => $key,
133                                 };
134                 }
135         }
136 }
137
138 sub load_class {
139         my $class_node = shift;
140
141         # Get attributes ---------------------------------------------
142
143         my $attribute_list = $class_node->attributes();
144
145         my $fm               = get_attribute( $attribute_list, 'oils_obj:fieldmapper' );
146         $fm                  = 'Fieldmapper::' . $fm;
147         my $id               = get_attribute( $attribute_list, 'id' );
148         my $controller       = get_attribute( $attribute_list, 'controller' );
149         my $virtual          = get_attribute( $attribute_list, 'virtual' );
150         if( ! defined( $virtual ) ) {
151                 $virtual = 'false';
152         }
153         my $tablename        = get_attribute( $attribute_list, 'oils_persist:tablename' );
154         if( ! defined( $tablename ) ) {
155                 $tablename = '';
156         }
157         my $restrict_primary = get_attribute( $attribute_list, 'oils_persist:restrict_primary' );
158
159         # Load the attributes into the Fieldmapper --------------------
160
161         $log->debug("Building Fieldmapper class for [$fm] from IDL");
162
163         $$fieldmap{$fm}{ hint }             = $id;
164         $$fieldmap{$fm}{ virtual }          = ( $virtual eq 'true' ) ? 1 : 0;
165         $$fieldmap{$fm}{ table }            = $tablename;
166         $$fieldmap{$fm}{ controller }       = [ split ' ', $controller ];
167         $$fieldmap{$fm}{ restrict_primary } = $restrict_primary;
168
169         # Load fields and links
170
171         for my $child ( $class_node->childNodes() ) {
172                 my $nodeName = $child->nodeName;
173                 if( $nodeName eq 'fields' ) {
174                         load_fields( $child, $fm );
175                 } elsif( $nodeName eq 'links' ) {
176                         load_links( $child, $fm );
177                 }
178         }
179 }
180
181 import();
182 sub import {
183         my $class = shift;
184         my %args = @_;
185
186         return if (keys %$fieldmap);
187         return if (!OpenSRF::System->connected && !$args{IDL});
188
189         # parse the IDL ...
190         my $parser = XML::LibXML->new();
191         my $file = $args{IDL} || OpenSRF::Utils::SettingsClient->new->config_value( 'IDL' );
192         my $fmdoc = $parser->parse_file( $file );
193         my $rootnode = $fmdoc->documentElement();
194
195         for my $child ( $rootnode->childNodes() ) {    # For each <class>
196                 my $nodeName = $child->nodeName;
197                 if( $nodeName eq 'class' ) {
198                         load_class( $child );
199                 }
200         }
201
202         #-------------------------------------------------------------------------------
203         # Now comes the evil!  Generate classes
204
205         for my $pkg ( __PACKAGE__->classes ) {
206                 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
207
208                 eval <<"                PERL";
209                         package $pkg;
210                         use base 'Fieldmapper';
211                 PERL
212
213                 if (exists $$fieldmap{$pkg}{proto_fields}) {
214                         for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
215                                 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
216                                 $pos++;
217                         }
218                 }
219
220                 OpenSRF::Utils::JSON->register_class_hint(
221                         hint => $pkg->json_hint,
222                         name => $pkg,
223                         type => 'array',
224                 );
225
226         }
227 }
228
229 sub new {
230         my $self = shift;
231         my $value = shift;
232         $value = [] unless (defined $value);
233         return bless $value => $self->class_name;
234 }
235
236 sub decast {
237         my $self = shift;
238         return [ @$self ];
239 }
240
241 sub DESTROY {}
242
243 sub AUTOLOAD {
244         my $obj = shift;
245         my $value = shift;
246         (my $field = $AUTOLOAD) =~ s/^.*://o;
247         my $class_name = $obj->class_name;
248
249         my $fpos = $field;
250         $fpos  =~ s/^clear_//og ;
251
252         my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
253
254         if ($field =~ /^clear_/o) {
255                 {       no strict 'subs';
256                         *{$obj->class_name."::$field"} = sub {
257                                 my $self = shift;
258                                 $self->[$pos] = undef;
259                                 return 1;
260                         };
261                 }
262                 return $obj->$field();
263         }
264
265         die "No field by the name $field in $class_name!"
266                 unless (exists $$fieldmap{$class_name}{fields}{$field} && defined($pos));
267
268
269         {       no strict 'subs';
270                 *{$obj->class_name."::$field"} = sub {
271                         my $self = shift;
272                         my $new_val = shift;
273                         $self->[$pos] = $new_val if (defined $new_val);
274                         return $self->[$pos];
275                 };
276         }
277         return $obj->$field($value);
278 }
279
280 sub Selector {
281         my $self = shift;
282         return $$fieldmap{$self->class_name}{selector};
283 }
284
285 sub Identity {
286         my $self = shift;
287         return $$fieldmap{$self->class_name}{identity};
288 }
289
290 sub RestrictPrimary {
291         my $self = shift;
292         return $$fieldmap{$self->class_name}{restrict_primary};
293 }
294
295 sub Sequence {
296         my $self = shift;
297         return $$fieldmap{$self->class_name}{sequence};
298 }
299
300 sub Table {
301         my $self = shift;
302         return $$fieldmap{$self->class_name}{table};
303 }
304
305 sub Controller {
306         my $self = shift;
307         return $$fieldmap{$self->class_name}{controller};
308 }
309
310 sub class_name {
311         my $class_name = shift;
312         return ref($class_name) || $class_name;
313 }
314
315 sub real_fields {
316         my $self = shift;
317         my $class_name = $self->class_name;
318         my $fields = $$fieldmap{$class_name}{fields};
319
320         my @f = grep {
321                         !$$fields{$_}{virtual}
322                 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
323
324         return @f;
325 }
326
327 sub has_field {
328         my $self = shift;
329         my $field = shift;
330         my $class_name = $self->class_name;
331         return 1 if grep { $_ eq $field } keys %{$$fieldmap{$class_name}{fields}};
332         return 0;
333 }
334
335 sub properties {
336         my $self = shift;
337         my $class_name = $self->class_name;
338         return keys %{$$fieldmap{$class_name}{fields}};
339 }
340
341 sub to_bare_hash {
342         my $self = shift;
343
344         my %hash = ();
345         for my $f ($self->properties) {
346                 my $val = $self->$f;
347                 $hash{$f} = $val;
348         }
349
350         return \%hash;
351 }
352
353 sub clone {
354         my $self = shift;
355         return $self->new( [@$self] );
356 }
357
358 sub api_level {
359         my $self = shift;
360         return $fieldmap->{$self->class_name}->{api_level};
361 }
362
363 sub cdbi {
364         my $self = shift;
365         return $fieldmap->{$self->class_name}->{cdbi};
366 }
367
368 sub is_virtual {
369         my $self = shift;
370         my $field = shift;
371         return $fieldmap->{$self->class_name}->{proto_fields}->{$field} if ($field);
372         return $fieldmap->{$self->class_name}->{virtual};
373 }
374
375 sub is_readonly {
376         my $self = shift;
377         my $field = shift;
378         return $fieldmap->{$self->class_name}->{readonly};
379 }
380
381 sub json_hint {
382         my $self = shift;
383         return $fieldmap->{$self->class_name}->{hint};
384 }
385
386
387 1;