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=~/cvs/ILS/OpenSRF/src/perlmods/:~/cvs/ILS/Open-ILS/src/perlmods/ GEN_JS=1 perl -MOpenILS::Utils::Fieldmapper -e 'print "\n";'
32 # ... adjusted for your CVS sandbox, of course.
36 return () unless (defined $fieldmap);
37 return keys %$fieldmap;
41 my $attr_list = shift;
42 my $attr_name = shift;
44 my $attr = $attr_list->getNamedItem( $attr_name );
45 if( defined( $attr ) ) {
46 return $attr->getValue();
52 my $field_list = shift;
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.
59 my $field_attr_list = $field_list->attributes();
61 my $sequence = get_attribute( $field_attr_list, 'oils_persist:sequence' );
62 if( ! defined( $sequence ) ) {
65 my $primary = get_attribute( $field_attr_list, 'oils_persist:primary' );
67 # Load attributes into the Fieldmapper ----------------------
69 $$fieldmap{$fm}{ sequence } = $sequence;
70 $$fieldmap{$fm}{ identity } = $primary;
72 # Load each field -------------------------------------------
74 for my $field ( $field_list->childNodes() ) { # For each <field>
75 if( $field->nodeName eq 'field' ) {
77 my $attribute_list = $field->attributes();
79 my $name = get_attribute( $attribute_list, 'name' );
80 my $array_position = get_attribute( $attribute_list, 'oils_obj:array_position' );
81 my $virtual = get_attribute( $attribute_list, 'oils_persist:virtual' );
82 if( ! defined( $virtual ) ) {
85 my $selector = get_attribute( $attribute_list, 'reporter:selector' );
87 $$fieldmap{$fm}{fields}{ $name } =
88 { virtual => ( $virtual eq 'true' ) ? 1 : 0,
89 position => $array_position,
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.
96 if( defined( $selector ) ) {
97 $$fieldmap{$fm}{selector} = $selector;
104 my $link_list = shift;
107 for my $link ( $link_list->childNodes() ) { # For each <link>
108 if( $link->nodeName eq 'link' ) {
109 my $attribute_list = $link->attributes();
111 my $field = get_attribute( $attribute_list, 'field' );
112 my $reltype = get_attribute( $attribute_list, 'reltype' );
113 my $key = get_attribute( $attribute_list, 'key' );
114 my $class = get_attribute( $attribute_list, 'class' );
116 $$fieldmap{$fm}{links}{ $field } =
126 my $class_node = shift;
128 # Get attributes ---------------------------------------------
130 my $attribute_list = $class_node->attributes();
132 my $fm = get_attribute( $attribute_list, 'oils_obj:fieldmapper' );
133 $fm = 'Fieldmapper::' . $fm;
134 my $id = get_attribute( $attribute_list, 'id' );
135 my $controller = get_attribute( $attribute_list, 'controller' );
136 my $virtual = get_attribute( $attribute_list, 'virtual' );
137 if( ! defined( $virtual ) ) {
140 my $tablename = get_attribute( $attribute_list, 'oils_persist:tablename' );
141 if( ! defined( $tablename ) ) {
144 my $restrict_primary = get_attribute( $attribute_list, 'oils_persist:restrict_primary' );
146 # Load the attributes into the Fieldmapper --------------------
148 $log->debug("Building Fieldmapper class for [$fm] from IDL");
150 $$fieldmap{$fm}{ hint } = $id;
151 $$fieldmap{$fm}{ virtual } = ( $virtual eq 'true' ) ? 1 : 0;
152 $$fieldmap{$fm}{ table } = $tablename;
153 $$fieldmap{$fm}{ controller } = [ split ' ', $controller ];
154 $$fieldmap{$fm}{ restrict_primary } = $restrict_primary;
156 # Load fields and links
158 for my $child ( $class_node->childNodes() ) {
159 my $nodeName = $child->nodeName;
160 if( $nodeName eq 'fields' ) {
161 load_fields( $child, $fm );
162 } elsif( $nodeName eq 'links' ) {
163 load_links( $child, $fm );
173 return if (keys %$fieldmap);
174 return if (!OpenSRF::System->connected && !$args{IDL});
177 my $parser = XML::LibXML->new();
178 my $file = $args{IDL} || OpenSRF::Utils::SettingsClient->new->config_value( 'IDL' );
179 my $fmdoc = $parser->parse_file( $file );
180 my $rootnode = $fmdoc->documentElement();
182 for my $child ( $rootnode->childNodes() ) { # For each <class>
183 my $nodeName = $child->nodeName;
184 if( $nodeName eq 'class' ) {
185 load_class( $child );
189 #-------------------------------------------------------------------------------
190 # Now comes the evil! Generate classes
192 for my $pkg ( __PACKAGE__->classes ) {
193 (my $cdbi = $pkg) =~ s/^Fieldmapper:://o;
197 use base 'Fieldmapper';
201 for my $vfield ( qw/isnew ischanged isdeleted/ ) {
202 $$fieldmap{$pkg}{fields}{$vfield} = { position => $pos, virtual => 1 };
206 if (exists $$fieldmap{$pkg}{proto_fields}) {
207 for my $pfield ( sort keys %{ $$fieldmap{$pkg}{proto_fields} } ) {
208 $$fieldmap{$pkg}{fields}{$pfield} = { position => $pos, virtual => $$fieldmap{$pkg}{proto_fields}{$pfield} };
213 OpenSRF::Utils::JSON->register_class_hint(
214 hint => $pkg->json_hint,
225 $value = [] unless (defined $value);
226 return bless $value => $self->class_name;
239 (my $field = $AUTOLOAD) =~ s/^.*://o;
240 my $class_name = $obj->class_name;
243 $fpos =~ s/^clear_//og ;
245 my $pos = $$fieldmap{$class_name}{fields}{$fpos}{position};
247 if ($field =~ /^clear_/o) {
249 *{$obj->class_name."::$field"} = sub {
251 $self->[$pos] = undef;
255 return $obj->$field();
258 die "No field by the name $field in $class_name!"
259 unless (exists $$fieldmap{$class_name}{fields}{$field} && defined($pos));
263 *{$obj->class_name."::$field"} = sub {
266 $self->[$pos] = $new_val if (defined $new_val);
267 return $self->[$pos];
270 return $obj->$field($value);
275 return $$fieldmap{$self->class_name}{selector};
280 return $$fieldmap{$self->class_name}{identity};
283 sub RestrictPrimary {
285 return $$fieldmap{$self->class_name}{restrict_primary};
290 return $$fieldmap{$self->class_name}{sequence};
295 return $$fieldmap{$self->class_name}{table};
300 return $$fieldmap{$self->class_name}{controller};
304 my $class_name = shift;
305 return ref($class_name) || $class_name;
310 my $class_name = $self->class_name;
311 my $fields = $$fieldmap{$class_name}{fields};
314 !$$fields{$_}{virtual}
315 } sort {$$fields{$a}{position} <=> $$fields{$b}{position}} keys %$fields;
323 my $class_name = $self->class_name;
324 return 1 if grep { $_ eq $field } keys %{$$fieldmap{$class_name}{fields}};
330 my $class_name = $self->class_name;
331 return keys %{$$fieldmap{$class_name}{fields}};
338 for my $f ($self->properties) {
348 return $self->new( [@$self] );
353 return $fieldmap->{$self->class_name}->{api_level};
358 return $fieldmap->{$self->class_name}->{cdbi};
364 return $fieldmap->{$self->class_name}->{proto_fields}->{$field} if ($field);
365 return $fieldmap->{$self->class_name}->{virtual};
371 return $fieldmap->{$self->class_name}->{readonly};
376 return $fieldmap->{$self->class_name}->{hint};