package LATMOS::Accounts::Task::Automatedrequest;

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

=head1 NAME

LATMOS::Accounts::Task::Automatedrequest - Task responsible of automatic request
validation

=cut

sub init {
    my ($self) = @_;
    my $LA = LATMOS::Accounts->new($self->{config}, noacl => 1);
    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base;
    $labase && $labase->load or die "Cannot load base";

    $self->{_la} = $LA;
    $self->{_base} = $labase;

    1;
}

sub run {
    my ($self) = @_;

    foreach my $id ($self->{_base}->list_auto_pending_requests) {
        my $datarequest = $self->{_base}->get_datarequest($id) or do {
            $self->{_base}->log(LA_ERR, "Cannot fetch request %d", $id);
            next;
        };
        if ($datarequest->auto_apply_to_object) {
            $datarequest->base->commit;
        } else {
            $datarequest->base->rollback;
        }
    }

    1;
}

1;

__END__

=head1 SEE ALSO

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

=head1 AUTHOR

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

=head1 COPYRIGHT AND LICENSE

Copyright (C) 2008, 2009, 2010, 2011, 2012 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
