somehow forgot to add the billing_type field
[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         eval 'use OpenILS::Application::WoRM;';
49         if ($@) {
50                 $log->debug("FAILURE LOADING WORM!  $@", ERROR);
51                 throw OpenILS::EX::PANIC ( "FAILURE LOADING WoRM!  :  $@" );
52         }
53
54         $log->debug("We seem to be OK...",DEBUG);
55 }
56
57 sub child_init {
58
59         $log->debug('Running child_init for ' . __PACKAGE__ . '...', DEBUG);
60
61         my $conf = OpenSRF::Utils::SettingsClient->new;
62
63         $log->debug('Calling the Driver child_init', DEBUG);
64         OpenILS::Application::Storage::CDBI->child_init(
65                 $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'database')
66         );
67
68         if (OpenILS::Application::Storage::CDBI->db_Main()) {
69                 #OpenILS::Application::Storage::WORM->child_init();
70                 OpenILS::Application::WoRM->child_init();
71                 $log->debug("Success initializing driver!", DEBUG);
72                 return 1;
73         }
74         $log->debug("FAILURE initializing driver!", ERROR);
75         return 0;
76 }
77
78 sub begin_xaction {
79         my $self = shift;
80         my $client = shift;
81
82         $log->debug(" XACT --> 'BEGIN'ing transaction for session ".$client->session->session_id,DEBUG);
83         try {
84                 OpenILS::Application::Storage::CDBI->db_Main->begin_work;
85                 $client->session->session_data( xact_id => $client->session->session_id );
86         } catch Error with {
87                 throw OpenSRF::DomainObject::oilsException->new(
88                         statusCode => 500,
89                         status => "Could not BEGIN transaction!",
90                 );
91         };
92         return 1;
93
94 }
95 __PACKAGE__->register_method(
96         method          => 'begin_xaction',
97         api_name        => 'open-ils.storage.transaction.begin',
98         api_level       => 1,
99         argc            => 0,
100 );
101
102 sub savepoint_placeholder {
103         return 1;
104 }
105 __PACKAGE__->register_method(
106         method          => 'savepoint_placeholder',
107         api_name        => 'open-ils.storage.savepoint.set',
108         api_level       => 1,
109         argc            => 1,
110 );
111 __PACKAGE__->register_method(
112         method          => 'savepoint_placeholder',
113         api_name        => 'open-ils.storage.savepoint.release',
114         api_level       => 1,
115         argc            => 1,
116 );
117 __PACKAGE__->register_method(
118         method          => 'savepoint_placeholder',
119         api_name        => 'open-ils.storage.savepoint.rollback',
120         api_level       => 1,
121         argc            => 1,
122 );
123
124 sub commit_xaction {
125         my $self = shift;
126         my $client = shift;
127
128         try {
129                 OpenILS::Application::Storage::CDBI->db_Main->commit;
130                 $client->session->session_data( xact_id => '' );
131         } catch Error with {
132                 throw OpenSRF::DomainObject::oilsException->new(
133                         statusCode => 500,
134                         status => "Could not COMMIT  transaction!",
135                 );
136         };
137         return 1;
138 }
139 __PACKAGE__->register_method(
140         method          => 'commit_xaction',
141         api_name        => 'open-ils.storage.transaction.commit',
142         api_level       => 1,
143         argc            => 0,
144 );
145
146
147 sub current_xact {
148         my $self = shift;
149         my $client = shift;
150
151         return $client->session->session_data( 'xact_id' );
152 }
153 __PACKAGE__->register_method(
154         method          => 'current_xact',
155         api_name        => 'open-ils.storage.transaction.current',
156         api_level       => 1,
157         argc            => 0,
158 );
159
160 sub rollback_xaction {
161         my $self = shift;
162         my $client = shift;
163
164         $log->debug(" XACT --> 'ROLLBACK'ing transaction for session ".$client->session->session_id,DEBUG);
165         $client->session->session_data( xact_id => '' );
166         return OpenILS::Application::Storage::CDBI->db_Main->rollback;
167 }
168 __PACKAGE__->register_method(
169         method          => 'rollback_xaction',
170         api_name        => 'open-ils.storage.transaction.rollback',
171         api_level       => 1,
172         argc            => 0,
173 );
174
175
176 sub _cdbi2Hash {
177         my $self = shift;
178         my $obj = shift;
179         return { map { ( $_ => $obj->$_ ) } ($obj->columns('All')) };
180 }
181
182 sub _cdbi_list2AoH {
183         my $self = shift;
184         my @objs = @_;
185         return [ map { $self->_cdbi2Hash($_) } @objs ];
186 }
187
188 1;