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