]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Fielder.pm
45bb5389ca2c908f18613b740a36515625c1ad3f
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Fielder.pm
1 # vim:et:ts=4:sw=4:
2
3 package OpenILS::Application::Fielder;
4 use OpenILS::Application;
5 use base qw/OpenILS::Application/;
6
7 use Unicode::Normalize;
8 use OpenSRF::EX qw/:try/;
9
10 use OpenSRF::AppSession;
11 use OpenSRF::Utils::SettingsClient;
12 use OpenSRF::Utils::Logger qw/:level/;
13
14 use OpenILS::Utils::Fieldmapper;
15 use OpenSRF::Utils::JSON;
16
17 use OpenILS::Utils::CStoreEditor qw/:funcs/;
18
19 use XML::LibXML;
20 use XML::LibXML::XPathContext;
21 use XML::LibXSLT;
22
23 our %namespace_map = (
24     oils_persist=> {ns => 'http://open-ils.org/spec/opensrf/IDL/persistence/v1'},
25     oils_obj    => {ns => 'http://open-ils.org/spec/opensrf/IDL/objects/v1'},
26     idl         => {ns => 'http://opensrf.org/spec/IDL/base/v1'},
27     reporter    => {ns => 'http://open-ils.org/spec/opensrf/IDL/reporter/v1'},
28     perm        => {ns => 'http://open-ils.org/spec/opensrf/IDL/permacrud/v1'},
29 );
30
31
32 my $log = 'OpenSRF::Utils::Logger';
33
34 my $parser = XML::LibXML->new();
35 my $xslt = XML::LibXSLT->new();
36
37 my $xpc = XML::LibXML::XPathContext->new();
38 $xpc->registerNs($_, $namespace_map{$_}{ns}) for ( keys %namespace_map );
39
40 my $idl;
41
42 sub initialize {
43
44     my $conf = OpenSRF::Utils::SettingsClient->new;
45     my $idl_file = $conf->config_value( 'IDL' );
46
47     $idl = $parser->parse_file( $idl_file );
48
49     $log->debug( 'IDL XML file loaded' );
50
51     generate_methods();
52
53 }
54 sub child_init {}
55
56 sub fielder_fetch {
57     my $self = shift;
58     my $client = shift;
59     my $obj = shift;
60
61     my $query = $obj->{query};
62     my $fields = $obj->{fields};
63
64     return undef unless $fields;
65     return undef unless $query;
66
67     $fields = [$fields] if (!ref($fields));
68
69     my $e = new_editor();
70
71     my $obj_class = $self->{class_hint};
72
73     my $req = $e-json_query({
74         select  => { $obj_class => $fields },
75         from    => $obj_class,
76         where   => $query
77     });
78
79     while (my $res = $req->recv) {
80         $client->respond($res->content);
81     }
82
83     return undef;
84
85 }
86
87 sub generate_methods {
88     try {
89         for my $class_node ( $xpc->findnodes( '//idl:class[@oils_persist:field_safe="true"]', $idl->documentElement ) ) {
90             my $hint = $class_node->getAttribute('id');
91             $log->debug("Fielder class_node $hint");
92         
93             __PACKAGE__->register_method(
94                 method          => 'fielder_fetch',
95                 api_name        => 'open-ils.fielder.' . $hint,
96                 class_hint      => $hint,
97                 stream          => 1,
98                 argc            => 1
99             );
100         }
101     } catch Error with {
102         my $e = shift;
103         $log->error("error generating Fielder methods: $e");
104     };
105 }
106
107
108 1;
109