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