]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Fielder.pm
fixing fielder with no fields
[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     my $obj_class = $self->{class_hint};
65
66     if (!$fields) {
67         $fields = [ "Fieldmapper::$obj_class"->real_fields ];
68     }
69
70     $log->debug( 'Field list: '. OpenSRF::Utils::JSON->perl2JSON( $fields ) );
71     $log->debug( 'Query: '. OpenSRF::Utils::JSON->perl2JSON( $query ) );
72
73     return undef unless $fields;
74     return undef unless $query;
75
76     $fields = [$fields] if (!ref($fields));
77
78
79     $log->debug( 'Query Class: '. $obj_class );
80
81     my $res = new_editor()->json_query({
82         select  => { $obj_class => $fields },
83         from    => $obj_class,
84         where   => $query
85     });
86
87     for my $value (@$res) {
88         $client->respond($value);
89     }
90
91     return undef;
92
93 }
94
95 sub generate_methods {
96     try {
97         for my $class_node ( $xpc->findnodes( '//idl:class[@oils_persist:field_safe="true"]', $idl->documentElement ) ) {
98             my $hint = $class_node->getAttribute('id');
99             $log->debug("Fielder class_node $hint");
100         
101             __PACKAGE__->register_method(
102                 method          => 'fielder_fetch',
103                 api_name        => 'open-ils.fielder.' . $hint,
104                 class_hint      => $hint,
105                 stream          => 1,
106                 argc            => 1
107             );
108         }
109     } catch Error with {
110         my $e = shift;
111         $log->error("error generating Fielder methods: $e");
112     };
113 }
114
115
116 1;
117