]> git.evergreen-ils.org Git - working/Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage.pm
added some sanity checking
[working/Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Storage.pm
1 package OpenILS::Application::Storage;
2 use OpenSRF::Application;
3 use base qw/OpenSRF::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 sub DESTROY {};
17
18 sub initialize {
19
20         my $conf = OpenSRF::Utils::SettingsClient->new;
21
22         $log->debug('Initializing ' . __PACKAGE__ . '...', DEBUG);
23
24         my $driver = "OpenILS::Application::Storage::Driver::".
25                 $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'driver');
26
27
28         $log->debug("Attempting to load $driver ...", DEBUG);
29
30         eval "use $driver;";
31         if ($@) {
32                 $log->debug( "Can't load $driver!  :  $@", ERROR );
33                 $log->error( "Can't load $driver!  :  $@");
34                 throw OpenILS::EX::PANIC ( "Can't load $driver!  :  $@" );
35         }
36
37         $log->debug("$driver loaded successfully", DEBUG);
38
39         # Suck in the method publishing modules
40         @OpenILS::Application::Storage::CDBI::ISA = ( $driver );
41
42         eval 'use OpenILS::Application::Storage::Publisher;';
43         if ($@) {
44                 $log->debug("FAILURE LOADING Publisher!  $@", ERROR);
45                 throw OpenILS::EX::PANIC ( "FAILURE LOADING Publisher!  :  $@" );
46         }
47         eval 'use OpenILS::Application::Storage::WORM;';
48         if ($@) {
49                 $log->debug("FAILURE LOADING WORM!  $@", ERROR);
50                 throw OpenILS::EX::PANIC ( "FAILURE LOADING WoRM!  :  $@" );
51         }
52
53         $log->debug("We seem to be OK...",DEBUG);
54 }
55
56 sub child_init {
57
58         $log->debug('Running child_init for ' . __PACKAGE__ . '...', DEBUG);
59
60         my $conf = OpenSRF::Utils::SettingsClient->new;
61
62         $log->debug('Calling the Driver child_init', DEBUG);
63         OpenILS::Application::Storage::CDBI->child_init(
64                 $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'database')
65         );
66
67         if (OpenILS::Application::Storage::CDBI->db_Main()) {
68                 OpenILS::Application::Storage::WORM->child_init();
69                 $log->debug("Success initializing driver!", DEBUG);
70                 return 1;
71         }
72         $log->debug("FAILURE initializing driver!", ERROR);
73         return 0;
74 }
75
76 sub begin_xaction {
77         my $self = shift;
78         my $client = shift;
79
80         $log->debug(" XACT --> 'BEGIN'ing transaction for session ".$client->session->session_id,DEBUG);
81         try {
82                 OpenILS::Application::Storage::CDBI->db_Main->begin_work;
83                 $client->session->session_data( xact_id => $client->session->session_id );
84         } catch Error with {
85                 throw OpenSRF::DomainObject::oilsException->new(
86                         statusCode => 500,
87                         status => "Could not BEGIN transaction!",
88                 );
89         };
90         return 1;
91
92 }
93 __PACKAGE__->register_method(
94         method          => 'begin_xaction',
95         api_name        => 'open-ils.storage.transaction.begin',
96         api_level       => 1,
97         argc            => 0,
98 );
99
100 sub commit_xaction {
101         my $self = shift;
102         my $client = shift;
103
104         try {
105                 OpenILS::Application::Storage::CDBI->db_Main->commit;
106                 $client->session->session_data( xact_id => '' );
107         } catch Error with {
108                 throw OpenSRF::DomainObject::oilsException->new(
109                         statusCode => 500,
110                         status => "Could not COMMIT  transaction!",
111                 );
112         };
113         return 1;
114 }
115 __PACKAGE__->register_method(
116         method          => 'commit_xaction',
117         api_name        => 'open-ils.storage.transaction.commit',
118         api_level       => 1,
119         argc            => 0,
120 );
121
122
123 sub current_xact {
124         my $self = shift;
125         my $client = shift;
126
127         return $client->session->session_data( 'xact_id' );
128 }
129 __PACKAGE__->register_method(
130         method          => 'current_xact',
131         api_name        => 'open-ils.storage.transaction.current',
132         api_level       => 1,
133         argc            => 0,
134 );
135
136 sub rollback_xaction {
137         my $self = shift;
138         my $client = shift;
139
140         $log->debug(" XACT --> 'ROLLBACK'ing transaction for session ".$client->session->session_id,DEBUG);
141         $client->session->session_data( xact_id => '' );
142         return OpenILS::Application::Storage::CDBI->db_Main->rollback;
143 }
144 __PACKAGE__->register_method(
145         method          => 'rollback_xaction',
146         api_name        => 'open-ils.storage.transaction.rollback',
147         api_level       => 1,
148         argc            => 0,
149 );
150
151
152 sub _cdbi2Hash {
153         my $self = shift;
154         my $obj = shift;
155         return { map { ( $_ => $obj->$_ ) } ($obj->columns('All')) };
156 }
157
158 sub _cdbi_list2AoH {
159         my $self = shift;
160         my @objs = @_;
161         return [ map { $self->_cdbi2Hash($_) } @objs ];
162 }
163
164 1;