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

Last change on this file since 28 was 28, checked in by nanardon, 15 years ago
  • add list_objects() functions
  • Property svn:keywords set to Id Rev
File size: 4.3 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 list_objects($otype)
81
82Return the list of UID for object of $otype.
83
84=cut
85
86sub list_objects {
87    my ($self, $otype) = @_;
88    my $pclass = $self->_load_obj_class($otype) or return;
89    $pclass->list($self);
90}
91
92=head2 get_object($type, $id)
93
94Return an object of $type (typically user or group) having identifier
95$id.
96
97=cut
98
99sub get_object {
100    my ($self, $otype, $id) = @_;
101
102    return LATMOS::Accounts::Bases::Objects->_new($self, $otype, $id);
103}
104
105=head2 create_object($type, $id, %data)
106
107Create and return an object of type $type with unique id
108$id having %data.
109
110This method should be provided by the data base handler.
111
112=cut
113
114sub create_object {
115    my ($self, $otype, $id, %data) = @_;
116    my $pclass = $self->_load_obj_class($otype);
117    $pclass->create($id, %data) or return;
118    $self->get_object($otype, $id);
119}
120
121=head2 create_c_object($type, $id, %data)
122
123Create and return an object of type $type with unique id
124$id having %data using canonical fields
125
126=cut
127
128sub create_c_object {
129    my ($self, $otype, $id, %cdata) = @_;
130
131    my %data;
132    foreach my $cfield (keys %cdata) {
133        my $field = $self->base->get_field_name($self->type, $cfield) or next;
134        $data{$field} = $cdata{$cfield};
135    }
136    keys %data or return 1; # TODO: return an error ?
137    $self->create_object($otype, $id, %data);
138}
139
140=head2 load
141
142Make account base loading data into memory if need.
143Should always be called, if database fetch data on the fly
144(SQL, LDAP), the function just return True.
145
146=cut
147
148sub load { 1 }
149
150=head2 is_transactionnal
151
152Return True is the database support commit and rollback
153
154=cut
155
156sub is_transactionnal {
157    my ($self) = @_;
158    return($self->can('_rollback') && $self->can('_commit'));
159}
160
161=head2 commit
162
163Save change into the database if change are not done immediately.
164This should always be called as you don't know when change are applied.
165
166Return always true if database does not support any transaction.
167
168The driver should provides a _commit functions to save data.
169
170=cut
171
172sub commit {
173    my ($self) = @_;
174    return $self->can('_commit') ? $self->_commit : 1;
175}
176
177=head2 rollback
178
179If database support transaction, rollback changes. Return false
180if database does not support.
181
182If supported, driver should provides a _rollback functions
183
184=cut
185
186sub rollback {
187    my ($self) = @_;
188    return $self->can('_rollback') ? $self->_rollback : 0;
189}
190
1911;
192
193__END__
194
195=head1 SEE ALSO
196
197=head1 AUTHOR
198
199Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt>
200
201=head1 COPYRIGHT AND LICENSE
202
203Copyright (C) 2009 by Thauvin Olivier
204
205This library is free software; you can redistribute it and/or modify
206it under the same terms as Perl itself, either Perl version 5.10.0 or,
207at your option, any later version of Perl 5 you may have available.
208
209=cut
Note: See TracBrowser for help on using the repository browser.