source: LATMOS-Accounts/lib/LATMOS/Accounts/Bases.pm @ 27

Last change on this file since 27 was 27, checked in by nanardon, 15 years ago
  • hide false new functions which load any object
  • Property svn:keywords set to Id Rev
File size: 4.1 KB
Line 
1package LATMOS::Accounts::Bases;
2
3use 5.010000;
4use strict;
5use warnings;
6use LATMOS::Accounts::Bases::Objects;
7
8our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];
9
10=head1 NAME
11
12LATMOS::Accounts::Bases - Base class for account data bases
13
14=head1 SYNOPSIS
15
16  use LATMOS::Accounts::Bases;
17  my $base = LATMOS::Accounts::Bases->new('type', %options);
18  ...
19
20=head1 DESCRIPTION
21
22This module provide basic functions for various account base
23
24=head1 FUNTIONS
25
26=cut
27
28=head2 new($type, %options)
29
30Return, if success, a new data base account object, $type is
31account base type, %options to setup the base.
32
33=cut
34
35sub new {
36    my ($class, $type, %options) = @_;
37
38    my $pclass = ucfirst(lc($type));
39    eval "require LATMOS::Accounts::Bases::$pclass;";
40    if ($@) { return } # error message ?
41    return "LATMOS::Accounts::Bases::$pclass"->new(%options);
42}
43
44sub _load_obj_class {
45    my ($self, $otype) = @_;
46
47    # finding perl class:
48    my $pclass = ref $self;
49    $pclass .= '::' . ucfirst(lc($otype));
50    eval "require $pclass;";
51    if ($@) { return } # error message ?
52    return $pclass;
53}
54
55=head2 list_canonicals_fields($otype)
56
57Return the list of supported fields by the database for object type $otype.
58
59=cut
60
61sub list_canonicals_fields {
62    my ($self, $otype) = @_;
63    my $pclass = $self->_load_obj_class($otype) or return;
64    $pclass->_canonical_fields;
65}
66
67=head2 get_field_name($otype, $c_fields)
68
69Return the internal fields name for $otype object for
70canonical fields $c_fields
71
72=cut
73
74sub get_field_name {
75    my ($self, $otype, $c_fields) = @_;
76    my $pclass = $self->_load_obj_class($otype) or return;
77    $pclass->_get_field_name($c_fields);
78}
79
80=head2 get_object($type, $id)
81
82Return an object of $type (typically user or group) having identifier
83$id.
84
85=cut
86
87sub get_object {
88    my ($self, $otype, $id) = @_;
89
90    return LATMOS::Accounts::Bases::Objects->_new($self, $otype, $id);
91}
92
93=head2 create_object($type, $id, %data)
94
95Create and return an object of type $type with unique id
96$id having %data.
97
98This method should be provided by the data base handler.
99
100=cut
101
102sub create_object {
103    my ($self, $otype, $id, %data) = @_;
104    my $pclass = $self->_load_obj_class($otype);
105    $pclass->create($id, %data) or return;
106    $self->get_object($otype, $id);
107}
108
109=head2 create_c_object($type, $id, %data)
110
111Create and return an object of type $type with unique id
112$id having %data using canonical fields
113
114=cut
115
116sub create_c_object {
117    my ($self, $otype, $id, %cdata) = @_;
118
119    my %data;
120    foreach my $cfield (keys %cdata) {
121        my $field = $self->base->get_field_name($self->type, $cfield) or next;
122        $data{$field} = $cdata{$cfield};
123    }
124    keys %data or return 1; # TODO: return an error ?
125    $self->create_object($otype, $id, %data);
126}
127
128=head2 load
129
130Make account base loading data into memory if need.
131Should always be called, if database fetch data on the fly
132(SQL, LDAP), the function just return True.
133
134=cut
135
136sub load { 1 }
137
138=head2 is_transactionnal
139
140Return True is the database support commit and rollback
141
142=cut
143
144sub is_transactionnal {
145    my ($self) = @_;
146    return($self->can('_rollback') && $self->can('_commit'));
147}
148
149=head2 commit
150
151Save change into the database if change are not done immediately.
152This should always be called as you don't know when change are applied.
153
154Return always true if database does not support any transaction.
155
156The driver should provides a _commit functions to save data.
157
158=cut
159
160sub commit {
161    my ($self) = @_;
162    return $self->can('_commit') ? $self->_commit : 1;
163}
164
165=head2 rollback
166
167If database support transaction, rollback changes. Return false
168if database does not support.
169
170If supported, driver should provides a _rollback functions
171
172=cut
173
174sub rollback {
175    my ($self) = @_;
176    return $self->can('_rollback') ? $self->_rollback : 0;
177}
178
1791;
180
181__END__
182
183=head1 SEE ALSO
184
185=head1 AUTHOR
186
187Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt>
188
189=head1 COPYRIGHT AND LICENSE
190
191Copyright (C) 2009 by Thauvin Olivier
192
193This library is free software; you can redistribute it and/or modify
194it under the same terms as Perl itself, either Perl version 5.10.0 or,
195at your option, any later version of Perl 5 you may have available.
196
197=cut
Note: See TracBrowser for help on using the repository browser.