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