source: trunk/LATMOS-Accounts/lib/LATMOS/Accounts/Task/Setattribute.pm @ 2439

Last change on this file since 2439 was 2439, checked in by nanardon, 4 years ago

Flag in log when change are made by SyncManager?

  • Property svn:executable set to *
File size: 2.7 KB
Line 
1package LATMOS::Accounts::Task::Setattribute;
2
3use strict;
4use warnings;
5use base qw(LATMOS::Accounts::Task);
6use LATMOS::Accounts;
7use LATMOS::Accounts::Log;
8use LATMOS::Accounts::Utils;
9
10=head1 NAME
11
12LATMOS::Accounts::Task::Setattribute - Task to disable expired users
13
14=head1 DESCRIPTION
15
16Set attribute to specific value according filter
17
18=head1 CONFIGURATION
19
20=over 4
21
22=item otype
23
24The type of object to modify
25
26=item filter
27
28The filter matching object to modify (can be multi-valued)
29
30=item attribute
31
32The attribute to set
33
34=item values
35
36The values the attribute must set to.
37Can be a queryformat expression.
38
39=item noeval
40
41If set, values are taken litteral w/o queryformat evaluation
42
43=back
44
45=cut
46
47sub order { 0 }
48
49sub init {
50    my ($self) = @_;
51    my $LA = $self->LA;
52    my $labase = $self->{base} ? $LA->base($self->{base}) : $LA->base;
53    $labase && $labase->load or die "Cannot load base";
54
55    my @errors = ();
56
57    $self->{otype}  = $self->config('otype') or do {
58        push(@errors, "Cannot find config `otype' value");
59    };
60    @{ $self->{filter} } = $self->config('filter') or do {
61        push(@errors, "Cannot find config `filter' value");
62    };
63    # Getting attributes to set
64    $self->{attribute} = $self->config('attribute') or do {
65        push(@errors, "Cannot find config `attribute' value");
66    };
67    @{ $self->{values} } = grep { $_ } $self->config('values');
68
69    if (@errors) {
70        la_log(LA_ERR, $_) foreach (@errors);
71        return;
72    }
73
74    $self->{_la} = $LA;
75    $self->{_base} = $labase;
76
77    1;
78}
79
80sub run {
81    my ($self) = @_;
82
83    my @objects = $self->{_base}->search_objects( $self->{otype}, @{ $self->{filter} });
84    foreach my $obj (@objects) {
85
86        my $o = $self->{_base}->get_object($self->{otype}, $obj) or next;
87
88        my $value = undef;
89
90        my @values = $self->config('noeval')
91            ? @{ $self->{values} }
92            : map { $o->queryformat($_) } @{ $self->{values} };
93
94        if (scalar(@values) > 1) {
95            $value = \@values;
96        } elsif (scalar(@values) == 1) {
97            $value = $values[0];
98        }
99        if ($o->_set_c_fields( $self->{attribute} => $value )) {
100            la_log(LA_DEBUG, "Attribute %s set to '%s' for Object %s", $self->{attribute}, join(', ', @values), $o->id);
101            $self->{_base}->commit;
102        }
103    }
104
105    1;
106}
107
1081;
109
110__END__
111
112=head1 SEE ALSO
113
114L<LATMOS::Accounts>, L<LATMOS::Accounts::Task>
115
116=head1 AUTHOR
117
118Olivier Thauvin, E<lt>olivier.thauvin@latmos.ipsl.frE<gt>
119
120=head1 COPYRIGHT AND LICENSE
121
122Copyright (C) 2012 CNRS SA/CETP/LATMOS
123
124This library is free software; you can redistribute it and/or modify
125it under the same terms as Perl itself, either Perl version 5.10.0 or,
126at your option, any later version of Perl 5 you may have available.
127
128=cut
129
Note: See TracBrowser for help on using the repository browser.