[2] | 1 | package LATMOS::Accounts::Bases; |
---|
| 2 | |
---|
| 3 | use 5.010000; |
---|
| 4 | use strict; |
---|
| 5 | use warnings; |
---|
| 6 | use LATMOS::Accounts::Bases::Objects; |
---|
| 7 | |
---|
[3] | 8 | our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0]; |
---|
[2] | 9 | |
---|
[3] | 10 | =head1 NAME |
---|
| 11 | |
---|
| 12 | LATMOS::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 | |
---|
| 22 | This module provide basic functions for various account base |
---|
| 23 | |
---|
| 24 | =head1 FUNTIONS |
---|
| 25 | |
---|
| 26 | =cut |
---|
| 27 | |
---|
| 28 | =head2 new($type, %options) |
---|
| 29 | |
---|
| 30 | Return, if success, a new data base account object, $type is |
---|
| 31 | account base type, %options to setup the base. |
---|
| 32 | |
---|
| 33 | =cut |
---|
| 34 | |
---|
[2] | 35 | sub 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 | |
---|
[6] | 44 | sub _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 | |
---|
| 57 | Return the list of supported fields by the database for object type $otype. |
---|
| 58 | |
---|
| 59 | =cut |
---|
| 60 | |
---|
| 61 | sub list_canonicals_fields { |
---|
| 62 | my ($self, $otype) = @_; |
---|
| 63 | my $pclass = $self->_load_obj_class($otype) or return; |
---|
[7] | 64 | $pclass->_canonical_fields; |
---|
[6] | 65 | } |
---|
| 66 | |
---|
[7] | 67 | =head2 get_field_name($otype, $c_fields) |
---|
| 68 | |
---|
| 69 | Return the internal fields name for $otype object for |
---|
| 70 | canonical fields $c_fields |
---|
| 71 | |
---|
| 72 | =cut |
---|
| 73 | |
---|
| 74 | sub 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 | |
---|
[3] | 80 | =head2 get_object($type, $id) |
---|
| 81 | |
---|
| 82 | Return an object of $type (typically user or group) having identifier |
---|
| 83 | $id. |
---|
| 84 | |
---|
| 85 | =cut |
---|
| 86 | |
---|
[2] | 87 | sub get_object { |
---|
| 88 | my ($self, $otype, $id) = @_; |
---|
| 89 | |
---|
[27] | 90 | return LATMOS::Accounts::Bases::Objects->_new($self, $otype, $id); |
---|
[2] | 91 | } |
---|
| 92 | |
---|
[16] | 93 | =head2 create_object($type, $id, %data) |
---|
| 94 | |
---|
| 95 | Create and return an object of type $type with unique id |
---|
| 96 | $id having %data. |
---|
| 97 | |
---|
| 98 | This method should be provided by the data base handler. |
---|
| 99 | |
---|
| 100 | =cut |
---|
| 101 | |
---|
| 102 | sub create_object { |
---|
| 103 | my ($self, $otype, $id, %data) = @_; |
---|
[27] | 104 | my $pclass = $self->_load_obj_class($otype); |
---|
| 105 | $pclass->create($id, %data) or return; |
---|
| 106 | $self->get_object($otype, $id); |
---|
[16] | 107 | } |
---|
| 108 | |
---|
| 109 | =head2 create_c_object($type, $id, %data) |
---|
| 110 | |
---|
| 111 | Create and return an object of type $type with unique id |
---|
| 112 | $id having %data using canonical fields |
---|
| 113 | |
---|
| 114 | =cut |
---|
| 115 | |
---|
| 116 | sub 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 | |
---|
[5] | 128 | =head2 load |
---|
| 129 | |
---|
| 130 | Make account base loading data into memory if need. |
---|
| 131 | Should always be called, if database fetch data on the fly |
---|
| 132 | (SQL, LDAP), the function just return True. |
---|
| 133 | |
---|
| 134 | =cut |
---|
| 135 | |
---|
| 136 | sub load { 1 } |
---|
| 137 | |
---|
[3] | 138 | =head2 is_transactionnal |
---|
[2] | 139 | |
---|
[3] | 140 | Return True is the database support commit and rollback |
---|
[2] | 141 | |
---|
[3] | 142 | =cut |
---|
[2] | 143 | |
---|
[3] | 144 | sub is_transactionnal { |
---|
| 145 | my ($self) = @_; |
---|
| 146 | return($self->can('_rollback') && $self->can('_commit')); |
---|
| 147 | } |
---|
[2] | 148 | |
---|
[3] | 149 | =head2 commit |
---|
[2] | 150 | |
---|
[3] | 151 | Save change into the database if change are not done immediately. |
---|
| 152 | This should always be called as you don't know when change are applied. |
---|
[2] | 153 | |
---|
[3] | 154 | Return always true if database does not support any transaction. |
---|
[2] | 155 | |
---|
[3] | 156 | The driver should provides a _commit functions to save data. |
---|
[2] | 157 | |
---|
[3] | 158 | =cut |
---|
[2] | 159 | |
---|
[3] | 160 | sub commit { |
---|
| 161 | my ($self) = @_; |
---|
| 162 | return $self->can('_commit') ? $self->_commit : 1; |
---|
| 163 | } |
---|
[2] | 164 | |
---|
[3] | 165 | =head2 rollback |
---|
[2] | 166 | |
---|
[3] | 167 | If database support transaction, rollback changes. Return false |
---|
| 168 | if database does not support. |
---|
[2] | 169 | |
---|
[3] | 170 | If supported, driver should provides a _rollback functions |
---|
[2] | 171 | |
---|
[3] | 172 | =cut |
---|
[2] | 173 | |
---|
[3] | 174 | sub rollback { |
---|
| 175 | my ($self) = @_; |
---|
[17] | 176 | return $self->can('_rollback') ? $self->_rollback : 0; |
---|
[3] | 177 | } |
---|
[2] | 178 | |
---|
[3] | 179 | 1; |
---|
[2] | 180 | |
---|
[3] | 181 | __END__ |
---|
[2] | 182 | |
---|
[3] | 183 | =head1 SEE ALSO |
---|
[2] | 184 | |
---|
| 185 | =head1 AUTHOR |
---|
| 186 | |
---|
[17] | 187 | Thauvin Olivier, E<lt>olivier.thauvin@latmos.ipsl.fr<gt> |
---|
[2] | 188 | |
---|
| 189 | =head1 COPYRIGHT AND LICENSE |
---|
| 190 | |
---|
| 191 | Copyright (C) 2009 by Thauvin Olivier |
---|
| 192 | |
---|
| 193 | This library is free software; you can redistribute it and/or modify |
---|
| 194 | it under the same terms as Perl itself, either Perl version 5.10.0 or, |
---|
| 195 | at your option, any later version of Perl 5 you may have available. |
---|
| 196 | |
---|
| 197 | =cut |
---|