detab
[dbsrgits/DBIx-Class-Journal.git] / lib / DBIx / Class / Schema / Journal.pm
1 package DBIx::Class::Schema::Journal;
2
3 use base qw/DBIx::Class/;
4
5 use Scalar::Util 'blessed';
6 use DBIx::Class::Schema::Journal::DB;
7
8 __PACKAGE__->mk_classdata('journal_storage_type');
9 __PACKAGE__->mk_classdata('journal_connection');
10 __PACKAGE__->mk_classdata('journal_deploy_on_connect');
11 __PACKAGE__->mk_classdata('journal_sources'); ## [ source names ]
12 __PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ]
13 __PACKAGE__->mk_classdata('_journal_schema'); ## schema object for journal
14 __PACKAGE__->mk_classdata('_journal_internal_sources'); # the sources used to journal journal_sources
15 __PACKAGE__->mk_classdata('journal_nested_changesets');
16
17 our $VERSION = '0.01';
18
19 use strict;
20 use warnings;
21
22 # sub throw_exception
23 # {
24 # }
25
26 # sub exception_action
27 # {
28 #     my $self = shift;
29 # #    print STDERR Carp::longmess;
30     
31 #     $self->next::method(@_);
32 # }
33
34 # sub load_classes
35 # {
36 #     my $class = shift;
37
38
39 #     $class->next::method(@_);
40     
41 # }
42
43 sub connection
44 {
45     my $self = shift;
46     my $schema = $self->next::method(@_);
47
48 #   print STDERR join(":", $self->sources), "\n";
49
50     my $journal_schema;
51     if(!defined($self->journal_connection))
52     {
53         ## If no connection, use the same schema/storage etc as the user
54         DBIx::Class::Componentised->inject_base(ref $self, 'DBIx::Class::Schema::Journal::DB');
55           $journal_schema = $self;
56     }
57     else
58     {
59         $journal_schema = DBIx::Class::Schema::Journal::DB->connect(@{ $self->journal_connection });
60         if($self->journal_storage_type)
61         {
62             $journal_schema->storage_type($self->journal_storage_type);
63         }
64     }
65
66     ## get our own private version of the journaling sources
67     $self->_journal_schema($journal_schema->compose_namespace(blessed($self) . '::Journal'));
68
69     ## Create auditlog+history per table
70     my %j_sources = map { $_ => 1 } $self->journal_sources
71                                       ? @{$self->journal_sources}
72                                       : $self->sources;
73
74     my @journal_sources = $journal_schema->sources; # not sources to journal, but sources used by the journal internally
75
76     foreach my $s_name ($self->sources)
77     {
78         next unless($j_sources{$s_name});
79         push @journal_sources, $self->create_journal_for($s_name);
80         $self->class($s_name)->load_components('Journal');
81 #        print STDERR "$s_name :", $self->class($s_name), "\n";
82     }
83
84     $self->_journal_internal_sources(\@journal_sources);
85
86     if ( $self->journal_nested_changesets ) {
87         $self->_journal_schema->nested_changesets(1);
88         die "FIXME nested changeset schema not yet supported... add parent_id to ChangeSet here";
89     }
90
91     $self->journal_schema_deploy()
92         if $self->journal_deploy_on_connect;
93
94     ## Set up relationship between changeset->user_id and this schema's user
95     if(!@{$self->journal_user || []})
96     {
97         #warn "No Journal User set!"; # no need to warn, user_id is useful even without a rel
98         return $schema;
99     }
100
101     $self->_journal_schema->class('ChangeSet')->belongs_to('user', @{$self->journal_user});
102     $self->_journal_schema->storage->disconnect();
103
104     return $schema;
105 }
106
107 sub deploy
108 {
109     my ( $self, $sqlt_args, @args ) = @_;
110
111     $self->next::method($sqlt_args, @args);
112
113     $sqlt_args ||= {};
114     local $sqlt_args->{sources} = $self->_journal_internal_sources;
115     $self->journal_schema_deploy($sqlt_args, @args);
116 }
117
118 sub journal_schema_deploy
119 {
120     my ( $self, $sqlt_args, @args ) = @_;
121
122     $sqlt_args ||= {};
123     $sqlt_args->{sources} = $self->_journal_internal_sources
124         unless exists $sqlt_args->{sources};
125
126     $self->_journal_schema->deploy( $sqlt_args, @args );
127 }
128
129 sub get_audit_log_class_name
130 {
131     my ($self, $sourcename) = @_;
132
133     return blessed($self->_journal_schema) . "::${sourcename}AuditLog";
134 }
135
136 sub get_audit_history_class_name
137 {
138     my ($self, $sourcename) = @_;
139
140     return blessed($self->_journal_schema) . "::${sourcename}AuditHistory";
141 }
142
143 sub create_journal_for
144 {
145     my ($self, $s_name) = @_;
146
147     my $source = $self->source($s_name);
148     my $newclass = $self->get_audit_log_class_name($s_name);
149     DBIx::Class::Componentised->inject_base($newclass, 'DBIx::Class::Schema::Journal::DB::AuditLog');
150     $newclass->table(lc($s_name) . "_audit_log");
151     my $log_source = "${s_name}AuditLog";
152     $self->_journal_schema->register_class($log_source, $newclass);
153                            
154
155     my $histclass = $self->get_audit_history_class_name($s_name);
156     DBIx::Class::Componentised->inject_base($histclass, 'DBIx::Class::Schema::Journal::DB::AuditHistory');
157     $histclass->table(lc($s_name) . "_audit_history");
158 #    $histclass->result_source_instance->name(lc($s_name) . "_audit_hisory");
159     $histclass->add_columns(
160                             map { $_ => $source->column_info($_) } $source->columns
161                            );
162                            
163     my $hist_source = "${s_name}AuditHistory";
164     $self->_journal_schema->register_class($hist_source, $histclass);
165
166     return ( $log_source, $hist_source );
167 }
168
169 sub txn_do
170 {
171     my ($self, $user_code, @args) = @_;
172
173     my $jschema = $self->_journal_schema;
174
175     my $code = $user_code;
176
177     my $current_changeset = $jschema->current_changeset;
178     if ( !$current_changeset || $self->journal_nested_changesets )
179     {
180         my $current_changeset_ref = $jschema->_current_changeset_container;
181
182         unless ( $current_changeset_ref ) {
183             # this is a hash because scalar refs can't be localized
184             $current_changeset_ref = { };
185             $jschema->_current_changeset_container($current_changeset_ref);
186         }
187
188         # wrap the thunk with a new changeset creation
189         $code = sub {
190             my $changeset = $jschema->journal_create_changeset( parent_id => $current_changeset );
191             local $current_changeset_ref->{changeset} = $changeset->id;
192             $user_code->(@_);
193         };
194
195     }
196
197     if ( $jschema->storage != $self->storage ) {
198         my $inner_code = $code;
199         $code = sub { $jschema->txn_do($inner_code, @_) };
200     }
201
202     return $self->next::method($code, @args);
203 }
204
205 sub changeset_user
206 {
207     my ($self, $userid) = @_;
208
209     return $self->_journal_schema->current_user() if(@_ == 1);
210
211     $self->_journal_schema->current_user($userid);
212 }
213
214 sub changeset_session
215 {
216     my ($self, $sessionid) = @_;
217
218     return $self->_journal_schema->current_session() if(@_ == 1);
219
220     $self->_journal_schema->current_session($sessionid);
221 }
222
223
224 1;