package LATMOS::Accounts::Bases::Objects;

use 5.010000;
use strict;
use warnings;

our $VERSION = (q$Rev$ =~ /^Rev: (\d+) /)[0];

=head1 NAME

LATMOS::Accounts::Bases::Objects - Base class for account objects

=head1 SYNOPSIS

  use LATMOS::Accounts::Bases::Objects;
  LATMOS::Accounts::Bases::Objects->new($base, $type, $id);

=head1 DESCRIPTION

=head1 FUNCTIONS

=cut

=head2 new($base, $type, $id, ...)

Return a new object of type $type having unique identifier
$id, all remaining arguments are passed to the subclass.

=cut

sub new {
    my ($class, $base, $otype, $id, @args) = @_;

    # finding perl class:
    my $pclass = $base->_load_obj_class($otype) or return;
    my $newobj = "$pclass"->new($base, $id, @args) or return;
    $newobj->{_base} = $base;
    $newobj->{_type} = lc($otype);
    return $newobj;
}

sub type {
    my ($self) = @_;
    return $self->{_type}
}

=head2 base

Return the base handle for this object.

=cut

sub base {
    return $_[0]->{_base}
}

=head2 _canonical_fields

Must return the list of field supported by the object.

Notice this query will always come from the upstream data base,
this function is just a ficility to store data in the module, but the
underling database can reply themself.

See list_canonical_fields().

=cut

sub _canonical_fields { 
    my ($self) = @_;
    return;
}

=head2 _get_fields_name($field)

Return the fields name for canonical field $field

=cut

sub _get_field_name {
    my ($self, $field) = @_;
    return;
}

=head2 get_field($field)

Return the value for $field, must be provide by data base.

=cut

sub get_field { return }

=head2 get_c_fields($cfield)

Return the value for canonical field $cfield

=cut

sub get_c_field {
    my ($self, $cfield) = @_;
    my $field = $self->base->get_field_name($self->type, $cfield) or return;
    $self->get_field($field);
}

=head2 set_fields(%data)

Set values for this object. %data is a list or peer field => values.

=cut

sub set_fields {
    return;
}

=head2 set_fields(%data)

Set values for this object. %data is a list or peer 
canonical field => values. Fields names are translated.

=cut

sub set_c_fields {
    my ($self, %cdata) = @_;
    my %data;
    foreach my $cfield (keys %cdata) {
        my $field = $self->base->get_field_name($self->type, $cfield) or next;
        $data{$field} = $cdata{$cfield};
    }
    keys %data or return 1; # TODO: return an error ?
    $self->set_fields(%data);
}

1;

__END__

=head1 CANICALS FIELDS

=head2 User class

=head2 Group class

=head1 SEE ALSO

Mention other useful documentation such as the documentation of
related modules or operating system documentation (such as man pages
in UNIX), or any relevant external documentation such as RFCs or
standards.

If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.

=head1 AUTHOR

Thauvin Olivier, E<lt>olivier.thauvin.ipsl.fr@localdomainE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2009 by Thauvin Olivier

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself, either Perl version 5.10.0 or,
at your option, any later version of Perl 5 you may have available.


=cut
