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

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