add journal_components to allow for setting components on journal schema
[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 use Class::C3::Componentised ();
8
9 __PACKAGE__->mk_classdata('journal_storage_type');
10 __PACKAGE__->mk_classdata('journal_connection');
11 __PACKAGE__->mk_classdata('journal_deploy_on_connect');
12 __PACKAGE__->mk_classdata('journal_sources'); ## [ source names ]
13 __PACKAGE__->mk_classdata('journal_user'); ## [ class, field for user id ]
14 __PACKAGE__->mk_classdata('journal_copy_sources');
15 __PACKAGE__->mk_classdata('__journal_schema_prototype');
16 __PACKAGE__->mk_classdata('_journal_schema'); ## schema object for journal
17 __PACKAGE__->mk_classdata('journal_component');
18 __PACKAGE__->mk_classdata('journal_components');
19 __PACKAGE__->mk_classdata('journal_nested_changesets');
20
21 use strict;
22 use warnings;
23
24
25 sub _journal_schema_prototype {
26     my $self = shift;
27     if (my $proto = $self->__journal_schema_prototype) {
28           return $proto;
29     }
30     my $c = blessed($self)||$self;
31     my $journal_schema_class = "${c}::_JOURNAL";
32     Class::C3::Componentised->inject_base($journal_schema_class, 'DBIx::Class::Schema::Journal::DB');
33     $journal_schema_class->load_components($self->journal_components)
34         if $self->journal_components;
35     my $proto = $self->__journal_schema_prototype (
36         $journal_schema_class->compose_namespace( $c.'::Journal')
37     );
38
39
40     my $comp = $self->journal_component || "Journal";
41
42     ## Create auditlog+history per table
43     my %j_sources = map { $_ => 1 } $self->journal_sources
44        ? @{$self->journal_sources}
45        : $self->sources;
46
47     foreach my $s_name ($self->sources) {
48         next unless($j_sources{$s_name});
49         $self->create_journal_for($s_name => $proto);
50         $self->class($s_name)->load_components($comp);
51     }
52     return $proto;
53 }
54
55 sub connection {
56     my $self = shift;
57     my $schema = $self->next::method(@_);
58
59     my $journal_schema = (ref $self||$self)->_journal_schema_prototype->clone;
60
61     if($self->journal_connection) {
62         $journal_schema->storage_type($self->journal_storage_type)
63             if $self->journal_storage_type;
64         $journal_schema->connection(@{ $self->journal_connection });
65     } else {
66         $journal_schema->storage( $schema->storage );
67     }
68
69     $self->_journal_schema($journal_schema);
70
71
72     if ( $self->journal_nested_changesets ) {
73         $self->_journal_schema->nested_changesets(1);
74         die 'FIXME nested changeset schema not yet supported... add parent_id to ChangeSet here';
75     }
76
77     $self->journal_schema_deploy()
78         if $self->journal_deploy_on_connect;
79
80     ## Set up relationship between changeset->user_id and this schema's user
81     if(!@{$self->journal_user || []}) {
82         #warn "No Journal User set!"; # no need to warn, user_id is useful even without a rel
83         return $schema;
84     }
85
86     $self->_journal_schema->class('ChangeSet')->belongs_to('user', @{$self->journal_user});
87     $self->_journal_schema->storage->disconnect();
88
89     return $schema;
90 }
91
92 sub deploy {
93     my $self = shift;
94
95     $self->next::method(@_);
96
97     $self->journal_schema_deploy(@_);
98 }
99
100 sub journal_schema_deploy {
101     my $self = shift;
102
103     $self->_journal_schema->deploy(@_);
104 }
105
106 sub create_journal_for {
107     my ($self, $s_name, $journal_schema) = @_;
108
109     my $source = $self->source($s_name);
110
111     foreach my $audit (qw(AuditLog AuditHistory)) {
112         my $audit_source = $s_name.$audit;
113         my $class = blessed($journal_schema) . "::$audit_source";
114
115         Class::C3::Componentised->inject_base($class, "DBIx::Class::Schema::Journal::DB::$audit");
116
117         $class->journal_define_table($source);
118
119         $journal_schema->register_class($audit_source, $class);
120
121         $self->register_class($audit_source, $class)
122             if $self->journal_copy_sources;
123     }
124 }
125
126 sub txn_do {
127     my ($self, $user_code, @args) = @_;
128
129     my $jschema = $self->_journal_schema;
130
131     my $code = $user_code;
132
133     my $current_changeset = $jschema->_current_changeset;
134     if ( !$current_changeset || $self->journal_nested_changesets ) {
135         my $current_changeset_ref = $jschema->_current_changeset_container;
136
137         unless ( $current_changeset_ref ) {
138             # this is a hash because scalar refs can't be localized
139             $current_changeset_ref = { };
140             $jschema->_current_changeset_container($current_changeset_ref);
141         }
142
143         # wrap the thunk with a new changeset creation
144         $code = sub {
145             my $changeset = $jschema->journal_create_changeset( parent_id => $current_changeset );
146             local $current_changeset_ref->{changeset} = $changeset->id;
147             $user_code->(@_);
148         };
149
150     }
151
152     if ( $jschema->storage != $self->storage ) {
153         my $inner_code = $code;
154         $code = sub { $jschema->txn_do($inner_code, @_) };
155     }
156
157     return $self->next::method($code, @args);
158 }
159
160 sub changeset_user {
161     my ($self, $userid) = @_;
162
163     return $self->_journal_schema->current_user()
164        if @_ == 1;
165
166     $self->_journal_schema->current_user($userid);
167 }
168
169 sub changeset_session {
170     my ($self, $sessionid) = @_;
171
172     return $self->_journal_schema->current_session()
173        if @_ == 1;
174
175     $self->_journal_schema->current_session($sessionid);
176 }
177
178
179 1;