]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Storage.pm
d3ad360d4f39e49639e5988f7aad9aa309e70213
[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         }
46         eval 'use OpenILS::Application::Storage::WORM;';
47         if ($@) {
48                 $log->debug("FAILURE LOADING WORM!  $@", ERROR);
49         }
50 }
51
52 sub child_init {
53
54         $log->debug('Running child_init for ' . __PACKAGE__ . '...', DEBUG);
55
56         my $conf = OpenSRF::Utils::SettingsClient->new;
57
58         $log->debug('Calling the Driver child_init', DEBUG);
59         OpenILS::Application::Storage::CDBI->child_init(
60                 $conf->config_value( apps => 'open-ils.storage' => app_settings => databases => 'database')
61         );
62
63         if (OpenILS::Application::Storage::CDBI->db_Main()) {
64                 OpenILS::Application::Storage::WORM->child_init();
65                 $log->debug("Success initializing driver!", DEBUG);
66                 return 1;
67         }
68         $log->debug("FAILURE initializing driver!", ERROR);
69         return 0;
70 }
71
72 sub begin_xaction {
73         my $self = shift;
74         my $client = shift;
75
76         $log->debug(" XACT --> 'BEGIN'ing transaction for session ".$client->session->session_id,DEBUG);
77         try {
78                 OpenILS::Application::Storage::CDBI->db_Main->begin_work;
79                 $client->session->session_data( xact_id => $client->session->session_id );
80         } catch Error with {
81                 throw OpenSRF::DomainObject::oilsException->new(
82                         statusCode => 500,
83                         status => "Could not BEGIN transaction!",
84                 );
85         };
86         return 1;
87
88 }
89 __PACKAGE__->register_method(
90         method          => 'begin_xaction',
91         api_name        => 'open-ils.storage.transaction.begin',
92         api_level       => 1,
93         argc            => 0,
94 );
95
96 sub commit_xaction {
97         my $self = shift;
98         my $client = shift;
99
100         try {
101                 OpenILS::Application::Storage::CDBI->db_Main->commit;
102                 $client->session->session_data( xact_id => '' );
103         } catch Error with {
104                 throw OpenSRF::DomainObject::oilsException->new(
105                         statusCode => 500,
106                         status => "Could not COMMIT  transaction!",
107                 );
108         };
109         return 1;
110 }
111 __PACKAGE__->register_method(
112         method          => 'commit_xaction',
113         api_name        => 'open-ils.storage.transaction.commit',
114         api_level       => 1,
115         argc            => 0,
116 );
117
118
119 sub current_xact {
120         my $self = shift;
121         my $client = shift;
122
123         return $client->session->session_data( 'xact_id' );
124 }
125 __PACKAGE__->register_method(
126         method          => 'current_xact',
127         api_name        => 'open-ils.storage.transaction.current',
128         api_level       => 1,
129         argc            => 0,
130 );
131
132 sub rollback_xaction {
133         my $self = shift;
134         my $client = shift;
135
136         $log->debug(" XACT --> 'ROLLBACK'ing transaction for session ".$client->session->session_id,DEBUG);
137         $client->session->session_data( xact_id => '' );
138         return OpenILS::Application::Storage::CDBI->db_Main->rollback;
139 }
140 __PACKAGE__->register_method(
141         method          => 'rollback_xaction',
142         api_name        => 'open-ils.storage.transaction.rollback',
143         api_level       => 1,
144         argc            => 0,
145 );
146
147
148 sub _cdbi2Hash {
149         my $self = shift;
150         my $obj = shift;
151         return { map { ( $_ => $obj->$_ ) } ($obj->columns('All')) };
152 }
153
154 sub _cdbi_list2AoH {
155         my $self = shift;
156         my @objs = @_;
157         return [ map { $self->_cdbi2Hash($_) } @objs ];
158 }
159
160 1;