]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Utils/SpiderMonkey.pm
added activity logging
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Utils / SpiderMonkey.pm
1 package OpenILS::Utils::SpiderMonkey;
2 use strict; use warnings;
3 use OpenSRF::Utils::Logger qw(:logger);
4 use OpenILS::Utils::ScriptRunner;
5 use base 'OpenILS::Utils::ScriptRunner';
6 use JavaScript::SpiderMonkey;
7
8 sub new {
9         my ( $class, %params ) = @_;
10         $class = ref($class) || $class;
11         my $self = { file => $params{file}, libs => $params{libs} };
12         return bless( $self, $class );
13 }
14
15 sub context {
16         my( $self, $context ) = @_;
17         $self->{ctx} = $context if $context;
18         return $self->{ctx};
19 }
20
21 sub init {
22         my $self = shift;
23         my $js = JavaScript::SpiderMonkey->new();
24         $js->init();
25         $js->function_set("perl_print",         sub { print "@_\n"; } );
26         $js->function_set("log_activity",       sub { $logger->activity(@_); return 1;} );
27         $js->function_set("log_error",          sub { $logger->error(@_); return 1;} );
28         $js->function_set("log_warn",                   sub { $logger->warn(@_); return 1;} );
29         $js->function_set("log_info",                   sub { $logger->info(@_); return 1;} );
30         $js->function_set("log_debug",          sub { $logger->debug(@_); return 1;} );
31         $js->function_set("log_internal",       sub { $logger->internal(@_); return 1;} );
32         $js->function_set("debug",                              sub { $logger->debug(@_); return 1;} );
33         $js->function_set("alert",                              sub { $logger->warn(@_); return 1;} );
34         $self->context($js);
35         $self->load_lib($_) for @{$self->{libs}};
36 }
37
38
39 sub load {
40         my( $self, $filename ) = @_;
41         $self->{file} = $filename;
42 }
43
44 sub run {
45         my $self = shift;
46         my $file = shift() || $self->{file};
47         my $js = $self->context;
48
49         if( ! open(F, $file) ) {
50                 $logger->error("Error opening script file: $file");
51                 return 0;
52         }
53
54         if( ! $js->eval(join("\n", <F>)) ) {
55                 $logger->error("Script ($file) eval failed in SpiderMonkey run: $@");  
56                 return 0;
57         }
58
59         close(F);
60         return 1;
61 }
62
63 sub load_lib { 
64         my( $self, $file ) = @_;
65         $self->run( $file );
66 }
67
68 sub _js_prop_name {
69         my $name = shift;
70         $name =~ s/^.*\.//o;
71         return $name;
72 }
73
74 sub retrieve {
75         my( $self, $key ) = @_;
76         return $self->context->property_get($key);
77 }
78
79 sub insert {
80         my( $self, $key, $val ) = @_;
81         return unless defined($val);
82
83         if (ref($val) =~ /^Fieldmapper/o) {
84                 $self->insert_fm($key, $val);
85         } elsif (ref($val) and $val =~ /ARRAY/o) {
86                 $self->insert_array($key, $val);
87         } elsif (ref($val) and $val =~ /HASH/o) {
88                 $self->insert_hash($key, $val);
89         } elsif (!ref($val)) {
90                 $self->context->property_by_path(
91                         $key, $val,
92                         sub { $val },
93                         sub { my( $k, $v ) = @_; $val = $v; }
94                 );
95         } else {
96                 return 0;
97         }
98
99         return 1;
100 }
101
102 sub insert_fm {
103
104         my( $self, $key, $fm ) = @_;
105         my $ctx = $self->context;
106         return undef unless ($ctx and $key and $fm);
107         my $o = $ctx->object_by_path($key);
108         
109         for my $f ( $fm->properties ) {
110                 my $val = $fm->$f();
111                 if (ref $val) {
112                         $self->insert("$key.$f", $val);
113                 } else {
114                         $ctx->property_by_path(
115                                 "$key.$f",
116                                 $val,
117                                 sub {
118                                         my $k = _js_prop_name(shift());
119                                         $fm->$k();
120                                 }, 
121
122                                 sub {
123                                         my $k = _js_prop_name(shift());
124                                         $fm->ischanged(1);
125                                         $fm->$k(@_);
126                                 }
127                         );
128                 }
129         }
130 }
131
132 sub insert_hash {
133
134         my( $self, $key, $hash ) = @_;
135         my $ctx = $self->context;
136         return undef unless ($ctx and $key and $hash);
137         $ctx->object_by_path($key);
138         
139         for my $k ( keys %$hash ) {
140                 my $v = $hash->{$k};
141                 if (ref $v) {
142                         $self->insert("$key.$k", $v);
143                 } else {
144                         $ctx->property_by_path(
145                                 "$key.$k", $v,
146                                 sub { $hash->{_js_prop_name(shift())} },
147                                 sub { 
148                                         my( $key, $val ) = @_;
149                                         $hash->{_js_prop_name($key)} = $val; }
150                         );
151                 }
152         }
153 }
154
155 sub insert_array {
156
157         my( $self, $key, $array ) = @_;
158         my $ctx = $self->context;
159         return undef unless ($ctx and $key and $array);
160
161         my $a = $ctx->array_by_path($key);
162         
163         my $ind = 0;
164         for my $v ( @$array ) {
165                 if (ref $v) {
166                         my $elobj = $ctx->object_by_path('__tmp_arr_el');
167                         $self->insert('__tmp_arr_el', $v);
168                         $ctx->array_set_element_as_object( $a, $ind, $elobj );
169                 } else {
170                         $ctx->array_set_element( $a, $ind, $v ) if defined($v);
171                 }
172                 $ind++;
173         }
174 }
175
176 1;