2 use OpenSRF::Utils::JSON;
4 use base 'OpenSRF::Application';
5 use OpenSRF::Utils::Logger;
6 use OpenSRF::Utils::SettingsClient;
10 my $log = 'OpenSRF::Utils::Logger';
12 use vars qw/$fieldmap $VERSION/;
14 sub publish_fieldmapper {
15 my ($self,$client,$class) = @_;
17 return $fieldmap unless (defined $class);
18 return undef unless (exists($$fieldmap{$class}));
19 return {$class => $$fieldmap{$class}};
21 __PACKAGE__->register_method(
22 api_name => 'opensrf.open-ils.system.fieldmapper',
24 method => 'publish_fieldmapper',
28 # To dump the Javascript version of the fieldmapper struct use the command:
30 # PERL5LIB=:~/vcs/ILS/Open-ILS/src/perlmods/lib/ GEN_JS=1 perl -MOpenILS::Utils::Fieldmapper -e 'print "\n";'
32 # ... adjusted for your VCS sandbox of choice, of course.
36 return () unless (defined $fieldmap);
37 return keys %$fieldmap;
40 # Find a Fieldmapper class given the json hint.
43 foreach (keys %$fieldmap) {
44 return $_ if ($fieldmap->{$_}->{hint} eq $hint);
50 my $attr_list = shift;
51 my $attr_name = shift;
53 my $attr = $attr_list->getNamedItem( $attr_name );
54 if( defined( $attr ) ) {
55 return $attr->getValue();
61 my $field_list = shift;
64 # Get attributes of the field list. Since there is only one
65 # <field> per class, these attributes logically belong to the
66 # enclosing class, and that's where we load them.
68 my $field_attr_list = $field_list->attributes();
70 my $sequence = get_attribute( $field_attr_list, 'oils_persist:sequence' );
71 if( ! defined( $sequence ) ) {
74 my $primary = get_attribute( $field_attr_list, 'oils_persist:primary' );
76 # Load attributes into the Fieldmapper ----------------------
78 $$fieldmap{$fm}{ sequence } = $sequence;
79 $$fieldmap{$fm}{ identity } = $primary;
81 # Load each field -------------------------------------------
83 my $array_position = 0;
84 for my $field ( $field_list->childNodes() ) { # For each <field>
85 if( $field->nodeName eq 'field' ) {
87 my $attribute_list = $field->attributes();
89 my $name = get_attribute( $attribute_list, 'name' );
90 next if( $name eq 'isnew' || $name eq 'ischanged' || $name eq 'isdeleted' );
91 my $required = get_attribute( $attribute_list, 'oils_obj:required' );
92 my $validate = get_attribute( $attribute_list, 'oils_obj:validate' );
93 my $virtual = get_attribute( $attribute_list, 'oils_persist:virtual' );
94 if( ! defined( $virtual ) ) {
97 my $selector = get_attribute( $attribute_list, 'reporter:selector' );
99 $$fieldmap{$fm}{fields}{ $name } =
100 { virtual => ( $virtual eq 'true' ) ? 1 : 0,
101 required => ( $required eq 'true' ) ? 1 : 0,
102 position => $array_position,
105 $$fieldmap{$fm}{fields}{ $name }{validate} = qr/$validate/ if (defined($validate));
107 # The selector attribute, if present at all, attaches to only one
108 # of the fields in a given class. So if we see it, we store it at
109 # the level of the enclosing class.
111 if( defined( $selector ) ) {
112 $$fieldmap{$fm}{selector} = $selector;
119 # Load the standard 3 virtual fields ------------------------
121 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
122 $$fieldmap{$fm}{fields}{ $vfield } =
123 { position => $array_position,
131 my $link_list = shift;
134 for my $link ( $link_list->childNodes() ) { # For each <link>
135 if( $link->nodeName eq 'link' ) {
136 my $attribute_list = $link->attributes();
138 my $field = get_attribute( $attribute_list, 'field' );
139 my $reltype = get_attribute( $attribute_list, 'reltype' );
140 my $key = get_attribute( $attribute_list, 'key' );
141 my $class = get_attribute( $attribute_list, 'class' );
142 my $map = get_attribute( $attribute_list, 'map' );
144 $$fieldmap{$fm}{links}{ $field } =
155 my $class_node = shift;
157 # Get attributes ---------------------------------------------
159 my $attribute_list = $class_node->attributes();
161 my $fm = get_attribute( $attribute_list, 'oils_obj:fieldmapper' );
162 $fm = 'Fieldmapper::' . $fm;
163 my $id = get_attribute( $attribute_list, 'id' );
164 my $controller = get_attribute( $attribute_list, 'controller' );
165 my $virtual = get_attribute( $attribute_list, 'virtual' );
166 if( ! defined( $virtual ) ) {
169 my $tablename = get_attribute( $attribute_list, 'oils_persist:tablename' );
170 if( ! defined( $tablename ) ) {
173 my $restrict_primary = get_attribute( $attribute_list, 'oils_persist:restrict_primary' );
174 my $field_safe = get_attribute( $attribute_list, 'oils_persist:field_safe' );
176 # Load the attributes into the Fieldmapper --------------------
178 $log->debug("Building Fieldmapper class for [$fm] from IDL");
180 $$fieldmap{$fm}{ hint } = $id;
181 $$fieldmap{$fm}{ virtual } = ( $virtual eq 'true' ) ? 1 : 0;
182 $$fieldmap{$fm}{ table } = $tablename;
183 $$fieldmap{$fm}{ controller } = [ split ' ', $controller ];
184 $$fieldmap{$fm}{ restrict_primary } = $restrict_primary;
185 $$fieldmap{$fm}{ field_safe } = $field_safe;
187 # Load fields and links
189 for my $child ( $class_node->childNodes() ) {
190 my $nodeName = $child->nodeName;
191 if( $nodeName eq 'fields' ) {
192 load_fields( $child, $fm );
193 } elsif( $nodeName eq 'links' ) {
194 load_links( $child, $fm );
204 return if (keys %$fieldmap);
205 return if (!OpenSRF::System->connected && !$args{IDL});
208 my $parser = XML::LibXML->new();
209 my $file = $args{IDL} || OpenSRF::Utils::SettingsClient->new->config_value( 'IDL' );
210 my $fmdoc = $parser->parse_file( $file );
211 my $rootnode = $fmdoc->documentElement();
213 for my $child ( $rootnode->childNodes() ) { # For each <class>
214 my $nodeName = $child->nodeName;
215 if( $nodeName eq 'class' ) {
216 load_class( $child );
220 #-------------------------------------------------------------------------------
221 # Now comes the evil! Generate classes
223 for my $pkg ( __PACKAGE__->classes ) {
224 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
228 use base 'Fieldmapper';
231 if (exists $$fieldmap{$pkg}{proto_fields}) {
232 for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
233 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
238 OpenSRF::Utils::JSON->register_class_hint(
239 hint => $pkg->json_hint,
250 $value = [] unless (defined $value);
251 return bless $value => $self->class_name;
264 (my $field = $AUTOLOAD) =~ s/^.*://o;
265 my $class_name = $obj->class_name;
268 $fpos =~ s/^clear_//og ;
270 my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
272 if ($field =~ /^clear_/o) {
274 *{$obj->class_name."::$field"} = sub {
276 $self->[$pos] = undef;
280 return $obj->$field();
283 die "No field by the name $field in $class_name!"
284 unless (exists $$fieldmap{$class_name}{fields}{$field} && defined($pos));
288 *{$obj->class_name."::$field"} = sub {
291 $self->[$pos] = $new_val if (defined $new_val);
292 return $self->[$pos];
295 return $obj->$field($value);
300 return $$fieldmap{$self->class_name}{selector};
305 return $$fieldmap{$self->class_name}{identity};
308 sub RestrictPrimary {
310 return $$fieldmap{$self->class_name}{restrict_primary};
315 return $$fieldmap{$self->class_name}{sequence};
320 return $$fieldmap{$self->class_name}{table};
325 return $$fieldmap{$self->class_name}{controller};
331 return undef unless ($f);
332 return $$fieldmap{$self->class_name}{fields}{$f}{required};
338 return undef unless ($f);
339 return 1 if (!exists($$fieldmap{$self->class_name}{fields}{$f}{validate}));
340 return $self->$f =~ $$fieldmap{$self->class_name}{fields}{$f}{validate};
344 my $class_name = shift;
345 return ref($class_name) || $class_name;
350 my $class_name = $self->class_name;
351 my $fields = $$fieldmap{$class_name}{fields};
354 !$$fields{$_}{virtual}
355 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
363 my $class_name = $self->class_name;
364 return 1 if grep { $_ eq $field } keys %{$$fieldmap{$class_name}{fields}};
370 my $class_name = $self->class_name;
371 return keys %{$$fieldmap{$class_name}{fields}};
378 for my $f ($self->properties) {
388 return $self->new( [@$self] );
393 return $fieldmap->{$self->class_name}->{api_level};
398 return $fieldmap->{$self->class_name}->{cdbi};
404 return $fieldmap->{$self->class_name}->{proto_fields}->{$field} if ($field);
405 return $fieldmap->{$self->class_name}->{virtual};
411 return $fieldmap->{$self->class_name}->{readonly};
416 return $fieldmap->{$self->class_name}->{hint};