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';
40 my $parser = XML::LibXML->new();
41 my $xslt = XML::LibXSLT->new();
43 my $xpc = XML::LibXML::XPathContext->new();
44 $xpc->registerNs($_, $namespace_map{$_}{ns}) for ( keys %namespace_map );
50 my $conf = OpenSRF::Utils::SettingsClient->new;
51 my $idl_file = $conf->config_value( 'IDL' );
53 $idl = $parser->parse_file( $idl_file );
55 $log->debug( 'IDL XML file loaded' );
57 $cache_timeout = $conf->config_value(
58 "apps", "open-ils.fielder", "app_settings", "cache_timeout" ) || 300;
60 $default_locale = $conf->config_value("default", "default_locale") || 'en-US';
66 $cache = OpenSRF::Utils::Cache->new('global');
74 my $locale = $self->session->session_locale || $default_locale;
75 my $query = $obj->{query};
76 my $nocache = $obj->{cache} ? 0 : 1;
77 my $fields = $obj->{fields};
78 my $distinct = $obj->{distinct} ? 1 : 0;
80 return undef unless $query;
82 my $obj_class = $self->{class_hint};
83 my $fm_class = $self->{class_name};
86 $fields = [ $fm_class->real_fields ];
89 $fields = [$fields] if (!ref($fields));
91 my $qstring = OpenSRF::Utils::JSON->perl2JSON( $query );
92 my $fstring = OpenSRF::Utils::JSON->perl2JSON( [ sort { $a cmp $b } @$fields ] );
94 $log->debug( 'Query Class: '. $obj_class );
95 $log->debug( 'Field list: '. $fstring );
96 $log->debug( 'Query: '. $qstring );
100 $key = 'open-ils.fielder_' . md5_hex(
109 $res = $cache->get_cache( $key );
112 $client->respond($_) for (@$res);
117 $res = new_editor()->json_query({
118 select => { $obj_class => $fields },
124 for my $value (@$res) {
125 $client->respond($value);
128 $client->respond_complete();
130 $cache->put_cache( $key => $res => $cache_timeout ) unless ($nocache);
134 sub generate_methods {
136 for my $class_node ( $xpc->findnodes( '//idl:class[@oils_persist:field_safe="true"]', $idl->documentElement ) ) {
137 my $hint = $class_node->getAttribute('id');
138 my $fm = $class_node->getAttributeNS('http://open-ils.org/spec/opensrf/IDL/objects/v1','fieldmapper');
139 $log->debug("Fielder class_node $hint");
141 __PACKAGE__->register_method(
142 method => 'fielder_fetch',
143 api_name => 'open-ils.fielder.' . $hint,
145 class_name => "Fieldmapper::$fm",
152 $log->error("error generating Fielder methods: $e");