package LATMOS::Accounts::Task::Basessynchro;

use strict;
use warnings;
use base qw(LATMOS::Accounts::Task);
use LATMOS::Accounts;
use LATMOS::Accounts::Log;

=head1 NAME

LATMOS::Accounts::Task::Basessynchro - Synchronize bases

=head1 DESCRIPTION

This module is responsible of the copy of object from main base to others

=cut

sub order { 2 }

sub runDelay { 900 }

sub run {
    my ($self) = @_;
    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1) or do {
        la_log LA_ERR, "Cannot instantiate LA";
        return 1;
    };

    if (my $mail = $LA->val($self->{syncname} || $LA->default_synchro_name, 'maillog')) {
        la_set_log(mail => $mail);
    }

    my $sync = $LA->create_synchro(
        $self->{syncname} || $LA->default_synchro_name,
        test     => $self->{test},
    ) or do {
       la_log LA_ERR, "cannot create sync object";
       return;
    };

    $sync->load_dest and do {
        la_log LA_ERR, "Cannot load destination";
        return;
    };

    $sync->process() or do {
        la_log LA_ERR, "Sync has failed\n";
        return;
    };

    return 1;
}

sub reset_savepoint {
    my ($self) = @_;
    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1) or do {
        la_log LA_ERR, "Cannot instantiate LA";
        return;
    };
    my $sync = $LA->create_synchro(
        $self->{syncname} || $LA->default_synchro_name,
        test     => $self->{test},
    ) or do {
       la_log LA_ERR, "cannot create sync object";
       return;
    };


    $sync->reset_savepoint
}

1;

=head1 SEE ALSO

L<LATMOS::Accounts>, L<LATMOS::Accounts::Bases>

=head1 AUTHOR

Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008, 2009 CNRS SA/CETP/LATMOS

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
