3 package OpenILS::Application::Fielder;
4 use OpenILS::Application;
5 use base qw/OpenILS::Application/;
7 use Unicode::Normalize;
8 use OpenSRF::EX qw/:try/;
10 use OpenSRF::AppSession;
11 use OpenSRF::Utils::SettingsClient;
12 use OpenSRF::Utils::Cache;
13 use OpenSRF::Utils::Logger qw/:level/;
15 use OpenILS::Utils::Fieldmapper;
16 use OpenSRF::Utils::JSON;
18 use OpenILS::Utils::CStoreEditor qw/:funcs/;
20 use Digest::MD5 qw(md5_hex);
23 use XML::LibXML::XPathContext;
26 our %namespace_map = (
27 oils_persist=> {ns => 'http://open-ils.org/spec/opensrf/IDL/persistence/v1'},
28 oils_obj => {ns => 'http://open-ils.org/spec/opensrf/IDL/objects/v1'},
29 idl => {ns => 'http://opensrf.org/spec/IDL/base/v1'},
30 reporter => {ns => 'http://open-ils.org/spec/opensrf/IDL/reporter/v1'},
31 perm => {ns => 'http://open-ils.org/spec/opensrf/IDL/permacrud/v1'},
35 my $log = 'OpenSRF::Utils::Logger';
39 my $parser = XML::LibXML->new();
40 my $xslt = XML::LibXSLT->new();
42 my $xpc = XML::LibXML::XPathContext->new();
43 $xpc->registerNs($_, $namespace_map{$_}{ns}) for ( keys %namespace_map );
49 my $conf = OpenSRF::Utils::SettingsClient->new;
50 my $idl_file = $conf->config_value( 'IDL' );
52 $idl = $parser->parse_file( $idl_file );
54 $log->debug( 'IDL XML file loaded' );
56 $cache_timeout = $conf->config_value(
57 "apps", "open-ils.fielder", "app_settings", "cache_timeout" ) || 300;
63 $cache = OpenSRF::Utils::Cache->new('global');
71 my $query = $obj->{query};
72 my $nocache = $obj->{cache} ? 0 : 1;
73 my $fields = $obj->{fields};
74 my $distinct = $obj->{distinct} ? 1 : 0;
76 return undef unless $query;
78 my $obj_class = $self->{class_hint};
79 my $fm_class = $self->{class_name};
82 $fields = [ $fm_class->real_fields ];
85 $fields = [$fields] if (!ref($fields));
87 my $qstring = OpenSRF::Utils::JSON->perl2JSON( $query );
88 my $fstring = OpenSRF::Utils::JSON->perl2JSON( [ sort { $a cmp $b } @$fields ] );
90 $log->debug( 'Query Class: '. $obj_class );
91 $log->debug( 'Field list: '. $fstring );
92 $log->debug( 'Query: '. $qstring );
96 $key = 'open-ils.fielder_' . md5_hex(
104 $res = $cache->get_cache( $key );
107 $client->respond($_) for (@$res);
112 $res = new_editor()->json_query({
113 select => { $obj_class => $fields },
119 for my $value (@$res) {
120 $client->respond($value);
123 $client->respond_complete();
125 $cache->put_cache( $key => $res => $cache_timeout ) unless ($nocache);
129 sub generate_methods {
131 for my $class_node ( $xpc->findnodes( '//idl:class[@oils_persist:field_safe="true"]', $idl->documentElement ) ) {
132 my $hint = $class_node->getAttribute('id');
133 my $fm = $class_node->getAttributeNS('http://open-ils.org/spec/opensrf/IDL/objects/v1','fieldmapper');
134 $log->debug("Fielder class_node $hint");
136 __PACKAGE__->register_method(
137 method => 'fielder_fetch',
138 api_name => 'open-ils.fielder.' . $hint,
140 class_name => "Fieldmapper::$fm",
147 $log->error("error generating Fielder methods: $e");