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