]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/lib/OpenILS/Application/Storage.pm
LP1615805 No inputs after submit in patron search (AngularJS)
[working/Evergreen.git] / Open-ILS / src / perlmods / lib / OpenILS / Application / Storage.pm
1 package OpenILS::Application::Storage;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
4
5 use OpenSRF::EX qw/:try/;
6 use OpenSRF::Utils::Logger qw/:level/;
7
8 # Pull this in so we can adjust it's @ISA
9 use OpenILS::Application::Storage::CDBI (1);
10 use OpenILS::Application::Storage::FTS;
11
12
13 # the easy way to get to the logger...
14 my $log = "OpenSRF::Utils::Logger";
15
16 our $QParser;
17 our $WRITE = 0;
18 our $IGNORE_XACT_ID_FAILURE = 0;
19
20 sub DESTROY {};
21
22 sub initialize {
23
24     my $conf = OpenSRF::Utils::SettingsClient->new;
25
26     $log->debug('Initializing ' . __PACKAGE__ . '...', DEBUG);
27
28     my $db_driver = $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'driver');
29     my $driver = "OpenILS::Application::Storage::Driver::$db_driver";
30
31     $log->debug("Attempting to load $driver ...", DEBUG);
32
33     $driver->use;
34     if ($@) {
35         $log->debug( "Can't load $driver!  :  $@", ERROR );
36         $log->error( "Can't load $driver!  :  $@");
37         throw OpenSRF::EX::PANIC ( "Can't load $driver!  :  $@" );
38     }
39
40     $log->debug("$driver loaded successfully", DEBUG);
41
42     # Suck in the method publishing modules
43     @OpenILS::Application::Storage::CDBI::ISA = ( $driver );
44
45     OpenILS::Application::Storage::Publisher->use;
46     if ($@) {
47         $log->debug("FAILURE LOADING Publisher!  $@", ERROR);
48         throw OpenSRF::EX::PANIC ( "FAILURE LOADING Publisher!  :  $@" );
49     }
50
51     $log->debug("We seem to be OK...",DEBUG);
52 }
53
54 sub register_method {
55     my $class = shift;
56     my %args = @_;
57
58     $args{package} ||= ref($class) || $class;
59
60     unless ($args{no_tz_force}) {
61         my %dup_args = %args;
62         $dup_args{api_name} = 'no_tz.' . $args{api_name};
63
64         $args{method} = 'force_db_tz';
65         delete $args{package};
66
67         __PACKAGE__->SUPER::register_method( %dup_args );
68
69     }
70
71     __PACKAGE__->SUPER::register_method( %args );
72
73 }
74
75 sub force_db_tz {
76     my $self = shift;
77     my $client = shift;
78     my @args = @_;
79
80     my ($current_xact) = $self->method_lookup('no_tz.open-ils.storage.transaction.current')->run;
81
82     if (!$current_xact && $ENV{TZ}) {
83         try {
84             OpenILS::Application::Storage::CDBI->db_Main->do(
85                 'SET timezone TO ?;',
86                 {},
87                 $ENV{TZ}
88             );
89         } catch Error with {
90             $log->error( "Could not set timezone: $ENV{TZ}");
91         };
92     }
93
94     my $method = $self->method_lookup('no_tz.' . $self->{api_name});
95     die unless $method;
96
97     $client->respond( $_ ) for ( $method->run(@args) );
98
99     if (!$current_xact && $ENV{TZ}) {
100         try {
101             OpenILS::Application::Storage::CDBI->db_Main->do(
102                 'SET timezone TO DEFAULT;',
103             );
104         } catch Error with {
105             $log->error( "Could not reset default timezone");
106         };
107     }
108
109     return undef;
110 }
111
112 sub child_init {
113
114     $log->debug('Running child_init for ' . __PACKAGE__ . '...', DEBUG);
115
116     my $conf = OpenSRF::Utils::SettingsClient->new;
117
118     $log->debug('Calling the Driver child_init', DEBUG);
119     OpenILS::Application::Storage::CDBI->child_init(
120         $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'database')
121     );
122
123     if (OpenILS::Application::Storage::CDBI->db_Main()) {
124         $log->debug("Success initializing driver!", DEBUG);
125
126         my $db_driver = $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'driver');
127         $QParser = 'OpenILS::Application::Storage::Driver::'.$db_driver.'::QueryParser';
128         $QParser->use;
129
130         if($@) {
131             $log->debug( "Can't load $QParser!  :  $@", ERROR );
132             $log->error( "Can't load $QParser!  :  $@");
133         } else {
134             return 1;
135         }
136     }
137
138     $log->debug("FAILURE initializing driver!", ERROR);
139     return 0;
140 }
141
142 sub begin_xaction {
143     my $self = shift;
144     my $client = shift;
145
146     local $WRITE = 1;
147
148     $log->debug(" XACT --> 'BEGIN'ing transaction for session ".$client->session->session_id,DEBUG);
149     try {
150         OpenILS::Application::Storage::CDBI->db_Main->begin_work;
151         $client->session->session_data( xact_id => $client->session->session_id );
152     } catch Error with {
153         throw OpenSRF::DomainObject::oilsException->new(
154             statusCode => 500,
155             status => "Could not BEGIN transaction!",
156         );
157     };
158     return 1;
159
160 }
161 __PACKAGE__->register_method(
162     method      => 'begin_xaction',
163     api_name    => 'open-ils.storage.transaction.begin',
164     api_level   => 1,
165     argc        => 0,
166 );
167
168 sub savepoint_placeholder {
169     return 1;
170 }
171 __PACKAGE__->register_method(
172     method      => 'savepoint_placeholder',
173     api_name    => 'open-ils.storage.savepoint.set',
174     api_level   => 1,
175     argc        => 1,
176 );
177 __PACKAGE__->register_method(
178     method      => 'savepoint_placeholder',
179     api_name    => 'open-ils.storage.savepoint.release',
180     api_level   => 1,
181     argc        => 1,
182 );
183 __PACKAGE__->register_method(
184     method      => 'savepoint_placeholder',
185     api_name    => 'open-ils.storage.savepoint.rollback',
186     api_level   => 1,
187     argc        => 1,
188 );
189
190 sub commit_xaction {
191     my $self = shift;
192     my $client = shift;
193
194     local $WRITE = 1;
195
196     try {
197         OpenILS::Application::Storage::CDBI->db_Main->commit;
198         $client->session->session_data( xact_id => '' );
199     } catch Error with {
200         throw OpenSRF::DomainObject::oilsException->new(
201             statusCode => 500,
202             status => "Could not COMMIT  transaction!",
203         );
204     };
205     return 1;
206 }
207 __PACKAGE__->register_method(
208     method      => 'commit_xaction',
209     api_name    => 'open-ils.storage.transaction.commit',
210     api_level   => 1,
211     argc        => 0,
212 );
213
214
215 sub current_xact {
216     my $self = shift;
217     my $client = shift;
218
219     return $client->session->session_data( 'xact_id' );
220 }
221 __PACKAGE__->register_method(
222     method      => 'current_xact',
223     api_name    => 'open-ils.storage.transaction.current',
224     api_level   => 1,
225     argc        => 0,
226 );
227
228 sub rollback_xaction {
229     my $self = shift;
230     my $client = shift;
231
232     local $WRITE = 1;
233
234     $log->debug(" XACT --> 'ROLLBACK'ing transaction for session ".$client->session->session_id,DEBUG);
235     $client->session->session_data( xact_id => '' );
236     return OpenILS::Application::Storage::CDBI->db_Main->rollback;
237 }
238 __PACKAGE__->register_method(
239     method      => 'rollback_xaction',
240     api_name    => 'open-ils.storage.transaction.rollback',
241     api_level   => 1,
242     argc        => 0,
243 );
244
245
246 sub _cdbi2Hash {
247     my $self = shift;
248     my $obj = shift;
249     return { map { ( $_ => $obj->$_ ) } ($obj->columns('All')) };
250 }
251
252 sub _cdbi_list2AoH {
253     my $self = shift;
254     my @objs = @_;
255     return [ map { $self->_cdbi2Hash($_) } @objs ];
256 }
257
258 1;